```python
x = 5 name = "John" ```
```python
# This is a single-line comment
""" This is a multi-line comment """ ```
```python
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")
```
```python
for i in range(5):
print(i)
```
```python
count = 0
while count < 5:
print(count)
count += 1
```
```python
def greet(name):
return f"Hello, {name}"
```
```python
add = lambda x, y: x + y print(add(2, 3)) ```
```python
lst = [1, 2, 3] lst.append(4) lst.remove(2) print(lst) ```
```python
d = {"name": "Alice", "age": 25}
print(d["name"])
d["age"] = 26
```
```python
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("End of try-except block")
```
```python
with open("file.txt", "r") as file:
content = file.read()
print(content)
```
```python
with open("file.txt", "w") as file:
file.write("Hello, World!")
```
```python
import math print(math.sqrt(16)) ```
```python
# In mymodule.py
def add(x, y):
return x + y
# In main file from mymodule import add print(add(3, 4)) ```
```python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name}")
p = Person("Alice", 25)
p.greet()
```
```python
squares = [x**2 for x in range(5)] print(squares) ```
```python
a, b = 1, 2 print(a, b) ```
This cheatsheet covers the basics of Python syntax and essential concepts for quick reference. For more details, refer to the official Python documentation.