Data Type, Variable in Python(2)(2)
Data Type, Variable in Python(2)(2)
• Use variables, assignment operator, arithmetic operators, and basic numeric data
types in coding using Python
Variables in Python
Names are used to make the program more readable, so that the “something” is easily understood.
e.g., radiusFloat
import math
radiusString = input("Enter the radius of your circle:")
radiusFloat = float (radiusString)
circumference = 2 * math.pi * radiusFloat
area = math.pi * radiusFloat * radiusFloat
• Name of the entity must be unique to be identified during the execution of the program
ELIZABETH
MATTHEW
Robert JOHN MARY Kimberly
John
NAMES
Stephen Larry
Joseph Sarah
Ronald
MICHAEL Deborah John
LISA PATRICIA RICHARD Ruth
MARK Sharon Paul Anthony
• Should not use keywords • Names starting with _ have special meaning
8 9
Introduction to Computational Thinking 7
Keywords
7
Introduction to Computational Thinking 8
Quick Check
Allowed Characters: Uppercase and lowercase letters A through Z, the • (Us$, Phone#): $ and # are not allowed;
underscore, '_' and the digits 0 through 9 (except for the first character) • (2person): a digit is not allowed as a first character
john_math_score = 90
Message 1
peter_math_score = 70
mary_math_score = 80 Be careful! Python is
john_eng_score = 60
peter_eng_score = 60
case sensitive!
All English scores are 60
mary_eng_score = 60
Message 2
total = john_math_score + peter_math_score + mary_math_score
average_math = total/3.0 A program, that can
print("average Math score = ", average_math) run doesn’t mean
Total = john_eng_score + peter_eng_score + mary_eng_score
average_eng = total/3.0
that it is correct.
print("average English score = ", average_eng) Logic error
Operations
3.14
Name Value
x = 3.14
x 3.14
X
x = 9
print (x)
x = 7.8
print (x)
x = "welcome"
print (x)
x = 9
print (x)
x = 7.8
print (x)
x = "welcome"
print (x)
9
Answer 7.8
welcome
Compared to C and Java, how does Python know the data types?
Examples >>> a = 99
Four variables!
>>> b = 99.9
>>> c = '100' What are their data types?
>>> d = True
total = 4 + 3
sum = total * 2
Total = total + sum
print (total)
print ('Total')
total = 4 + 3
sum = total * 2
Total = total + sum
print (total)
print ('Total')
7
Answer Total
Flowchart
start
Read horizonDist
Read vertDist
Preparatory Questions
dist = horizonDist + vertDist
• How many variables should you define? (3)
Display distance between • What is the data type of each variable? (integer)
these two points
• Do you need assignment operator in your program? (Yes)
end • Do you need arithmetic operators in your program? (Yes)
Flowchart 1
Python Code Version 3
2
horizon_dist =
horizon_dist = 4
int(input("Read horizonDist"))
start vertical_dist =
vertical_dist = 3int(input("Read vertDist"))
travel_dist = horizon_dist + vertical_dist
Read horizonDist print(travel_dist)
print("distance from A to B is ", travel_dist)
Read vertDist
Output
dist = horizonDist + vertDist
Read horizonDist
distance 7 A to
from 4 B is 7
Display distance between Read vertiDist 3
these two points distance from A to B is 7
print input
end (for displaying data) (for reading data)
Version 1 Version 2
horizon_dist = 4 horizon_dist = 4
vertical_dist = 3 vertical_dist = 3
travel_dist = horizon_dist + vertical_dist travel_dist = horizon_dist + vertical_dist
print(travel_dist) print("distance from A to B is ", travel_dist)
Version 3
Syntax Rules:
Identifier
Integer
Variables in Operators in Python
Python =, +, -, *, /
Float Data Types Subsequent assignments
can update the associated
value
There are different
Examples of Variables
String data types.
with Operators
value =99
Naming total_price = rice + coffee
Convention area_square = side * side
average = total/number_students
Placeholder
Hands-on Demonstration