In this part, we are going to look at the logical operators available in Python. We will also look at loops, more precisely for
loops and while
loops. In the last section, we looked at Data Types and Variables now, we will use them in our examples.
Common logical operators
The operators are commonly used to evaluate the truthness of a statement or a condition. For example, if we need to compare a variable, we will use the following operators or a combination.
- Less than ( < )
- Greater than ( > )
- Equal to ( == )
- Not equal to ( != )
- And ( and )
- OR ( or )
- Special ( not, is )
var1 = 10
var2 = 15
# Less than or less than or equal to
var1 < var2
var1 <= var2
# as we all know 10 is less than 15 so the above two statement evaluates true.
# Greater than or grater than or equal to
var1 > var2
var1 >= var2
# Equal to
var1 == var2
# Not equal to
var1 != var2
# "and" operator "or" operator
True and False
True or False
# to compare objects, let's try to compare the type of the variable.
type(var1) is int
type(var2) is not float
# this will be evaluated to true, because, the type of var1 is integer.
Let's put the above operators into action.
# let's define two dictionaries contains marks of 2 students.
student1 = {
'Name' : 'Jhon',
'Gender' : 'M',
'DOB' : '11-Mar-1988',
'Maths' : 75,
'Chemistry' : 79,
'Physics' : 55
}
student2 = {
'Name' : 'Mary',
'Gender' : 'F',
'DOB' : '28-June-1993',
'Maths' : 98,
'Chemistry' : 88,
'Physics' : 55
}
# to access the member of a dictionary, we can use the [] (square brakets) as shown below,
if student1['Maths'] >= student2['Maths']:
print(f"{student1['Name']} obtained more marks in Maths")
else:
print(f"{student2['Name']} obtained more marks in Maths")
# Let's write an elif block to check if any of the above students scored more than 90 in chemistry.
if student1['Chemistry'] >= 90:
print(f"{student1['Name']} obtained more than or equal 90 in Chemisty")
elif student2['Chemistry'] >= 90:
print(f"{student2['Name']} obtained more than or equal 90 in Chemisty")
else:
print(f"No one had scored more than or equal 90 in Chemisty")
Loops or Iterations
At one point, we all wanted to perform an operation a number of times over and over again. The simplest solution would be to copy and duplicate the code that we want to repeat. However, there is an efficient way to do so and that's where the loops come in handy.
There are two types of loops in python.
For
LoopWhile
Loop
To use for loop we should also know about two important functions
Range()
- The range function returns a sequence of numbers. If only one argument is supplied the function returns a sequence starting from 0 less than the argument supplied. The function also takes two or three arguments (start, end, [steps])
Enumerate()
- Enumerate function returns the index and the item from the iterable. We need to unpack the index and value to consume in our logic.
# Let's look at range() and enumerate() function in action.
numbers = range(10)
print(list(numbers))
#this will generate numbers from 0 to 9, then we cast it to a list
number_from_to = range(5, 10)
print(list(number_from_to))
#this will generate numbers from 5 to 9
even_number = range(0, 10, 2)
print(list(even_number))
#this will generate a squence of even numbers less than 10
# List of marks scored by 10 students
Marks = [90, 45, 87, 69, 78, 84, 68, 98, 73, 72]
number_of_marks = len(Marks)
# let's wirte a logic to caluate the average score using "for" loop and "while" loop
sum_of_marks = 0
average_mark = 0.0
for i in range(number_of_marks):
sum_of_marks = sum_of_marks + Marks[i]
average_mark = sum_of_marks / number_of_marks
print(f'The average mark using "for" loop & range is {average_mark}')
# Now, let's replicate the same logic using a while loop.
sum_of_marks = 0
average_mark = 0.0
counter = 0
while(counter < number_of_marks):
sum_of_marks = sum_of_marks + Marks[counter]
counter+=1
average_mark = sum_of_marks / number_of_marks
print(f'The average mark using "while" loop is {average_mark}')
# Let's look at how to use the enumerate function to do the same task.
sum_of_marks = 0
average_mark = 0.0
for index, mark in enumerate(Marks):
sum_of_marks = sum_of_marks + mark
average_mark = sum_of_marks / number_of_marks
print(f'The average mark using "for" loop & enumerate is {average_mark}')
# Lastly, we can replicate the same logic using for loop alone, without using range or enumerate.
sum_of_marks = 0
average_mark = 0.0
for mark in Marks:
sum_of_marks = sum_of_marks + mark
average_mark = sum_of_marks / number_of_marks
print(f'The average mark using "for" loop alone is {average_mark}')
Great!
We have covered one of the foundational concepts in any programming language.
Click here to download the Jupiter notebook.