Basic data types, and various types of data

Basic data types

1. What is the data type

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, as to what type of data will be described in detail in the next chapter.

2. The different data types

2.1 Digital Type

1. integer (int)

effect:

It represents the person's age, all kinds of numbers, level

definition:
age = 18 # age = int(18)
print(id(age))
print(type(age))
print(age)

140722974123184
<class 'int'>
18

how to use:

Addition, subtraction, logical judgment (greater than, less than)

2. Float (float)

effect:

There are numbers behind the decimal representation of height and weight, payroll and other characters.

definition

salary = 2.2  #salary = float(2。2)
print(id(salary))
print(type(salary))
print(salary)

1433294378376
<class 'float'>
2.2

how to use

Addition, subtraction, logical judgment (greater than, less than)

2.2 Type String (str)

effect

Indicate the name, hobbies, etc.

definition

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.

name1 = 'wwb'
name2 = "wang"
print(id(name1))
print(type(name1))
print(name1)

3131992073360
<class 'str'>
wwb

name3 = """wwbwang"""
print(name3)

wwb
cheek

how to 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, and then stitching.

Note: If the quoted string, the string inside the quotation marks and wrapping string can have the same

mag2 = "my name is 'wang'"
mag3 = "my name is 'bin'"
print(mag2+mag3)

my name is 'wang'my name is 'bin'

k nick nick 

Note: multiplication string of numbers can only be multiplied.

name = 'wang'
print(name*10)

wangwangwangwangwangwangwangwangwangwang

Note: compare the size of the string, the ASCII code comparison, the future will go into detail.

pig1 = 'hello'
pig2 = 'z'
print(pig1 > pig2)

False

Note: string comparison is alphabetical order.

pig3 = 'zero'
pig4 ='bin'
print('z' > 'a')
print('Z' > 'A')

True
True

2.3 List (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 **

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

2.4 Dictionary (dict)

If there is a demand now I need to save the information, the data types that we learned before, we only list capable of storing information.

usur_info = ['wang','zhuang','19',['boy''shanghai''30']]
print(usur_info[1])#zhuang
print(usur_info[0])#wang
print(usur_info[3][1])#shanghai

zhuang

zhuang

While using the list to achieve our aim, but we can list the value of the time, we already know is based on an internal list of all the elements. So we can not give each element in the list are adding a description of it? Otherwise, if my information is not stored, but with access to a list of thousands of value, how we value it by index accurate? This looks as if it is impossible, so you can use the new data types - dictionary.

1.1 Role
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.

user_info = {'name': 'nick', 'gender': 'male', 'age': 19,
             'company_info': ['oldboy', 'shanghai', 50]}

print(id(user_info))
print(type(user_info))
print(user_info)

4396183344
<class 'dict'>
{'name': 'nick', 'gender': 'male', 'age': 19, 'company_info': ['oldboy', 'shanghai', 50]}

1.3 How

#字典套列表
usur_info1 = {'name':'wang','gender':"man",'age':19,
              'team':['boy''shanghai''30']}
print(usur_info['name'])
print(usur_info['team']['0'])
#字典套字典
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

Second, exercise

students = [
    {'name': 'nick', 'age': 19},
    {'name': 'egon', 'age': 18}
]
print(students[0]['name'])

nick

Guess you like

Origin www.cnblogs.com/wwbplus/p/11274190.html