Learn Python 3 - Hello World Cheatsheet
Learn Python 3 - Hello World Cheatsheet
Hello World
Comments
A comment is a piece of text within a program that is
not executed. It can be used to provide additional # Comment on a single line
information to aid in understanding the code.
Arithmetic Operations
Python supports different types of arithmetic
operations that can be performed on literal numbers, # Arithmetic operations
variables, or some combination. The primary arithmetic
operators are:
result = 5 ** 3
/ for division
** for exponentiation
Plus-Equals Operator +=
The plus-equals operator += provides a convenient
way to add a value to an existing variable and assign the # Plus-Equal Operator
new value back to the same variable. In the case where
the variable and the value are strings, this operator counter = 0
performs string concatenation instead of addition.
counter += 10
The operation is performed in-place, meaning that any
# This is equivalent to
updated will also be updated.
counter = 0
Variables
A variable is used to store data that will be used by the
program. This data can be a number, a string, a # These are all valid variable names and
Boolean, a list or some other data type. Every variable assignment
has a name which can consist of letters, numbers, and
the underscore character _ . user_name = "@sonnynomnom"
The equal sign = is used to assign a value to a variable.
user_id = 100
After the initial assignment is made, the value of a
verified = False
variable can be updated to new values as needed.
points = 100
points = 120
Modulo Operator %
A modulo calculation returns the remainder of a
division between the first and second number. For # Modulo operations
example:
zero = 8 % 4
The result of the expression 4 % 2 would result
in the value 0, because 4 is evenly divisible by 2
nonzero = 12 % 5
leaving no remainder.
Integers
An integer is a number that can be written without a
fractional part (no decimal). An integer can be a # Example integer numbers
positive number, a negative number or the number 0
chairs = 4
The number 0 represents an integer value but the
tables = 1
same number written as 0.0 would represent a
broken_chairs = -2
floating point number.
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 # String concatenation
used for mathematical addition operations. If the
parameters passed to the + operator are strings, then first = "Hello "
concatenation will be performed. If the parameter second = "World"
passed to + have different types, then Python will
report an error condition. Multiple variables or literal
result = first + second
strings can be joined together using the + operator.
Errors
The Python interpreter will report errors present in
your code. For most error cases, the interpreter will if False ISNOTEQUAL True:
display the line of code where the error was detected ^
and place a caret character ^ under the portion of SyntaxError: invalid syntax
the code where the error was detected.
ZeroDivisionError
A ZeroDivisionError is reported by the Python
interpreter when it detects a division operation is being numerator = 100
performed and the denominator (bottom number) is 0. denominator = 0
In mathematics, dividing a number by zero has no bad_results = numerator / denominator
defined value, so Python treats this as an error
condition and will report a ZeroDivisionError and
ZeroDivisionError: division by zero
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, numbers,
whitespace or punctuation) enclosed by quotation user = "User Full Name"
SyntaxError
A SyntaxError is reported by the Python interpreter
when some portion of the code is incorrect. This can age = 7 + 5 = 4
include misspelled keywords, missing or too many
NameError
A NameError is reported by the Python interpreter
when it detects a variable that is unknown. This can misspelled_variable_name
occur when a variable is used before it has been
assigned a value or if a variable name is spelled NameError: name
differently than the point at which it was defined. The 'misspelled_variable_name' is not defined
Python interpreter will display the line of code where
the NameError was detected and indicate which name
it found that was not defined.
pi = 3.14159
that have fractional quantities. For example, a = 3/5
meal_cost = 12.99
can not be represented as an integer, so the variable
tip_percent = 0.20
a is assigned a floating point value of 0.6 .
print() Function
The print() function is used to output text, numbers,
or other printable information to the console. print("Hello World!")
It takes one or more arguments and will output each of
print(100)
no arguments are provided, the print() function will
pi = 3.14159
print(pi)