03 Program Flow and Control
03 Program Flow and Control
September 2, 2023
[1]: # In this unit we look into the basic program flow and control structures.
# and use this alias to refer to functionality within the module in a compact␣
↪manner
# We will be covering numpy in detail later, but we will start using some of␣
↪it's functions
[3]: # Just to start using numpy, let's create a list of random numbers (type :␣
↪real) using the numpy.random module
random_numbers = np.random.rand(10)
print(random_numbers)
[4]: # We want to write our own code to calculate the 'sum' of the numbers in the␣
↪list
# Also note the syntax of the for statement. The 'for' statement line ends with␣
↪a ':'
1
# and the next line is indented. In Python this signals the beginning of a new␣
↪block
# So long as the subsequent statements are indented, they all belong to the␣
↪'forblock
# The average of the list is simply the sum divided by the number of elements␣
↪in the list
my_sum = 0
list_len = len(random_numbers)
i = 0
# Notice that we have created a block of two statements in the while loop
[6]: # Q: When will you use 'for' and when will you use 'while'?
[7]: # For some strange reason, if we want the sum of only those numbers greater␣
↪than 0.5 ...
my_sum = 0
2
my_count = 0
for i in random_numbers:
if(i > 0.5):
my_sum += i
my_count += 1
print(f"There are {my_count} numbers greater than 0.5 in the list, and their␣
↪Sum is : {my_sum}")
# Notice that a new 'block' of two statements is created for the 'if' statement
There are 3 numbers greater than 0.5 in the list, and their Sum is :
2.255671171923966
[8]: # A couple of keywords are relevant in the context of for, while and if␣
↪statements
my_sum = 0
my_count = 0
for i in random_numbers:
if(i > 0.5):
my_sum += i
my_count += 1
if(my_sum > 1.0): # If the sum exceeds 1.0, stop the loop by breaking␣
↪out of it
break
my_sum += i
my_count += 1
3
print(f"The list: {random_numbers}")
print(f"There are {my_count} entries in the list with value less than 0.5.␣
↪Their sum is : {my_sum}")
[10]: # Of course, we could have avoided the 'manual' summing by using the␣
↪appropriate function in the numpy module
print(np.sum(random_numbers))
print(np.average(random_numbers))
3.5706369890466965
0.35706369890466966
[16]: # We have started talking about 'function's, what are they? How to create them?
# Functions allow you to create reusable blocks of code that get executed when␣
↪the function is invoked (i.e. called)
def sum_of_numbers(a_list):
my_sum = 0
my_count = 0
for i in a_list:
my_sum += i
my_count += 1
# The above statements defined a function, and we can now call it with multiple␣
↪lists, one by one ...
# Notice that the list returns two values ... sum and count ... and we need two␣
↪variables to store them
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
4
print(sum_of_numbers(list1))
print(sum_of_numbers(list2))
print(sum1, count1)
print(sum2, count2)
# The functions np.sum and np.average are functions defined in this manner and␣
↪packaged into 'numpy'
(15, 5)
(40, 5)
15 5
40 5
[13]: # We now know the very basics of Python: types, variables, control structures,␣
↪functions, ...
# ... but for now, we will use the basics to solve some real world problems