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

[Python basics 2022 latest] lesson 4 basic grammar

編輯:Python

【Python Basics 2022 newest 】 The fourth lesson Basic grammar

  • summary
  • loop
    • for loop
    • while loop
  • Judgment statement
    • if Judge
    • Ternary expression
    • break
    • continue
    • pass

summary

Starting today , Xiaobai, I will lead you to learn Python Introduction to zero foundation . This column will explain + Practice mode , Let's get familiar with it Python The grammar of , application , And the basic logic of the code .

loop

Loop statements can help us repeat a block of code many times , Improve the reuse rate and simplicity of code .

Python Type of loop in :

  • for loop : Repeat the statement
  • while loop : When the judgment condition is True when , Repeat the statement

for loop

example 1:

# utilize for Cyclic output 0-9
for i in range(10):
print(i)

Output results :

0
1
2
3
4
5
6
7
8
9

example 2:

# Create a list of
list1 = [1, 2, 3, 4, 5]
# utilize for Loop through the list
for num in list1:
print(num)

Output results :

1
2
3
4
5

while loop

Example :

# Defining variables i
i = 0
# while Cyclic output 0-9
while i < 10:
print(i) # Debug output i
i += 1 # Each cycle i+1

Output results :

0
1
2
3
4
5
6
7
8
9

Judgment statement

The judgment statement determines the code block to be executed later by judging and formulating conditions .

if Judge

Format :

if Judge the condition :
Execute statement

Example :

# if Judgment statement
if 1 < 2:
print(" Hello, motherland 1") # Condition is True, perform
if 2 < 1:
print(" Hello, motherland 2") # Condition is False, Not execute

Output results :

 Hello, motherland 1

Format :

if Judge the condition :
Condition is True Execute statement
else:
Condition is False Execute statement

Example :

# Create variables
num1 = 1
num2 = 2
# if...else... Judgment statement
if num1 > num2:
print(" Numbers num1 > Numbers num2")
else:
print(" Numbers num2 > Numbers num1")

Output results :

 Numbers num2 > Numbers num1

Ternary expression

Format :

 Condition is True perform if Conditions else Condition is False perform

Example :

# Create variables
num1 = 1
num2 = 2
# Ternary expression
result = " Numbers num1 > Numbers num2" if num1 > num2 else " Numbers num2 > Numbers num1"
print(result) # Debug output

Output results :

 Numbers num2 > Numbers num1

break

By using break sentence , python The code can jump out of the loop in advance .

Example :

# for loop
for i in range(10):
# When i by 3 Jump out of the loop
if i == 3:
break
# Debug output
print(i)

Output results :

0
1
2

continue

Compared to using break Statement directly jumps out of the loop , Use contine Statement will jump out of this loop , The rest of the code will not be executed .

Example :

# for loop
for i in range(10):
# When i by 3 Skip this cycle when
if i == 3:
continue
# Debug output
print(i)

Output results :

0
1
2
4
5
6
7
8
9

We can see , When i = 3 When , Skipped the current loop , i No printing .

pass

pass Statements in python The role of is to occupy space .

Example :

# Defined function
def func():
pass # placeholder

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