PythonThe primary data types include objects such as list, tuple (tuple), Dict (dictionary), and set (collection), each of which describes these Python data types.
List (list) is a python built-in data type, as an ordered set of data, the elements of the list are variable, you can arbitrarily add or subtract elements in the list. Run the list-related code in the python interactive command:
>>> list_1 = [' One ', ' one ', ' three ']
>>> list_1
[' One ', ' I ', ' three ']
the object list_1 is a list, and we can use the index to access each element in the list, and the index in Python is calculated starting at 0:
>>> List_1[0]
' One '
>>> List_1[2]
' Three '
You can also access each object in the list backwards:
>>> List_1[-1]
' Three '
You can use the Append method when you add an object to a list:
>>> list_1.append (' four ')
>>> list_1
[' One ', ' one ', ' three ', ' four ']
to remove an object from list, you can use the Pop method:
>>> List_1.pop (1)
' Both '
>>> list_1
[' One ', ' three ']
the list can also be contained in another list as a single element:
>>> player=[' Curry ', ' Leonard ']
>>> nbaplayer=[' Westbrook ', ' Harden ', Palyer, ' Durant ']
Another important data type for Python: tuple (tuple ). Tuple and list are very similar, the difference is that the tuple is in the form of parentheses (), and once the tuple is initialized, it will not be as easy as the list can be modified.
>>> tuple_1 = (' One ', ' both ', ' three ')
>>> Tuple_1
(' One ', ' both ', ' three ')
tuple has access to the same object elements as list, which is not mentioned here. It is important to note that because the tuple element is an immutable object, there is no way to modify elements like list append, pop, and so on.
finally look at a special type of data in Python: Dict (dictionary). The dictionary, as the name implies, is definitely a powerful data query function. Dict is called map in other programming languages and has a key-value (key-value) storage function, see the following example:
>>> dict_1={' One ': 1, ' both ': 2}
>>> dict_1[' one ']
1
In addition to specifying the key-value of individual elements when creating dict, you can also specify values by key to put in:
>>> dict_1 [' three '] = 3
>>> dict_1[' three ']
3
dict finds or inserts data very quickly, but also consumes a lot of memory, which is exactly the opposite of list. Another type of data similar to Dict is called set (set), which is a collection of key sets but does not hold value, and is not introduced here.
>>>>
Python Programming Basics
Today I mainly introduce the if-else conditional judgment and the for and while Loop statements. Conditional judgment and circulation as a basic course of any programming language, it is necessary to emphasize this emphasis. First look at the If-else conditional judgment statement in Python:
Score = 66
If score >= 60:
Print (' The scores is qualified! ')
Else
Print (' The scores is unqualified! ')
we can also use elif to make more detailed conditions to judge:
score =
Score
If >= &NBSP;&N Bsp print (' Elif 60<=points<80:
print ('
the Py Loop statement, which is consistent with other language principles, is no longer detailed in detail, using the for and while loops as an example of the common Gaussian summation to demonstrate the functionality of the Python loop.
for Loop:
Sum=0
For x in range (101):
sum = sum + x
Print (sum)
5050
while loop:
Sum=0
n = 99
While n > 0:
sum = SUM + N
n = n-2
Print (sum)
5050
Python data type