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
- String
- 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
- Use quotation marks (
- Functions
- Use parentheses (
()
) after the name to use a function - Add the parameter between the parentheses if needed (like in print)
- Use parentheses (
- Comments
- Use an octothorpe (
#
) to start a single-line comment - Use triple quotes (
"""
) around a multi-line comment
- Use an octothorpe (
- 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
- A
Code Snippets / Examples
"""Use comments to describe your code.Each single line below is a statement."""# values and variablesstring_variable = "hello world"int_variable = 1float_variable = 1.5boolean_variable = True# operatorsmath_problem = (int_variable + float_variable) / 3# functionsprint(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