E&C ENGR 216/217
Engineering Computation
Python Programming
Lecture 3
Muhammad Talha
Graduate Teaching Assistant
Electrical and Computer Engineering Department
Email: mtgcf@[Link]
Office Hours: 16:00 – 17:00 PM – Tue/Thu
FH: 506
1
Review
• Object naming convention
• Python Datatypes
• Int, Float, Bool, Str, List, Tuple, Dict, Set
• Python Operators
• Arithmetic Operators
• Logical Operators
2
Algorithm
Computer system is
INPUT Black Box OUTPUT
3
Algorithm
INPUT Algorithm OUTPUT
4
Algorithm Example in Real Life
• We are given an English Dictionary and we are asked to find definition
of a word “Physics”.
• What are the steps involved in finding a word in a dictionary?
5
Steps: Divide and Conquer Search Algorithm
1. Load the dictionary
2. Take input
3. Divide in half
4. Check if word in turned page:
i) Print the definition
ii) Exit program
5. Else if check word in right half:
i) Ignore left half
ii) Go to line 3
6. Else if check word in left half:
i. Ignore right half
ii. Go to line 3
6
Error Handling
1. Load the dictionary
2. Take input
3. Divide in half
4. Check if word in turned page:
i) Print the definition
ii) Exit program
5. Else check if word in right half:
i) Ignore left half
ii) Go to line 3
6. Else if check word in left half:
i. Ignore right half
ii. Go to line 3
7. Else
i. Print “Word not found !”
7
Looping
1. Load the dictionary
2. Take input
3. Divide in half
4. Check if word in turned page:
i) Print the definition
ii) Exit program
5. Else if check word in right half:
i) Ignore left half
ii) Go to line 3
6. Else if check word in left half:
i. Ignore right half
ii. Go to line 3
7. Else
i. Print “Word not found !”
8
Function or Methods
1. Load the dictionary
2. Take input
3. Divide in half
4. Check if word in turned page:
i) Print the definition
ii) Exit program
5. Else if check word in right half:
i) Ignore left half
ii) Go to line 3
6. Else if check word in left half:
i. Ignore right half
ii. Go to line 3
7. Else
i. Print “Word not found !”
9
Conditions
1. Load the dictionary
2. Take input
3. Divide in half
4. Check if word in turned page:
i) Print the definition
ii) Exit program
5. Else if check word in right half:
i) Ignore left half
ii) Go to line 3
6. Check if check word in left half:
i. Ignore right half
ii. Go to line 3
7. Else:
i. Print “Word not found !”
10
Boolean expression
1. Load the dictionary
2. Take input
3. Divide in half
4. Check if word in turned page:
i) Print the definition
ii) Exit program
5. Else if check word in right half:
i) Ignore left half
ii) Go to line 3
6. Else if check word in left half:
i. Ignore right half
ii. Go to line 3
7. Else:
i. Print “Word not found !”
11
File Handling
1. Load the dictionary from file
2. Take input
3. Divide in half
4. Check if word in turned page:
i) Save the definition in file
ii) Exit program
5. Else check if word in right half:
i) Ignore left half
ii) Go to line 3
6. Check if word in left half:
i. Ignore right half
ii. Go to line 3
7. Else
i. Print “Word not found !”
12
Control Structure
Control Structure
❑ Sequential
❑ Selection
❑ Iteration
Sequential
Selection or Conditional
Statement
Selection or condition
• Selection helps programs make choices based on what is given and
what is computed.
Note that == is equality,
= is assignment
Python if statement
if boolean expression :
#perform some task 1
#task 2
#some other code
• evaluate the boolean (True or False)
• if True, execute all statements inside the if statement (task 1)
• If False, skips task 1 and executes rest of the code
Warning about indentation
• Elements of the suite must all be indented the same number of
spaces/tabs
• Python only recognizes suites when they are indented the same
distance (standard is 4 spaces)
• You must be careful to get the indentation right to get suites right.
Python Selection, Round 2
if boolean expression:
task1
else: The process is:
task2
• evaluate the boolean
• if True, run task1
• if False, run task2
Python Selection, Round 2
if boolean expression1:
task1
elif boolean expression2:
task2
else:
task3
Repetition/Iteration or
Looping
Repetition/Iteration or Loop
• The ability to execute instructions—especially decisions—over and
over.
• Application:
• Repeatedly making billions of simple decisions per second allows
the computer to make complex decisions to complete complex
tasks.
• Python offers two different styles of repetition: while and for.
26
The while statement
• Allows to repeat a suite of Python code as long as some condition (Boolean
expression) is True.
• When the condition becomes False, repetition ends, and control moves on to the
code following the repetition.
The for statement
• Examines all elements of a collection or list, one at a time, allowing
performing some operations on each element.
27
More on while statement
1. Program enters the while construct and evaluates the Boolean expression
2. If expression is True, then associated while suite executed
3. At the end of suite, control flows back to the top of the while, where the Boolean
expression is reevaluated.
4. If the Boolean expression is True, the loop executes again.
5. If False, while suite is skipped and the code following the while loop is executed.
28
while loop
• Top-tested loop (pretest)
• test the boolean before running
• test the boolean before each iteration of the loop
while boolean expression:
suite
Repeat while the boolean is true
• while loop will repeat the statements in the suite while the boolean is
True (or its Python equivalent)
• If the Boolean expression never changes during the course of the loop,
the loop will continue forever.
The for loop
for element in collection:
for statement blocks
• The variable element is a variable associated with the for loop that is assigned
the value of an element in the collection.
• The variable element is assigned a different element during each pass of the
for loop.
• Eventually, element will be assigned to each element in the collection.
• The variable collection is a collection that acts as the source of elements.
• The keyword in precedes the collection.
• Like the if and while statement, the for statement has a header and an
associated statement block.
32
Fig. Operation of a for loop.