Learn Python 3 Hello World Cheatsheet Codecademy
Learn Python 3 Hello World Cheatsheet Codecademy
Learn Python 3 Hello World Cheatsheet Codecademy
Hello World
Print Cheatsheet Share
TOPICS
Hello World
Control Flow
Comments
Lists
A comment is a piece of text within a
# Comment on a single line
Loops program that is not executed. It can be used
to provide additional information to aid in user = "JDoe" # Comment after code
Functions
understanding the code.
Python: Code Challenges
(Optional) The # character is used to start a comment
and it continues until the end of the line.
Strings
Plus-Equals Operator +=
The plus-equals operator += provides a
# Plus-Equal Operator
convenient way to add a value to an existing
variable and assign the new value back to the counter = 0
same variable. In the case where the variable counter += 10
and the value are strings, this operator
performs string concatenation instead of # This is equivalent to
addition.
counter = 0
The operation is performed in-place, counter = counter + 10
Variables
A variable is used to store data that will be
# These are all valid variable names and assignment
used by the program. This data can be a
number, a string, a Boolean, a list or some user_name = "codey"
other data type. Every variable has a name user_id = 100
which can consist of letters, numbers, and verified = False
the underscore character _ .
# A variable's value can be changed after assignment
The equal sign = is used to assign a value to
a variable. After the initial assignment is points = 100
Modulo Operator %
A modulo calculation returns the remainder
# Modulo operations
of a division between the first and second
number. For example: zero = 8 % 4
Integers
An integer is a number that can be written
# Example integer numbers
without a fractional part (no decimal). An
integer can be a positive number, a negative chairs = 4
number or the number 0 so long as there is tables = 1
no decimal portion. broken_chairs = -2
sofas = 0
The number 0 represents an integer value
but the same number written as 0.0 would # Non-integer numbers
represent a floating point number.
lights = 2.5
left_overs = 0.0
String Concatenation
Python supports the joining (concatenation)
# String concatenation
of strings together using the + operator. The
+ operator is also used for mathematical first = "Hello "
addition operations. If the parameters second = "World"
passed to the + operator are strings, then
concatenation will be performed. If the result = first + second
Errors
The Python interpreter will report errors
if False ISNOTEQUAL True:
present in your code. For most error cases, ^
the interpreter will display the line of code SyntaxError: invalid syntax
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
numerator = 100
interpreter when it detects a division denominator = 0
operation is being performed and the bad_results = numerator / denominator
denominator (bottom number) is 0. In
mathematics, dividing a number by zero has ZeroDivisionError: division by zero
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.
Strings
A string is a sequence of characters (letters,
user = "User Full Name"
numbers, whitespace or punctuation) game = 'Monopoly'
enclosed by quotation marks. It can be
enclosed using either the double quotation longer = "This string is broken up \
mark " or the single quotation mark ' . over multiple lines"
SyntaxError
A SyntaxError is reported by the Python
age = 7 + 5 = 4
interpreter when some portion of the code is
incorrect. This can include misspelled File "<stdin>", line 1
keywords, missing or too many brackets or SyntaxError: can't assign to operator
parentheses, incorrect operators, missing or
too many quotation marks, or other
conditions.
NameError
A NameError is reported by the Python
misspelled_variable_name
interpreter when it detects a variable that is
unknown. This can occur when a variable is NameError: name 'misspelled_variable_name' is not defined
used before it has been assigned a value or if
a variable name is spelled differently than the
point at which it was defined. The Python
interpreter will display the line of code where
the NameError was detected and indicate
which name it found that was not defined.
print() Function
The print() function is used to output text,
print("Hello World!")
numbers, or other printable information to
the console. print(100)