python為什么叫爬蟲,【python】基本控制流程(4)

 2023-10-07 阅读 28 评论 0

摘要:參考: Python從零開始系列連載,by 王大偉 Python愛好者社區 Hellobi Live | 1小時破冰入門Python 《簡明python教程》 python為什么叫爬蟲、《小甲魚零基礎入門學python》 莫煩PYTHON 10 個不為人知的Python冷知識 Note: 更多連載請查看【python】 文章目錄1 復

參考:

  • Python從零開始系列連載,by 王大偉 Python愛好者社區

  • Hellobi Live | 1小時破冰入門Python

  • 《簡明python教程》

  • python為什么叫爬蟲、《小甲魚零基礎入門學python》

  • 莫煩PYTHON

  • 10 個不為人知的Python冷知識

Note:

  • 更多連載請查看【python】

文章目錄

  • 1 復合賦值語句
    • 1.1 多變量同時賦值
    • 1.2 變量交換
    • 1.3 復合賦值
  • 2 順序
  • 3 分支
    • 3.1 if 語句
    • 3.2 if-else 語句
      • 3.2.1補充:豐富的else
    • 3.3 if-elif-else 語句
    • 3.4 分支語句嵌套
    • 3.5 條件表達式三元操作符
  • 4 循環
    • 4.1 while
    • 4.2 for
      • for ... else ...
    • 4.3 循環中斷
      • 4.3.1 break
      • 4.3.2 continue
      • 4.3.3 assert
  • 附錄
    • if 多個條件同時判斷

python和JAVA的區別,


這里寫圖片描述

1 復合賦值語句

1.1 多變量同時賦值

在Python中,可以使用一次賦值符號,給多個變量同時賦值
這里寫圖片描述

1.2 變量交換

非常方便,不用中間變量

age_1 = 'male'#轉換性別
age_2 = 'female'
age_1,age_2 = age_2,age_1#age_1,age_2 = age_2,age_1這種操作是Python獨有的,不用第三者temp
print(age_1,age_2)

輸出為

python中divmod。female male

1.3 復合賦值

如下,+=,-=,*=,=, **=,%=

age = 25
age += 2
print (age)#27
age -=3
print (age)#24
age *= 2
print (age)#48
age /= 4
print (age)#12
age **= 2
print (age)#144
age %= 5
print (age)#4

輸出為

27
24
48
12.0
144.0
4.0

2 順序

從上到下,順序執行

import calendar
year = int(input('請輸入年份:')) #輸出指定某年的日歷
tab = calendar.calendar(year) 
print(tab)

輸入年份(我輸入的是2017),結果如下
這里寫圖片描述

3 分支

3.1 if 語句

python 類,可以通過判斷條件是否成立來決定是否執行某個語句;
這里寫圖片描述

3.2 if-else 語句

就是在原有的if成立執行操作的基礎上,當不成立的時候,也執行另一種操作;
這里寫圖片描述

3.2.1補充:豐富的else

else 除了和if搭配實現要么怎樣,要么不怎樣外,還可以和 while(干完了能怎樣,干不完就別想怎樣),except 搭配(沒有問題,那就干吧

num = int(input("請輸入一個整數"))
while(num):if num>=5:print("輸入的數大于等于5")breaknum-=1
else:print("輸入的數小于5")

例如輸入6,結果為

請輸入一個整數6
輸入的數大于等于5

try:1+'1'
except TypeError as reason:print("error")
else:print('no error')try:1+1
except TypeError as reason:print("error")
else:print('no error')

結果為:

error
no error

3.3 if-elif-else 語句

python和java,這種語句是處理可能有多種情況的判斷。

這里寫圖片描述

ans = int(input('請輸入你的身高:'))
if ans < 180:print('一般身高')
elif 180 <= ans < 185:print('王子身高')
elif 185 <= ans < 190:print('男神身高')
else:print('巨人身高')

輸入身高后,結果為

請輸入你的身高:172
一般身高

3.4 分支語句嵌套

if中還有if

gender = input('請輸入你的性別(M或者F):')
age = int(input('請輸入你的年齡(1-130):'))
if gender == 'M':if age >= 22:print('你已經達到合法結婚年齡,有對象了沒?\n沒有?男的也行啊!還不去相親?!!')else:print('你還是個可愛的男孩子')
else:if age >= 20:print('你已經達到合法結婚年齡,有男票了沒?\n沒有?還不去相親?!!')else:print('你還是個美膩的女孩子')

按提示輸入后,結果為:

請輸入你的性別(M或者F):M
請輸入你的年齡(1-130):24
你已經達到合法結婚年齡,有對象了沒?
沒有?男的也行啊!還不去相親?!!

3.5 條件表達式三元操作符

python怎么用,這里寫圖片描述


Note: 在條件語句中(if,while)
??None、False、0、‘’、“”、()、{}、[]**都當作False

while None:print('進入循環')
print('退出循環')

結果為

退出循環

4 循環

4.1 while

count = 1
sum = 0
while count < 11:sum = sum + countprint('sum = %d,count = %d'%(sum,count))count += 1

結果為

sum = 1,count = 1
sum = 3,count = 2
sum = 6,count = 3
sum = 10,count = 4
sum = 15,count = 5
sum = 21,count = 6
sum = 28,count = 7
sum = 36,count = 8
sum = 45,count = 9
sum = 55,count = 10

while 可以與 else 搭配

while 條件:語句
else:語句

python程序、Q:下面的例子中,循環中的 break 語句會跳過 else 語句嗎?

while 條件:語句break
else:語句

會,因為如果將 else 語句與循環語句(while 和 for 語句)進行搭配,那么只有在循環正常執行完成后才會執行 else 語句塊的內容

4.2 for

for 循環變量 in 對象:
??循環語句

對象可以是字符串,列表,元組,字典

for i in range(0,10,2):print(i)

結果為:

0
2
4
6
8

qpython、說明下range的使用
這里寫圖片描述

打印乘法口訣表

for i in range(1,10):for j in range(1,i+1):q = i*jprint('i*j = %d    '%q,end='')print('\n')

結果為:

i*j = 1    i*j = 2    i*j = 4    i*j = 3    i*j = 6    i*j = 9    i*j = 4    i*j = 8    i*j = 12    i*j = 16    i*j = 5    i*j = 10    i*j = 15    i*j = 20    i*j = 25    i*j = 6    i*j = 12    i*j = 18    i*j = 24    i*j = 30    i*j = 36    i*j = 7    i*j = 14    i*j = 21    i*j = 28    i*j = 35    i*j = 42    i*j = 49    i*j = 8    i*j = 16    i*j = 24    i*j = 32    i*j = 40    i*j = 48    i*j = 56    i*j = 64    i*j = 9    i*j = 18    i*j = 27    i*j = 36    i*j = 45    i*j = 54    i*j = 63    i*j = 72    i*j = 81   

小技巧
python 如何在一個for循環中遍歷兩個列表

A = [1,2,3,4,5]
B = ['LJR','WL','MYH','LCL','WD']
for i,j in zip(A,B):print (i,j)

結果為

1 LJR
2 WL
3 MYH
4 LCL
5 WD

for … else …

python控制軟件、來面來看下比較冷門的 for 和 else 搭配

for case in "Bryant":print(case)
else:print("All words have been printed!\n")

output

B
r
y
a
n
t
All words have been printed!

可以看到 else 會在 for 執行結束后運行

1)for break else

for case in "Bryant":print(case)break
else:print("All words have been printed!\n")

output

B

python控制其他應用程序。for 被中途 break 了, 則程序不會進入 else 分支

2)for continue else

for case in "Bryant":print(case)continue
else:print("All words have been printed!\n")

output

B
r
y
a
n
t
All words have been printed!

可以看出 else 不受 continue 的影響

4.3 循環中斷

4.3.1 break

結束本次循環,跳出所在的循環

i = 0
while 1:print('這是第%d次循環'%i)i += 1if i >5:break

python如何控制硬件。結果為:

這是第0次循環
這是第1次循環
這是第2次循環
這是第3次循環
這是第4次循環
這是第5次循環

4.3.2 continue

結束本次循環,繼續進行下一次循環

for i in range(0,5):num = int(input('請輸入你本次抓娃娃需要多少秒(1~60秒):'))if num > 30:print('時間到啦,機器自動給你抓了!')continueprint('你本次用了%d秒抓了一下'%num)

結果為

請輸入你本次抓娃娃需要多少秒(1~60秒):1
你本次用了1秒抓了一下
請輸入你本次抓娃娃需要多少秒(1~60秒):2
你本次用了2秒抓了一下
請輸入你本次抓娃娃需要多少秒(1~60秒):31
時間到啦,機器自動給你抓了!
請輸入你本次抓娃娃需要多少秒(1~60秒):32
時間到啦,機器自動給你抓了!
請輸入你本次抓娃娃需要多少秒(1~60秒):33
時間到啦,機器自動給你抓了!

4.3.3 assert

assert這個關鍵字我們稱之為“斷言”,當這個關鍵字后邊的條件為假的時候,程序自動崩潰并拋出 AssertionError 的異常。

一般來說我們可以用Ta再程序中置入檢查點,當需要確保程序中的某個條件一定為真才能讓程序正常工作的話,assert關鍵字就非常有用了。

assert 3>4

怎么用python控制另一個程序。結果如下

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-8-020d57da1a31> in <module>()
----> 1 assert 3>4AssertionError: 

再看看如下一個例子,來加深對 break,continue 的印象

1)break

while True:a = input("please input a number:")if a == '1':breakelse:passprint('still in while')
print("not in while")

先輸入 2,再輸入1

please input a number:2
still in while
please input a number:1
not in while

符合 break 條件后,會直接結束循環,不執行 else 后面的語句


2)continue

while True:a = input("please input a number:")if a == '1':continueelse:passprint('still in while')
print("not in while")

先輸入2,再一直輸入1

please input a number:2
still in while
please input a number:1
please input a number:1
please input a number:1
……

符合 continue 條件后不會執行 else 后面的代碼,而會直接進入下一次循環。


3)True and False

a = True
while a:b = input("please input a number:")if b == '1':a = Falseelse:passprint('still in while')
print("not in while")

會執行 else 后面的代碼,然后再退出。


補充
python 的 … 也可以代替 pass,python 無處不對象
在這里插入圖片描述

附錄

if 多個條件同時判斷

if 包含多個條件,全部滿足

math_points = 51
biology_points = 78
physics_points = 56
history_points = 72my_conditions = [math_points > 50, biology_points > 50,physics_points > 50, history_points > 50]if all(my_conditions):print("Congratulations! You have passed all of the exams.")
else:print("I am sorry, but it seems that you have to repeat at least one exam.")
# Congratulations! You have passed all of the exams.

if 包含多個條件,任意一個滿足

math_points = 51
biology_points = 49
physics_points = 48
history_points = 47my_conditions = [math_points > 50, biology_points > 50,physics_points > 50, history_points > 50]if any(my_conditions):print("Congratulations! You have passed all of the exams.")
else:print("I am sorry, but it seems that you have to repeat at least one exam.")
# Congratulations! You have passed all of the exams.

Note: 更多連載請查看【python】

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/132182.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息