Development Type
Weeemake Education Series
Python Learning Kit
Knowledge Point - Data Types
• A data type is a category for values, and every value belongs to exactly one data type. Data types
generally refer to Data Element, is a data unit that describes its definition, identification, representation,
and allowed value with a set of properties. In a certain context, it is usually used to build a semantic
correct, independent and unambiguous specific conceptual semantic information unit. The data
element can be understood as the basic unit of the data. Make a certain order of the data elements is a
data model.
Sequence type data can add
same type data, or multiple with
(repeat) integer, or be divided.
Access to values through keys. Keys
can be integer, tuple, string, but
cannot be repeated. 2
Knowledge Point - Data Types
Type Variable Name Value Assignment
Name Name David Name=’David’
Age Age 13 Age=13
Gender Gender male Gender=True
Weight Weight 1.9m Weight=1.9m
Hight Hight 85 Hight=85
3
Knowledge Point - Data Types
Variable Type Instruction
Name=’David’ String str A string of text value that should be quoted.
Age=13 Integer int Whole numbers such as 0/1/2/3/-10/10
Gender=True Boolean bool the Boolean data type has only two values: True
(1) and False (0).
Weight=1.9m Float float Numbers with a decimal point, such as 3.14,
2.343
• In programming, Python can automatically derive the data type in the variable according to the value on
the right of ‘=’ equal sign.
4
Knowledge Point - Operators Addition ‘+’
• ‘+’ is the addition operator when it operates on two integers or floating-point values. We can use ‘+’ with
numberic values to perform common mathematical operations:
a=10 #assign a integer variable
b=20
c=a+b
print(c) • Notice: If you try to use the ‘+’
operator on a string and an integer
• We can use ‘+’ combines the text of two strings. value, Python will not know how to
handle this, and it will display an error
message.
name1="I'm Mike " #assign string type variable
name2="I'm Alice "
print(name1+name2) #combine the texts of the two strings
5
Knowledge Point - Operators Multiplication ‘*’
• The ‘*’ operator multiplies two integer or floating-point values.
Apple is $5.98 per pounds, how much is it to buy 6 pounds?
print(5.98*6)
• We can use ‘*’ on one string value and one integer value, it becomes the string replication operator.
name='Alice'
print(name*10) #print 10 'Alice'
• Shortcut key of ‘*’:
Press Shift+8 at the same time.
6
Knowledge Point - Operators ‘/’ ‘//’ ‘%’
• Let’s try it:
a=22
b=10
c=a/b
print(c)
d=a//b # // returns the largest possible integer after division
e=a%b # % returns the remainder after division
print(d)
print(e)
What’s the value of the ‘c’, ‘d’ and ‘e’?
• Division ‘/’
• Floor division ‘//’
• Modulus ‘%’
7
Practice
Price automatic calculator
Mike helps his mother sell eggs. Each egg is sold in18 cents, it is a very good price so their
eggs are very popular. He wants to have a tool, as long as the quantity is input, the total price
will come out automatically. Let us try to use Python to make a calculator for Mike!
8
Practice
Code A:
price=0.68
quantity=input("Please input the egg quantity: ")
subtotal=price*quantity
print(subtotal)
We get error message with above codes, why?
• The default data type of input () is string, even the input data from keyboard is a numberic
data, it will become string type in program.
• When we learn multiplication operator ‘*’, there is no option to use between string and float
number.
9
Practice
Code B:
price=0.68
quantity=input("Please input the egg quantity: ")
subtotal=price*int(quantity)
print(subtotal)
10
Practice
Try with the code C:
price=0.68
quantity=input("Please input the egg quantity: ")
quantity=int(quantity)
subtotal=price*quantity
print(subtotal)
Q&A:
• How many variables in this program? A: Three. Price/quantity/subtotal.
• In quantity=int(quantity), is it assign a new variable or use the variable?
A: Only the first time that variable name appear is assigning.
When the variable name appears again, it is using the assigned variable.
• Shall we revise the saved value of variable during programming?
A: Yes, we can overwrite the stored value of variables. Variable is changable.
11
Practice
Circumference calculator
Circle (a shape)
• The circle is a shape that seems very simple, but it’s
acutally very wonderful. The ancient people get
concept of circle from the sun in the beginning. A circle
is a shape consisting of all points in a plane that are at
a given distance from a given point, the centre.
• The distance between any point of the circle and the
centre is called the radius.
• The circumference = radius*2*pi (π)
12
Practice
Pi 3.1415926535897932384626433832…….
• In Babylon, a clay tablet dated 1900–1600 BC has a
25
geometrical statement that, by implication, treats π as =
8
3.125.
• In Egypt, the Rhind Papyrus, dated around 1650 BC but
copied from a document dated to 1850 BC, has a formula for
16
the area of a circle that treats π as ( )2 ≈ 3.16.
9
• In one of the oldest Chinese mathematical book Zhoubi
Suanjing ((1046–256 BCE)), it mentioned “the ratio between
circle’s diameter and circumference is 1: 3”.
13
Practice
• The Chinese mathematician Zu Chongzhi, around 480
AD, calculated that 3.1415926 < π < 3.1415927 and
suggested the approximations π ≈ 355 113
22
=3.14159292035... and π ≈ =3.142857142857...,
7
which he termed the Milü (''close ratio") and Yuelü
("approximate ratio"). With a correct value for its seven
first decimal digits, this value remained the most
accurate approximation of π available for the next 800
years.
• The Persian astronomer Jamshīd al-Kāshī produced 9
sexagesimal digits, roughly the equivalent of 16
decimal digits, in 1424 using a polygon with 3×228
sides, which stood as the world record for about 180
years.
• On March 14th, 2019, Google computed π to 31.4
trillion decimal places.
14
Practice
The circumference (c) = radius (r)*2*pi (π)
π=3.1415926535
If:
r=3 cm, c=2* 3 * 3.1415926535= 18.849555921
r=7 cm, c=2 * 7* 3.1415926535 = 43.982297149
r=12 cm, c=2* 12 * 3.1415926535 = 75.398223684
15
Practice
• C=2πr
• π=3.1415926535
• Now let’s make a calculator. With this calculator, we
can simply enter the radius and get the circumference
of the circle immediatly.
Code:
r=int(input("Please enter the radius of the circle: "))
a=3.1415926535
c=2*r*a
print(c)
16
Quiz
Quiz
1. What are the types of numberic variables?
String, integer, float, boolean.
2. What operators do we learn today?
Addition +, multiplication *, division /, floor division //, modulus %.
3. What’s the data type of input ()?
The default data type of input () is string. When use operators, we should assign variable int(input()).
18
See you next time!