Python3 basic data type, Python3 Data Type
Variables in Python do not need to be declared. Each variable must be assigned a value before being used. A variable is created only after being assigned a value. In Python, a variable is a variable and has no type. The "type" we call is the type of the object in the memory referred to by the variable.
Equal sign (=) is used to assign values to variables. The left side of the equal sign (=) operator is the variable name, and the right side of the equal sign (=) operator is the value stored in the variable name. For example:
Integer = 100 # This is an integer, And the integer variable float = 100.0 # This is a floating point variable. The floating point variable string = "this is a string" # This is a string, and the string variable print (integer) print (float) print (string)
The following results are output when the above program is executed:
100100.0 this is a string
Assign values to multiple variables
Python allows you to assign values to multiple variables at the same time. For example:
a=b=c=1print(a)print(b)print(c)
The following results are output when the above program is executed:
111
In the preceding example, an integer object is created with a value of 1. The three variables a, B, and c are allocated to the same memory space.
You can also specify multiple variables for multiple objects. For example
a,b,c=1,2,3print(a)print(b)print(c)
The following results are output when the above program is executed:
123
In the preceding example, three objects 1, 2, and 3 are allocated to three variables a, B, and c respectively.
Standard Data Type
Python has six standard data types:
- Number)
- String (String)
- List)
- Tuple (tuples)
- Sets)
- Dictionary)
The built-in type () function of Python can be used to query the object type referred to by a variable. For example
Integer = 100 float = 100.0 string = "this is a string" print (type (integer) # print (type (float) # print (type (string )) # string variables
The following results are output when the above program is executed:
<class 'int'><class 'float'><class 'str'>
Number)
In Python3, int, float, bool, and complex (plural) are supported ). Like most languages, value assignment and computation are intuitive.
In Python2, there is no boolean type. It uses numbers 0 to indicate False, and 1 to indicate True. In Python3, True and False are defined as keywords, but their values are still 1 and 0. They can be added together with numbers. For example:
Print (100 + False) # False indicates the number 0 print (100 + True) # True indicates the number 1
The following results are output when the above program is executed:
101100
Python supports three different numeric types:
- INTEGER (Int)-It is usually called an integer or integer. It is a positive or negative integer without a decimal point. Python3 has no size limit and can be used as the Long type. Therefore, Python3 does not have the Long type of Python2.
- Float)-The floating point type consists of the integer part and the decimal part. The floating point type can also be expressed by scientific notation (2.5e2 = 2.5x102 = 250)
- Plural (complex ))-A complex number consists of a real number and a virtual number. It can be expressed by a + bj or complex (a, B). The real part a and virtual part B of a complex number are float.
Sometimes, we need to convert the built-in data types and convert the data types. You only need to use the data type as the function name.
Int (x)Converts x to an integer.
Float (x)Converts x to a floating point number.
Complex (x)Convert x to a plural number. The real part is x, and the imaginary part is 0.
Complex (x, y)Convert x and y to a plural number. The real part is x, and the imaginary part is y. X and y are numeric expressions.
The Python interpreter can be used as a simple calculator. You can enter an expression in the interpreter, which outputs the value of the expression.
The expression syntax is straightforward: + ,-,*,/,//,**.
In addition, different types of numeric values are converted into floating-point numbers during mixed operation.
Example:
Print (1 + 2) # Addition print (2-1) # subtraction print (2*3) # multiplication print (10/5) # division, get the floating-point type result print (21 // 5) # print (21% 5) of the result after the integer division is returned (5) # return the remainder of the Division print (2 ** 3) # power operation, that is, print (2*3.0) to the power of 2 # different types of numeric values will be converted into floating-point numbers for Calculation During the Hybrid Operation
The following results are output when the above program is executed:
3162.04186.0
String (String)
A string is the most common data type in Python. You can use quotation marks ('or ") to create a string.
It is easy to create a string by assigning a value to the variable. For example:
var1 = 'Hello World!'var2 = "I don't know this person"
The syntax format of string truncation is as follows:
Variable [header Subscript: tail subscript]
The index value starts with 0 and ends with-1. Subscripts are truncated to the end of the subscripts (excluding the characters whose index value is the end of The subscripts ). For example:
Var = 'abcdefhijklnm 'print (var) # output string print (var [4]) # output string fifth character print (var [-1]) # print (var [-2]) output the last character # print (var [0:-1]) # print (var [: 4]) All characters from the first to the last (excluding the last) # print (var []) All characters before the fifth character of the output string # output from the third character to the fifth character (excluding the fifth character) print (var [2:]) # print (var * 2) All characters starting from the third string # print (var + "TEST") the output string twice ") # connection string
The following results are output when the above program is executed:
abcdefghijklnmemnabcdefghijklnabcdcdecdefghijklnmabcdefghijklnmabcdefghijklnmabcdefghijklnmTEST
Python uses the backslash (\) to escape special characters. If you do not want the backslash to be escaped, you can add an r before the string to indicate the original string. For example:
Print ("abcd \ efg") # \ (at the end of the row) Continue line operator # output result: abcdefuplint ("abcdef \ g ") # \ backslash = "\" # output result: abcdef \ uplint ("abcde \ 'f \ 'G") # \ 'single quotes # output result: abcde 'F' uplint ("abcde \" f \ "g") # \ "Double quotation marks # output result: abcde" f "uplint (" abcdef \ bg ") # \ B Return, delete the first character of \ B # output result: abcdeuplint ("abcdef \ 000g") # \ 000 null
# Output result: abcdef uplint ("abcdef \ ng") # \ n # new line feed output result: abcdef # uplint ("abcdef \ tg") # \ t horizontal tab # output result: abcdef uplint (r "abcde \ n \ t \'' \ "") # Add an r prior to the string to indicate the original string # output result: abcde \ n \ t \''\"
Python string operators:
Var1 = "hello" var2 = "python" print ("var1 + var2 output:", var1 + var2) # + String connection print (var1 * 2) # * print (var1 [1]) # [] obtain the character print (var1 []) in the string through the index # [: b] truncate part of the string if "h" in var1: # in member operator-returns True print ("h in variable var1") else if the string contains the given characters: print ("h does not exist in the variable var1") if "o" not in var1: # not in member operator-returns True print ("o does not exist in variable var1") else: print ("o exist in variable var1") if the string does not contain the given character ")
The following results are output when the above program is executed:
The output result of var1 + var2 is: hellopythonhellohelloeel h exists in the variable var1, and o exists in the variable var1.