You are on page 1of 35

Chapter 6

Python Programming

CSC1400 H.Talei 1
ILOs
An Introduction
Steps toward solving a problem
Algorithm
Natural Language, Pseudo Code, Flowcharts
Python Programming
Input/output, Data types.
Selection Statements
Loops
Exercises

CSC1400 H.Talei 2
Introduction
Computer programming or coding is the
process of designing, writing, testing, and
maintaining the source code of computer
programs. This source code is written in
one or more programming languages such
as Java, C++, C#, Python, etc.. The
purpose of programming is to create a set
of instructions that computers use to
perform specific operations or to exhibit
desired behaviors.

CSC1400 H.Talei 3
- Solve problems

Why- Mainly to use computer efficiency:


programming??
time and accuracy
Example: Calculating the GPA for
every student in a university.

CSC1400 H.Talei 4
Steps
toward
Before building
you write a software
a software, you need
to through some steps:
Requirements Specification
Problem Analysis
Algorithm Design
Coding
Testing
Maintenance

5
Step 1: Requirements Specification

In this step, you should understand


clearly the problem. You need to go
through the following steps:
Read the Problem Handout carefully
Underline the important points and
eliminate the useless information
To get more clarifications, you should ask
client to know exactly his/her need

6
Step 2: Problem Analysis
At this step, you should have already
understood the problem.
The next step now is to identify the input
and the output
You need also to write down any needed
Formula that would help you in
producing the Output

7
Step 3: Algorithm Design (1)
This is the most important step
An algorithm is a set of steps when
followed, it solve the problem
Make sure that you use the top
down design: decomposing the
main problem into sub problems
You need now to write an algorithm
that solves all the sub problems

8
Step 3: Algorithm Design (2)
Writing an algorithm can be done
using one of these methods:
Natural Language
Pseudo Code
Flow Charts

9
Step 4, 5, 6: Coding, Testing,
Maintenance
Coding: consists of translating your
algorithm to a code: you will use a high level
language
Testing: you need to check if the program
works as desired. Make sure that you dont use
only one set of data but rather different ones
Maintenance: Correct any errors that would
appear when the program is executed

10
Example 1
We need to write a program that reads 3
grades from the user and print the valid
ones
Problem Requirement:
Check whether a grade is a valid one or
not This process has to be repeated 10
times
2. Problem Analysis:
Input: 10 grades
Output: Print out the valid grades
formula: any formula to use??
11
Example 1: cont
3. Algorithm:
- Ask the user for a grade
- Print out the grade if it is a valid one
- Repeat the above steps 9 times

12
Example 1: coding

13
Algorithm: a very important
step

Pseudo Code

14
Pseudo Code: for previous
problem
i=1
Repeat
Ask for a grade
If grade>=0 and grade<=100
Print it
i=i+1
Until i > 10

15
Flowcharts

16
Flowcharts (second lab)
Flowchart: a graphical way of writing algorithms
Rectangle is used for calculations
Parallelogram is used for input and output
Diamond is used as decision
Symbols are connected by arrows to represent
the order of the operations
Any flowchart should have a start and an end

Start End

17

17
Flowchart for the previous
exercise using RAPTOR

CSC1400 H.Talei 18
Python Programming

CSC1400 H.Talei 19
Python Programming
From Python.org: Python is a programming
language that lets you work more quickly and
integrate your systems more effectively. You
can learn to use Python and see almost
immediate gains in productivity and lower
maintenance costs.
Python was conceived in the late 80s and its
implementation was started in December
1989 by Guido van Rossum at CWI in the
Netherlands

CSC1400 H.Talei 20
Input/output(1)
Output:
Syntax
print(Text to print)
print(Text to print, variable name)
Variable?
is a name associated with a memory cell whose
value can change
3 data types
int: 12
float: 12.6
str:C , CS,CSC, CSC1400

CSC1400 H.Talei 21
Input/output(2)
Input:
VariableName=input(Text to print)
When you use the input function
variableName become of type str
N1=input(Please input a number: )
N1 is of class str
You can not use the expression: N1+10
This will cause a syntax problem
Either you cast N1: int(N1)+10
Or you use eval(): Number=eval(N1) then you
can use Number+10 as a correct expression

CSC1400 H.Talei 22
Variables
You have some freedom in naming
your variables
But some rules should be respected while
naming. A valid identifier must:
Not start with a digit
Have no space
Have only letters and digits also underscores
Not be a reserved word for Python
if, print, input, else, for, while, import
Refer to the table you have in your book

CSC1400 H.Talei 23
More about variables
An assignment shortcut
X,Y,Z=10,20,30
Equivalent to X=10 Y=20 Z=30
For a string we have to use quotes
CourseCode=CSC1400
For variables you can use the
Math operators in the following
table

CSC1400 H.Talei 24
Operations on strings
Refer to the following
Table
Refer to this program:

CSC1400 H.Talei 25
In Class Exercise
Write a Python program that asks the
use for two numbers N1 and N2 and
your program should display the sum,
the difference, the quotient (as an
integer) and the product of the two
numbers.

CSC1400 H.Talei 26
Solution: 2 ways

CSC1400 H.Talei 27
In Class Exercise/ Solution
Write a Python program that asks the
user for a number in seconds and
your program should display the
correspondent hours, minutes and
seconds.

CSC1400 H.Talei 28
Selection Statements

CSC1400 H.Talei 29
Selection Statements
Remember:
Boolean Expression
Logical Operators
And:

CSC1400 H.Talei 30
If Statement Syntax
One way decision:
if Condition:
Statement1
Statement2
Remember, Indentation is very important!
Two way decision
if Condition:
Statement
else:
Statement
CSC1400 H.Talei 31
If Statement Syntax(Continue)
Multiple way decision
if Condition:
Statement
elif Condition:
Statement
else:
Statement
CSC1400 H.Talei 32
Exercise
Write a Python program that asks the
user for two words and your program
should check whether the two keywords
are of the same length or not.

CSC1400 H.Talei 33
Exercise
Write a Python program that asks the
user for a grade and then your code
should
Check whether the grade is valid or
not
Print the correspondent letter grade
for a valid grade

CSC1400 H.Talei 34
Exercises to do/redo
Exercise 1
I read in a magazine that Marjane is offering a promotion next month which is the following: if you
buy 2 items with at least 250dhs then we make a discount of 10%
Write a Python program that asks the user for the price of the two items and your program should
inform the user whether he/she will get the discount or not.
Exercise 2
I am in the process of applying to a position of computer science PhD in university of Huston. Yet to
get admitted, the university requested taking the TOEFL and GRE tests and get passing scores as
follow:
For TOEFL: I have to get at least 550
For GRE (the exam consists of three parts: Verbal, quantitative and writing): I have to get no less
than 550/1000 in the verbal part, at least 700/1000 in the quantitative part, and 15/20in the writing
part
Write a Python program that asks the user for the TOEFL, verbal, quantitative, writing scores and
inform the user whether he/she got accepted or not
Exercise 3
Assume I am changing the requirement of the syllabus as follows:
We will have only three quizzes
If the average of your first two exams is a passing one then I will drop the lowest quiz grade for you
Write a Python program that computes the average of your quizzes.

CSC1400 H.Talei 35

You might also like