Conditionals and Loops usage is imperative in building a strong foundation for a career that requires one supporting either development or infrastructure operations.
← GO BACK : Python basics: print statement, reading user input, string and array operations
Unlike curley braces marking the begin and end of blocks on other Languages like C, all the statements inside a block shoud be equally intended, for python to know the beginning and end of a block.
Executes something based on conditions.
create variables for two persons with their roll number and age.
person_number1 = 10 person_number2 = 17 person_number1_age = 60 person_number2_age = 40
call out person-1 , if he has the smaller roll number
if (person_number1 < person_number2): print "call out person-1 "
call out person-1
call out the person that has the smaller roll number.
if (person_number1 < person_number2): print "call out person-1 " else: print "call out person-2"
call out person-1
Call out the person that has the smaller roll number.
Also, Check if they have the same roll numbers
if (person_number1 < person_number2): print "call out person-1 " elif (person_number2 > person_number1): print "call out person-2" else: print "They both have the same number!?"
call out person-2
Create variables, but with same roll number for both.
person_number1 = 10 person_number2 = 10 person_number1_age = 60 person_number2_age = 40
Call out the person that has the smaller roll number.
Check if they have the same roll numbers
If they do, call out the elderly person first
if (person_number1 < person_number2): print "call out person-1 " elif (person_number2 < person_number1): print "call out person-2" else: print "They both have the same number! ?" print "Call out the elderly first" if (person_number1_age > person_number1_age): print "call out person-1 , he is the elder one" else: print "call out person-2, he is the elder one"
call out person-2, he is the elder one
Conditionals would do just fine as long as the number of variables are limited.
Assuming a large number of variables, it would only make sense to use a looping mechanism to loop through the list.
In an array called ‘arr’ containing people’s ages, remove the ones that are under 18.
arr=[33, 11, 19, 12, 99, 24, 88, 67] for age in arr: if (age<18): #check if the age number is less than 18 print "under age!" position=arr.index(age) #get the index position of number in array arr.pop(position) #remove the number
under age! 11 under age! 12
print arr #print the array again.
[33, 19, 99, 24, 88, 67]
Call the oldest person out first, by looking at an array of their ages.
while ( len(a) != 0 ): #Do the below items while array is not empty. smallest=a.index(max(a)) #Gets the position of the element that has the smallest value in an array a.pop(smallest) #Removes the element with smallest value print a #print the array
99 [33, 19, 24, 88, 67] 88 [33, 19, 24, 67] 67 [33, 19, 24] 33 [19, 24] 24 [19] 19 []
break stops execution and exits the loop.
Let’s Create a array called basket with differents fruits as array elements.
basket = ["apples","mangos","bananas","oranges","coconuts"] for fruit in basket: print "checking item", fruit if (fruit == "apples"): print ("apples found")
checking item apples apples found checking item mangos checking item bananas checking item oranges checking item coconuts
In the above example, we could see that even after apples are found, the iteration continues.
break is useful when we are sure when to stop the loop from going further.
for fruit in basket: print "checking item", fruit if (fruit == "apples"): print ("apples found") break
checking item apples apples found
In the above example, since we know there is an occurance of apples only once, we break when its found.
continue on the other hand continues to the next iteration of the loop and if placed conditionally, it would not execute the statements inside the block if the condition is met.
for i in basket: if (i == "bananas"): continue print "checking out",i
checking out apples checking out mangos checking out oranges checking out coconuts