Python CodeAcademy
Python CodeAcademy
Comments
A comment is a piece of text within a program that is not executed. It can be
used to provide additional information to aid in understanding the code.
The # character is used to start a comment and it continues until the end of
the line.
Arithmetic Operations
Python supports different types of arithmetic operations that can be
performed on literal numbers, variables, or some combination. The primary
arithmetic operators are:
● + for addition
● - for subtraction
● * for multiplication
● / for division
● % for modulus (returns the remainder)
● ** for exponentiation
# Arithmetic operations
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 1/8
3/30/2020 Introduction to Programming: Learn Python: Syntax Reference Guide | Codecademy
result = 10 + 30
result = 40 - 10
result = 50 * 5
result = 16 / 4
result = 25 % 2
result = 5 ** 3
Plus-Equals Operator +=
The operation is performed in-place, meaning that any other variable which
points to the variable being updated will also be updated.
# Plus-Equal Operator
counter = 0
counter += 10
# This is equivalent to
counter = 0
counter = counter + 10
Variables
A variable is used to store data that will be used by the program. This data
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 2/8
3/30/2020 Introduction to Programming: Learn Python: Syntax Reference Guide | Codecademy
can be a number, a string, a Boolean, a list or some other data type. Every
variable has a name which can consist of letters, numbers, and the underscore
character _ .
The equal sign = is used to assign a value to a variable. After the initial
assignment is made, the value of a variable can be updated to new values as
needed.
user_name = "@sonnynomnom"
user_id = 100
verified = False
points = 100
points = 120
Modulo Operator %
A modulo calculation returns the remainder of a division between the first and
second number. For example:
# Modulo operations
zero = 8 % 4
nonzero = 12 % 5
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 3/8
3/30/2020 Introduction to Programming: Learn Python: Syntax Reference Guide | Codecademy
Integers
An integer is a number that can be written without a fractional part (no
decimal). An integer can be a positive number, a negative number or the
number 0 so long as there is no decimal portion.
The number 0 represents an integer value but the same number written as
0.0 would represent a floating point number.
chairs = 4
tables = 1
broken_chairs = -2
sofas = 0
# Non-integer numbers
lights = 2.5
left_overs = 0.0
String Concatenation
Python supports the joining (concatenation) of strings together using the +
operator. The + operator is also used for mathematical addition operations. If
the parameters passed to the + operator are strings, then concatenation will
be performed. If the parameter passed to + have different types, then Python
will report an error condition. Multiple variables or literal strings can be joined
together using the + operator. The concatenation process does not add any
whitespace between the strings that are joined.
# String concatenation
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 4/8
3/30/2020 Introduction to Programming: Learn Python: Syntax Reference Guide | Codecademy
second = "World"
Errors
The Python interpreter will report errors present in your code. For most error
cases, the interpreter will display the line of code where the error was
detected and place a caret character ^ under the portion of the code where
the error was detected.
ZeroDivisionError
A ZeroDivisionError is reported by the Python interpreter when it detects a
division operation is being performed and the denominator (bottom number)
is 0. In mathematics, dividing a number by zero has no defined value, so
Python treats this as an error condition and will report a ZeroDivisionError and
display the line of code where the division occurred. This can also happen if a
variable is used as the denominator and its value has been set to or changed
to 0.
numerator = 100
denominator = 0
bad_results = numerator / denominator
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 5/8
3/30/2020 Introduction to Programming: Learn Python: Syntax Reference Guide | Codecademy
Strings
A string is a sequence of characters (letters, numbers, whitespace or
punctuation) enclosed by quotation marks. It can be enclosed using either the
double quotation mark " or the single quotation mark ' .
If a string has to be broken into multiple lines, the backslash character \ can
be used to indicate that the string continues on the next line.
SyntaxError
A SyntaxError is reported by the Python interpreter when some portion of the
code is incorrect. This can include misspelled keywords, missing or too many
brackets or parenthesis, incorrect operators, missing or too many quotation
marks, or other conditions.
age = 7 + 5 = 4
NameError
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 6/8
3/30/2020 Introduction to Programming: Learn Python: Syntax Reference Guide | Codecademy
misspelled_variable_name
pi = 3.14159
meal_cost = 12.99
tip_percent = 0.20
print() Function
The print() function is used to output text, numbers, or other printable
information to the console.
It takes one or more arguments and will output each of the arguments to the
console separated by a space. If no arguments are provided, the print()
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 7/8
3/30/2020 Introduction to Programming: Learn Python: Syntax Reference Guide | Codecademy
print("Hello World!")
print(100)
pi = 3.14159
print(pi)
https://www.codecademy.com/learn/paths/computer-science/tracks/cspath-intro/modules/cspath-python-syntax/cheatsheet 8/8