0% found this document useful (0 votes)
2 views15 pages

Week 3 Slides

This document covers programming concepts in Python, focusing on loops, conditionals, and string functions. It includes examples and exercises for using 'while' and 'for' loops, as well as nested loops, to perform tasks such as counting and summing user inputs. Additionally, it introduces sorting algorithms like Bubble, Selection, and Insertion Sort.

Uploaded by

anniezw120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

Week 3 Slides

This document covers programming concepts in Python, focusing on loops, conditionals, and string functions. It includes examples and exercises for using 'while' and 'for' loops, as well as nested loops, to perform tasks such as counting and summing user inputs. Additionally, it introduces sorting algorithms like Bubble, Selection, and Insertion Sort.

Uploaded by

anniezw120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

INF1340H F

Programming for Data Science


Week 3

Amirsina Eskandarifar
Review last session

 Condition
 If
 Elif
 Else
 Input
 String functions
Example

 Write a program to ask the user to insert a number, and then print
how many 0s their number has.

 Now write a program to ask 3 times the same question, and every
time print number of 0s (what about 100 times?)

What should we do if we want to


run a block of code repeatedly?
Loop

 Do you want to do the same thing multiple times?


 Is there any repetition in your logic?
 Do you want to iterate on something?
 You should answer this:
 When to start/when to stop?
Loop: example i=1

 Let’s look at the previous


example:
 Write a program to ask the user i <= 10
for a number 10 times and each
time print number of 0s.
 What is iterator?

Iterator is a variable that changes every


iteration (or every loop), and the loop is
dependent on this variable to stop or
continue. a =input(‘num?’)
print(a.count(‘1’)
)
i = i+1
Loop: example
x = input(‘what is your
number?’)
i=1
 Sometimes you may use iterator for more
than just counting your iterations:
 Write a program to get a number from user and
then print its first 10 exponentiation:
i <= 10
x^1, x^2, x^3, …, x^10

print(x**i)
i = i+1
Loop: while
 While is a kind of loop that continues as long as the condition is true
 Let’s see how while loop looks like in Python
while (condition):
executable code 1
executable code 2
.
.
.
executable code out of loop

 Be careful! The while loop may go forever, unless you change the factors in the
condition
 Be careful! The indentation and tabulation is important to Python to understand
what block of code is part of the loop, what is not.
Loop: for
 for is another kind of loop in python that its iterator iterates over
a sequence of data in each iteration:

for (i in [1,2,3,4,5]):
executable code 1 (like print(i))
executable code 2
.
.
.
executable code out of loop

 range(0,5) -> range is kind of sequential data type in python to


define sequence of numbers easier
Loop: range
 range is kind of sequential data type in python to define
sequence of numbers easier
 a sequence includes the first number, and excludes the last number

for (i in [1,2,3,4,5]):

for (i in range(?,?)):
Loop: practice
 Write a program that:
 1- Ask the user for multiple numbers until they insert 000, and
reserve all inserted values
 2- Then, find and print the summation and average of inserted
numbers
 3- Then, find and print the min & max
Loop: practice 2
 Write a program that:
 1- Ask the user for both a sentence and a letter
 2- Then check if the sentence has that letter
 3- Also count number of appearance of that letter

 Obviously, don’t use built-in string functions or membership


operators
Nested Loop
 Why not having a loop inside another loop?
 The iterator of inner loop will be initialized again for each iteration of
outer loop
 Write a program to print 10x10 multiplication chart
Nested Loop: Practice
 Write a program that:
 1- Ask the user for both a sentence and a word
 2- Find out if the word is in the sentence or not (don’t use built-in string
functions or membership operators)
We can do pretty much everything
now
 With what we have learnt so far, we can write any program and
implement any logic or algorithm!
Sort Algorithms:
Bubble, Selection, and Insertion
Sorts
There is an array of number, and we want to sort them

1 9 2 8 5 4 3

 https://www.youtube.com/watch?v=JU767SDMDvA
 https://www.youtube.com/watch?v=g-PGLbMth_g
 https://www.youtube.com/watch?v=xli_FI7CuzA

You might also like