日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python笔记(3)

發布時間:2024/4/14 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python笔记(3) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python的基本運算表達式

(1)判斷語句

關于if語句的規則我不再介紹,只在這里提出Python下if語句的用法,以及特點。

#!/usr/bin/python # Filename: if.py number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' # New block starts here print "(but you do not win any prizes!)" # New block ends here elif guess < number: print 'No, it is a little higher than that' # Another block # You can do whatever you want in a block ... else: print 'No, it is a little lower than that' # you must have guess > number to reach here print 'Done' # This last statement is always executed, after the if statement is executed

?

以下為輸出

?

$ python if.py Enter an integer : 50 No, it is a little lower than that Done $ python if.py Enter an integer : 22 No, it is a little higher than that Done $ python if.py Enter an integer : 23 Congratulations, you guessed it. (but you do not win any prizes!) Done

在這里,我們使用了一個函數raw_input(),用以獲取用戶的輸入。

我們為內建的raw_input函數提供一個字符串,這個字符串被打印在屏幕上,然后等待用戶的輸入。一旦我們輸入一些東西,然后按回車鍵之后,函數返回輸入。對于raw_input函數來說是一個字符串。我們通過int把這個字符串轉換為整數,并把它存儲在變量guess中。事實上,int是一個類,不過你對它所需了解的只是它把一個字符串轉換為一個整數。

?

注意:我們使用了縮進層次來告訴Python每個語句分別屬于哪一個塊。這就是為什么縮進在Python如此重要的原因。

???????? 在Python中沒有switch語句。不過你可以使用if..elif..else語句來完成同樣的工作。

?

?

(2)循環語句

?

1.while語句

Python的while語句,與其他語言的while語句區別不大。

#!/usr/bin/python # Filename: while.py number = 23 running = True while running: guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it.' running = False # this causes the while loop to stop elif guess < number: print 'No, it is a little higher than that' else: print 'No, it is a little lower than that' else: print 'The while loop is over.' # Do anything else you want to do here print 'Done'

?

輸出為:

?

$ python while.py Enter an integer : 50 No, it is a little lower than that. Enter an integer : 22 No, it is a little higher than that. Enter an integer : 23 Congratulations, you guessed it. The while loop is over. Done

?

注意:在Python中,你可以在while循環中使用一個else從句。

?

2.for語句

for語句,在這里,我通過一個例子在作出說明

#!/usr/bin/python # Filename: for.py for i in range(1, 5): print i else: print 'The for loop is over'

?

輸出為:

$ python for.py 1 2 3 4 The for loop is over

?

注意:else部分是可選的。如果包含else,它總是在for循環結束后執行一次,除非遇到break語句。

與C/C++語言相比,Python的if語句,無疑簡單了許多。

?

(3)其他語句

1.break語句

break語句在for循環和while循環中使用。

while True: s = raw_input('Enter something : ') if s == 'quit': break print 'Length of the string is', len(s) print 'Done'

?

輸出為

$ python break.py Enter something : Programming is fun Length of the string is 18 Enter something : When the work is done Length of the string is 21 Enter something : if you wanna make your work also fun: Length of the string is 37 Enter something : use Python! Length of the string is 12 Enter something : quit Done

?

?

2.continue語句

在這里,我只通過一個例子來說明。

#!/usr/bin/python # Filename: continue.py while True: s = raw_input('Enter something : ') if s == 'quit': break if len(s) < 3: continue print 'Input is of sufficient length' # Do other kinds of processing here...

?

輸出為:

$ python continue.py Enter something : a Enter something : 12 Enter something : abc Input is of sufficient length Enter something : quit

?

轉載于:https://www.cnblogs.com/karying/archive/2009/10/12/2015421.html

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的Python笔记(3)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。