首頁 > 軟體

Python基礎學習--lesson10

2021-05-24 18:30:33
Python第十課的基礎知識
01:58來自LearningYard學苑

01條件表示式

選擇或迴圈結構中,若條件表示式值不為Falsea、0、None等值,則皆認定為True

In the selection or loop structure, if the value of the conditional expression is not Falsea, 0, None, etc., it will all be regarded as True

1.關係運算符:可連用

1. Relational operators: can be used together

2.不允許使用賦值運算符=

2. The assignment operator = is not allowed

3. 邏輯運算符and和or具備惰性求值的特徵

3. The logical operators and and or have the characteristics of lazy evaluation

02選擇結構

單分支、多分支、跳轉分支、巢狀分支

Single branch, multiple branch, jump branch, nested branch

1.單分支選擇結構

Single branch selection structure

2.雙分支選擇結構

2. Double branch selection structure

例:雞兔同籠問題

Example: Chicken and rabbit in the same cage

三元運算符:value1 if condition else value2

當條件表示式condition的值與True等價時,表示式的值為value1,否則表示式的值為value2

Ternary operator: value1 if condition else value2

When the value of the conditional expression condition is equivalent to True, the value of the expression is value1, otherwise the value of the expression is value2

03多分支選擇結構

例:將成績按百分制轉換為等級制

Example: Convert the grades from a hundred-point system to a grade system

迴圈結構

01while語句

1. while: 一般用於迴圈次數難以提前確定的情況,當然也可以用於迴圈次數確定的情況

1. while: Generally used in situations where the number of cycles is difficult to determine in advance, of course, it can also be used in situations where the number of cycles is determined.

猜數字遊戲

Guess the number game

02for語句

for: 一般用於迴圈次數可以提前確定的情況,尤其適用於列舉或遍歷

for: Generally used in situations where the number of loops can be determined in advance, especially for enumeration or traversal

使用迴圈結構遍歷並輸出列表的所有元素

Use a loop structure to traverse and output all elements of the list

輸出1~100之間能被7整除且同時被5整除的所有整數

Output all integers between 1 and 100 that can be divisible by 7 and at the same time by 5

列印九九乘法表

Print nine-nine multiplication table

03break和continue語句

break:結束迴圈

continue:結束本次迴圈,進入下一次迴圈

break: end the loop

continue: End this cycle and enter the next cycle

參考資料:谷歌翻譯

本文由LearningYard學苑原創,如有侵權,請聯絡刪除。


IT145.com E-mail:sddin#qq.com