Click the blue word "python tutorial" to follow us!
Python variable types
The value of the variable stored in memory. This means that when a variable is created, a space is created in memory.
Based on the data type of the variable, the interpreter allocates designated memory and decides what data can be stored in the memory.
Therefore, variables can specify different data types, and these variables can store integers, decimals, or characters.
Variable assignment
Variable assignment in Python does not require type declarations.
Each variable is created in memory and includes information such as the variable's identification, name, and data.
Each variable must be assigned a value before it is used, and the variable will be created after the variable is assigned.
The equal sign (=) is used to assign values to variables.
The left side of the equal sign (=) operator is a variable name, and the right side of the equal sign (=) operator is the value stored in the variable. E.g:
Instance
#!/usr/bin/python # -*- coding: UTF-8 -*- counter = 100 # Assign an integer variable miles = 1000.0 # Floating point name = "John" # string print counter print miles print name ''' In the learning process, if you don’t know anything, you can add my python learning exchange button qun, 934109170 There are good learning tutorials, development tools and e-books in the group. Share with you the current talent needs of python companies and how to learn python from a zero basis, and what to learn. '''
In the above example, 100, 1000.0 and "John" are assigned to the counter, miles, and name variables respectively.
Executing the above program will output the following results:
Multiple variable assignment
Python allows you to assign values to multiple variables at the same time. E.g:
a = b = c = 1
In the above example, an integer object is created with a value of 1, and three variables are allocated to the same memory space.
You can also specify multiple variables for multiple objects. E.g:
a, b, c = 1, 2, "john"
In the above example, two integer objects 1 and 2 are assigned to variables a and b respectively, and the string object "john" is assigned to variable c.
Standard data type
There are many types of data stored in the memory.
For example, a person's age can be stored in numbers, and his name can be stored in characters.
Python defines some standard types for storing various types of data.
Python has five standard data types:
Numbers
String
List
Tuple
Dictionary
Python numbers
The numeric data type is used to store numeric values.
They are immutable data types, which means that changing the numeric data type will allocate a new object.
When you specify a value, the Number object is created:
var1 = 1 var2 = 10
You can also use the del statement to delete references to some objects.
The syntax of the del statement is:
del var1[,var2[,var3[....,varN]]]]
You can delete references to single or multiple objects by using the del statement. E.g:
del var del var_a, var_b
Python supports four different number types:
int (signed integer)
long (long integer [can also represent octal and hexadecimal])
float (floating point)
complex
Instance
Some examples of numeric types:
Lowercase l can also be used for long integers, but it is recommended that you use uppercase L to avoid confusion with the number 1. Python uses L to display long integers.
Python also supports complex numbers. A complex number is composed of a real part and an imaginary part, which can be represented by a + bj, or complex(a,b). The real part a and imaginary part b of a complex number are both floating-point types.
Note: The long type only exists in the Python 2.X version. In versions after 2.2, the int type data will automatically be converted to the long type after overflow. In the Python 3.X version, the long type is removed, and int is used instead.
Python string
A string or string (String) is a string of characters composed of numbers, letters, and underscores.
Generally denoted as:
s="a1a2···an"(n>=0)
It is a data type that represents text in a programming language.
Python's string list has 2 order of values:
The index from left to right starts at 0 by default, and the maximum range is 1 less than the length of the string
The index from right to left starts at -1 by default, and the maximum range is the beginning of the string
If you want to obtain a substring from a string, you can use [head subscript: tail subscript] to intercept the corresponding string, where the subscript starts from 0 and can be a positive or negative number. The label can be empty to indicate the beginning or the end.
[Head subscript: Tail subscript] The acquired substring contains the characters of the head subscript but not the characters of the tail subscript.
such as:
>>> s ='abcdef' >>> s[1:5] 'bcde'
When using a colon-separated string, python returns a new object, the result contains the continuous content identified by the pair of offsets, and the beginning on the left contains the lower boundary.
The above result contains the value b of s[1], and the maximum range taken does not include the tail subscript, which is the value f of s[5].
The plus sign (+) is the string concatenation operator, and the asterisk (*) is the repeated operation. Examples are as follows:
#!/usr/bin/python # -*- coding: UTF-8 -*- str ='Hello World!' print str # output complete string print str[0] # Output the first character in the string print str[2:5] # output the string from the third to the sixth in the string print str[2:] # Output the string starting from the third character print str * 2 # output string twice print str + "TEST" # output the connected string
The output of the above example:
Python list interception can receive the third parameter. The parameter is used to intercept the step length. The following example intercepts the string at the position of index 1 to index 4 and set the step length to 2 (one position apart):
Python list
List is the most frequently used data type in Python.
The list can complete the data structure realization of most collections. It supports characters, numbers, strings and even lists (ie nesting).
The list is marked with [], which is the most common compound data type in python.
The cutting of the value in the list can also use the variable [head subscript: tail subscript] to intercept the corresponding list. The index from left to right starts at 0 by default, and the index from right to left starts at -1 by default, and the subscript can be Empty means to get to the beginning or the end.
The plus sign + is the list concatenation operator, and the asterisk * is the repetitive operation. Examples are as follows:
#!/usr/bin/python # -*- coding: UTF-8 -*- list = ['runoob', 786, 2.23,'john', 70.2] tinylist = [123,'john'] print list # output complete list print list[0] # print the first element of the list print list[1:3] # Output the second to third elements print list[2:] # Print all elements from the third to the end of the list print tinylist * 2 # output the list twice print list + tinylist # print the combined list
The output of the above example:
['runoob', 786, 2.23,'john', 70.2] runoob [786, 2.23] [2.23,'john', 70.2] [123,'john', 123,'john'] ['runoob', 786, 2.23,'john', 70.2, 123,'john']
Python tuple
Tuple is another data type, similar to List.
Tuples are identified by (). Internal elements are separated by commas. But tuples cannot be assigned twice, which is equivalent to a read-only list.
Instance
#!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ('runoob', 786, 2.23,'john', 70.2) tinytuple = (123,'john') print tuple # output complete tuple print tuple[0] # output the first element of the tuple print tuple[1:3] # output the second to fourth (not included) elements print tuple[2:] # Print all elements from the third to the end of the list print tinytuple * 2 # output tuple twice print tuple + tinytuple # print the combined tuple
The output of the above example:
('runoob', 786, 2.23,'john', 70.2) runoob (786, 2.23) (2.23,'john', 70.2) (123,'john', 123,'john') ('runoob', 786, 2.23,'john', 70.2, 123,'john')
The following are invalid tuples, because tuples are not allowed to be updated. The list is allowed to update:
Instance
#!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ('runoob', 786, 2.23,'john', 70.2) list = ['runoob', 786, 2.23,'john', 70.2] tuple[2] = 1000 # Illegal application in tuple list[2] = 1000 # The list is a legal application
Python dictionary
Dictionary (dictionary) is the most flexible built-in data structure type in Python besides lists. A list is an ordered collection of objects, and a dictionary is an unordered collection of objects.
The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.
The dictionary is identified by "{ }". The dictionary is composed of an index (key) and its corresponding value.
Instance
#!/usr/bin/python # -*- coding: UTF-8 -*- dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name':'john','code':6734,'dept':'sales'} print dict['one'] # output the value of the key as'one' print dict[2] # Output the value of key 2 print tinydict # output complete dictionary print tinydict.keys() # output all keys print tinydict.values() # output all values
The output result is:
This is one This is two {'dept':'sales','code': 6734,'name':'john'} ['dept','code','name'] ['sales', 6734,'john']
Python data type conversion
Sometimes, we need to convert the built-in type of the data, the conversion of the data type, you only need to use the data type as the function name.
The following built-in functions can perform conversion between data types. These functions return a new object that represents the converted value.
Precautions
01
Students who are interested in Python development technology, welcome to join the communication group below to learn together and discuss with each other.
02
If you don’t understand in the process of learning python, you can join my python zero-base system to learn and exchange Qiuqiu qun: 934109170, to share with you the current talent needs of Python companies and how to learn Python from a zero-base, and what to learn. Related learning video materials and development tools are shared
Alright! The article is shared with the readers here
Finally, if you find it helpful, remember to follow, forward, and favorite