You can learn Python articles without reading online classes (day 4)

Why_ does_ it_ work 2021-11-25 15:31:37


Number( Numbers )

Python  character string

Python  list (List)

Python  Tuples

Python  Dictionaries (Dictionary)

Number( Numbers )

python Number Data types are used to store values .

Data type cannot be changed , That means if it changes Number Value of data type , Memory space will be reallocated

Python  character string

The string is Python The most commonly used data type in . We can use quotation marks (' or ") To create a string .

Creating a string is simple , Just assign a value to the variable

Python  list (List)

Sequence is Python The most basic data structure in . Each element in the sequence is assigned a number - Its location , Or index , The first index is 0, The second index is 1, And so on .

Python Yes 6 Built in types of sequences , But the most common are lists and tuples .

The operations that can be performed by a sequence include indexing , section , Add , ride , Check members .

Besides ,Python There's a built-in way to determine the length of the sequence and determine the maximum and minimum elements .

Lists are the most commonly used Python data type , It can appear as a comma separated value in square brackets .

The data items of a list do not need to have the same type

Create a list , Just enclose the different data items separated by commas in square brackets

list1 = ['physics', 'chemistry', 1997, 2000]

list2 = [1, 2, 3, 4, 5 ]

list3 = ["a", "b", "c", "d"]

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

The output of the above example :

list1[0]: physics
list2[1:5]: [2, 3, 4, 5]

Delete list elements

have access to del Statement to delete the elements of the list

del list1[2]

Key functions in the list

Serial number function
1 cmp(list1, list2)
Compare the elements of two lists
2 len(list)
Number of list elements
3 max(list)
Returns the maximum value of a list element
4 min(list)
Returns the minimum value of a list element
5 list(seq)
Converts a tuple to a list

Python  Tuples

Python A tuple of is similar to a list , The difference is that the elements of a tuple cannot be modified .

Tuples use braces , Use square brackets for lists .

Tuples are easy to create , You just need to add elements in parentheses , And separate them with commas .

tup1 = ('physics', 'chemistry', 1997, 2000)

tup2 = (1, 2, 3, 4, 5 )

tup3 = "a", "b", "c", "d"

Create an empty tuple

tup1 = ()

When a tuple contains only one element , You need to add a comma after the element

tup1 = (50,)
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]

The output of the above example :

tup1[0]: physics
tup2[1:5]: (2, 3, 4, 5)

Modify tuple

Element values in tuples are not allowed to be modified , But we can join tuples

tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2
print tup3

The output of the above example :

(12, 34.56, 'abc', 'xyz')

Delete tuples

Element values in tuples are not allowed to be deleted , But we can use del Statement to delete the entire tuple

del tup

Tuple operators

Same as string , You can use... Between tuples + Number and * Number to calculate . That means they can combine and copy , A new tuple is generated after the operation

Python  Dictionaries (Dictionary)

Dictionary is another variable container model , And can store any type of object .

Each key value of the dictionary  key=>value  Yes, with a colon  :  Division , Comma between each key value pair  ,  Division , The whole dictionary is enclosed in curly brackets  {}  in , The format is as follows :

d = {key1 : value1, key2 : value2 }

Keys are usually the only , If you repeat the last key value pair, the previous one will be replaced , Value doesn't need to be unique .

dict = {'a': 1, 'b': 2, 'b': '3'}

Will be output :3

The value can take any data type , But the bond has to be immutable , Such as a string , A number or tuple .

A simple dictionary example :

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

You can also create a dictionary :

dict1 = { 'abc': 456 }

dict2 = { 'abc': 123, 98.6: 37 }

Visit the values in the dictionary

Put the corresponding key in the familiar square brackets , The following example :


 dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

The output of the above example :

dict['Name']: Zara
dict['Age']: 7

Revise the dictionary

The way to add new content to the dictionary is to add new keys / It's worth it , Modify or delete existing keys / The value pairs are as follows :

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8 # to update
dict['School'] = "RUNOOB" # add to
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

The output of the above example :

dict['Age']: 8
dict['School']: RUNOOB

Delete dictionary elements

Can delete a single element can also empty the dictionary , Emptying takes only one operation .

Show delete a dictionary with del command , The following example :

del dict['Name'] # Delete key is 'Name' The entry of

dict.clear() # Empty all dictionary entries

del dict # Delete Dictionary

Dictionary key features

The dictionary value can take any python object , It can be a standard object , It can also be user-defined , But the key doesn't work .

Two important points to remember :

1) The same key is not allowed to appear twice . When creating, if the same key is assigned twice , The latter value will be remembered , The following example :2) The key must be immutable , So you can use numbers , A string or tuple acts as , So using lists doesn't work , The following example

dict = {['Name']: 'Zara', 'Age': 7}
print "dict['Name']: ", dict['Name']

In short, the dictionary repeats and remembers the following , The dictionary cannot appear in the list .

python It is expected to end tomorrow , But the article will continue to share ,c Language and python The details of , Turn on c++ Algorithm

The third language !


Function and code reuse
One , Definition and use of functions

(1) Definition of function :

        a, General functions

         def< Function name >( Parameters (0 One or more )):

               < The body of the function >

              return < Return value >

      b, lambda function

        < Function name >=lambda< Parameters >:< expression >

(2) The return value of the function

        The function can return 0 One or more results

(3) Local and global variables

     #1: Local variables and global variables are different variables . Can be renamed ; At the end of the function operation , Local variables are released ; It can be done by global Reserved words use global variables within functions

    #2: The local variable is a composite data type and has not been created , Equal to global variable


Please bring the original link to reprint ,thank
Similar articles