Python3 keyword Usage notes

Source: Internet
Author: User
Tags assert custom name

There are 33 keywords, of which false,none,true is capitalized and the rest are lowercase.

1, False: Boolean false, commonly used as a return value, or when the condition is judged. The number 0 and the empty set belong to False.

2, None: First put, add later.

3, True: Boolean true, commonly used as the return value, or when the condition is judged. Non-0 numeric and non-empty collections are true.

4, and: Connect two expressions. Equivalent to logic.

if  and Age<20

5, as: Take a new name for the introduced module. The name of the reference module in the function uses a custom name.

import Tkinter as TK
T=tk. Tk () #本来应该是t =tkinter. Tk ()

6, Assert: Declares that an expression must be true, and if the expression is not true on the way to the program, it will error assertionerror.

assert number=10number =5

7,break: Jumps out of the current loop body loop, executes the next line of code, (in the example, the range () function does not all traverse, jumps out of the For loop)

 for  in range (1,100):    x=x+1    print(x)    if x>10:          Break

8, class: Used to define the class, (the class is capitalized; The class has a constructor __init__ (), which is the property of the Class)

class Car:     def __init__ (self,color):        self.color=color

9, continue: Do not perform this cycle in the loop body, continue circulation in the body. (In the example, the range () function is all traversed except x=5,)

 for  in range (1,10):    x=x+1    if x==5:        continue    Print (x)

def: defines a function for the object to invoke. (defines a function, where the function body is a pass statement)

Def jump ():    Pass

11,del: Removes unwanted objects from the list.

list=[5,4,3,2,1, 0]del  list[0]print(list)

Elif: Used with the IF keyword to indicate the judgment of the If branch.

13,else: Used with the IF keyword to indicate that none of the above conditions are met when the Else statement is executed.

14,except: Paired with try and finally to handle exception errors in the body of the function, there can be multiple except statements, each except executed. After a try code error occurs, the compiler does not make an error and executes the except statement.

Try :     Print (" execute the code here if there is an error to execute the code in except ")    rint (" the print here is wrong, However, the compiler did not error, because the error occurred after the execution of the except statement ")except:    Pass   

Finally: with try and except, the code error in try is traversed after the except statement, and then the finally statement is executed.

Try :     Print (" execute the code here if there is an error to execute the code in except ")    rint (" the print here is wrong, However, the compiler did not error, because the error occurred after the execution of the except statement ")except:    Pass finally :     Print (" every except and finally is executed.") "

For: Creates a loop body of known number of times.

 for  in range (0,5):    print(i)

17,from: Import a function or an entire module from a module, you can omit the calling module name when calling.

 from Import *tk=tk ()#import tkinter#tk=tkinter. Tk ()

18,global: Defines global variables that can be used throughout the module, and the scope of global variables defined in the function body is also the entire module.

a=100b=200def  func ():    Global  a    a=300  #  The scope is the entire module    b=400  # scope only in this function body func ()print(a)  Print(b)

19,if: Judgment statements, often used with elif and else. If the condition is true, execute the code for the IF statement, otherwise do not execute the IF statement.

 import   Randoma  =random.randint ( 1,200)  if  a>100 :  print  ( " more than   " )  elif  a>50 :  print  ( " beside 50-100   " )  else  :  print  ( " no more than   ") 

Import: Imports the module, the call needs to write the module name, for example, refer to the FROM keyword example.

In: Determines whether the object belongs to a list. (I'm not sure if it's only used in lists and for)

if inch [0,1,2,3,4,5,6]:     Print ("YES") Else :     Print ("No")

is: Determines whether two objects are equal. (I'm a little confused, I'm clear to add)

a=10b=10if is B:    print("YES")  Else:    print("No")

23,lambda: (Don't understand, add later)

24,nolocal: Defines a local variable scoped to the body of the function. (Examples I have not met, do not understand.) Supplement later)

Not: Equivalent logical non, which negates the Boolean value returned by an expression.

x=Truey= not xprint (y)   # Prints "False"

The Boolean value returned by the expression is true, or either: equivalent logic or if any of the expressions connected by or are true.

X=6y=0ifor  y:    print(" any one is true is true " Print (" off topic, empty set is false, non-empty is true ")          Print (" these three prints will print ")    

Pass: Define a new function, but when you do not think about the function body, you can first use PASS statement, define if condition statement, but do not want to execute code how to write, you can also use pass

def func ():     Pass    # if the defined function body does not execute code, it will error syntax error. a=10If a>10:    pass  # Ibid .

Raise: (Active write an error?) Don't understand, add later)

Return: Returns the value of the function or expression after execution, used to invoke the object.

(after defining a class or function, we use the object to invoke the class or function to implement the desired function, return value is used when the object is called)

Import randomx=random.randint (1,100)def  func (x):    if x>10:          return to    else:         return  xprint( Func (x))  # Here is the return value of the returned print (x)    # printed here is the resulting random number x value 

Try: Often used with except,finally, executes the code of the try statement, executes the code of except if an error occurs, and then executes finally if except does not have corresponding code execution.

Try :     Print (" execute the code here if there is an error to execute the code in except ")    rint (" the print here is wrong, However, the compiler did not error, because the error occurred after the execution of the except statement ")except:    Pass finally :     Print (" every except and finally is executed.") ")

When the while:while expression is true, a while loop is executed, and a while loop is used to be careful about the dead loop.

X=1 while x==1:    Print(x)    break# If there is no break, will always print x, code infinite Loop, you can use CTRL + C shortcut to terminate the loop.     

With: Do not understand how to use, later to add.

33,yield: Do not understand how to use, later Add.

Python3 keyword Usage notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.