Sie sind auf Seite 1von 59

The Digital World

Data Types and Variables

10.009: The Digital World


Cohort 4

Week 1
Term 3, 2016

Flowcharts and Others

The Digital World

Data Types and Variables

Flowcharts and Others

Learning Outcomes

By end of this week, you should be able to:


1. Use Python as a calculator.
2. Learn how to program using Python.
3. Understand comment, print, variables, types, assignment,
operators.
Note: Refer to Chapters 1 and 2 of the textbook.

The Digital World

Data Types and Variables

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

Applications of Python (Week 8-14)


Classes and Objects
State Machines
Proportional Controller
GUI Widgets
2D and 1D Projects

Flowcharts and Others

The Digital World

Data Types and Variables

Flowcharts and Others

What is Python?

Python is a high-level programming language that specifies

a set of instructions on how to perform computation using a


computer.
Python programs are executed by an interpreter.
Two ways to use the interpreter: Command-line mode and

Script mode.
Use Canopy.

The Digital World

Data Types and Variables

Activity 1: Use Canopy


First start your Canopy Editor!
Command-line mode

2 ` 2

#your program

Flowcharts and Others

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 1: Use Canopy


First start your Canopy Editor!
Command-line mode

2 ` 2
4

#your program
#result of the interpreter

#I am a comment - This is used to explain your code and


is not interpreted by Python.

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 1: Use Canopy


First start your Canopy Editor!
Command-line mode

2 ` 2
4

#your program
#result of the interpreter

#I am a comment - This is used to explain your code and


is not interpreted by Python.
Script mode
1. Go to File and open New Window (new editor window).
2. Enter your program and save your program (xxx.py).
3. Run your program and see your results in Python Shell.

Now, enter 2 + 2 in your program and run. What happens?

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 1: Use Canopy


First start your Canopy Editor!
Command-line mode

2 ` 2
4

#your program
#result of the interpreter

#I am a comment - This is used to explain your code and


is not interpreted by Python.
Script mode
1. Go to File and open New Window (new editor window).
2. Enter your program and save your program (xxx.py).
3. Run your program and see your results in Python Shell.

Now, enter 2 + 2 in your program and run. What happens?


Next, enter print 2 + 2 in your program and run. It works!

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 1: Use Canopy


First start your Canopy Editor!
Command-line mode

2 ` 2
4

#your program
#result of the interpreter

#I am a comment - This is used to explain your code and


is not interpreted by Python.
Script mode
1. Go to File and open New Window (new editor window).
2. Enter your program and save your program (xxx.py).
3. Run your program and see your results in Python Shell.

Now, enter 2 + 2 in your program and run. What happens?


Next, enter print 2 + 2 in your program and run. It works!
print - A statement that causes the interpreter to display a
value on the Python Shell.

The Digital World

Data Types and Variables

Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1

Flowcharts and Others

The Digital World

Data Types and Variables

Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
2 1

Flowcharts and Others

The Digital World

Data Types and Variables

Activity 2: Operators `, , , {, , %
Now, use Python as a calculator:
1 ` 1
#Addition
#Subtraction
2 1
3 2

Flowcharts and Others

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

Python Isnt Very Good at Calculating

If you give Python integers, it will assume that you want

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.

The Digital World

Data Types and Variables

Flowcharts and Others

Object (data) types


Int - Integers belong to the type int. It is at least 32 bits so

it should be sufficient for most computations.


long - Integers with a larger range. It is represented as a

number followed by an uppercase L.


float - Real number with decimal points belong to the

type float.
complex - Complex numbers. Imaginary numbers are

written with a suffix of j and it is written as (real + imag j).


str - String belongs to the type str and it is a sequence of

letters within single () or double quotation () marks.

The Digital World

Data Types and Variables

Flowcharts and Others

Object (data) types


Int - Integers belong to the type int. It is at least 32 bits so

it should be sufficient for most computations.


long - Integers with a larger range. It is represented as a

number followed by an uppercase L.


float - Real number with decimal points belong to the

type float.
complex - Complex numbers. Imaginary numbers are

written with a suffix of j and it is written as (real + imag j).


str - String belongs to the type str and it is a sequence of

letters within single () or double quotation () marks.


If you are not sure what type a value has, the interpreter can tell
you. Use typepq.
typep3.0q
type(Hello!q

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


floatp7q{3

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


floatp7q{3
intp7.0q{3

#Type conversion from integer to float

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


floatp7q{3
intp7.0q{3
intp3.9q

#Type conversion from integer to float


#Type conversion from float to int

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


floatp7q{3
intp7.0q{3
intp3.9q
roundp3.9q

#Type conversion from integer to float


#Type conversion from float to int
#Returns the floor when convert float to int

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


#Type conversion from integer to float
floatp7q{3
#Type conversion from float to int
intp7.0q{3
intp3.9q
#Returns the floor when convert float to int
#Rounding function
roundp3.9q
float(3.0 + 4j)

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


#Type conversion from integer to float
floatp7q{3
#Type conversion from float to int
intp7.0q{3
intp3.9q
#Returns the floor when convert float to int
#Rounding function
roundp3.9q
float(3.0 + 4j) #Does not work for complex numbers!
strp3.0q

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


#Type conversion from integer to float
floatp7q{3
#Type conversion from float to int
intp7.0q{3
intp3.9q
#Returns the floor when convert float to int
#Rounding function
roundp3.9q
float(3.0 + 4j) #Does not work for complex numbers!
#Type conversion from float to str
strp3.0q
int("a"q

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


#Type conversion from integer to float
floatp7q{3
#Type conversion from float to int
intp7.0q{3
intp3.9q
#Returns the floor when convert float to int
#Rounding function
roundp3.9q
float(3.0 + 4j) #Does not work for complex numbers!
#Type conversion from float to str
strp3.0q
#Cannot convert letter to number
int("a"q
int("5"q

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


#Type conversion from integer to float
floatp7q{3
#Type conversion from float to int
intp7.0q{3
intp3.9q
#Returns the floor when convert float to int
#Rounding function
roundp3.9q
float(3.0 + 4j) #Does not work for complex numbers!
#Type conversion from float to str
strp3.0q
#Cannot convert letter to number
int("a"q
int("5"q
#Type conversion from str to int
float("5"q

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 3: Conversion - int(), float(), str(), round()


#Type conversion from integer to float
floatp7q{3
#Type conversion from float to int
intp7.0q{3
intp3.9q
#Returns the floor when convert float to int
#Rounding function
roundp3.9q
float(3.0 + 4j) #Does not work for complex numbers!
#Type conversion from float to str
strp3.0q
#Cannot convert letter to number
int("a"q
int("5"q
#Type conversion from str to int
float("5"q
#Type conversion from str to float

The Digital World

Data Types and Variables

Flowcharts and Others

Practice Session 1

Work on cohort session problems 1, 2, 6, and 7.

The Digital World

Data Types and Variables

Flowcharts and Others

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

The Digital World

Data Types and Variables

Flowcharts and Others

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

#This is an assignment statement

The Digital World

Data Types and Variables

Flowcharts and Others

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

#This is an assignment statement


#This is an assignment statement

The Digital World

Data Types and Variables

Variables and Python Keywords


What happens with the following assignment?
77message = "no fun"

Flowcharts and Others

The Digital World

Data Types and Variables

Variables and Python Keywords


What happens with the following assignment?
77message = "no fun"
#Illegal variable
message$ = "no fun"

Flowcharts and Others

The Digital World

Data Types and Variables

Variables and Python Keywords


What happens with the following assignment?
77message = "no fun"
#Illegal variable
message$ = "no fun"
#Illegal variable
class = "no fun 101"

Flowcharts and Others

The Digital World

Data Types and Variables

Variables and Python Keywords


What happens with the following assignment?
77message = "no fun"
#Illegal variable
message$ = "no fun"
#Illegal variable
#Illegal variable
class = "no fun 101"

Flowcharts and Others

The Digital World

Data Types and Variables

Flowcharts and Others

Variables and Python Keywords


What happens with the following assignment?
77message = "no fun"
#Illegal variable
message$ = "no fun"
#Illegal variable
#Illegal variable
class = "no fun 101"
There are 29 keywords that cannot be used as variable names!

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 4: Tracing Code with Variables


Understand the Python code below without using Canopy:
x = 15
y = 10
y = x
x = x + 1
y = x + y
What are the values of x and y in the end?

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 4: Tracing Code with Variables


Understand the Python code below without using Canopy:
x = 15
y = 10
y = x
x = x + 1
y = x + y
What are the values of x and y in the end?
x = 15

#x = 15

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 4: Tracing Code with Variables


Understand the Python code below without using Canopy:
x = 15
y = 10
y = x
x = x + 1
y = x + y
What are the values of x and y in the end?
x = 15
y = 10

#x = 15
#x = 15, y = 10

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 4: Tracing Code with Variables


Understand the Python code below without using Canopy:
x = 15
y = 10
y = x
x = x + 1
y = x + y
What are the values of x and y in the end?
x = 15
y = 10
y = x

#x = 15
#x = 15, y = 10
#x = 15, y = 15

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 4: Tracing Code with Variables


Understand the Python code below without using Canopy:
x = 15
y = 10
y = x
x = x + 1
y = x + y
What are the values of x and y in the end?
x = 15
y = 10
y = x
x = x + 1

#x = 15
#x = 15, y = 10
#x = 15, y = 15
#x = 16, y = 15

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 4: Tracing Code with Variables


Understand the Python code below without using Canopy:
x = 15
y = 10
y = x
x = x + 1
y = x + y
What are the values of x and y in the end?
x = 15
y = 10
y = x
x = x + 1
y = x + y

#x = 15
#x = 15, y = 10
#x = 15, y = 15
#x = 16, y = 15
#x = 16, y = 31

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 4: Tracing Code with Variables


Understand the Python code below without using Canopy:
x = 15
y = 10
y = x
x = x + 1
y = x + y
What are the values of x and y in the end?
x = 15
y = 10
y = x
x = x + 1
y = x + y

#x = 15
#x = 15, y = 10
#x = 15, y = 15
#x = 16, y = 15
#x = 16, y = 31

Thus, x = 16 and y = 31.

The Digital World

Data Types and Variables

Flowcharts and Others

Practice Session 2

Work on cohort session problems 3, 4, 5, and 8.

The Digital World

Data Types and Variables

Flowcharts and Others

Flowcharts and Pseudocode

A flow chart gives the logical flow of the solution in a

diagram, and provides a plan from which the computer


program can be written.
An algorithm is a step-by-step procedure to solve a given

problem.
The logical flow of an algorithm can be seen by tracing

through the flowchart.


Pseudocode is a sequence of steps of an algorithm.

The Digital World

Data Types and Variables

Flowcharts and Others

Standard Symbols in Flowcharts


Some standard symbols used in the formation of flow charts
are given below:

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 5: Flowcharts and Pseudocode


Draw a flowchart or write a pseudocode to give an algorithm
that sums all the even numbers between 1 and 20 inclusive and
then displays the sum.

The Digital World

Data Types and Variables

Flowcharts and Others

Activity 5: Flowcharts and Pseudocode


Draw a flowchart or write a pseudocode to give an algorithm
that sums all the even numbers between 1 and 20 inclusive and
then displays the sum.
Pseudocode
sum = 0
Input
count = 1
REPEAT
IF count is even THEN sum = sum + count
count = count + 1
UNTIL count > 20
DISPLAY sum
Output

The Digital World

Data Types and Variables

Activity 5: Flowcharts and Pseudocode

Flowcharts and Others

The Digital World

Data Types and Variables

Flowcharts and Others

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)

The Digital World

Data Types and Variables

Flowcharts and Others

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

because 1/49 is not correctly represented in the computer.


Real numbers in a computer and the results of

mathematical computations are only approximate!

The Digital World

Data Types and Variables

Flowcharts and Others

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

#This is the preferred style.

The Digital World

Data Types and Variables

Flowcharts and Others

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

#This is the preferred style.

Blank spaces do matter (more about this next week):

def counter(y):

return y + 1 #Correct (4 leading blanks)


def counter(y):
return y + 1

#invalid syntax

The Digital World

Data Types and Variables

Practice Session 3

Work on cohort session problem 9.

Flowcharts and Others

Das könnte Ihnen auch gefallen