0% found this document useful (0 votes)
13 views42 pages

Lec 1 Signals and Systems

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views42 pages

Lec 1 Signals and Systems

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Chapter Elementary Programming

1
Introducing Programming with an
Example
Listing 2.1 Computing the Area of a
Circle
This program computes the area of the
circle.

2
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))

3
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)

4
animation

Trace a Program Execution


print a message to
# Assign a radius the console

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)

5
Reading Input from the Console
1. eval function converts strings to numbers :

a = eval('3.568’) → a = 3.568

x = eval("51 + (54 * (3 + 2))") → x =


321.

2. input function returns strings:


Name = input("Enter a string: ")
radius = eval(input(“Enter the radius: "))

6
eval()
a = input('a= ') # Enter a = '50'
b = input('b= ') # Enter b = '100'
print(a+b) # '50100’
##################################
a = eval( input('a= ') ) # Enter a = 50
b = eval( input('b= ') ) # Enter b = 100
print(a+b) # 150

7
Comments in Python
• Anything after a # is ignored by Python
• Why comment?
• Describe what is going to happen in a sequence
of code
• Turn off a line of code - perhaps temporarily
Identifiers/Variable Names
An identifier is a sequence of characters that
consists of letters, digits, underscores (_).
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.

9
Python Variable Name Rules
• Must start with a letter or underscore _
• consist of letters and numbers and
underscores
• Case Sensitive
• Good: spam eggs spam23 _speed
• Bad: 23spam #sign var.12
• Different: spam Spam SPAM
Reserved Words
• You can not use reserved words as variable
names / identifiers

and del for is raise


assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in
Variables
A variable is a named place in the memory where a
programmer can store data and later retrieve the
data using the variable “name”
Programmers get to choose the names of the
variables
You can change the contents of a variable in a later
statement

x = 12.2 x 12.2 100


y = 14
x = 100 y 14
Variables
# Compute the first area
radius = 1.0
area = radius * radius * 3.14159
print("The area is ", area,
" for radius ", radius)
# Compute the second area
radius = 2.0
area = radius * radius * 3.14159
print("The area is ", area,
" for radius ", radius)

13
Expression
x = 1 # Assign 1 to variable x
radius = 1.0 # Assign 1.0 to variable radius

# Assign the value of the expression to x


x = 5 * (3 / 2) + 3 * 2

x = y + 1 # Assign the addition of y and 1 to x


area = radius * radius * 3.14159 # Compute area

14
Assignment Statements
• We assign a value to a variable using the
assignment operator (=)
• An assignment statement consists of an
expression on the right hand side and a
variable to store the result

x = 3.9 * x * ( 1 - x )
A variable is a memory
location used to store a value x 0.6
(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 new value x 0.6 0.93
(0.93).

x = 3.9 * x * ( 1 - x )

Right side is an expression. Once 0.93


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
i = j = k = 1

18
Simultaneous Assignment
var1, var2, ..., varn = exp1, exp2, ..., expn
x, y = 1+5, 20+3

x, y = y, x # Swap x with y

19
Numerical Data Types

integer: e.g., 3, 4
float: e.g., 3.0, 4.035

20
Several Types of Numbers
• Numbers have two main >>> xx = 1
types >>> type (xx)
<class 'int'>
• Integers are whole >>> temp = 98.607
numbers: -14, -2, 0, 1, 100, >>> type(temp)
401233 < class 'float'>
• Floating Point Numbers >>> type(1)
< class 'int'>
have decimal parts: -2.58 , >>> type(1.0)
0.0, 98.607, 14.0 < class 'float'>
>>>
Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35

- Subtraction 34.0 – 0.1 33.9

* Multiplication 300 * 30 9000

/ Float Division 1 / 2 0.5

// Integer Division 1 // 2 0

** Exponentiation 4 ** 0.5 2.0

% Remainder 20 % 3 2

22
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

23
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:

24
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 ** 100000000000000000000
OverflowError: 'Result too large'

25
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.

26
Scientific Notation
Floating-point literals can also be specified in
scientific notation, for example,
1.23456e+2 is equivalent to 123.456, and
1.23456e-2 is equivalent to 0.0123456.
E (or e) represents an exponent and it can be
either in lowercase or uppercase.

27
Arithmetic Expressions
3 + 4 x 10( y − 5)(a + b + c) 4 9+ x
− + 9( + )
5 x x y

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

28
Order of Evaluation
When we write operators together - Python
must know which one to do first
This is called “operator precedence”
Which operator “takes precedence” over
the others
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules

Parenthesis
Power
Multiplication, Division and Remainder
Addition and Subtraction
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 1 + 10
Power
Multiplication, Division and Remainder 11
Addition and Subtraction
Left to Right
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print x 1 + 2 ** 3 / 4 * 5
11
>>> 1+8/4*5

1+2*5
Note 8/4 goes before 4*5
because of the left-right rule. 1 + 10

11
How to Evaluate an 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
33
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

>>> print (1 + 2 * 3 / 4.0 – 5)


-2.5
Augmented Assignment Operators
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8

35
Type Conversion and Rounding
datatype(value)

i.e., int(4.5) => 4


float(4) => 4.0
str(4) => “4”

round(1.3) => 1
round(9.5) => 10

36
Problem: Keeping Two Digits After
Decimal Points
Write a program that displays the sales tax with two
digits after the decimal point.

37
38
Case Study: Computing Distances
This program prompts the user to enter two points,
computes their distance, and displays the points.

39
Case Study: Computing Distances

This program prompts the user to enter two


points, computes their distance, and displays
the points and their distances in graphics.

40
41
import turtle

# Prompt the user for inputting two points


x1, y1 = eval(input("Enter x1 and y1 for point 1: "))
x2, y2 = eval(input("Enter x2 and y2 for point 2: "))

# Compute the distance


distance = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

# Display two points and the connecting line


turtle.penup()
turtle.goto(x1, y1) # Move to (x1, y1)
turtle.pendown()
turtle.write("Point 1")
turtle.goto(x2, y2) # Draw a line to (x2, y2)
turtle.write("Point 2")

# Move to the center point of the line


turtle.penup()
turtle.goto((x1 + x2) / 2, (y1 + y2) / 2)
turtle.write(distance)

42

You might also like