python
python
Day Revision)
1. Getting Started with Python
Basic I/O:
Data Types: int, float, str, bool, list, tuple, dict, set.
Type Conversion:
Operators:
OperatorMeaning+Addition-Subtraction*Multiplication/
Division%Modulus//Floor Division**Exponentiation
x = 10
if x > 5:
print("Greater than 5")
elif x == 5:
print("Equal to 5")
else:
print("Less than 5")
Loops
Loops in Python
python
CopyEdit
x=5
while x > 0:
print("Counting down:", x)
x -= 1 # Decrease x by 1
1.
Explanation:
2.
o
o
1.
2.
python
CopyEdit
for i in range(5):
print("Iteration:", i)
1.
Explanation:
2.
o
o
1.
3. For Loop with Lists
2.
python
CopyEdit
fruits = ["Apple", "Banana", "Cherry"]
for fruit in fruits:
print("Fruit:", fruit)
1.
Explanation:
2.
1.
2.
python
CopyEdit
for i in range(3):
for j in range(2):
print(f"Outer loop {i}, Inner loop {j}")
1.
Explanation:
2.
o
o
Output:
1.
2.
o
o
o
o
python
CopyEdit
for i in range(5):
if i == 3:
break # Stops loop when i is 3
print(i)
for i in range(5):
if i == 2:
continue # Skips when i is 2
print(i)
for i in range(5):
pass # Placeholder, loop does nothing
4. Lists in Python
Lists in Python
A list is a collection of items stored in a single variable. It can
contain integers, floats, strings, or even other lists. Lists are
mutable (can be changed after creation).
1. Creating a List
fruits = ["Apple", "Banana", "Cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "Hello", 3.14, True]
empty_list = []
print(fruits[0]) # Apple
print(fruits[1]) # Banana
print(fruits[-1]) # Cherry (Negative index accesses from end)
Negative indexing allows access from the end (-1 for last
item, -2 for second last, etc.)
3. Slicing a List
numbers = [10, 20, 30, 40, 50, 60]
4. Modifying Lists
Changing Elements
fruits.append("Orange")
print(fruits) # ['Apple', 'Mango', 'Cherry', 'Orange']
fruits.extend(["Pineapple", "Watermelon"])
print(fruits) # ['Apple', 'Grapes', 'Mango', 'Cherry', 'Orange',
'Pineapple', 'Watermelon']
6. Removing Elements from a List
fruits.pop(2) # Removes 'Cherry'
print(fruits) # ['Apple', 'Grapes', 'Orange', 'Pineapple', 'Watermelon']
fruits.remove("Mango")
print(fruits) # ['Apple', 'Grapes', 'Cherry', 'Orange', 'Pineapple',
'Watermelon']
fruits.clear()
print(fruits) # []
i=0
while i < len(fruits):
print(fruits[i])
i += 1
numbers = [5, 2, 9, 1, 5, 6]
numbers.sort()
print(numbers) # [1, 2, 5, 5, 6, 9]
Reversing a List
numbers.reverse()
print(numbers) # [6, 5, 5, 2, 1, 9]
Method Description
append(x
Adds x to the end of the list
)
insert(i,
Inserts x at index i
x)
Dictionary in Python
A dictionary in Python is an unordered, mutable collection of
key-value pairs. It is used to store data in a structured way, like a
real-world dictionary where words (keys) have meanings (values).
1. Creating a Dictionary
python
CopyEdit
student = {
"name": "Dhairya",
"age": 17,
"class": "11th",
"subjects": ["Math", "Physics", "Chemistry"]
}
python
CopyEdit
print(student.get("age")) # 17
print(student.get("marks", "Not Available")) # Default value if key is
missing
4. Removing Items
Using pop() (Removes key and returns value)
python
CopyEdit
age = student.pop("age")
print(age) # 18
print(student)
python
CopyEdit
del student["class"] # Deletes a specific key
del student # Deletes entire dictionary
python
CopyEdit
student.popitem()
print(student)
python
CopyEdit
student.clear()
print(student) # {}
python
CopyEdit
for key in student:
print(key) # name, age, subjects, school
python
CopyEdit
for value in student.values():
print(value) # Dhairya, 18, ['Math', 'Physics', 'Chemistry'], 'XYZ High
School'
python
CopyEdit
for key, value in student.items():
print(key, ":", value)
Returns value of
dict.get(key,
key, or default if key
default)
not found
Removes and
dict.pop(key) returns value of the
key
Removes and
returns the last
dict.popitem()
inserted key-value
pair
Empties the
dict.clear()
dictionary
Aggregate Functions
Joins
Emerging Trends
1. Artificial Intelligence (AI) & Machine Learning (ML)
3. Blockchain Technology
4. Cloud Computing
Delivery of computing services (storage, databases, servers)
over the internet.
9. Quantum Computing