Python Cheat Sheet
- Python terminal:
python3in cmd. - Comments:
#at the start of any line. - Print:
print("Stay Safe…) - Indentation: must be followed.
Examples
Section titled “Examples”Example 1
Section titled “Example 1”num = 5print(num)returns 5type(num)<class 'int'>Example 2
Section titled “Example 2”num = 5.0print(num)returns 5type(num)<class 'float'>Example 3
Section titled “Example 3” greet = "Hello user" print(greet) returns: 'Hello user' type(greet) <class 'str'>Example 4
Section titled “Example 4”is_available = Trueprint(is_available)returns: Truetype(is_available)returns: <class 'bool'>Example 5
Section titled “Example 5”num = Noneprint(num)returns: Nonetype(num)returns: <class 'NoType'>Operators
Section titled “Operators”+ - * / % ** // = += -= *= /= == != > < >= <= and or not
Example 1
Section titled “Example 1”a = 6b = 2a + breturns: 8a - breturns = 4a / breturns: 3a * breturns: 12a ** breturns: 36Example 2
Section titled “Example 2”a = 7b = 3a % breturns: 1a // breturns: 2Example 3
Section titled “Example 3”a =- 5b = 2a > breturns: Truea < breturns: Falsea == breturns: Falsea >= 5returns: Trueb <= 1returns: FalseExample 4
Section titled “Example 4”a = 10b = 2a == 2 and b == 10returns: Falsea == 10 or b == 20returns: Truenot(a == 10)returns: Falsenot(a == 2)returns: TrueConditional Statements
Section titled “Conditional Statements”Example 1
Section titled “Example 1”number = 5if number == 10:print("Number is 10")elif number < 10:print("Number is less than 10")else:print("Number is more than 10")Returns: Number is less than 10
Example 2
Section titled “Example 2”is_available = Truef is_available: print("Yes it is available")else: print("Not available")Returns: Yes it is available
Example 3
Section titled “Example 3”is_available = Trueif not is_available: print("Not available")else: print("Yes it is available")Returns: Yes it is available