Python Syntax Guide

Guide

Key Concepts

  • Value (data types)
    • String
      • Text value
    • Int
      • Number value (no decimal places)
    • Float
      • Number value (has decimal places)
    • Boolean
      • True or false value
  • Variable
    • Used to store values
    • Has a unique name that you choose
  • Operator
    • Used for assigning a value to a variable
    • Used for math between two values or variables
  • Function
    • Pre-written code that can be used repeatedly
  • Print
    • Function that displays a string in the console
  • Error
    • Text displayed in the console when Python detects a mistake
    • Shows the location (line number) of the mistake
  • Statement
    • A single line of code that does something
    • Like a sentence, every statement has a subject and a verb
  • Comment
    • Not interpreted by Python
    • Used to explain what the code does
  • Console
    • Where you see the program’s output (print results and errors)

Syntax

  • Values
    • Use quotation marks (" or ') around a string
    • Use decimal points (.) to turn an int into a float
    • Booleans can only be True or False
  • Functions
    • Use parentheses (()) after the name to use a function
    • Add the parameter between the parentheses if needed (like in print)
  • Comments
    • Use an octothorpe (#) to start a single-line comment
    • Use triple quotes (""") around a multi-line comment
  • Some common operators
    • A = B
      • Set variable A to the value of B
    • A + B
      • Add A and B
    • A - B
      • Subtract B from A
    • A * B
      • Multiply A by B
    • A / B
      • Divide A by B

Code Snippets / Examples

"""
Use comments to describe your code.
Each single line below is a statement.
"""
# values and variables
string_variable = "hello world"
int_variable = 1
float_variable = 1.5
boolean_variable = True
# operators
math_problem = (int_variable + float_variable) / 3
# functions
print(string_variable) # hello world

Author

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team