CF - Chapter-006 STU
CF - Chapter-006 STU
C E – 119
Computing Fundamentals (CF)
Compiled By:
2
CE - 119 : Computing Fundamentals (CF)
Course Learning Outcomes ( CLO )
CLO Level
Outcome Statement
No. *
Explain the fundamental knowledge and concepts about
1 computing infrastructure including hardware, software, C2
database and networks.
Applying and Implementing number systems and logic
2 C3
gates.
Applying and Implementing problem solving skills and
3 solve problems incorporating the concept of C3
programming. 3
Books
Text Books
1. Computing Essentials, Timothy O’Leary and Linda O’Leary
2. Introduction to Computers, Peter Norton, 6th Edition, McGraw-Hill
3. Introduction to Programming using Python, Daniel Liang
4. Turbo C Programming For The PC, Robert Lafore, Revised Edition
Reference Books:
1. Discovering Computers, Misty Vermaat and Susan Sebok,
Cengage Learning
2. Using Information Technology: A Practical Introduction to Computers
& Communications, Williams Sawyer, 9th Edition, McGraw-Hill
3. Introduction to Python, Paul Deitel, Harvey Deitel
4. Let Us C, Yashavant Kanetkar 4
Marks Distribution
https://sites.google.com/view/muzammil2050
5
Course Instructors
6
CE – 119: Computing Fundamentals Chapter
Elementary Programming
Compiled By:
Engr.Syed Atir Iftikhar [email protected]
7
Motivations
10
Program 2.1 ComputingtheAreaofaCircle.py
radius = 20
# Compute area
# Display results
Output:
The area for the circle of radius 20 is 1256.636
11
str ( ) function
The str ( ) function converts values to a string form so they
can be combined with other strings.
# Assign a radius
radius = 20
# Compute area
# Display results
Output:
The area for the circle of radius 20 is 1256.636
14
animation
Trace a Program Execution
Assign 20 to
# Assign a radius radius
radius = 20 # radius is now 20
radius 20
# Compute area
area = radius * radius * 3.14159
# Display results
print("The area for the circle of radius " +
str(radius) + " is " + str(area))
15
animation
Trace a Program Execution
Assign result to
# Assign a radius area
radius = 20 # radius is now 20
radius 20
# Compute area
area = radius * radius * 3.14159 area 1256.636
# Display results
print("The area for the circle of radius“,
radius, " is "area)
16
animation
Trace a Program Execution
print a message to
# Assign a radius the console
# Display results
print("The area for the circle of radius",
radius, "is", area)
17
Reading Input from the Console
1. Use the input function
variable = input("Enter a string: ")
var = eval(stringVariable)
18
Reading Input from the Console
eval is a built-in- function used in python, eval
function parses the expression argument and
evaluates it as a python expression.
20
Program 2.3
# Prompt the user to enter a radius
# Compute area
# Display results
Output:
Enter a value for radius: 5.1
The area for the circle of radius 5.1 is 81.71275589999999 21
Program 2.4
# Prompt the user to enter three numbers
# Compute average
# Display result
print("Greetings \nSir")
Output:
Greetings
Sir
Welcome to the world of "Programming"
nice day...
25
Comments in Python
Anything after a # is ignored by Python
Why comment?
Describe what is going to happen in a sequence of code
Document who wrote the code or other ancillary
information
Turn off a line of code - perhaps temporarily
Identifiers/Variable Names
An identifier is a sequence of characters that
consists of letters, digits, underscores (_), and
asterisk (*).
An identifier must start with a letter or an
underscore. It cannot start with a digit.
An identifier cannot be a reserved word.
(See Appendix A, "Python Keywords," for a list of
reserved words.)
Reserved words have special meanings in Python,
which we will later.
An identifier can be of any length.
27
Python Variable Name Rules
Case Sensitive
Output
33
Program 2.6 Variables arithmetic.py
Output
Addition is: 60
Subtraction is: 0
35
Program 2.7 Variables arithmetic_input.py
Output
Addition is: 60
Subtraction is: 0
37
Program 2.8 Variables ERROR
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
num3 = input("Enter the third number: ")
sum = num1+num2+num3
print("Addition is: ",sum)
Output:
Enter the first number: 30
Enter the second number: 20
Enter the third number: 10
Addition is: 302010
38
ERROR
Expression
x=1 # Assign 1 to variable x
radius = 1.0 # Assign 1.0 to variable radius
39
Assignment Statements
An assignment statement consists of an expression
on the right hand side and a variable to store the
result
The statement for assigning a value to a variable is
called an assignment statement.
In Python, the equal sign (=) is used as the
assignment operator. The syntax for assignment
statements is as follows:
variable = expression
X = 3.9 * x * ( 1 - x )
A variable is a memory location
x 0.6
used to store a value (0.6)
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
0.93
Right side is an expression.
Once expression is evaluated, the
result is placed in (assigned to) x.
A variable is a memory location used
to store a value. The value stored in
a variable can be updated by
replacing the old value (0.6) with a x 0.6 0.93
new value (0.93).
x = 3.9 * x * ( 1 - x )
0.93
Right side is an expression.
Once expression is evaluated, the
result is placed in (assigned to) the
variable on the left side (i.e. x).
Assignment Statements
x=1 # Assign 1 to x
x=x+1
Every variable has a scope. The scope of a
variable is the part of the program where the
i=j=k= 1 variable can be referenced
44
Assignment Statements
A variable must be assigned a value before it can be
used in an expression.
For example,
interestRate = 0.05
interest = interestrate * 45
This code is wrong, because …..
45
Assignment Statements
For example,
interestRate = 0.05
interest = interestrate * 45
This code is wrong, because interestRate is
assigned a value 0.05, but interestrate is
not defined.
Python is case-sensitive. interestRate and
interestrate are two different
46
variables.
Simultaneous Assignment
x, y = y, x # Swap x with y
47
Simultaneous Assignment
Swapping variable values is a common operation in programming and
simultaneous assignment is very useful to perform this operation.
Consider two variables: x and y. How do you write the code to swap their
values? A common approach is to introduce a temporary variable as
follows:
>>> x = 1
>>> y = 2
>>> temp = x # Save x in a temp variable
>>> x = y # Assign the value in y to x
>>> y = temp # Assign the value in temp to y
But you can simplify the task using the following statement to swap the
values of x and y.
>>> x, y = y, x # Swap x with y
48
Program 2.9 Variables
# Compute Average with Simultaneous Assignment
# Compute average
# Display result
Output
50
Named Constants
The value of a variable may change during the
execution of a program, but a named constant or
simply constant represents permanent data that
never changes.
Python does not have a special syntax for naming
constants. You can simply create a variable to denote
a constant.
To distinguish a constant from a variable, use all
uppercase letters to name a constant.
51
Numerical Data Types
integer: e.g., 3, 4
52
Several Types of Numbers
Numbers have two main types >>> xx = 1
Integers are whole numbers: >>> type (xx)
-14, -2, 0, 1, 100, 401233 <class 'int'>
>>> temp = 98.6
Floating Point Numbers have
>>> type(temp)
decimal parts: -2.5 , 0.0, 98.6,
< class 'float'>
14.0
>>> type(1)
There are other number types - < class 'int'>
they are variations on float and >>> type(1.0)
integer < class 'float'>
>>>
Numeric Operators
+ Addition 34 + 1 35
// Integer Division 1 // 2 0
% Remainder 20 % 3 2
54
/ and // Operator
The / operator performs a float division that results in a
floating number.
>>> 4 / 2
2.0
>>> 2 / 4
0.5
The // operator performs an integer division; the result
is an integer, and any fractional part is truncated.
>>> 5 // 2
2
>>> 2 // 4
0
55
The % Operator
2 3 3 1 Quotient
3 7 4 12 8 26 Divisor 13 20 Dividend
6 12 24 13
1 0 2 7 Remainder
56
Remainder Operator
Remainder is very useful in programming. For example,
an even number % 2 is always 0 and an odd number % 2
is always 1. So you can use this property to determine
whether a number is even or odd. Suppose today is
Saturday and you and your friends are going to meet in
10 days. What day is in 10 days? You can find that day is
Tuesday using the following expression:
57
Numeric Data Types
How do we tell Python whether a number is an
integer or a float? A number that has a decimal point
is a float even if its fractional part is 0.
For example, 1.0 is a float, but 1 is an integer. These
two numbers are stored differently in the computer.
In the programming terminology, numbers such as
1.0 and 1 are called Literals.
A Literal is a constant value that appears directly in
a program.
58
Problem: Displaying Time
Write a program that obtains hours and minutes
from seconds.
59
Program 2.10 Displaying Time
# Prompt the user for input
seconds = eval(input("Enter an integer for seconds: "))
60
Program 2.10 Displaying Time
Output
61
Overflow
When a variable is assigned a value that is too
large (in size) to be stored, it causes overflow.
For example, executing the following statement
causes overflow.
>>>245.0 ** 1000
OverflowError: 'Result too large'
62
Underflow
When a floating-point number is too small (i.e., too
close to zero) to be stored, it causes underflow.
Python approximates it to zero. So normally you
should not be concerned with underflow.
63
Scientific Notation
Floating-point literals can also be specified in
scientific notation, for example,
64
Arithmetic Expressions
3 4 x 10( y 5)( a b c) 4 9 x
9( )
5 x x y
is translated to
65
Order of Evaluation
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule
Parenthesis are always respected
Exponentiation (raise to a power)
Multiplication, Division, and Remainder
Addition and Subtraction
Parenthesis
Left to right Power
Multiplication
Addition
Left to Right
>>> x = 1 + 2 ** 3 / 4 * 5 1 + 2 ** 3 / 4 * 5
>>> print(x)
11 1+8/4*5
>>>
1+2*5
Parenthesis
Power 1 + 10
Multiplication
Addition 11
Left to Right
>>> x = 1 + 2 ** 3 / 4 * 5 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4*5
>>> Note 8/4 goes before 4*5 1+2*5
because of the left-right
rule.
1 + 10
Parenthesis
Power
Multiplication 11
Addition
Left to Right
Operator Precedence
Remember the rules top to bottom
When writing code - use parenthesis
When writing code - keep mathematical expressions
simple enough that they are easy to understand
Break long series of mathematical operations up to
make them more clear
Parenthesis
Power
Multiplication
Exam Question: x = 1 + 2 * 3 - 4 / 5 Addition
Left to Right
How to Evaluate an Expression
Though Python has its own way to evaluate an
expression behind the scene, the result of a Python
expression and its corresponding arithmetic expression
are the same. Therefore, you can safely apply the
arithmetic rule for evaluating a Python expression.
3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
3 + 16 + 5 * 7 – 1
(3) multiplication
3 + 16 + 35 – 1
(4) addition
19 + 35 – 1
(5) addition
54 - 1
(6) subtraction
53
71
Mixing Integer and Floating
When you perform an operation where one operand
is an integer and the other operand is a floating
point the result is a floating point
The integer is converted to a floating point before
the operation
74
Type Conversion and Rounding
datatype(value)
i.e., int(4.5) => 4
float(4) => 4.0
str(4) => “4”
round(4.6) => 5
round(4.5) => 4
75
Problem: Keeping Two Digits After
Decimal Points
76
Program 2.11 Sales Tax
# Prompt the user for input
79
Program 2.12 Show Current Time
import time
totalSeconds = int(currentTime) # Obtain the total seconds since midnight, Jan 1, 1970
80
Program 2.12 Show Current Time
Output
81
Software Development Process
The software development life cycle is a
multistage process that includes requirements
specification, analysis, design, implementation,
testing, deployment, and maintenance.
Developing a software product is an engineering
process. Software products, no matter how large or
how small, have the same life cycle: requirements
specification, system analysis, system design,
implementation, testing, deployment, and
maintenance, as shown in following figure.
82
Software Development Process
Requirement
Specification
System
Analysis
System
Design
Implementation
Testing
Deployment
Maintenance
83
Requirement Specification
Requirement Specification is a
Requirement
Specification
formal process that seeks to
understand the problem and
System document in detail what the software
Analysis
system needs to do. This phase
System involves close interaction between
Design users and designers.
Implementation
requirements. 84
System Analysis
Requirement System Analysis seeks to analyze the
Specification
business process in terms of data flow,
System and to identify the system’s input and
Analysis output.
System
Design
Implementation
Testing
Part of the analysis entails modeling
the system’s behavior. The model is Deployment
intended to capture the essential
elements of the system and to define
Maintenance
services to the system.
85
System Design
Requirement System Design is the process of
Specification
designing the system’s components.
System
Analysis
System
Design
Implementation
Testing
This phase involves the use of many
levels of abstraction to decompose the
Deployment
problem into manageable components,
identify classes and interfaces, and
establish relationships among the Maintenance
classes and interfaces.
86
IPO
Requirement
Specification
System
Analysis Input, Process, Output
System
Design
Implementation
Testing
Deployment
87
Implementation
Requirement Implementation is the process
Specification of translating the system
System
design into programs.
Analysis Separate programs are written
for each component and put to
System
Design work together.
Implementation
Testing
88
Testing
Requirement
Testing ensures that the code meets
Specification the requirements specification and
weeds out bugs.
System
Analysis
System
Design
Implementation
Testing
An independent team of software
engineers not involved in the
Deployment
design and implementation of the
project usually conducts such
testing. Maintenance
89
Deployment
Requirement
Specification
Deployment makes the project
available for use.
System
Analysis
System
Design
Implementation
Testing
Deployment
Maintenance
90
Maintenance
Requirement
Specification Maintenance is concerned with
changing and improving the product.
System
Analysis
System
Design
Implementation
Testing
A software product must continue to
perform and improve in a changing
environment. This requires periodic Deployment
upgrades of the product to fix newly
discovered bugs and incorporate Maintenance
changes.
91
Problem: Computing Loan Payments
This program lets the user enter the interest rate,
number of years, and loan amount, and computes
monthly payment and total payment.
loanAmount monthlyInterestRate
monthlyPayment
1 1
(1 monthlyInterestRate) numberOfYears12
93
Program 2.13 Computing Loan Payments
Output
94
Case Study: Computing Distances
95
Program 2.14 Computing Distances
96
Program 2.14 Computing Distances
Output
97
Case Study: Computing Distances
98
Program 2.15 Computing Distances in Graphics
import turtle
99
Program 2.15 Computing Distances in Graphics
turtle.penup()
turtle.pendown()
turtle.write("Point 1")
turtle.write("Point 2")
100
Program 2.15 Computing Distances in Graphics
turtle.penup()
turtle.write(distance)
turtle.done()
101
Program 2.15 Computing Distances in Graphics
Output:
Enter x1 and y1 for Point 1: 10,100
Enter x2 and y2 for Point 2: 50,200
102
103
104