Essentials_of_Python_Programming
Essentials_of_Python_Programming
x = 10 # Integer
- Data Types:
- Text: str
- Mapping: dict
- Boolean: bool
2. Control Flow
- Conditional Statements:
if condition:
# code block
elif another_condition:
# code block
else:
# code block
- Loops:
- for Loop:
for i in range(5):
print(i)
- while Loop:
while condition:
# code block
for i in range(10):
if i == 5:
if i % 2 == 0:
3. Functions
- Defining Functions:
def greet(name):
return f"Hello, {name}!"
- Lambda Functions:
print(square(5))
4. Data Structures
- Lists:
fruits.append("orange")
fruits[0] = "kiwi"
- Tuples:
- Dictionaries:
person["age"] = 31
- Sets:
unique_numbers = {1, 2, 3, 3}
5. Object-Oriented Programming (OOP)
class Person:
self.name = name
self.age = age
def greet(self):
print(john.greet())
- Inheritance:
class Employee(Person):
super().__init__(name, age)
self.salary = salary
6. File Handling
- Reading Files:
with open("file.txt", "r") as file:
content = file.read()
- Writing Files:
file.write("Hello, World!")
7. Error Handling
- Try-Except Blocks:
try:
x=1/0
except ZeroDivisionError as e:
print("Error:", e)
finally:
print("Execution complete.")
- Importing Modules:
import math
print(math.sqrt(16))
- Using Libraries:
import requests
response = requests.get("https://api.example.com")
print(response.text)
9. Useful Tips
- List Comprehensions:
- Unpacking:
a, b, *rest = [1, 2, 3, 4]
- Enumerate:
print(index, value)