05-List and Loop-1
05-List and Loop-1
• To access items from a list, we use the index number (0, 1, 2 ...). For
example,
range(5)
Out[1]: range(0, 5)
list(range(5))
Out[8]: [0, 1, 2, 3, 4]
Loop
• for loop
• while loop
Flowchart of Python for Loop
Python for Loop
• A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
• With the for loop we can execute a set of statements, once for each item
in a list, tuple, set etc.
apple
banana
cherry
Python for Loop
for x in “Apple":
print(x)
A
p
p
l
e
The range() Function
for x in range(6):
print(x)
0
1
2
3
4
5
for i in range(x):
print(i)
Enter range : 5
0
1
2
3
4
Python for Loop
2
3
4
5
Python for Loop
for x in range(10,0,-1):
print(x)
10
9
8
7
6
5
4
3
2
1
What does end =‘ ’ do in Python?
• The end parameter in the print function is used to add any string. At the end of the output of the print
statement in python.
• By default, the print function ends with a newline.
• Passing the whitespace to the end parameter (end=‘ ‘) indicates that the end character has to be
identified by whitespace and not a newline.
print("Comsats")
print("University")
Comsats
University
• (Ctrl+Enter)
Python for Loop
for x in range(10,0,-1):
print(x, end=' ')
10 9 8 7 6 5 4 3 2 1
Python Nested Loops
Outer_loop Expression:
Inner_loop Expression:
Statement inside inner_loop
Python Nested Loops
https://www.geeksforgeeks.org/python-nested-loops/
Python Nested Loops
x = [1, 2]
y = [4, 5]
for i in x:
for j in y:
print(i, j)
1 4
1 5
2 4
2 5
Python Nested Loops
for x in range(5):
print("Value of x:",x)
for y in range(1,5):
print("Value of y:",y,end=", ")
print("\n")
Value of x: 0
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,
Value of x: 1
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,
Value of x: 2
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,
Value of x: 3
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,
Value of x: 4
Value of y: 1, Value of y: 2, Value of y: 3, Value of y: 4,
Python Nested Loops
Value of x: 0
Value of y: 1
Value of y: 2
Value of y: 3
Value of y: 4
Value of x: 3
Value of y: 1
Value of y: 2
Value of y: 3
Value of y: 4
Value of x: 4
Value of y: 1
Value of y: 2
Value of y: 3
Value of y: 4
Python Nested Loops
for i in range(10):
for j in range(i):
print("*",end=' ')
print(" ")
*
**
***
****
*****
******
*******
********
*********
Python Nested Loops
for i in range(10):
for j in range(i):
print(j ,end=' ')
print(" ")
0
01
012
0123
01234
012345
0123456
01234567
012345678
Python Nested Loops
for i in range(10,0,-1 ):
for j in range(i):
print(j ,end=' ')
print(" ")
0123456789
012345678
01234567
0123456
012345
01234
0123
012
01
0
Python Nested Loops
x = [1, 2]
y = [4, 5]
for i in x:
for j in y:
print(i, j)
1 4
1 5
2 4
2 5
lst = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
var = int(input())
lst.append(var)
print(lst)
Enter number of elements : 2
orderList=[]
order = input("place your order :")
while not order == "q":
orderList.append(order)
order = input("place your order or press Q for quit :")
print("Hello : you order ", orderList)
While Loop
i=1
while i < 6:
print(i)
i = i+ 1
1
2
3
4
5
i=1
while i in range(0,10):
print("Hello world", i)
i=i+1
• Hello world 1
• Hello world 2
• Hello world 3
• Hello world 4
• Hello world 5
• Hello world 6
• Hello world 7
• Hello world 8
• Hello world 9
While Loop
print("Valid")
While Loop
print("Valid")
Sal = []
Tax= []
n = int(input("Enter number of elements : "))
for i in range(0, n):
salVar = int(input("Enter Your Sal :"))
Sal.append(salVar)
taxVar=salVar*.25
Tax.append(taxVar)
print(Sal)
print(Tax)
print(list(zip(Sal,Tax)))