What is a data type? Why should data classification?
First, let's review what variables are: Variable is used to record the state of things in the world, things in the world has their own attributes to Helenians For example, we normally filled by a person's name, age, sex, height to know a people, we can imagine a computer can rely on these attributes come to know us in the computer age, height can be used to record numbers, but gender-loving people can not use digital to describe. it can be seen the data type refers to the variable values different types, the name may be a data type, age may be a data type, and gender may be another type of data. because the variable is used to reflect the status and state of change, so please state that we should apply to identify different types of data.
Different data types
Digital Type
String type
List Type
Dictionary Type
Boolean
Digital Type
整型和浮点型统称为数字类型
#整型用于表示人的年龄,各种号码,级别
##定义方式
age=int(16)
print(id(age))
print(type(age))
print(age)
###用于加减乘除,逻辑判断(大于,小于)
#浮点型表示身高,体重,薪资
##定义方式
salary=3.2
salary2=float(3)
print(salary)
###用于加减乘除,逻辑运算(大于,小于)
String type
Role: represents the name, hobby
Is defined by: a string corresponding to mutton, the string together, wrapped in single and double quotation marks, three marks, only three characters in quotation marks newline
name1='jack'
name2="aden"
print(id(name1))
print(type(name1))
print(name1)
Usage: string only +, * and the logical comparison.
If quotation marks within the string, the quotation marks and internal character string is not the same package.
Multiplication strings are only multiplied digital comparison string is the order of the letters.
Compare strings in ASCII code size than teaching, the future will go into detail.
List Type
Role: store multiple values
Definition: separated by commas any type of value within []
hobby='read'
hobby_list=[hobby,'run','girl']
print(id(hobby_list))
print(type(hobby_list))
print(hobby_list)
Usage: deposit is not an end, is taking aim, we introduce the method list index value, bearing in mind the index number from 0
hobby_list=['read','run','girl']
#索引序列 0 1 2
取出第二个爱好
print(hobby_list[1])
Dictionary Type
Action: a plurality of values used to access, according to key: value stored-value method, the time taken to go through the key values instead of indexing, having described key role v value and storing various kinds of data. more data when you can use a dictionary.
Is defined: {} in the plurality of elements separated by commas, each element is key: value format, the format in which the value is an arbitrary data type, since the key has a function descriptive, the key is usually a string type.
user_info = {'name': 'nick', 'gender': 'male', 'age': 19,
'company_info': ['oldboy', 'shanghai', 50]}
print(user_info['name'])
print(user_info['company_info'][0])
How: dictionary index value no longer depends on the way, but rather on the key, value by value [key] key can be obtained corresponding to the
Boolean
Action: result determination condition
Definitions: True, Flase usually not directly quoted, required logic operation result obtained
Usage: python values of all data types Boolean value that comes with so many types of data need only remember only 0, None, an empty, False Boolean value is False, the rest to True.
print(bool(0))
print(bool(None))
print(bool(''))
print(bool([]))
print(bool({}))
print(bool(False))
False
False
False
False
False
False
unzip
Decompression can be understood: the supermarket package is put together more merchandise, decompression is actually unpack multiple disposable goods out.
name_list = ['nick', 'egon', 'jason', ]
x, y, z = name_list
print(f'x:{x}, y:{y}, z:{z}')
x:nick, y:egon, z:jason
Sometimes we decompressed value may not be what we want, then we can use universal underscore
name_list = ['nick', 'egon', 'jason', 'tank']
x, y, z, a = name_list
x, _, z, _ = name_list # _相当于告诉计算机不要了,不能以_开头和结尾
还可以用*_代替连续的下划线,*_会把前面所有的省略掉,都不要
ame_list = ['nick', 'egon', 'jason', 'tank', 'kevin', 'jerry']
x, y, _, _, _, z = name_list
x, y, *_, z = name_list
Write a program not used to hold force, the key is to create value. python pursuit of simplicity, the code do not write too long. Therefore dictionary is also possible, but dictionaries decompression is key.
python interaction with the user
We're going to assume that the ATM machine withdrawals, we have to enter the account number and password, which is a kind of human-computer interaction
name = input('请输入你的姓名:')
pwd = input('请输入你的密码:')
print(type(name))
print(type(pwd))
请输入你的姓名:nick
请输入你的密码:123
<class 'str'>
<class 'str'>
input () method is to let you enter, your role is to receive input value, and then assigned to the variable
** Whether we value the input of numeric type, strings, lists, receiving input value is a string type.