The role of the five data types Python 1. Briefly, the defined manner using the method:
Digital Type
Integer:
Role: represents the person's age, all kinds of numbers, level
Use:
x = 1 y = 2 print(x + y) print(x - y) print(x * y) print(x / y) print(x % y) # 取余 print(x // y) # 取整 print(x ** y) # 幂
Definition method:
age=18 age=int(18)
Float:
Role: represents the height, weight, payroll
Defined method:
salary=3.2 salary=float(3.2)
Instructions:
+ - * / % // **
String type
Role: represents the name of hobbies
definition:
name='nick' int_str=str(123)
Instructions:
Self string comparison logic + *
List
Action: storing a plurality of (of any data type) element
Definition: a plurality of spaced elements (of any data type) with a comma in []
Instructions:
hobby_list=['read','run','girl'] 0 1 2 print(bobby_list[1])
dictionary
Action: storing a plurality of values, each value by the description but
Definition: {} the plurality of keys separated by commas (described, string): the value (a specific value, can be any data type)
Instructions:
user_info={'name':'nick','gender':'male','age':19} print(user_info['name'])
Boolean
Effect: Conditions for determination result
Definitions: True, False usually not directly quoted, required logic operation result obtained
Instructions:
print(type(True)) print(True) print(bool(0)) print(bool('nick')) print(bool(1 > 2)) print(bool(1 == 1))
2. The following line of code to implement the functions of the code implementation:
x = 10
y = 10
z = 10
x=y=z=10
3. Write two ways exchange x, y values:
x = 10
y = 20
#第一种
m=x
x=y
y=x
#第二种
x,y=y,x
4. Remove the line of code nick
of 3 interests:
nick_info_dict = {
'name':'nick',
'age':'18',
'height':180,
'weight':140,
'hobby_list':['read','run','music','code'],
}
print(nick_info_dict['hobby_list'][1],nick_info_dict['hobby_list'][2])
5. Use the formatted output of three ways to achieve the following outputs (name changed to his name, acquired modify height and weight, do not brazen)
name = 'Nick'
height = 180
weight = 140
# "My name is 'Nick', my height is 180, my weight is 140"
print(f'My name is {name},my height is {height},my weight is {weight}')
print('My name is {},my height is {},my weight is {}'.format(name,height,weight))
print('My name is %s,my height is %d,my weight is %d'%(name,height,weight))