■ Python Basics Cheat Sheet
A complete guide for Python beginners, covering all essential basics.
1. Python Setup
Install Python from [Link].
Run scripts: python [Link]
Interactive mode: python
2. Printing & Comments
print("Hello, World!")
# Single-line comment
"""
Multi-line
comment
"""
3. Variables & Data Types
x = 10 (int), y = 3.14 (float), name = "Amin" (string), is_on = True (boolean)
Use type(x) to check data type.
4. Numbers
Supports int, float, complex.
Operators: +, -, *, /, //, %, **
Example: 10 // 3 = 3 (floor division).
5. Strings
s = "Python"
Indexing: s[0] = P
Slicing: s[0:3] = Pyt
Useful methods: upper(), lower(), replace(), split()
Formatting: f"My age is {age}"
6. Collections
List: [1,2,3], mutable.
Tuple: (1,2,3), immutable.
Set: {1,2,3}, unique values.
Dictionary: {'key': 'value'}
7. Operators
Arithmetic (+,-,*,/,% etc.), Comparison (==, !=, >), Logical (and, or, not), Membership (in, not in), Identity
(is, is not).
8. Conditionals
if x>0: ... elif x==0: ... else: ...
9. Loops
for i in range(5): ...
while i<5: ...
Break/Continue to control loops.
10. Functions
def greet(name="World"):
return f"Hello, {name}"
11. Classes & Objects
class Person:
def __init__(self, name): [Link]=name
def say(self): print([Link])
12. Input & Output
name = input("Enter name: ")
print("Hello", name)
13. File Handling
with open("[Link]", "w") as f: [Link]("Hello")
with open("[Link]", "r") as f: print([Link]())
14. Error Handling
try: ... except: ... finally: ...
15. Useful Tools
List comprehensions, lambda, len(), max(), sum(), sorted().