Beruflich Dokumente
Kultur Dokumente
Week 1
Term 3, 2016
Learning Outcomes
Course Structure
Basic Programming with Python (Week 1-6)
Mapping Math Formula to Python
Functions and Conditionals
Loops and List
File Input/Output
Dictionary
Mini Projects
What is Python?
Script mode.
Use Canopy.
2 ` 2
#your program
2 ` 2
4
#your program
#result of the interpreter
2 ` 2
4
#your program
#result of the interpreter
2 ` 2
4
#your program
#result of the interpreter
2 ` 2
4
#your program
#result of the interpreter
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
2 1
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
#6 is an integer and integer is a data type
7{3
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
#6 is an integer and integer is a data type
#Integer division returns the floor
7{3
7{ 3
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
#6 is an integer and integer is a data type
#Integer division returns the floor
7{3
7{ 3
3 2
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
#6 is an integer and integer is a data type
#Integer division returns the floor
7{3
7{ 3
#Exponentiation
3 2
2%2
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
#6 is an integer and integer is a data type
#Integer division returns the floor
7{3
7{ 3
#Exponentiation
3 2
2%2
#Remainder
3 ` 2 2 8
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
#6 is an integer and integer is a data type
#Integer division returns the floor
7{3
7{ 3
#Exponentiation
3 2
2%2
#Remainder
3 ` 2 2 8 #Operator precedence
How can we get division in decimal points?
7.0{3
Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2
#6 is an integer and integer is a data type
#Integer division returns the floor
7{3
7{ 3
#Exponentiation
3 2
2%2
#Remainder
3 ` 2 2 8 #Operator precedence
How can we get division in decimal points?
7.0{3
7{3.0
integers back!
For fractions, one uses floating point numbers.
Python interprets any number with a decimal in it as a float.
Floats are only approximations of real numbers.
type float.
complex - Complex numbers. Imaginary numbers are
type float.
complex - Complex numbers. Imaginary numbers are
Practice Session 1
Variables
A variable is a name that refers to a value.
Variables let us store and reuse values in several places.
To do this, we need to define the variable and then tell it to
refer to a value.
This step is call assignment statement.
Form: variable = expression
Variables
A variable is a name that refers to a value.
Variables let us store and reuse values in several places.
To do this, we need to define the variable and then tell it to
refer to a value.
This step is call assignment statement.
Form: variable = expression
message = "fun"
print message
Variables
A variable is a name that refers to a value.
Variables let us store and reuse values in several places.
To do this, we need to define the variable and then tell it to
refer to a value.
This step is call assignment statement.
Form: variable = expression
message = "fun"
print message
x = round(3.5)
print x
#x = 15
#x = 15
#x = 15, y = 10
#x = 15
#x = 15, y = 10
#x = 15, y = 15
#x = 15
#x = 15, y = 10
#x = 15, y = 15
#x = 16, y = 15
#x = 15
#x = 15, y = 10
#x = 15, y = 15
#x = 16, y = 15
#x = 16, y = 31
#x = 15
#x = 15, y = 10
#x = 15, y = 15
#x = 16, y = 15
#x = 16, y = 31
Practice Session 2
problem.
The logical flow of an algorithm can be seen by tracing
Round-off Errors
Most real numbers are not represented exactly in a
computer.
a 1{49.0 49
b 1{51.0 51
print float(a)
print float(b)
Round-off Errors
Most real numbers are not represented exactly in a
computer.
a 1{49.0 49
b 1{51.0 51
print float(a)
print float(b)
Reason why we do not get exactly 1.0 in the first case is
Blank Spaces
Blank space may or may not be important in Python
programs.
These statements are equivalent (blanks do not matter):
a100
a 100
a 100
a 100
Blank Spaces
Blank space may or may not be important in Python
programs.
These statements are equivalent (blanks do not matter):
a100
a 100
a 100
a 100
def counter(y):
#invalid syntax
Practice Session 3