"The Road to Python Learning--Data Types of Python Basics"

  Computers are advanced electronic devices that process numerical operations and logical operations. Therefore, computer programs will naturally process various data, and different data need to define different data types. In Python, the data types that can be directly processed are as follows:

①Integer (int)

  Python can handle integers of any size, including negative integers of course. The representation method in the program is basically the same as the mathematical writing method, for example: 1, 200, -8080 and so on. Integers are widely used in programs. In order to optimize speed, Python uses a small integer pool to avoid frequent application and destruction of memory space for integers. Python's definition of small integers is [-5,257) These integer objects are established in advance and will not be garbage collected, that is, in a Python program, all integers in this range use the same object, E.g:

As can be seen from the above figure, 5000 is not in the range of the integer pool, so the two objects used, and the value of 5 is the same object. (Note: the id() method is used to return the memory address of the variable)

 

②Floating point (float)

   Floating-point numbers are also decimals. The reason why floating-point numbers are floating-point numbers is that the decimal point position of a floating-point number is variable when expressed in scientific notation. For example, 1.23 * 10 9 is 1.23e9, or 12.3e8. You can use mathematical writing, such as: 1.23, 3.14, -9.07, etc. But for very large or very small floating-point numbers, scientific notation must be used. Note: Integers and floating-point numbers are stored differently in the computer. Integer operations are always accurate (including division), while floating-point operations may have rounding errors.

 

③Character type (str)

  A string is any text enclosed in single or double quotes, such as 'abc' , "xyz", etc. Please note that '' or "" itself is just a representation, not part of a string, so the string 'abc' has only 3 characters of a, b, and c. If ' itself is also a character, it can be enclosed in "", for example, "I'm OK" contains six characters of I, ', m, space, O, K. Like JS, you can use \ to represent escape characters in Python, but in Python, if the string contains ambiguous escape characters, but you don't need to escape, you can use r'' to express:

print ( ' hello\tworld ' ) # output: hello world
 print (r ' hello\tworld ' ) # output: hello\tworld

Therefore, if the string contains escape characters but does not expect escape, just add r before the string to achieve.

In addition, there are many ways to manipulate strings. For details, see "The Road to Python Learning -- Methods of Strings" 

 

④ Boolean value (bool)

  Like other programming languages, Python has only two: True and False, pay attention to the capitalization of the first letter (lowercase in some programming languages, such as JavaScript), and also provides Boolean or and not operations in Python:

AND operation: use the and keyword

True and True
>>>True
True and False
>>>False
False and False
>>>False
5 > 3 and 3 > 1
>>>True
1 and 2
>>>2

 

OR operation: use the or keyword

True or True
>>>True
True or False
>>>True
False or False
>>>False
5 > 3 or 1 > 3
>>>True
1 or 2
>>>1

 

NOT operation: use the not keyword

not True
>>>False
not False
>>>True
not 1 > 2
>>>True

 

Similar to JavaScript, in the OR AND operation, if the two operations are not of boolean type, the result will not return a boolean value, but the corresponding value (the red part above)

 

⑤None (None)

  A null value is a special value in Python, represented by None (equivalent to null in JavaScript).

 

⑥ list (list) (equivalent to an array in JavaScript)

  List is a built-in data type list in Python. It is an ordered collection. Just like arrays in JavaScript, it is represented by square brackets [ ]. The elements stored in it are called elements. A list can store multiple elements (defining a list does not need to define its length like Java, and there is no length limit for lists in Python), you can add or delete elements at any time; you can also read or modify elements by subscript indexing.

#Define a list to store the names of people 
names = [ ' Jack ' , ' Jonas ' , ' Tom ' , ' Bob ' ]
 # Read the elements in the list by subscript index (natural number starting from 0) 
print (names[0 ])   #Output Jack #Modify 
the third element Tom to Jerry by subscript index 
names[2] = ' Jerry ' #Print 
the entire list print (names) #Output   [ ' Jack', 'Jonas', 'Jerry', ' Bob']

In addition to manipulating elements by indexing, lists also provide many methods for manipulating elements. For details, see "The Way to Learn Python -- Methods of Lists". 

 

⑦tuple (tuple)

   Tuples are also a built-in data type in Python. Accessing elements in tuples is also consistent with lists, but unlike lists, tuples cannot be modified once initialized. Lists are represented by square brackets [ ], while tuples use Parentheses ( ) to indicate that when using tuples, you need to pay attention to the following:

1. Once the tuple is initialized, it cannot be modified, but if the elements in the tuple are of a variable type (such as a list), the data of the variable type in the tuple can be modified:

#Create a tuple containing a list and two numbers 
num_tuple = ([1,2,3],5,6 )
 #Modify the list (append() method is a built-in method of the list, used to add to the end of the list Add elements) 
num_tuple[0].append(4 )
 #Print tuple observation   print (num_tuple) #Output   : ( [1,2,3,4],5,6)

 

 2. When the tuple stores only one element, a comma must be added after the element to inform the interpreter that the data type stored in the variable is a tuple instead of a parenthesis

#If there is only one element, no comma is added 
num_tuple = (1 )
 #Print the result, the type() method is used to check the type of the variable 
print (num_tuple,type(num_tuple)) #Output   1 The type is int 
#Add a comma to A tuple, this is the charm of commas 
num_tuple = (1 ,)
 print (num_tuple,type(num_tuple)) #output   ( 1,) type is tuple

 

 

 3. Once an element in a tuple is declared, it cannot be modified, but the reference variable that holds the tuple can be deleted through the del keyword

 

 ⑧Dictionary (dict) (equivalent to ordinary objects in JavaScript)

   Similarly, a dictionary is also a built-in data type that stores data in a key-value manner. A dictionary is represented in the form of curly brackets { }. A dictionary can store multiple key-value pairs. Use a colon: to separate between key-value pairs, and commas to separate key-value pairs from key-value pairs. The dictionary has a very fast search speed. The corresponding value value is accessed by means of dict['key], but if the corresponding value cannot be found in this way, an error will be reported. If the built-in method of the dictionary is used dict.get('key ') method to access, if not found, it will not report an error, but return None

#Create a dictionary to represent a person's information 
person = { ' name ' : ' jonas ' , ' age ' :18, ' gender ' : ' male ' , ' address ' : ' China ' }
 # Access a value in the dictionary 
print (person[ ' name ' ])   #output jonas 
print (person[ ' salary ' ])   #this attribute does not exist, the program reports an error

 

In addition to this operation, there are many methods for dictionaries. For details, please refer to "The Road to Python Learning--Dictionary Methods"

 

⑨ Collection (set)

  Sets are also a data type of Python. Sets are represented by curly brackets { }. Sets can be understood as weakened dictionaries, because sets only store keys and no values, and the stored keys are immutable types and cannot be repeated. , if there is duplicate data, the interpreter will directly filter out the duplicate data.

#Create a set num_set 
= {1,2,3,3 }
 #If there is duplicate data, the duplicated part will be filtered directly print (num_set) #Output   { 1,2,3} 
#Add element 
num_set to the set. add(4 )
 print (num_set) #output   { 1, 2, 3, 4} #remove elements in the set 
num_set.remove 
(2 )
 print (num_set) #output   { 1, 3, 4}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693832&siteId=291194637