Basic data types
First, what is the data type
In fact, you can understand the data type refers to different types of variable values, the name may be a data type, age may be a data type, but may be interested in another data type
Second, why the data classification
Variable is used to reflect the status and state of change, there is no doubt you should use a different type of de-identified data for different states.
Third, different types of data
[Numeric type]
[String type]
[List type]
[Dictionaries]
[Boolean]
Digital Type
Integer (int)
1.1 Role
It represents the person's age, all kinds of numbers, level
1.2 Definitions
age = 18 # age=int(18)
print(id(age))
print(type(age))
print(age)
4530100848
<class 'int'>
18
1.3 How
Addition, subtraction, logical judgment (greater than, less than)
Float (float)
2.1 Role
He represents height, weight, payroll
2.2 definitions
salary = 2.1 # salary=float(2.1)
print(id(salary))
print(type(salary))
print(salary)
4569240656
<class 'float'>
2.1
2.3 How to
multiplication and division and subtraction, logical judgment (greater than, less than)
String type
1.1 Role
Indicate the name, hobby
1.2 Definitions way
String is a string of characters are strung together, wrapped in single quotes, or three double quotation marks a string of characters. Note that: three characters in quotation marks can newline, and characters in single or double quotation marks can not. Such as:
name1 = 'nick'
name2 = "egon"或
name3 = """nick
egon"""
1.3 Use :
+ Only string, and the logical comparison *
String concatenation, namely re-apply for a small space a copy of the two strings are then spliced. Instead you YY of the variable value in a small space inside the copy to another variable of a small space, then splicing
note:
1, if the quoted string, the string inside the quotation marks and wrapping string can not be the same.
2, multiplied by the multiplication string of numbers only.
3, compare the size of the string, the ASCII code comparison, will go into detail later.
4, the string comparison is alphabetical order.
List type (list)
1.1 Role
Storing a plurality of values, such as a plurality of girlfriend, a plurality of hobbies.
1.2 Definitions
Any type of value are separated by a comma within [].
hobby = 'read'
hobby_list = [hobby, 'run', 'girl']
print(id(hobby_list))
print(type(hobby_list))
print(hobby_list)
4558605960
<class 'list'>
['read', 'run', 'girl']
1.3 How
Deposit is not an end, is taking aim, we introduce the method list index value, bearing in mind the index number from zero.
hobby_list = ['read', 'run', 'girl']
# 索引序号 0 1 2
# 取出第二个爱好
print(hobby_list[1])
run
hobby_list = ['read', 'run', ['girl_name', 18, 'shanghai']]
# 取出girl的年龄
print(hobby_list[2][1])
18
Dictionary (dict)
1.1 Role
A plurality of values used to access, in accordance with key: value of the stored-value mode, may not take the time to go to the index value by the key, key has a function of descriptive value. Store a variety of types of data and more data when you can use a dictionary.
1.2 Definitions
{} 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.
1.3 How
Dictionary index value no longer depends on the way, but rather on the key, to obtain the value corresponding to the key value through the [key].
# 字典套列表
user_info = {'name': 'nick', 'gender': 'male', 'age': 19,
'company_info': ['oldboy', 'shanghai', 50]}
print(user_info['name'])
print(user_info['company_info'][0])
nick
oldboy
# 字典套字典
user_info = {'name': 'nick', 'gender': 'male', 'age': 19, 'company_info': {
'c_name': 'oldboy', 'c_addr': 'shanghai', 'c_num_of_employee': 50}}
print(user_info['name'])
print(user_info['company_info']['c_name'])
nick
oldboy
Boolean
1.1 Role
Conditions for determination result
1.2 Definitions
True, False usually not directly quoted, required logic operation result obtained.
1.3 How
print(bool(0))
print(bool('nick'))
print(bool(1 > 2))
print(bool(1 == 1))
False
True
False
True
$ \ color {red} {$ Note}: the Python values of all comes with Boolean data type. So much data types only need to 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
If we give a list, we need to remove the one-time multiple values, we can not use the following way to achieve it?
name_list = ['nick', 'egon', 'jason']
x = name_list[0]
y = name_list[1]
z = name_list[2]
print(f'x:{x}, y:{y}, z:{z}')
x:nick, y:egon, z:jason
Speak true, the above method is really with who knows who, we can decompress try.
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 decompress values may be we do not want, you can use an underscore, omnipotent underlined.
name_list = ['nick', 'egon', 'jason', 'tank']
x, y, z, a = name_list
x, _, z, _ = name_list # _相当于告诉计算机不要了,不能以_开头和结尾
name_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.
info = {'name': 'nick', 'age': 18}
x, y = info
print(x, y)
name age
Python interaction with the user
First, why interaction
Let's review what is the significance of computers invention, invention of the computer to computer slavery, the liberation of labor. Suppose we now write an ATM system replaces teller, if we want to withdraw money from this ATM, the ATM is not so that we will be asked to enter your name and password? We are not required to enter the withdrawal amount we need? This is not to be understood as an interaction. Let us now understand how to achieve the next Python is interactive.
Second, how to interact
name = input('请输入你的姓名:')
pwd = input('请输入你的密码:')
print(type(name))
print(type(pwd))
Please enter your name: nick
Please enter your password: 123
<class 'str'>
<class 'str'>
By printing the above results, we can find action input () method is only received values. As we currently lack the basics, we do use the input () interaction, then use the Web / visual form of interactive interface.
$ \ Color {red} {Note: We input regardless of the value of a digital type, string type, list type, input values are received string type. } $