<>PYTHON Final review for beginners

This semester, we have learned a lot python The grammar of , Program control structure , list , tuple , Dictionaries , aggregate , character string , function , modular , File and directory operation , exception handling , Visualization Foundation . Now I will show my own final summary . Welcome to point out the shortcomings .

<>python The cycle of final review

Python Provided for Cycle and while loop .

for Loops can traverse any sequence of items , For example, a list or a string loop can traverse any sequence of items , Such as a list or a string

if

Clause looks familiar . It consists of three parts ： Keywords themselves , A conditional expression used to judge whether the result is true or false , And the code block to execute when the expression is true or nonzero . single if Statements can be made by using Boolean operators and,or and not Realize multiple or negative judgment conditions . Just like other languages ,Python It provides information about if The use of sentence collocation else sentence . If if The resulting Boolean value of the conditional expression of the statement is false , Then the program will execute else Code after statement .

elif

yes Python Of else-if sentence , It checks whether multiple expressions are true , And execute the code in a specific block when true . and else equally ,elif The declaration is optional , The difference is that ,if Statement can have at most one else sentence , But there can be any number elif sentence .

while Is a conditional loop statement .while The code blocks in are executed in a loop all the time , Until the loop condition is no longer true .

break Statement can end the current loop and jump to the next statement

continue

Statements and other high-level languages continue It's no different . It can be used in while and for In the circle .while Circulation is conditional , and for The loop is iterative , therefore continue There are some prerequisites to be met before starting the next cycle , Otherwise, the cycle will end normally

<>PYTHON Grammar of final review

It will be shown in the form of a mind map python Basic grammar of English .

<>PYTHON The operators of final review

Python The language supports the following types of operators :

Arithmetic operator

compare （ relationship ） operator

Assignment Operators

Logical operators

Bitwise Operators

member operator

Identity operator

Operator priority

<>PYTHON Combined data types of final review

1. List type （ Flexible and changeable ）

The list is in brackets （[]） express

Direct use list() An empty list can be generated

list() You can also convert tuples and strings to lists .

2. Collection type （ Element type is not repeatable , And it is a fixed data type , For example, integer , character ）

The set is in curly braces （{}） express

set() Functions can generate collections .

3. Dictionary type （ Key value pair , key （ A property ）, value （ The content of the property ））

Dictionaries are represented by braces （{}） Connect with colon , Different key value pairs are connected by commas

Braces can also create an empty dictionary , You can add content to it through brackets .

4. Tuple type （ Immutable sequence , Cannot delete , It cannot be modified ）

Tuples are represented by commas and parentheses

Use parentheses () or tuple() Or create it directly , Elements are separated by commas .

It's similar to the list type .

<>PYTHON String type of final review

<>PYTHON Examples

Examples 1:

a = {1: 2, 2: 3} # This is a dictionary b = {2: 22, 3: 33} # Dictionary type , The key is 2 and 3, The value is 22 and 33 a.update(b)

# The main test points are update Usage of ,update()

Method to modify the current collection , You can add new elements or collections to the current collection , If the added element already exists in the collection , The element appears only once , Repeated will be ignored . print( a )

The answer is {1: 2, 2: 22, 3: 33}

Examples 2:

a = {8, 2, 3, 4} a.remove(3) #remove() Function to remove the first occurrence of a value in the list . This question has been removed 3 print(

sum(a) )

The answer is 14

Examples 3:

a = {1, 2, 94} b = {2, 94} c = sorted( a|b ) #sorted()

Function to sort all iteratable objects , Ascending order （ default ） The list is generated ,| Represent union set print( c )

The answer is [1,2,94]

Examples 4:

a = {2, 12, 13} b = {2, 12, 15} print( sorted(a^b) ) #^ It's a complement ,a^b by 13 and 15

The answer is [13,15]

Examples 5:

a = {11, 12, 14} b = 12 in a #in In the dictionary , If the key is in the dictionary dict Come back in true, Otherwise, return false. therefore in a be equal to 1

print( b+1 )

The answer is 13

Examples 6:

a = {2, 4, 6} b = {1, 2, 4, 6, 8} a.update(b) # After assignment a be equal to {1,2,4,6,8}update()

Function dictionary dict2 Key for / Value pair updated to dict in . Step size is not taken into account 0 What , print( len(a) )

#len Function is to calculate how many numbers it contains , Because after assignment a contain 5 number , So the result is 5

The answer is 5

Examples 7:

a = {1: 2, 2: 3, 3: 4} del a[2] #a[2] It means {2:3},del Variables are deleted , Not data , So it was deleted a In 2:3

print( a )

The answer is {1:2,3:4}

Examples 8:

a = {1: 2, 2: 3, 3: 4} b = a.pop(3) #pop Function sum del similar , however pop There is a return value print( b )

The answer is 4

Examples 9:

a = (10, 11, 12, 13) b = (93, 30, 71, 68) d = dict( zip(a,b) )

# A list packaged as tuples , And the number of elements is consistent with the shortest list .zip(a,b) express [(10,93),(11,30),(12,71),(13,68)],dict() Convert it to a dictionary {10:93,11:30,12:71,13:68}

print( tuple(d.keys())

)#keys Returns a list , And extract d The value of the middle key is placed in the list , namely (10,11,12,13),tuple Convert list to tuple .

The answer is (10, 11, 12, 13)

Examples 10:

def liang(x): #def Write function ,liang Is the name of the function x = x * 2 #* Represents multiplication return x # return x Value of a = 18 b =

liang(a) #liang() Representation function , and a It can be understood as x print(a, b)

The answer is 18 36

Examples 11:

def shi(x): x = x * 2 return x a = [8, 5] b = shi(a)

# because a It's a list , list *2 Indicates that the list copies the elements in the list twice print(a, b)

The answer is [8, 5] [8, 5, 8, 5]

Examples 12:

import math as m # Import math Package and named m def sBall(r): s=m.pi*pow(r,2)*4 #m. Indicates use math package ,

pi Represents PI ,pow(r,2) express r Square of , Can be used to calculate the surface area of the ball return s def vBall(r): v =4/3*m.pi*pow(r,3)

# Can be used to calculate the volume of the ball return v

Examples 13:

def demo(): return x_ x_ = 763 print( demo() )

The answer is 763

Examples 14:

def demo(): x = 762 return None # The value returned is null x = 23 demo() print(x)

The answer is 23

Examples 15:

w = zip('ABC', [8,6,7]) x = sorted(w, key=lambda x:x[1]) print(x)

The answer is [(‘B’, 6), (‘C’, 7), (‘A’, 8)]

This is the end of the summary , Thanks for watching ! Many suggestions are welcome , Make up for the lack of the next rookie ^ _ ^, I also hope to communicate with the great gods .

Technology

- Python146 blogs
- Java131 blogs
- Vue86 blogs
- Flow Chart79 blogs
- javascript42 blogs
- C++41 blogs
- programing language38 blogs
- MySQL37 blogs
- more...

Daily Recommendation

©2020-2021 ioDraw All rights reserved

The problem of string left rotation Interview essential ： Data structure time complexity and usage Remove outliers , use python to write matlab Of hampel(X) function BN layer pytorch realization Linux C Three methods of redirecting standard input and output to file [ Thesis reading ]HRNetV1,HRNetV2,HRNetV2p2021 World ranking of Computer Science Major ! This year's ranking reshuffle What certificates can big data test ? use Python and Pygame Implementation of code rain 2020 The 11th National Blue Bridge Cup B group C/C++