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