Python_Code_Formulas
Python_Code_Formulas
- Addition: result = a + b
- Subtraction: result = a - b
- Multiplication: result = a * b
- Division: result = a / b
2. String Operations
3. List Operations
- Check Even/Odd:
if num % 2 == 0:
print("Even")
else:
print("Odd")
5. Loops
- For Loop:
print(i)
- While Loop:
print(num)
num -= 1
6. File Operations
- Read a File:
content = file.read()
- Write to a File:
file.write("Hello, World!")
7. Importing Modules
- Random Number:
import random
result = random.randint(1, 100)
- Current Date:
now = datetime.now()
8. Algorithms
- Factorial:
import math
result = math.factorial(n)
- Fibonacci Series:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
9. Error Handling
- Try-Except Block:
try:
result = 10 / 0
except ZeroDivisionError:
- One-Liner if-else:
- List Comprehension:
squares = [x**2 for x in range(10)]