Python Programming Language Cheatsheet
Python Programming Language — Complete Beginner Cheat Sheet
1. What Is Python?
Python is a high-level, interpreted programming language known for its simple syntax and readability.
It’s widely used for backend development, automation, data science, AI, scripting, and APIs.
Python focuses on writing less code and doing more work.
2. Installing Python
After installing Python, you can run code using:
python app.py
Python executes files line by line (no compilation step like Java).
3. Hello World
print("Hello World")
This prints text to the console.
Python does not require classes or main methods to start.
4. Variables
Variables store values in memory.
age = 25
name = "Sumit"
active = True
Python is dynamically typed, meaning you don’t declare data types.
5. Comments
Used to explain code.
# single line comment
Comments improve readability and maintenance.
6. Data Types
Common built-in types:
int → whole numbers
float → decimal numbers
str → text
bool → True / False
Python automatically detects types.
7. Type Checking
type(age)
Returns the variable’s data type.
8. Type Casting
Convert one type into another.
int("5")
str(10)
float(3)
Used when reading user input or APIs.
9. Operators
Arithmetic
+ - * / %
Comparison
== != > < >= <=
Logical
and or not
Operators perform calculations and conditions.
10. Input From User
name = input("Enter name: ")
Input always returns a string.
🟢 Control Flow
11. if / else
if age > 18:
print("Adult")
else:
print("Minor")
Used to make decisions.
Indentation is mandatory in Python.
12. elif
elif age == 18:
Used for multiple conditions.
13. Loops
Loops repeat code automatically.
for loop
for i in range(5):
print(i)
Used when number of iterations is known.
while loop
while x < 5:
Runs while condition remains true.
14. break / continue
break → exits loop
continue → skips current iteration
🧺 Collections
15. List
Ordered, changeable, allows duplicates.
nums = [1,2,3]
Used for storing multiple values.
16. Tuple
Ordered but immutable.
t = (1,2,3)
Used when data should not change.
17. Set
Unordered, no duplicates.
s = {1,2,3}
Used for uniqueness.
18. Dictionary
Key-value pairs.
user = {"name":"Sumit","age":25}
Used heavily in APIs and JSON.
19. Common Collection Methods
append()
remove()
pop()
keys()
values()
items()
Manipulate collections.
🔧 Functions
20. Function Definition
def add(a,b):
return a+b
Functions make code reusable.
21. Default Parameters
def greet(name="Guest"):
Used when argument is optional.
22. *args and **kwargs
Handle variable arguments.
def test(*args, **kwargs):
Used in frameworks.
23. Lambda Function
Short anonymous functions.
lambda a,b: a+b
Used with filters and maps.
🧱 OOP in Python
24. Class
Blueprint of objects.
class Car:
25. Object
Instance of class.
c = Car()
26. Constructor
Runs automatically on object creation.
def __init__(self):
27. self
Refers to current object.
Used to access variables and methods.
28. Inheritance
Child class inherits parent.
class B(A):
Promotes reuse.
29. Encapsulation
Protect data using underscores.
_name (protected)
__name (private)
30. Polymorphism
Same method behaves differently.
Python supports dynamic polymorphism.
31. Abstraction
Hide implementation details using ABC module.
⚠ Error Handling
32. try / except
try:
except Exception:
Prevents program crash.
33. finally
Always executes.
34. Custom Exception
class MyError(Exception):
File Handling
35. Read File
open("a.txt","r")
36. Write File
open("a.txt","w")
37. with Statement
Automatically closes files.
with open() as f:
Modules & Packages
38. Import Module
import math
39. From Import
from math import sqrt
40. pip
Python package manager.
pip install requests
Multithreading
41. Threading
Runs tasks in parallel.
import threading
42. Async / Await
Used for non-blocking code.
async def func():
Common in APIs.
Database
43. SQLite / PostgreSQL / MySQL
Python connects using libraries:
sqlite3
psycopg2
mysql-connector
Virtual Environment
44. venv
Creates isolated environment.
python -m venv env
Prevents dependency conflicts.
Professional Python
45. Popular Frameworks
Django
Flask
FastAPI
Used for backend APIs.
46. Testing
unittest
pytest
Ensures correctness.
47. Logging
Tracks application behavior.
import logging
48. Deployment
Docker
Gunicorn
Nginx
Cloud
Used for production.
49. Advanced Topics
Decorators
Generators
Context Managers
Microservices
Celery
Redis
Kafka
Used in large systems.