Imperative Programming
Imperative Programming
Imperative Programming
§ Python Programs
§ Interactive Input/Output
§ One-Way and Two-Way if Statements
§ for Loops
§ User-Defined Functions
§ Assignments Revisited and Parameter Passing
Python program
>>> print(0)
0
>>> print(0.0)
0.0
>>> print('zero')
zero
>>> print([0, 1, 'two'])
[0, 1, 'two']
Introduction to Computing Using Python
When executed:
>>> name = input('Enter your name: ')
1. The input request message is printed Enter your name: Michael
2. The user enters the input >>> name
'Michael'
3. The string typed by the user is >>> ========= RESTART =============
assigned to the variable on the left >>>
Enter your first name: Michael
side of the assignment statement Enter your last name: Lee
’Hello Michael Lee’
‘Welcome to the world of Python!’
first = input('Enter first name: ')
last = input('Enter last name: ')
line1 = 'Hello’ + first + '' + last + '…'
print(line1)
print('Welcome to the world of Python!')
input.py
Introduction to Computing Using Python
6
Introduction to Computing Using Python
One-way if statement
Indentation matters in Python!
if <condition>:
if temp > 86:
<indented code block>
print('It is hot!')
<non-indented statement>
print('Be sure to drink liquids.')
print('Goodbye.')
oneWay.py
True
temp > 86:
print('It is hot!')
False
Print('Goodbye.')
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Imperative Programming
9
Introduction to Computing Using Python
a) If age is greater than 62 then print 'You can get Social Security benefits’
b) If string 'large bonuses' appears in string report then print 'Vacation time!’
Indentation is critical!
if temp > 86: if temp > 86:
print('It is hot!') print('It is hot!')
print('Drink liquids.') print('Drink liquids.')
print('Goodbye.') print('Goodbye.')
True True
temp > 86: temp > 86:
print('Goodbye.') print('Goodbye.')
Introduction to Computing Using Python
Two-way if statement
if <condition>: if temp > 86:
<indented code block 1> print('It is hot!')
else: print('Be sure to drink liquids.')
<indented code block 2> else:
<non-indented statement> print('It is not hot.')
print('Bring a jacket.')
print('Goodbye.')
False True
temp > 86:
print(‘Goodbye.')
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Imperative Programming
13
Introduction to Computing Using Python
for loop
for loop
word = 'desktop'
>>>
stop
word = 'post' desktop
top
Done.
word = 'top'
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Imperative Programming
18
Introduction to Computing Using Python
============RESTART================
Write a “spelling” program that: >>>
Enter a word: omnipotent
The word spelled out:
1) Requests a word from the user o
2) Prints the characters in the word m
n
from left to right, one per line
i
p
o
t
e
n
t
>>>
Introduction to Computing Using Python
• To iterate over the range with step c: j, j+c, j+2c, j+3c, …, n-1
for i in range(j, n, c):
>>> for i in range(2,
range(4):16,
range(0):
range(1):
range(0, 3): 10):
2):
6): 4):
for i in range(j, n, 1): print(i)
for i in range(j, n):
>>>
2
0
1
>>>
3
6
4
12
2
4
10
8
>>>
3
5
14
12
>>>
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Imperative Programming
21
Introduction to Computing Using Python
a) 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10
b) 1, 2, 3, 4, 5, 6, 7, 8, 9
c) 0, 2, 4, 6, 8
d) 1, 3, 5, 7, 9
e) 20, 30, 40, 50, 60
Introduction to Computing Using Python
c
The general format of a function definition is a
>>> hyp(3,4)
5.0
>>>
import math
def hyp(a, b):
res = math.sqrt(a**2 + b**2)
return res
Let’s practice!
• Jupyter Notebook:
Canvas –> Modules –> Imperative Programming
26
Introduction to Computing Using Python
>>> hello('Julie')
Welcome, Julie, to the world of Python.
>>>
Introduction to Computing Using Python
>>> help(f)
Docstring (first line after function name) Help on function f in module
__main__:
def f(x):
'returns x**2 + 10' f(x)
res = x**2 + 10 # compute result returns x**2 + 10
return res # and return it
>>>
Introduction to Computing Using Python
3 [1, 2, 3] >>> a
'three' Traceback (most recent call
3.3 last):
File "<pyshell#66>", line
1, in <module>
a
NameError: name 'a' is not
<variable> = <expression> defined
>>> a = 3
>>> b = 2 + 1.3
1. <expression> is evaluated and its >>> c = 'three'
>>> d = [1, 2] + [3]
value put into an object of appropriate type
2. The object is assigned name <variable>
Introduction to Computing Using Python
a b c d
>>> a
3
3 [1, 7,
2, 3] >>> a = 6
'three' >>> a
3.3 6
6 >>> d
[1, 2, 3]
>>> d[1] = 7
The object (3) referred to by variable a does not >>> d
change; instead, a refers to a new object (6) [1, 7, 3]
• Integers are immutable
a b c d >>>
>>> aa
66
>>>
>>> bb
9 3.3
3.3
>>> b = a
>>> b
6
3 [1, 7, 9]
3] >>> a = 9
'three' >>> b
3.3 6
>>> c = d
6 >>> c
[1, 7, 3]
>>> d[2] = 9
>>> c
a and b refer to the same integer object [1, 7, 9]
c and d refer to the same list object
a now refers to a new object (9); b still refers to the old object (6)
• Because integers are immutable, a change to a does not affect the value of b
The list that c refers to changes; d refers to the same list object, so it changes too
• Because lists are mutable, a change to d affects c
Introduction to Computing Using Python
Swapping values
a b tmp Want: a b
Have:
3 6 3 6
>>> a
3
>>> b
6
>>> a,
tmp
b =b=
a=bb, a
>>> b = a
>>> a = tmp
Introduction to Computing Using Python
a x
3 5
>>> a = 3
def g(x): >>> g(a)
x = 5 >>>
lst l
[1,2,3]
[5,2,3]
>>> lst = [1,2,3]
>>> h(lst)
def h(l):
>>>
l[0] = 5