Python Loops: SR - No. Loop Type and Description
Python Loops: SR - No. Loop Type and Description
Python Loops: SR - No. Loop Type and Description
Output:
Current letter is : T
Current letter is : u
Current letter is : t
Current letter is : o
Current letter is : r
Current letter is : i
Current letter is : a
Current letter is : l
Current letter is : s
Current letter is : C
Current letter is : l
Current letter is : o
Current letter is : u
Current letter is : d
While Loop: Example:
#initialize count variable to 1
count =1
while count < 6 :
print (count)
count+=1
#the above line means count = count + 1
Output:
1
2
3
4
5
Nested Loop:
Example: for g in range(1, 6):
for k in range(1, 3):
print ("%d * %d = %d" % ( g, k,
g*k))
Output: 1 * 1 = 1
1 * 2 = 2
2 * 1 = 2
2 * 2 = 4
3 * 1 = 3
3 * 2 = 6
4 * 1 = 4
4 * 2 = 8
5 * 1 = 5
5 * 2 = 10
Python Lists
The list is a most versatile datatype available in Python which can be written as
a list of comma-separated values (items) between square brackets.
Output:
lst1[0] computersc
lst[2:4] [6,’g’]
Updating lists:
lst1 = ['computersc', 'IT', 'CSE']; print
("Second value of the list is:") print
(lst1[1]) lst1[1] = 'Robotics' print ("Updated
value in the second index of list is:") print
(lst1[1])
Output:
Second value of the list is:
IT
Updated value in the second index of list is:
Robotics
•They are enclosed within parenthesis and not within square braces.
•Negative indices are counted from the end of the tuple, just like lists.
•Tuple also has the same structure where the values are separated by commas.
Example:
tupl3 = (2, 4, 6, 8, 10, 12, 14,
16); del tupl3;