Python Programming Basics
1. Introduction to Python
Python is a high-level, interpreted, and general-purpose language. It is beginner-friendly and widely
used.
2. Variables and Data Types
Example:
name = "Noorjahan" # String
age = 20 # Integer
height = 5.4 # Float
is_student = True # Boolean
3. User Input
name = input("Enter your name: ")
print("Hello", name)
4. Conditional Statements
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
5. Loops
for i in range(5):
print("Hello", i)
count = 0
while count < 3:
print("Count:", count)
count += 1
6. Functions
def greet(name):
print("Hello", name)
greet("Noor")
7. Lists
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits.remove("banana")
for fruit in fruits:
print(fruit)
8. Tuples
colors = ("red", "green", "blue")
print(colors[1])
9. Dictionary
student = {"name": "Noor", "age": 20, "grade": "A"}
print(student["name"])
10. File Handling
with open("demo.txt", "w") as file:
file.write("Hello, Python!")
with open("demo.txt", "r") as file:
print(file.read())
11. Exception Handling
try:
x = int(input("Enter number: "))
print(100 / x)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
12. Class and Object
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def display(self):
print("Name:", self.name)
print("Age:", self.age)
s1 = Student("Noor", 20)
s1.display()