Python
Getting Started
Introduction
Python (python.org)
Learn X in Y minutes (learnxinyminutes.com)
Regex in python
Hello World
>>> print("Hello, World!")
Hello, World!The famous "Hello World" program in Python
Variables
age = 18 # age is of type int
name = "John" # name is now of type str
print(name)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
See: Data Types
Slicing String
See: Strings
Lists
See: Lists
If Else
See: Flow control
Loops
See: Loops
Functions
See: Functions
File Handling
See: File Handling
Arithmetic
The / means quotient of x and y, and the // means floored quotient of x and y, also see StackOverflow
Plus-Equals
f-Strings (Python 3.6+)
See: Python F-Strings
Python Built-in Data Types
Strings
See: Strings
Numbers
Booleans
Lists
See: Lists
Tuple
Similar to List but immutable
Set
Set of unique items/objects
Dictionary
Key: Value pair, JSON like object
Casting
Integers
Floats
Strings
Python Advanced Data Types
Heaps
Negate all values to use Min Heap as Max Heap
Heaps are binary trees for which every parent node has a value less than or equal to any of its children. Useful for accessing min/max value quickly. Time complexity: O(n) for heapify, O(log n) push and pop. See: Heapq
Stacks and Queues
Deque is a double-ended queue with O(1) time for append/pop operations from both sides. Used as stacks and queues. See: Deque
Python Strings
Array-like
Get the character at position 1 or last
Looping
Loop through the letters in the word "foo"
Slicing string
With a stride
String Length
The len() function returns the length of a string
Multiple copies
Check String
Concatenates
Formatting
format() Method
Input
Get input data from console
Join
Endswith
Python F-Strings (Since Python 3.6+)
f-Strings usage
it is available since Python 3.6, also see: Formatted string literals
f-Strings Fill Align
f-Strings Type
F-Strings Others
F-Strings Sign
Python Lists
Defining
Generate
Append
List Slicing
Syntax of list slicing:
Slicing
Omitting index
With a stride
Remove
Access
Concatenating
Sort & Reverse
Count
Repeating
Python Flow control
Basic
One line
else if
Python Loops
Basic
Prints: 2 3 5 7
With index
Prints: 0 dog 1 cat 2 mouse
While
Prints: 0 1 2 3
Break
Prints: 0 10 20 30 40
Continue
Prints: 30 40 60 70
Range
With zip()
Prints: 1:Mon, 2:Tue, 3:Wed,
for/else
Also see: Python Tips
Python Functions
Basic
Return
Positional arguments
Keyword arguments
Returning multiple
Default Value
Anonymous functions
Python Modules
Import modules
From a module
Import all
Shorten module
Functions and attributes
Python File Handling
Read file
Line by line
With line number
String
Write a string
Read a string
Object
Write an object
Read an object
Delete a File
Check and Delete
Delete Folder
Python Classes & Inheritance
Defining
Constructors
Method
Class Variables
Super() Function
repr() method
User-defined exceptions
Polymorphism
Overriding
Inheritance
Miscellaneous
Comments
Generators
Generators help you make lazy code.
Generator to list
Handle exceptions
Last updated