Loops and Conditionals¶
In programming often times there is a requirement the a particular set of instructions need to be repeated certain number of times. This can be easily achieved using loops
. These statements execute a code block repeatedly until a condition is met. The most common loop that is used to repeat a block of code for fixed number of times is for loop
.
List¶
A collection of items is refered to as a list
. A list can have elements of same data type or multiple data types. Lists are mutable data types i.e. the contents of the collection can be changed.
fruits = ["apple","grapes","mango"]
print(fruits)
print(fruits[1])
['apple', 'grapes', 'mango']
grapes
fruits.append("banana")
print(fruits)
['apple', 'grapes', 'mango', 'banana']
fruits.pop()
print(fruits)
['apple', 'grapes', 'mango']
fruits.pop()
print(fruits)
['apple', 'grapes']
veggies = ["tomato", "potato"]
fruits.extend(veggies)
print(fruits)
['apple', 'grapes', 'tomato', 'potato']
For loop – iterating through list elements¶
list_of_numbers = [1, 2, 3, 4, 5]
for var1 in list_of_numbers:
print(var1*2)
print("Done")
2
4
6
8
10
Done
Same result can be achieved using the range
function which has a default step size of 1.
new_list = range(1,6)
for x in new_list:
print(x*2)
print("Done")
2
4
6
8
10
Done
We can change the step size by adding a third argument to the range
function.
for x in range(1,6,2):
print(x)
1
3
5
Conditionals¶
To test a particular condition, if statement is used with an argument having a Boolean
operator. Multiple conditions can be tested in one code block using elif
. The else
keyword take no argument such that its code block gets executed when the if
condition (and elif
) conditions are return False
.
x = 5
y = 6
if(x>y):
print("X is greater than Y")
else:
print("X is less than Y")
X is less than Y
Write a program that takes two numbers as input and prints the result of their comparison¶
x = 5
y = 5
if(x>y):
print("X is greater than Y")
elif(x<y):
print("X is less than Y")
else:
print("X is equal to Y")
X is equal to Y
While loop¶
While
loop is another frequently used construct to perform task repeatedly when the number of iterations is not fixed. This loop is executed till certain condition is met irrespective of number of iterations.
While
loop has an implicit if
conditional statement.
x = 5
while (x>2):
print(x)
x = x-1
print ("Loop is over")
When writting a while loop you must ensure that the condition is met at some point within the loop otherwise the loop will iterate infinitely. E.g. in the above code if the condition within while loop is changed to x>2 then it result in an infinite loop.
Controlling the execution of loops¶
Many a times it is require to skip the execution of loop for certain steps or to terminate the loop altogether. Python has some reserved keyword to facilitate this task. continue
can be used to jump to subsequent iteration of the loop and break
can be used to end the loop.
for x in range (1,10):
if (x == 5):
continue
print(x)
print("Done")
1
2
3
4
6
7
8
9
Done
for x in range (1,10):
if (x == 5):
break
print(x)
print("Done")
1
2
3
4
Done
Assignment¶
Take a list of first 10 numbers and print the square for even numbers.