Programming summary
Saturday, 16 March 2024 21:18
QUICK SHOW CASE OF LOOPS
# Example: Using range to print numbers
# Example: Iterating over a list
# Example: Iterating over dictionary keys and values
fruits = ["apple", "banana", "cherry"]
for i in range(5):
# Example: Looping with condition
# Example: Using break and continue
print(i)
LISTS ARE LIKE LISTS IN JAVA AND DELPHI BUT WE
MAINGLY USED ARRAYS BUT LISTS THE SAME
person = {'name': 'John', 'age': 30, 'city': 'New York'}
OUTPUT:
Quick Notes Page 1
MAINGLY USED ARRAYS BUT LISTS THE SAME
person = {'name': 'John', 'age': 30, 'city': 'New York'}
OUTPUT:
# Example: Iterating over a list
for fruit in fruits: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 1
# Example: List comprehension
2
3
4
# Example: Using enumerate
# Example: Using break and continue # Example: Loopi
# Example: Using zip
print(fruit)
# Example: Using break and continue
numbers = [1, 2, 3, 4, 5]
FUNCTIONS
squared = [x**2 for x in range(1, 6)]
OUTPUT: for num in numbers:
fruits
names
def= =["apple",
add(a,
["Alice",
b):"banana",
"Bob", "Charlie"]
"cherry"]
for num in numbers:
def greet(name="World"):
numbers = [1, 2, 3
# Iterate over keys
numbers = [1, 2, 3, 4, 5]
for num
return
in numbers:
a +b
def multi_add(*args):
# Example: Using enumerate
if num % 2 == 0:
if num == 5:
numbers
double
= [1,
= lambda
2, 3, 4,x:
5]x * 2
def
resultgreet(name):
= add(3, 4) # Here, 3 and 4 are arguments passed to the function add
for key in person:
print(num, "is even")
break # Exit the loop when num is 5
def foo():
print(squared)
print(num)
print("Hello,", name) even_numbers =
Quick Notes Page 2
for key in person:
print(num, "is even")
break # Exit the loop when num is 5
def foo():
print(squared)
print(num)
print("Hello,", name) even_numbers =
for num in numbers:
ages = [25, 30, 35]
return sum(args)
for idx, fruit in enumerate(fruits):
x = 10
"""Print a greeting message."""
fruits = result
x = 10
= double(5)
["apple", "banana",# "cherry"]
result will be 10
for num in numbers:
def foo():
print(key)
print(x)
for num in numbe
for name, age in zip(names, ages): if num == 3:
else:
global x
result = multi_add(1, 2, 3, 4) # result will be 10
greet() # Outputs: Hello, World
# print(x) # This will raise a NameError because x is not defined in the global scope
# Iterate over values
print(num, "is odd")
if num == 3:
if num % 2 == 0:
break # Exit the loop when num is 3
x += 1
if num % 2 == 0
print(x)
break # Exit the loop when
print("Hello,", num is 3
name)
print(idx, fruit)
continue # Skip even numbers
for value in person.values():
for idx, fruit in enumerate(fruits):
foo() # Outputs: 11 OUTPUT: even_numbe
print(name, "is", age, "years old")
greet("Alice") # Outputs: Hello, Alice
1 is odd
greet("Nat") #Prints Hello, Nat print(num)
2 is even
print(num) 3 is odd
print(num)
Quick Notes Page 3
print(num) 3 is odd
print(num)
print(value)
print(even_numb
4 is even
print(idx, fruit)
OUTPUT:
5 is odd
# Iterate over key-value pairs
1
6 is even
3
7 is odd
OUTPUT:
8 is even
0 apple
9 is odd
1 banana
for key, value in person.items():
10 is even
2 cherry
Quick Notes Page 4
print(key, ":", value)
OUTPUT:
name
age
city
John
30
New York
name : John
age : 30
Quick Notes Page 5
age : 30
city : New York
Quick Notes Page 6