Getting Started
Introduction
- Python (python.org)
- Python Document (docs.python.org)
- Learn X in Y minutes (learnxinyminutes.com)
- Regex in python (cheatsheets.zip)
Hello World
The famous "Hello World" program in Python
Variables
Python can't declare a variable without assignment.
Data Types
str |
Text |
int, float, complex |
Numeric |
list, tuple, range |
Sequence |
dict |
Mapping |
set, frozenset |
Set |
bool |
Boolean |
bytes, bytearray, memoryview |
Binary |
Slicing String
Lists
If Else
Loops
Functions
>>> def my_function():
... print("Hello from a function")
...
>>> my_function()
Hello from a function
File Handling
Arithmetic
result = 10 + 30 # => 40
result = 40 - 10 # => 30
result = 50 * 5 # => 250
result = 16 / 4 # => 4.0 (Float Division)
result = 16 // 4 # => 4 (Integer Division)
result = 25 % 2 # => 1
result = 5 ** 3 # => 125
The / means quotient of x and y, and the // means floored quotient of x and y, also see
StackOverflow
Plus-Equals
counter = 0
counter += 10 # => 10
counter = 0
counter = counter + 10 # => 10
message = "Part 1."
# => Part 1.Part 2.
message += "Part 2."
f-Strings (Python 3.6+)
>>> website = 'cheatsheets.zip'
>>> f"Hello, {website}"
"Hello, cheatsheets.zip"
>>> num = 10
>>> f'{num} + 10 = {num + 10}'
'10 + 10 = 20'