Python Notes
1. Introduction to Python
Python is a high-level, interpreted, and object-oriented programming language. It is known
for its simplicity and readability. Python uses indentation instead of curly brackets {}.
2. Variables and Data Types
Example of variables:
x = 10 # Integer
y = 3.14 # Float
name = "John" # String
is_active = True # Boolean
3. Input and Output
Printing Output:
print("Hello, World!")
Taking User Input:
name = input("Enter your name: ")
print("Hello,", name)
4. Operators in Python
Arithmetic Operators:
a = 10
b=3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
5. Conditional Statements
x = 20
if x > 10:
print("x is greater than 10")
elif x == 10:
print("x is 10")
else:
print("x is less than 10")
6. Loops in Python
For Loop:
for i in range(5):
print(i) # 0 1 2 3 4
While Loop:
x=1
while x <= 5:
print(x)
x += 1
7. Functions in Python
def greet(name):
print("Hello,", name)
greet("Alice") # Output: Hello, Alice
8. Lists in Python
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
9. Dictionaries in Python
student = {
"name": "John",
"age": 20,
"grade": "A"
}
print(student["name"]) # Output: John
10. Exception Handling
try:
num = int(input("Enter a number: "))
result = 10 / num
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Invalid input! Enter a number.")