Python Programming Essentials
1. List
fruits = ['apple', 'banana', 'cherry']
[Link]('mango')
print(fruits[1])
2. Tuple
dimensions = (1920, 1080)
print(dimensions[0])
3. Set
nums = {1, 2, 3, 3}
[Link](4)
print(nums)
4. Dictionary
person = {'name': 'John', 'age': 25}
person['city'] = 'New York'
print(person['name'])
5. Function
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
6. File Handling
with open("[Link]", "w") as file:
[Link]("Hello, world!")
with open("[Link]", "r") as file:
print([Link]())
7. Exception Handling
try:
x = int(input("Enter number: "))
print(10 / x)
except ZeroDivisionError:
print("Cannot divide by zero")
8. List Comprehension
squares = [x**2 for x in range(5)]
print(squares)
9. Class Example
class Student:
def __init__(self, name):
[Link] = name
def greet(self):
print(f"Hello, I'm {[Link]}")
s1 = Student("Alice")
[Link]()
10. Lambda Function
add = lambda x, y: x + y
print(add(5, 3))