Uploaded by giveaway giveaway

Introduction

advertisement
Introduction to Computation
and Programming
Introduction to Python
Reading: [Guttag, Chapter 2]
Slides prepared for EECE 230C, Fall 2018-19, MSFEA, AUB
Updated with minor edits during the offering of EECE 230, Spring 2018-19, MSFEA, AUB
Some of the material in these slides is taken from [Guttag, Chapter 2]
EECE 230 - Introduction to Computation and Programming
Outline
• Objects and Types
• Operators
• Expressions
• Variables and Assignment
• Strings
• Input and Output
• Modules
• Miscellaneous notes
EECE 230 - Introduction to Computation and Programming
Objects and types
• Example in python shell:
• Python guesses the type from initialization
EECE 230 - Introduction to Computation and Programming
Objects and types (continued)
• Objects:
• Scalar, e.g., 12, 4.2, True
• Non-scalar, e.g., ‘eece 230C’
• Each object has a type
• Scalar types: indivisible
• int
• float
• bool
• NoneType (later)
• Non-scalar types: have internal structure
e.g., strings, lists (later), etc.
EECE 230 - Introduction to Computation and Programming
int type
• Used to represent integers, e.g., 12, -1243, 4242424100
• Recall binary representation from previous lecture
• Unlimited precision in python (bounded by memory)
EECE 230 - Introduction to Computation and Programming
float type
• Floating point representation
• Used to represent real number up to limited precision
• Ex: 24.056, -56.0
• Scientific notation: 13E5 means 13 ∗ 105 , i.e., 1300000.0
EECE 230 - Introduction to Computation and Programming
float type (Continued)
• Represented using 8 bytes, i.e., 64 bits
1 bit for the sign
52 bits the significant part (e.g., 10100101…1)
remaining 11 bits for the exponent to put the floating decimal
point, hence the name float
More on this representation in EECE 320
• Limited accuracy, e.g.,
0.1 has an infinite length binary representation
0.000110011001100110….. (i.e., 0.1 = 2−4 + 2−5 + 2−8 + ⋯ )
EECE 230 - Introduction to Computation and Programming
bool type
• Used to represent Boolean/logical values: True and False
• True and False are keywords
• Originates from logical tests:
EECE 230 - Introduction to Computation and Programming
Arithmetic operators
• Operators for the int and float types:
Addition (+): x+y
Subtraction (-): x-y
Multiplication(*): x*y
Division (/): x/y
Power (**): x**y, i.e., 𝑥 𝑦
EECE 230 - Introduction to Computation and Programming
Arithmetic operators (Continued)
• Except for division (/), if both x and y are of type integer, the result is
an integer.
• In all other cases (i.e., if x or y is float or if the operator is division),
the result is float
• All above are binary operator:
x <operator> y  z
where x, y, and z are of type int or float
• We also have the unary operator minus(-):
-x is the negative of x
EECE 230 - Introduction to Computation and Programming
Arithmetic operators (Continued)
• Operators specific to the int type :
Integer Division (//): x//y is the quotient of x/y
e.g., 7//2 gives 3 and 1//3 gives 0
Modulo (%): x%y is the remainder of x/y
• Thus
x is equal to (x//y)*y + (x%y)
EECE 230 - Introduction to Computation and Programming
Arithmetic operators (Continued)
• Operators specific to the int type :
Integer Division (//): x//y is the quotient of x/y
e.g., 7//2 gives 3 and 1//3 gives 0
Modulo (%): x%y is the remainder of x/y
• Thus
x is equal to (x//y)*y + (x%y)
EECE 230 - Introduction to Computation and Programming
Relational operators
x <operator> y  z of type bool
• Equal (==)
Example:
3 ==2 evaluates to False
• Not equal (!=)
Example:
3!=2 evaluates to True
• Less than (<)
Example:
3.1<2 evaluates to False
• Greater than (>)
• Less than or equal (<=)
• Greater than or equal (>=)
3==3 evaluates to True
2<3.1 evaluates to True
EECE 230 - Introduction to Computation and Programming
Boolean/logical operators
• For the bool type: x <operator> y  z, where x,y, and z are bool
• and: x and y
x
y
x and y
x
y
x or y
• or: x or y
False
False
False
False
False
False
• not: not x
False
True
False
False
True
True
• Example:
Ture
False
False
Ture
False
True
True
True
True
True
True
True
x
not x
False
True
True
False
EECE 230 - Introduction to Computation and Programming
Boolean/logical operators
• For the bool type: x <operator> y  z, where x,y, and z are bool
• and: x and y
x
y
x and y
x
y
x or y
• or: x or y
False
False
False
False
False
False
• not: not x
False
True
False
False
True
True
• Example:
Ture
False
False
Ture
False
True
True
True
True
True
True
True
x
not x
False
True
True
False
EECE 230 - Introduction to Computation and Programming
Expressions
• Combine more than operator
• Example:
EECE 230 - Introduction to Computation and Programming
Order of precedence
• If we skip parentheses, the following order of precedence will be
followed from lowest to highest
1
or
5
Addition, Subtraction: +,-
2
and
6
Multiplication, division, modulo *, /, //, %
3
not
7
- Unary operator minus
4
Relational: <, <=, >=, >=, ==, != 8
** power
• Addition and subtraction are evaluated from left to right if
parentheses are not included
• Same for multiplication, division, and modulo
EECE 230 - Introduction to Computation and Programming
Order of precedence (continued)
Examples:
• -2*3/5+4*5
is interpreted as
(((-2)*3)/5) +(4*5)
• x>=1 and x<10 or x>=20 and x<20
is interpreted as
((x>=1) and ( x<10)) or ((x>=20) and (x<20))
EECE 230 - Introduction to Computation and Programming
Order of precedence (continued)
Examples:
• -2*3/5+4*5
is interpreted as
(((-2)*3)/5) +(4*5)
• x>=1 and x<10 or x>=20 and x<20
is interpreted as
((x>=1) and ( x<10)) or ((x>=20) and (x<20))
EECE 230 - Introduction to Computation and Programming
Order of precedence (continued)
Examples:
• -2*3/5+4*5
is interpreted as
(((-2)*3)/5) +(4*5)
• x>=1 and x<10 or x>=20 and x<20
is interpreted as
((x>=1) and ( x<10)) or ((x>=20) and (x<20))
EECE 230 - Introduction to Computation and Programming
Variables and the assignment operator
• In Python, a variable is just a name associated with an object
radius = 2.0
# assignment operator (=) binds the variable radius to the object 2.0 of type float
area = 3.14* (radius**2) # evaluates to 12.56
# assignment operator (=) binds the variable area to the object 12.56 of type float
radius = 9.7
# assignment operator (=) binds the variable radius to the object 9.7 of type float
radius
2.0
radius
2.0
area
12.56
area
12.56
9.7
After executing first 2 lines
After executing third line
EECE 230 - Introduction to Computation and Programming
Variables and the assignment operator
(continued)
• Key point from previous lecture: assignment operator is not an equation
• In python, variables can change type also !
x = 12.1 # x is the name of double
x = "abc" # now x is the name of a string !
• Python is not strongly typed, which has advantages and disadvantages
• Rules for choosing variable names
• Invalid names:
2dnumber : can’t start with a digit
a b : spaces or symbols such as + are not allowed
• Valid names:
secondNumber or second_number
a_b
EECE 230 - Introduction to Computation and Programming
Strings
• Sequence of characters
• Use single or double quotations: "abc d" or 'abc d'
• The backslash \ has a special meaning in a string
• Escape sequences: special characters starting with \, including
Escape
sequences
Meaning
\n
New line
\'
'
\"
"
\\
\
Examples
EECE 230 - Introduction to Computation and Programming
Strings (continued)
• Concatenation: using operator + , which is overloaded for strings
"abc"+"xyz" gives "abcxyz"
"abc"+" "+"xyz" gives "abc xyz"
• Repetition: using * operator: int*string:
3*"ab" gives "ababab"
• Built-in length function len :
len("abc") gives 3
EECE 230 - Introduction to Computation and Programming
Casting between types
• int() function: casts to int when possible
If x is a float, int(x) is the integer part of x
• float() function: casts to float when possible
• bool can be cast to int without the int() function: True -> 1 and False -> 0
• str() function: casts into a string
EECE 230 - Introduction to Computation and Programming
Input
• Use input() function which returns a string
• Then, use the functions int() or float() functions to cast if needed
approximation error resulting from the fact that real
numbers are represented in python with limited accuracy
(recall that the float type uses 8 bytes)
EECE 230 - Introduction to Computation and Programming
Output
• Use the function print(), which takes one or more arguments
print(expression)
print(expression1,expression2,…) # adds space between expressions
• Run script:
x = 12
y=5
print( "x+y=" ,x+y, "and\nx*y=" ,x*y)
print( "x%y=",x%y) # starts on a new line
Output:
x+y= 17 and
x*y= 60
x%y= 2
EECE 230 - Introduction to Computation and Programming
Output
• Use the function print(), which takes one or more arguments
print(expression)
print(expression1,expression2,…) # adds space between expressions
• Run script:
x = 12
y=5
print( "x+y=" ,x+y, "and\nx*y=" ,x*y)
print( "x%y=",x%y) # starts on a new line
Output:
x+y= 17 and
x*y= 60
x%y= 2
EECE 230 - Introduction to Computation and Programming
Modules
• Useful scientific modules which we are going to use in this
course:
• math
• numpy: Numerical Python
• scipy: scientific tools for Python.
• matplotlib: 2D plotting library
• Installed with Anaconda
EECE 230 - Introduction to Computation and Programming
Importing modules illustrated on math
• Docs:
https://docs.python.org/3/library/math.html
• For instance, it has the log function: math.log(x), which returns the natural
logarithm of x
• Various importing methods
import math
using log: math.log(3)
form math import *
using log: log(3)
form math import log
only math.log is imported
using log: log(3)
form math import log as ln
only math.log is imported
using log: ln(3)
EECE 230 - Introduction to Computation and Programming
Miscellaneous notes
• Naming variables:
• Capitalization style: secondNumber
• Underscore style: first_number
• Comments:
• Line by line: Start line with hash symbol #
• Docstring(documentation string): start and end the
block with """
EECE 230 - Introduction to Computation and Programming
Download