Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science

Similar presentations


Presentation on theme: "Introduction to Computer Science"— Presentation transcript:

1 Introduction to Computer Science
Programming with Python

2 Loops & Iteration Chapter 5 - Finding the Largest Value

3 Looping Through a Set

4 What is the Largest Number?
Imagine we have a list of 1 billion numbers, and our task is to find the largest number How would you do this?

5 What is the Largest Number?
Imagine we have a list of 1 billion numbers, and our task is to find the largest number How would you do this? Since computers can perform repetitive tasks in an extremely fast manner, the optimal and most efficient way is to let the computer find the largest number Write a program that loops through the list and find the largest number for loop Counter (in) And simply, a list of numbers Conditional statement to compare numbers

6 What is the Largest Number?
In this example, we will not use 1 billion numbers so we can debug the code quicker. But, next, we will try this on larger scale Given list [9, 41, 12, 3, 74, 15] Initialize a variable named “largest_so_far” and initialize it with -1, which indicates I have not seen any numbers so far If the next number on the list is 9, and it is greater than -1, so, the largest so far is 9 However, we keep looping until the end of the list and whenever we find a greater number, we update the value of largest_so_far Lets code! Happy coding!

7 Finding the Largest value

8 How about the smallest Value?
Tip, just a minor change

9 How about the smallest Value?
Tip, just a minor change


Download ppt "Introduction to Computer Science"

Similar presentations


Ads by Google