You are on page 1of 32

Lecture 2

 Program Design: Control Structure


 Program Design Techniques
 Why Python?
 Python History
 Python Success
 Python Development Environment - Installation
 Displaying Hello World message

2
Program Design: Control Structures
 Sequence:  is the order which the instructions are
executed by the computer.
 Selection: select which path of an algorithm to

execute depending on some criteria.


 Iteration: repeat a set of operations a specific

number of times or until some condition occurs.

3
Program Design: Control Structures
Sequence
A sequence control structure is simply a series of
procedures that follow one another.

Statement Statement Statement ...

4
Program Design: Control Structures
Selection
 The selection (if-then-else) control structure involves a choice: if
a certain condition is true, then follow one procedure; else, if
false, follow another.

When more than two possible choices exist, the case control
structure can be used instead.

True Statement 1
Condition ...
Statement
False Statement 2

5
Program Design: Control Structures
Iteration
 Loop is an operation that repeats until a certain condition is met.
 A looping (iteration) control structure can take two forms.
 With the do-while structure, the loop is executed as long as a
condition is true; with the do-until structure, the loop continues until a
certain condition becomes true.
False
...
Condition
Tr u
e
Statement

6
Algorithm Design Techniques:
Flowchart
 use geometric symbols and familiar relational operators
to provide a graphic display of the sequence of steps
involved in a program.

7
 Draw a flowchart that asks the user to enter the
length of the square side and then calculate the area.

Start

read the
side length

Area = Length2

Stop

8
 The flowchart segment and code below show
how a decision structure in a flowchart is
expressed in C++ as an if/else statement.

NO YES
x < y?

Calculate a Calculate a
as x plus y. as x times 2.

9
If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400
If years_employed = 1, If years_employed is
CASE
bonus is set to 100 years_employed any other value, bonus
is set to 800

1 2 3 Other

bonus = 100 bonus = 200 bonus = 400 bonus = 800

10
 The flowchart segment below shows a
repetition structure expressed in C++ as a
while loop.

YES
x < y? Add 1 to x

11
•The “A” connector
indicates that the second START A

flowchart segment begins


where the first segment
ends.

END
A

12
Algorithm Design Techniques:
Pseudocode
uses English-like statements in place of the graphic
symbols of the flowchart.

Unlike a flowchart, pseudocode is easy to modify


and can be embedded into a program as comments.

No standard set of rules exists for writing


pseudocode, although a number of conventions have
been developed.

13
Write a pseudocode algorithm that computes the area of
the circle
Problem: Calculate and display the area of the circle
Discussion: Uses mathematical knowledge, that is area = r2

Output: area of the circle


Input: radius of the circle
Processing: Find the area of the circle;
area = 3.14 * radius * radius

pseudocode algorithm
input radius
set area to 3.14 * radius * radius
output area
14
Write a pseudocode algorithm that display passed if student
grade is greater than or equal to 60 and display failed if
student grade is less than 60

pseudocode algorithm
If student's grade is greater than or equal to 60
Print "passed"
else
Print "failed"
endif

15
Write a pseudocode algorithm that computes the sum of the
numbers between 1 and 10

pseudocode algorithm
set counter to 1
set sum to 0
while counter <= 10
set sum to sum + counter
increment counter
output sum

16
17
Calculate Pay with Overtime
Begin
input hours, rate
if hours <= 40 then
pay = hours * rate
else
pay = 40 * rate + (hours – 40) * rate * 1.5
print pay
End

Pseudocode

flowchart
18
Average of 10 Numbers

Begin
i = 0
sum = 0
while i < 10
input x
sum = sum + x
Increment i
avg = sum / 10.0
print avg
End

Pseudocode

flowchart
19
Solving the equation 2x2 + x + 5 for
all x between 5 and 10
Begin with x = 5. Next, substitute 5
for each value of x in equation
and then solve it (answer=60).
Now take the next x, which is 6
and so on till x = 10.

Pseudocode
Flowchart 20
In the last 10 years, Python has become one of the most important
languages for data science, machine learning, and general software
development in academia and industry.

21
 Python was developed in the early 1990s by Guido
van Rossum (1991).
 He needed to carry out repetitive tasks for
administering computer systems.
 He was dissatisfied with other available languages
that were optimized for writing large and fast
programs.
 He needed to write smaller programs that didn’t have to run at
optimum speed.
 Therefore, he designed a language that made it very easy to work
with complex data.
 Python has evolved considerably since its beginnings. Van Rossum
is still the principal author of the language, but the effort now
includes many volunteers.

22
 Python has become popular for business, scientific, and
academic applications and is very suitable for the
beginning programmer
 Python has become one of the most popular interpreted
programming languages, along with Perl, Ruby, and
others.
 Python is often called scripting language, as it can be
used to quickly write small programs or scripts to
automate other tasks.

23
 From IEEE spectrum 2017: programming languages
ranking

24
 From IEEE spectrum 2020: programming languages
ranking

25
 From Stackoverflow.com 2021:
insights.stackoverflow.com/trends

26
There are many reasons for the success of Python:
 Python has a much simpler and cleaner syntax than other

popular languages such as Java, C, and C++, which makes


it easier to learn.
 It supports programs in an interactive environment, which

encourages experimentation and rapid turnaround.


 Python is a suitable language not only for doing research

and prototyping but also for building production systems.


 Python is also very portable between computer systems.

The same Python program will run, without change, on


Windows, UNIX, Linux, and Macintosh.
 Python has developed a large and active scientific

computing and data analysis community.


27
1. Download Python 3.x from official Python website
https://www.python.org/downloads/
2. Set environment variable:
Right click on Computer icon, then click on properties
Then click on Advanced system systems, then click on
Environment variables
In “User variables for user”, search for variable “PATH”, then
double click to edit it, then append line by ; following by
python path ( for example:
C:\Users\....\AppData\Local\Programs\
Python\Python38\; ) , after that restart the computer

28
3. To run Python, you can use either:
A. Official Python Shell which is IDLE
Open command window, then type IDLE through
B. (Optional) Install third party IDE such as
i. Pycharm .. Link: https://www.jetbrains.com/pycharm/
OR
ii. Spyder .. Link: https://www.spyder-ide.org/
OR
iii. PyDev .. Link: https://www.pydev.org/

 Note: Check Best Python IDEs And Code Editors in 2021


https://www.softwaretestinghelp.com/python-ide-code-editors/
29
 To display the “Hello World” message: Through
Python shell or through running python file
(containing source code)
 From Python Shell:

Open IDLE python shell


Type
print(“Hello World”) and press Enter

30
 From Python File (containing source code)
Open IDLE python shell
Click on File then New File
(Or press Ctrl+N)

Type print(“Hello World”)

Then click on File then File Save As …


Type test.py in the file name field
To run python File, click on Run then click on Run Module (or
press F5 key)

31

You might also like