“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
In order to adapt to more application scenarios, the data is divided into multiple types, each type has its own characteristics and use scenarios, to help the computer process and display data more efficiently
Python contains the following data types
- Number type: Number. There is only one integer type int, which is a long integer and immutable data type
- String type: String, the data type enclosed in single or double quotation marks, is immutable data type
- Boolean: the transaction is True or False, with only True or False values
- Air type:
- List type: List, can complete most of the collection class data structure implementation, the List of elements can be the same or not the same, the List of elements between [], with a comma separated List of elements, is a variable data type
- Tuple type: Similar to a list except that elements in a tuple cannot be modified. Tuples are written between () and separated by commas
- Dictionary type: A dictionary is a mapping type, identified by {}, an unordered collection of key-value pairs, and a mutable data type
- Set type: a Set consists of one or more elements of different types. The things or objects that make up a Set are called elements or members. You can use curly braces {} or Set () to create a Set
Python numeric types
The integer int
- An integer is an integer, and 0 is an integer, and it’s a special integer
- Int is
plastic
And again, the definition of plasticBuilt-in function
- Defines an integer, either directly or using the int built-in function
num = 100
number = int(100)
Copy the code
Plus: Python2 also includes a long type, but Python3 has abandoned it
Float float
Floating-point is a decimal, so anything that has a decimal point in it is a floating-point number. Float is a representation of floating-point, and it’s one of the built-in functions that defines floating-point. You can define a floating-point either directly using a number with a decimal point, or you can define it using a built-in function called float
price = 3.8
total_price = float(49.9)
Copy the code
Built-in function type
The built-in function type can return the type of a variable. You can use the print built-in function to print out the type of the data in the console or the data type stored in the variable
type(Variable name or data)Copy the code
Code practice
Create a new number_type. Py file in the PYTHon_herOS folder
name = 'peter'
age = 18
address = 'Queens'
heigth = 1.78
if __name__ == '__main__':
print('name =', name)
print('age =', age)
print('address =', address)
print('heigth =', heigth)
Copy the code
Execute main
The type of data stored in the output variable. Add the type of data stored in the output variable to main
print('Name is of type:'.type(name))
print('Age is of type:'.type(age))
print('Address is of type:'.type(address))
print('Heigth is of type:'.type(heigth))
Copy the code
Execute mainThe console outputs the data type stored in the variable
The Python string
A string is information wrapped in single or double quotation marks (“). A string can contain any character or number in any order. It is important to note that strings are immutable data types
Python uses STR to represent string data, and STR is a built-in Python function that defines strings
Strings are immutable data types
Built-in function ID
The id() function returns the memory address of a variable
Built-in function len
Len () can return the length of a string. Numeric types have no length, so len() cannot be used
name = 'stark'
name_02 = 'tony stark'
print(id(name))
print(id(name_02))
new_name = name
print(new_name)
print(id(new_name))
print(type(new_name))
# Triple quotes define strings
info = ''' Message From Stark Industry '''
print(info)
print(type(info))
# Double quotes and single quotes define strings
info1 = 'alpha'
info2 = "bravo"
Output single or double quotes can be wrapped
print('" he said.")
print("' I said '")
Copy the code
Simple manipulation of strings
- Built-in member operator IN: The member operator is used to determine whether the specified data exists
- Built-in function Max: Returns the largest member of the data
- Chinese symbols > Letters > Numbers > English symbols
- Chinese is calculated according to the first letter of pinyin
- Built-in function min: Returns the smallest member of the data
- Chinese symbols > Letters > Numbers > English symbols
- Chinese is calculated according to the first letter of pinyin
- The sum of strings
- Strings cannot be subtracted, multiplied and divided
- Strings can be concatenated with the “+” plus operator
Code demonstration, create string_apply.py file
info = 'I am IRONMAN'
result = 'am' in info
print(result)
result = 'am' not in info
print(result)
Copy the code
print(max(info))
print(min(info))
Copy the code
String splicing
info1 = 'We are '
info2 = 'Aveners'
print(info1 + info2)
Copy the code
print(len(info1 + info2))
print(type(len(info1 + info2)))
Copy the code
Python Boolean and null types
Boolean type
The result of the truth-false judgment is Boolean, both True and False; Bool represents a Boolean type, and is a built-in function that can be used to determine whether the result is true or false
The bool type is often used to determine whether it is true or false
Run the following code in bool_sample.py
print(bool(0))
print(bool(1))
print(bool(str(' ')))
print(bool(str('Stark')))
Copy the code
Empty type
An empty type is:
- A null type is one that is not of any data type
- A fixed value None
- Null types fall into the False category
- When you are unsure of the type of a variable, you can use a null type
alpha = 'None'
bravo = None
charlie = ' '
print(bool(alpha))
print(bool(bravo))
print(bool(charlie))
Copy the code