![Page 1: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/1.jpg)
1
Lecture 2: Lists & Loops
Autumn 2016/17
Programming
for Engineers
in Python
![Page 2: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/2.jpg)
2
Admin • Python installation status?
![Page 3: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/3.jpg)
3
Last Week Highlights • Memory and variables
• Different variables types (number, string, bool)
• Different operations for different types
• If-else statements
if expression: statement1 else: statement2
![Page 4: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/4.jpg)
4
Plan for today •While loop
• Lists
• For loop
![Page 5: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/5.jpg)
Algorithms and Pseudo Codes
How can I get to the university in the morning?
![Page 6: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/6.jpg)
Algorithms and Pseudo Codes
How can I get to the university in the morning?
1. Get up
2. Drink coffee if there is time
3. Get out of the house
4. Walk for four blocks
5. While waiting for the bus:
play angry birds
text friends
6. Get on the bus
…
![Page 7: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/7.jpg)
Think First, Code Later
How can I get to the university in the morning?
1. Get up
2. Drink coffee if there is time
3. Get out of the house
4. Walk for four blocks
5. While waiting for the bus:
play angry birds
text friends
6. Get on the bus
…
![Page 8: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/8.jpg)
While Loop
Used to repeat the same instructions until a stop
criterion is met
while expression:
statement1
statement2
…
expression
true
false
statement(s)
![Page 9: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/9.jpg)
9
Example - factorial
#factorial
n = 7
fact = 1
i = 1
while i <= n:
fact = fact * i
i = i + 1
print n, "! = ", fact
![Page 10: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/10.jpg)
Example – smallest divisor
# Find the smallest divisor
n = 2015
div = 2
while n % div != 0:
div = div + 1
print "Smallest divisor of", n, "is", div
Can the while loop above be infinite?
10
![Page 11: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/11.jpg)
Infinite Loops
i = 1
while i < 4:
print i
11
![Page 12: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/12.jpg)
12
Plan for today •While loop
• Lists
• For loop
![Page 13: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/13.jpg)
Lists
A list is an ordered sequence of elements.
Create a list in Python:
>>> my_list = [2, 3, 5, 7, 11]
>>> my_list
[2,3,5,7,11]
13
![Page 14: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/14.jpg)
Lists are Indexable
Remember this?
The same indexing + slicing works for lists!
14
H e l l o
0 1 2 3 4 5
-5 -4 -3 -2 -1
![Page 15: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/15.jpg)
Lists are Indexable
>>> my_list = [2, 3, 5, 7, 11]
>>> my_list[0]
2
>>> my_list[4]
11
>>> my_list[-3]
5
>>> my_list[5]
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
my list[5]
IndexError: list index out of range
15
11 7 5 3 2
4 3 2 1 0
-1 -2 -3 -4 -5
![Page 16: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/16.jpg)
Slicing
Slicing format: list_name[from:to:step(optional)]
>>> my_list = [1,2,3,4,5,6,7,8,9,10]
>>> my_list[1:5] # slicing
[2, 3, 4, 5]
>>> my_list[0:-1] # forward/backward indexing
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> my_list[::2] # add a step
[1, 3, 5, 7, 9] 16
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9 10
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
![Page 17: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/17.jpg)
Slicing # reverse
>>> my_list[::-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
# output is an empty list. This is NOT an error
>>> my_list[3:8:-2]
[]
# slicing does NOT change original list
>>> my_list
[1,2,3,4,5,6,7,8,9,10]
17
![Page 18: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/18.jpg)
Lists Lists can contain strings:
>>> days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
>>> days[3]
'Wed'
>>> len(days)
7
Lists can mix different types: >>> pi = ['pi', 3.14159, True]
# student: name, age, height, SAT >>> student = ['Roi', 21, 1.83, 782]
18
![Page 19: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/19.jpg)
Lists – Dynamic
Maintain a list of the students either by name or by id:
>>> students = ['Itay',9255587, 'Alon', 'Zohar',744554887]
>>> students[2]
'Alon'
• Michal decided to join the course, so we update the list:
# append - add an element to the end of the list
>>> students.append('Michal')
>>> students
['Itay', 9255587, 'Alon', 'Zohar', 744554887, 'Michal']
19
![Page 20: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/20.jpg)
Lists – Dynamic
• Alon wants to leave the course:
>>> students.remove('Alon')
>>> students
['Itay', 9255587, 'Zohar', 744554887, 'Michal']
remove removes only the first occurrence of a value.
20
![Page 21: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/21.jpg)
Nested Lists
>>> mat = [ [1, 2, 3],[4, 5, 6] ]
>>> mat[1]
[4,5,6]
>>> mat[1][2]
6
• What is len(mat) ?
21
![Page 22: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/22.jpg)
Nested Lists
>>> family = [‘Meir‘,
[‘Yossi‘,
[‘Yuval‘,
[‘Elisha‘]] ],
[‘Yitzhak‘,
[‘Meir‘], [‘Orna‘], [‘Evyatar‘] ],
[‘Gavri‘,
[’Uri‘], [‘Boaz‘]]]
22
![Page 23: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/23.jpg)
Range
An ordered list of integers in the range.
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(from, to) contains all integers k satisfying from ≤ k < to.
range(to) is a shorthand for range(0, to).
>>> range(2,10)
[ 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(-2,2)
[-2, -1, 0, 1]
>>> range(4,2)
[] 23
![Page 24: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/24.jpg)
Range
>>> type(range(3))
<type 'list'>
Step size:
range(from, to, step) returns:
from, from+step, from+2*step,…, from+i*step
until to is reached, not including to itself.
>>> range(0,10,2)
[0, 2, 4, 6, 8]
>>> range(10,0,-2)
[10, 8, 6, 4, 2] 24
![Page 25: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/25.jpg)
Range
>>> range(0, 10, -1)
[]
>>> range(0,10,0)
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
range(0,10,0)
ValueError: range() step argument must not be zero
25
![Page 26: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/26.jpg)
Sorting a list
26
<<<sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]
>>> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
Splits a string to words (tokens)
based on default delimiters.
Returns a list of strings.
Optional parameter specifying a
function to be applied on each list
element before sorting
sorted(list) creates a new list whose elements are sorted:
list.sort() sorts the original list:
![Page 27: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/27.jpg)
Lists documentation
Complete documentation on Python lists is available at:
https://docs.python.org/2/tutorial/datastructures.html
27
![Page 28: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/28.jpg)
28
Plan for today •While loop
• Lists
• For loop
![Page 29: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/29.jpg)
For Loop for element in iterable:
statement1 statement2 …
Run over all elements in the iterable (list, string, etc.)
Iteration 0: Assign element = iterable[0]
• Execute the statements
Iteration 1: Assign element = iterable[1]
• Execute the statements
…
• Variable element is defined by the loop!
29
![Page 30: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/30.jpg)
For Loop
determines the scope of the
iteration.
Note
No infinite lists in Python
No infinite for loops!
30
![Page 31: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/31.jpg)
For Example
Compute 1 + 2 + … + 100:
>>> partial_sum = 0
>>> for i in range(1,101):
partial_sum = partial_sum + i
>>> print "The sum is", partial_sum
The sum is 5050
Shortcut:
sum(range(1,101))
31
![Page 32: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/32.jpg)
32
For Example
# factorial
n = 8
fact = 1
for i in range(2, n+1):
fact = fact * i
print n, "! = ", fact
Syntactic sugar:
fact *= i is equivalent to fact = fact * i
![Page 33: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/33.jpg)
33
Fibonacci series
Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Definition
fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2)
en.wikipedia.org/wiki/Fibonacci_number Leonardo Fibonacci
1170-1250, Italy
![Page 34: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/34.jpg)
34
י'סלט פיבונאצ
![Page 35: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/35.jpg)
35
Fibonacci series
Write a program that for an integer n > 0,
prints the nth Fibonacci number.
![Page 36: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/36.jpg)
36
Fibonacci series - code
n = 10
if n < 2:
curr = n
else:
prev = 0
curr = 1
for i in range(2, n+1):
new = prev + curr
prev = curr
curr = new
print "The nth Fibonacci number is", curr
![Page 37: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/37.jpg)
For Loop and Strings
Iterate over strings:
name = "Kobe"
for letter in name:
print "Give me", letter
print "What did we get?", name
Give me K
Give me o
Give me b
Give me e
What did we get?
37
![Page 38: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/38.jpg)
Break – breaking loops
break terminates the nearest enclosing loop, skipping the
code that follows the break inside the loop.
Used for getting out of loops when a condition occurs.
Example:
for elem in lst:
if elem < 0:
print "First negative number is", elem
break
38
![Page 39: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/39.jpg)
# Find smallest divisor using for loop:
for div in range(2, n+1):
if n % div == 0:
break
print div
Break Example
39
![Page 40: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/40.jpg)
40
Example - Prime
n = 2013
for div in range(2,n):
if n % div == 0:
break
if n % div == 0:
print n, "is not prime"
else:
print n, "is prime"
![Page 41: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/41.jpg)
41
Example - Optimization
n = 2013
for div in range(2,int(n**0.5)):
if n % div == 0:
break
if n % div == 0:
print n, "is not prime"
else:
print n, "is prime"
range must accept argument
of the type int so we perform
casting on the result of the
power operation.
![Page 42: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/42.jpg)
42
Where is the bug?...
n = ???
for div in range(2,int(n**0.5)):
if n % div == 0:
break
if n % div == 0:
print n, "is not prime"
else:
print n, "is prime"
![Page 43: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/43.jpg)
43
Where is the bug?...
n = 4
for div in range(2,int(n**0.5)+1):
if n % div == 0:
break
if n % div == 0:
print n, "is not prime"
else:
print n, "is prime"
![Page 44: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/44.jpg)
Continue
44
The continue statement, continues with the next
iteration of the loop.
Example - create a list of unique elements:
>>> lst = [1,4,5,8,3,5,7,1,2] >>> uniques = [] >>> for x in lst: if x in uniques: continue uniques.append(x) >>> print uniques [1,4,5,8,3,7,2]
![Page 45: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/45.jpg)
45
for or while?
• In most cases it is more natural to use for
• In some cases it is better to use while
• for:
• Predefined number of iterations
• No need to initialize or advance the loop variable
• while:
• Unknown number of iterations
• Can specify a stop condition
![Page 46: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/46.jpg)
46
Programming Style
• Comments: #
• Meaningful variables names
Why is it important?
![Page 47: Programming for Engineers in Python - TAUcourses.cs.tau.ac.il/pyProg/1617a/lectures/2_lists_loops.pdf · for Engineers in Python . 2 Admin • Python installation status? 3 Last Week](https://reader030.vdocuments.net/reader030/viewer/2022021611/5e114a9f406ba203dc07e7bb/html5/thumbnails/47.jpg)
47
Bonus (if time allows)
• Python web environment: • http://pythontutor.com/visualize.html - choose the option “render all objects on heap”.
• (http://www.codeskulptor.org/)