4. Basic data types for getting started with Python syntax

Keywords: Python Back-end

An introduction

We learn variables so that the computer can remember a certain state of things like people, and the value of variables is used to store the state of things. Obviously, the state of things is divided into different types (such as people's age, height, position, salary, etc.), so the value of variables should also have different types, such as

salary = 3.1 # Use floating point to record salary
age = 18 # Use integer to record age
name = 'lili' # Use string type to record person name

II. Digital type

2.1 int integer

2.1.1 function

It is used to record the integer related status of people's age, year of birth, number of students, etc

2.1.2 definitions

age=18

birthday=1990

student_count=48

2.2 float float

2.2.1 function

It is used to record people's height, weight, salary and other decimal related status

2.2.2 definitions

height=172.3

weight=103.5

salary=15000.89

2.3 use of digital types

1. Mathematical operation

>>> a = 1
>>> b = 3
>>> c = a + b
>>> c
4

2. Compare size

>>> x = 10
>>> y = 11
>>> x > y
False

Three string types str

3.1 function

It is used to record people's name, home address, gender and other descriptive states

3.2 definitions

name = 'Tony'

address = 'Pudong New Area, Shanghai'

sex = 'male'

Strings can be defined with single quotation marks, double quotation marks and multiple quotation marks. There is no difference in essence, but

#1. We need to consider the pairing problem of quotation mark nesting
msg = "My name is Tony , I'm 18 years old!" #If the inner layer has single quotation marks, the outer layer needs double quotation marks
#2. Multiple quotes can write multiple lines of string
msg = '''
        There are only two kinds of people in the world. For example, when you get a bunch of grapes, one person chooses the best to eat first, and the other person saves the best to eat last.
        As a rule, the first person should be optimistic, because every one he eats is the best of the leftover grapes; The second kind of person should be pessimistic, because every one he eats is the worst of the leftover grapes.
        However, the fact is just the opposite, because the second kind of people still have hope, and the first kind of people only have memories.
      '''

3.3 use

Numbers can be added, subtracted, multiplied and divided. What about strings? Yes, but only"Add"and"Multiply"Operation.
>>> name = 'tony'
>>> age = '18'
>>> name + age #Addition is actually a simple string splicing
'tony18'
>>> name * 5 #Multiplying is equivalent to adding strings five times
'tonytonytonytonytony'

Four list

4.1 function

If we need to use a variable to record the names of multiple students, it is impossible to use the number type. The string type can indeed be recorded, for example

stu_names = 'Zhang San, Li Si
But the purpose of saving is to get the name of the second student. At this time, it is quite troublesome to get the name of the second student, and the list type is specially used to record the values of multiple attributes of the same kind (such as the names of multiple students in the same class, multiple hobbies of the same person, etc.), and the access is very convenient

4.2 definitions

>>> stu_names=['Zhang San','Li Si','Wang Wu']

4.3 use

# 1. The list type uses the index to correspond to the value, and the index represents the position of the data, counting from 0
>>> stu_names=['Zhang San','Li Si','Wang Wu']
>>> stu_names[0] 
'Zhang San'
>>> stu_names[1]
'Li Si'
>>> stu_names[2]
'Wang Wu'
# 2. The list can be nested. The nested values are as follows
>>> students_info=[['tony',18,['jack',]],['jason',18,['play','sleep']]]
>>> students_info[0][2][0] #Take out the first student's first hobby
'play'

Five dictionary dict

5.1 function

If we need to record multiple values with one variable, but multiple values have different attributes, such as people's name, age and height, we can save them with a list, but the list corresponds to the value with an index, and the index can not clearly express the meaning of the value, we use the dictionary type. The dictionary type stores data in the form of key: value, in which key can have a descriptive function on value

5.2 definitions

>>> person_info={'name':'tony','age':18,'height':185.3}

5.3 use

# 1. Dictionary type uses key to correspond to value. Key can have descriptive function for value, usually string type
>>> person_info={'name':'tony','age':18,'height':185.3}
>>> person_info['name']
'tony'
>>> person_info['age']
18
>>> person_info['height']
185.3
# 2. Dictionaries can be nested. The nested values are as follows
>>> students=[
... {'name':'tony','age':38,'hobbies':['play','sleep']},
... {'name':'jack','age':18,'hobbies':['read','sleep']},
... {'name':'rose','age':58,'hobbies':['music','read','sleep']},
... ]
>>> students[1]['hobbies'][1] #Take the second student's second hobby
'sleep'

Six Boolean bool

6.1 function

Used to record true and false

6.2 definitions

>>> is_ok = True
>>> is_ok = False

6.3 use

Usually used as a condition of judgment, we will if Use it in judgment

Reprint source https://zhuanlan.zhihu.com/p/108682319

Posted by dzelenika on Sun, 31 Oct 2021 11:21:46 -0700