Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views9 pages

Python Loops: SR - No. Loop Type and Description

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 9

Python Loops

Sr.No. Loop Type and Description


While Loop:
1. Repeats a statement or group of statements while a given
condition is TRUE. It tests the condition before executing the
loop body.
For Loop:
2. oExecutes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
Nested loop:
3. You can use one or more loop inside any another while, for or
do while loop.
For Loop: Example:
for letter in 'TutorialsCloud’:
print ('Current letter is:', letter )

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.

Accessing Values in Lists :

lst1 = ['computersc', 'IT', 'CSE’];


lst2 = [1993, 2016];
lst3 = [2, 4, 6, "g", "k", "s"];
print ("lst1[0]", lst1[0]) print ("lst3[2:4]",
lst3[2:4]

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

Deleting Elements From Lists:


Syntax:
del list_name[index_val];
Python Tuples
Some of the characteristics features of Tuples are:

•Tuples are defined in the same way as lists.

•They are enclosed within parenthesis and not within square braces.

•Elements of the tuple must have a defined order.

•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.

An example showing how to build tuples in Python

tupl1 = ('computersc', 'IT', 'CSE’);


tup2 = (6,5,4,3,2,1);
Accessing Value in Tuples:
tupl1 = ('computersc', 'IT', 'CSE’);
tupl2 = (1993, 2016);
tupl3 = (2, 4, 6, 8, 10, 12, 14, 16);
print ("tupl1[0]",
tupl1[0]) print ("tupl3[2:4]", tupl3[2:4])
Output:
tupl1[0] computersc
tupl3[2:4] (6, 8)

Updating Value in Tuples:


tupl1 = (2, 3, 4);
tupl2 = ('ab', 'cd’);
tupl3 = tupl1 + tupl2
print (tupl3
Output:
(2, 3, 4, 'ab', 'cd')

Deleting Elements From Tuples:


Syntax:
del tuple_name;

Example:
tupl3 = (2, 4, 6, 8, 10, 12, 14,
16); del tupl3;

You might also like