Lec 1 Signals and Systems
Lec 1 Signals and Systems
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
3
animation
# Display results
print("The area for the circle of radius“,
radius, " is “, area)
4
animation
# 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
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
13
Expression
x = 1 # Assign 1 to variable x
radius = 1.0 # Assign 1.0 to variable radius
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 )
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
+ Addition 34 + 1 35
// Integer Division 1 // 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
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
35
Type Conversion and Rounding
datatype(value)
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
40
41
import turtle
42