Python Basics-Basic data type (number (numeric) string (string) list (list) Tuple (tuple) sets (collection) Dictionary (dictionary))
There are six standard data types in the Python3:
Number (numeric)
String (String)
List (lists)
Tuple (tuple)
Sets (collection)
Dictionary (dictionary)
Among the six standard data types of Python3:
Immutable data (four): Number, String (string), tuple (tuple), sets (set);
Variable data (two): List, Dictionary (dictionary).
Python3 Basic data types
Variables in Python do not need to be declared. Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned. In Python, a variable is a variable, it has no type, and what we call "type" is the type of object in memory that the variable refers to.
Variable = object
1. Assigning values to multiple variables
Python allows you to assign values to multiple variables at the same time.
For example:
A = b = c = 1
The above example creates an integer object with a value of 1 and three variables pointing to the same memory location.
2. You can also specify multiple variables for multiple objects.
For example:
A, b, C = 1, 2, "Runoob"
For the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "Runoob" is assigned to the variable C.
one, tuple (tuple)
Tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified. Tuples are written in parentheses (), and the elements are separated by commas.
1. Create an empty tuple
tup1 = ();
When you include only one element in a tuple, you need to add a comma after the element, or the parentheses will be used as an operator:
>>> tup1 = (+)>>> type (tup1) # without commas, type int <class' int'>>>> tup1 = (,)>>> type (tup1) # With commas, the type is tuple <class'tuple'>
2. Accessing tuples
Tuples can use the subscript index to access the values in the tuple, as in the following example:
#!/usr/bin/Python3tup1= ('Google',' World',1997, -) tup2= (1,2,3,4,5,6,7) Print ("Tup1[0]:", tup1[0]) print ("Tup2[1:5]:", tup2[1:5]) above example output: tup1[0]: googletup2[1:5]: (2,3,4,5)
3. Modifying tuples
The element values in the tuple are not allowed to be modified, but we can combine the tuples with the following example:
#!/usr/bin/Python3tup1= ( A,34.56); Tup2= ('ABC','XYZ'# The following modifications to tuple element operations are illegal. # tup1[0] = -# Create a new tuple tup3= Tup1 +Tup2;print (TUP3) above instance output result: ( A,34.56,'ABC','XYZ')
4. Delete tuples
element values in tuples are not allowed to be deleted, but we can use the DEL statement to delete an entire tuple, as in the following example:
#!/usr/bin/= ('Google' World'1997 ) print (tup) del tup;
5, tuple built-in functions
The Python tuple contains the following built-in functions
Serial Number |
Method and Description |
Example |
1 |
len (tuple) calculates the number of tuple elements. |
>>> Tuple1 = ( ' Google ' ' biying ' , ' Taobao ' Span class= "pun" >) >>> Len (tuple1) 3 >>> |
2 |
max (tuple) Returns the maximum value of an element in a tuple. |
>>> Tuple2 = ( ' 5 ' , span class= "str" > ' 4 ' , ' 8 ' ) >>> Max ( Tuple2 ' 8 ' >> |
3 |
MIN (tuple) Returns the element minimum value in a tuple. |
>>>=(' 5 ',' 4 ',' 8 ')>>> min(tuple2 )' 4 '>>> |
4 |
Tuple (SEQ) Converts a list to a tuple. |
>>>List1= [' Google ', ' Taobao ', ‘biying‘, ' Baidu ' ] >>> Tuple1=tuple ( list1) >>> ( ' Google ' , Taobao ' , ' biying ' , ' Baidu ' |
Attention:
1. As with strings, elements of tuples cannot be modified.
2, tuples can also be indexed and sliced, the same way.
3. Note the special syntax rules that construct tuples that contain 0 or 1 elements.
4, tuples can also use the + operator for stitching.
Python Basics-Basic data types (tuple (tuple)-immutable data)