Loops and Fnction
Loops and Fnction
0
1
2
3
4
1
2
3
4
Example
list = [1, 2, 3, 4, 5]
for el in list:
print(el)
1
2
3
4
5
list = [1, 2, 3, 4, 5]
for el in list:
print(el)
else:
print("END")
1
2
3
4
5
END
blue
black
green
red
grey
1
2
3
4
5
6
7
8
9
for x in reversed(range(1,11)):
print(x)
10
9
8
7
6
5
4
3
2
1
Hello Class
1
3
5
7
9
1
4
7
10
credit_Card = "1234-5678-9012-3456"
for x in credit_Card:
print(x)
1
2
3
4
-
5
6
7
8
-
9
0
1
2
-
3
4
5
6
1
2
3
4
5
6
7
8
9
10
11
12
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
valid no 4
Lab Work
using for
1. Print the elements of the following list using a loop: [1, 4, 9, 16, 25, 36, 49, 64, 81,100]
2. Search for a number x in this tuple using loop: [1, 4, 9, 16, 25, 36, 49, 64, 81,100]
1
2
3
4
x=0
while not(1<=x<=100):
x = int(input("Please enter a number between 1 and 100:"))
print("valid no", x)
valid no 87
Hello Salim
name = input("Enter your name: ")
print(f"Hello {name}")
Hello Salim
print("bye")
bye
num = int(input("Enter a number between 1-10: "))
Your number is 7
Lab Work
1. Print numbers from 1 to 100.
2. Print numbers from 100 to 1.
3. Print the multiplication table of a number n.
4. Print the elements of the following list using a loop: [1, 4, 9, 16, 25, 36, 49, 64, 81,100]
5. Search for a number x in this tuple using loop: [1, 4, 9, 16, 25, 36, 49, 64, 81,100]
Nested Loops
Nested loop = A loop within nother loop (outer, inner)
outer loop:
inner loop:
while loop inside a while loop while x > 0: while y > 0: print("do something")
for loop inside a for loop for x in range(3): for y in range (9): print("do something")
while loop inside a for loop while x > 0: for y in range (9): print("do something")
for loop inside a while loop for x in range(3): while y > 0: print("do something")
Function
Functions help to organize code into logical blocks that
perform specific tasks.
Function = A block of reusable code. Place () after the
function name to invoke it
print("Happy birthday to you!")
print("You are old!")
print("Happy birthday to you!")
print()
def happy_birthday():
print("Happy birthday to you!")
print("You are old!")
print("Happy birthday to you!")
print()
happy_birthday()
happy_birthday()
happy_birthday()
def happy_birthday(name):
print(f"Happy birthday to {name}!")
print("You are old!")
print("Happy birthday to you!")
print()
happy_birthday("Bro")
happy_birthday("Salim")
happy_birthday("Warith")
happy_birthday("Bro", 20)
happy_birthday("Salim", 40)
happy_birthday("Warith", 15)
Hello Brocode
Your bill of $ 42.50 is due: 01/01
Hello Harith
Your bill of $ 100.00 is due: 01/02
print(full_name)
Project
1. write a program for weight conversion
2. write a program for temperature conversion
3. write a program for email slicer
4. write a program for compound interest
5. write a program for countdown timer
6. write a program for shopping cart
7. create a quiz game
8. write a program for concession stand
9. write a program for number guessing game
10. write a program for dice roller
11. write a program for encryption
12. develop a project for banking
13. develop a project for slot machine