To start coding, just assume that a single "=" means "assignment". So, "x = y" means "y assigned to x". In Python you do not need to tell when is the end of the instruction, so forget about the ";" of other coding languages (thanks Universe!). Also, keep in mind that the python function "type()" returns the type of variable, simple.
These will help you to understand and explain code. To write a single line as comment, you need to write a "#" and eveything to the left of it will be ignored by the program. If you want to comment multiple lines, you need to enclose the comment with """ or '''.
# This is a single line of comment
# Second line as a comment
"""
Multiple lines
as comments
"""
'''
Comment 1
Comment 2
'''
Funny, but "A = None" means that A is nothing, but also that "A" actually exists as nothing (beautiful irony). In better words, "A = None" means that "A" points to a part of the memory that is empty. This type of variable will be really helpful in the future, it can be used as the "end" of something. Let's say that "1 then 0" and "1 then none" are not the same, the first one is "10" and the second one is "1".
A = None # A points to an empty space of memory
type(A) # Returns < class 'NoneType' >
Very simple, it can be True or False, just like that. It can be used as a flag or indicator. It also is the result of a comparison.
A = True # A is True
B = False # B is False
C = (A == B) # C is False
type(A) # Returns < class 'bool' >
Its a number, but with not a decimal part.
A = 10
B = 8
C = int(3.99) # C = 3
D = int(-0.5) # D = -1
E = int('15') # E = 15
type(A) # Returns < class 'int' >
Its a number with a decimal part, but "A = float('inf')" means that "A" is a number which nothing is bigger than it.
A = 5.5
B = 3.0
C = float('inf')
D = float('-inf')
type(A) # Returns < class 'float' >
The "round(x,y)" function returns a float number that is a rounded version of "x", with the "y" decimals. The default for "y" is 0, meaning that the function will return the nearest integer, notice it is no the same than "int(x)" function, that returns the int part of the float number.
pi = 3.14159265
A = round(pi, 4) # A = 3.1416
B = round(pi, 1) # B = 3.1
C = round(7.5) # C = 8.0
D = int(7.5) # D = 7
They are not widely used, but they exists. They are complex numbers.
A = 3+5j
B = 5j
C = -5j
type(A) # Returns < class 'complex' >
The strings enclose with '' or "" are almost the same, in some deep practices you can insert images between the '' strings, but so far just use the ones you prefer. The python function "ord()" returns the integer that represent the character (string of lenght 1) in the ASCII code, and "chr()" is the same but backwards.
A = 'A'
B = "B"
C = ord(A) # C = 65
D = chr(65) # D = 'A'
E = '1' # E = '1'
F = str(5) # F = '5'
type(A) # Returns < class 'str' >
There are a few of them and most of them are widely used.
These are used with numeric values to perform the most common mathematical operations.
A = 10
B = 3
A + B # 13 Addition
A - B # 7 Subtraction
A * B # 30 Multiplication
A / B # 3.33333 Division
A % B # 1 Residue of A divided by B (modulus)
A ** B # 1000 A to the power of B (exponentiation)
A // B # 3 Floor division
These are used to self-assign something.
A = 2
A += 2 # 4 A = A + 2
A -= 2 # 0 A = A - 2
A *= 2 # 4 A = A * 2
A /= 2 # 1.0 A = A / 2
A %= 2 # 0 A = A % 2 (A-2*round(A/2,0))
A **= 2 # 4 A = A ** 2 (A^2)
A //= 2 # 1 A = A // 2 (round(A/2,0))
A &= 2 # 2 A = A & 2
A |= 2 # 2 A = A | 2
A ^= 2 # 0 A = A ^ 2
A >>= 2 # 0 A = A >> 2
A <<= 2 # 8 A = A << 2
These are used to compared 2 variables, these return a boolean.
A = 7
B = 8
A == B # False Equal
A != y # True Not equal
A >= y # False Greater than or equal
A <= y # True Less than or equal
A > y # False Greater than
A < y # True Less than
These combine booleans.
A = True
B = False
C = A and B # False Logical conjunction
D = A or B # True Logical disjunction
E = not A # False Negation
F = A ^ B # True Exclusive or
These are used to test if something is presented in another variable.
A = 'abcdef'
'a' in A # True
'b' not in A # False
'z' in A # False
These are used to work with binary numbers. These are really powerful, don't underestimate them.
A = 9 # B1001
B = 5 # B0101
A & B # B0001 1
A | B # B1101 13
A ^ B # B1100 12
~ A # B0110 6
B << 1 # B1010 10
B >> 1 # B0010 2
These will tell you if 2 objects are actually the same (the same memory location), not if they are equal.
A = 1.0
B = 1.0
A is B # False