“Offer comes, ask friends to take it! I am participating in the 2022 Spring Recruit Punch card campaign. Click here for more details.”

Data types in Python

Python list

What is a list

  • A list is a queue
  • A list is a collection of data types and a data type
  • A list is an ordered, repeatable collection of contents

In Python, a list is a list data type. A list is also a built-in list function. A list exists in [], and a list is a data structure of unlimited length

Lists can hold various data types, including the null type None, or they can be nested

Built-in functions in, Max, min

  • In determines whether an element exists in the list. Returns a bool
  • Max gets the largest element in the list. Elements in the list cannot be of multiple types. An error is reported if the types are inconsistent
  • Min gets the smallest element in the list. Elements in the list cannot be of multiple types. An error is reported if the types are inconsistent

List code exercises

none_list = [None.None.None]

print(none_list)
print(bool(none_list))
print(len(none_list))

# an empty list
print([])
print(len([]))
print(bool([]))
Copy the code

Use of the # in operator
str_list = ['Python'.'JAVA'.'PHP'.'Go']

is_exist = 'Python' in str_list
print(is_exist)
is_exist = 'HTML' in str_list
print(is_exist)

# Max, min function used
num_list = [1.4.7.0.10]
print(max(num_list))
print(min(num_list))
Copy the code

# Use min, Max for mixed type lists
mix_list = [1.'stark'[3.5]]
print(min(mix_list))
print(max(mix_list))
Copy the code

The elements in the list are inconsistent. Using min and Max will cause an error

The Python tuple

A tuple, like a list, is a queue that stores multiple data structures, and a tuple is an ordered queue that can store repeated elements.

Tuples are used to represent elements in Python, and tuples are also built-in functions that convert data into tuples. Tuples exist in (), and tuples are data structures of unlimited length

Differences between lists and tuples: Tuples occupy smaller resources and tuples are immutable, whereas lists are mutable data types

alpha_tuple = (1.4.9)
bravo_tuple = (2.4.10)

print(id(alpha_tuple))
alpha_tuple += bravo_tuple
print(id(alpha_tuple))
Copy the code

If the ID is changed, a new memory block is created instead of the original memory address, indicating that the tuple is immutable

The in, Max, and min built-in functions are used in tuples the same way they are used in lists

Tuple code exercise

Code demo, create

tuple_one = ('stark')
print(tuple_one)
print(type(tuple_one))
Copy the code

Execute the code

tuple_one_list = (['stark'.'steve'.'peter'])
print(tuple_one_list)
print(type(tuple_one_list))
Copy the code

Execute the code

Define a tuple that contains a single element. Be sure to include a comma. If there is no comma, the type of the data is the type of the element in the parentheses. Modify the contents of the script and execute the script again

tuple_one = ('stark'.)print(tuple_one)
print(type(tuple_one))
Copy the code

You can use () to define empty tuples

tuple_empty = ()
print(tuple_empty)
print(type(tuple_empty))
Copy the code

You can also use the tuple built-in function to define tuples

Use bool to determine that an empty tuple is False

tuple_bool = ()
print(bool(tuple_bool))
Copy the code

tuple_int = (1.2.5)
print(bool(tuple_int))
print(len(tuple_int))
print(max(tuple_int))
print(min(tuple_int))
Copy the code

Python dictionary

A dictionary is a data type consisting of multiple key-value pairs

Dict represents dictionary data types in Python, and dict is a built-in function used to create dictionaries, whose elements are wrapped in {}

herso_dict = {
    'name': 'stark'.'nickname': 'IronMAN'.'name': 'peter'.'nickname': 'SpriderMan'
}

human_dict = {}
Copy the code

Dictionary keys support immutable data types such as strings, numbers and arrays, and dictionary values support all Python data types

Dictionaries were unordered prior to Python 3.7, and ordered after 3.7

The dictionary Key is unique

Code demo

Define a dictionary of user information
user_info = {'name': 'peter'.'age': 18.'address': 'Queens'}

result = 'name' in user_info
print(result)

result = 'hope' in user_info
print(result)

result = 'hope' not in user_info
print(result)
Copy the code

Define a dictionary of user information
user_info = {'name': 'peter'.'age': 18.'address': 'Queens'}

# the dictionary returns the length
length = len(user_info)
print(length)

result_bool = bool(user_info)
print(result_bool)

dict_empty = {}
print(type(dict_empty))
print(bool(dict_empty))
Copy the code

Use of Max and min built-in functions

Define a dictionary of user information
user_info = {'name': 'peter'.'age': 18.'address': 'Queens'}

# Use of min Max
print(max(user_info))
print(min(user_info))
Copy the code

Python operator.

Numerical operation

The assignment operators in Python are

  • = : equals the operator
  • += : addition operator
  • -= : Subtraction operator
  • *= : multiplication operator
  • /= : Division operator
  • %= : modulo operator
  • **= : power operator
  • //= : divisible operator

Code demo

Create the operation_number script

a = 1
b = 2
c = 3

d = a + b + c
print(d)

# += operator
d += c
print(d)

The # -= operator
d -= b
print(d)

# *= operator
d *= a
print(d)

# /= operator
d /= a
print(d)

# //= operator
d //= a
print(d)

# %= operator
d %= c
print(d)

# **= operator
f = 10
f **= 2
print(f)
Copy the code

Strings cannot be multiplied, but strings and numbers can be multiplied

list_01 = [1.3.5.0.10]
print(list_01 * 3) # return a new list
print(list_01)


tuple_01 = (1.3.5.10)
print(tuple_01 * 3) Return a new tuple
print(tuple_01)
Copy the code

Duplicate keys are not allowed in dictionaries, and dictionaries do not support this type of multiplication

Comparison operator

The comparison operators in Python are:

  • == : Checks whether the value is equal to
  • ! = : Determines whether it is not equal to
  • > : Checks whether the value is greater than
  • < : Determines whether the value is less than
  • >= : Determines whether the value is greater than or equal to the value
  • <= : Determines whether the value is less than or equal to
  • Is: Checks whether two object storage units are equal
  • Is not: Determines whether two object stores are expected to be different
  • <> : Determines whether or not Python3 is deprecated

Is and IS not are identity operators

# Date: 2022/3/26

a = 1
a_01 = 1
b = 2.2
c = 0
d = 10
e = -2
f = 300
f_01 = 300

print(a == b)
print(a ! = b)print(a < b)
print(a > e)
print(b >= c)
print(a is a_01)
print(id(a))
print(id(a_01))
print(f == f_01)
Copy the code

The native Python interpreter predefines numbers between 0 and 255 into memory, so numbers between 0 and 255 have the same memory address, except for numbers above 255