程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python self study

編輯:Python

control flow —— The second day of self-study

  • 1. control flow
    • 1.1 The operator
    • 1.2 Boolean operator
    • 1.3 if sentence
    • 1.3 Loop statement
    • 1.4 The import module
  • 2. function
    • 2.1 Function format
    • 2.2 Local and global scope
    • 2.2 exception handling

1. control flow

1.1 The operator

“ Comparison operator ” Compare two values , Evaluate to a Boolean value .

The operator meaning == be equal to != It's not equal to < Less than > Greater than <= Less than or equal to >= Less than or equal to

1.2 Boolean operator

The operator meaning and And or or not Not

1.3 if sentence

if Statement by if、elif、else form .

if "a" == "b":
print("a == b")
elif 2 != 2:
print("2!=2")
elif 2 < 1:
print("2 < 1")
elif 2 > 3:
print("2 > 3")
elif 2 <= 1:
print("2 <= 1")
elif 2 >= 3:
print("2 >= 3")
elif True and False:
print("True and False")
elif False or False:
print("False or False")
elif not True:
print("not True")
else:
print("nothing is right")

1.3 Loop statement

while True:
print("Your are right")
for i in range(10):
if i == 2:
continue
if i == 5:
break;
print(i)

1.4 The import module

  • import random Import random modular
import random
i = random.randint(1, 10)
print(i)
  • from random import * Import random All methods in the module
from random import *
i = randint(1, 10)
print(i)

2. function

2.1 Function format

def Method name ( Parameters ):
Method body
return Return value

2.2 Local and global scope

 Arguments and variables assigned in the called function , At the end of the function “ Local scope ”.
Variables assigned outside all functions , Belong to “ Global scope ”. Variables in local scope , go by the name of “ local variable ”.
Variables in global scope , go by the name of “ Global variables ”.
If you need to modify global variables within a function , Just use global sentence .
Yes 4 Rules , To distinguish whether a variable is in local scope or global scope :
1. If the variable is used in the global scope ( That is, outside of all functions ), It is always a global variable .
2. If in a function , There are... For this variable global sentence , It's a global variable .
3. otherwise , If the variable is used for an assignment statement in a function , It's a local variable .
4. however , If the variable is not used in the assignment statement , It's a global variable .

2.2 exception handling

try:
Code block
except Error code :
Code block
finally:
Code block

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved