Python3循環

Python中while語句的一般形式:

while 判斷條件:

?????? 語句

?

同樣需要注意冒號和縮進,另外在Python中沒有do…while循環

下面的實例計算1到100總和

##calc.py
n = 100sum = 0
counter = 1
while counter <= n:sum = sum + countercounter += 1print("total from 1 to 100 : %d",sum)

運行結果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 calc.py

total from 1 to 100 : %d 5050

?

while循環中使用else語句

在while…else在條件語句為false時執行els語句塊

#while.py
count = 0
while count < 5:print(count," < 5")count = count + 1
else :print(count ," >= 5")

運行結果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 while.py

0? < 5

1? < 5

2? < 5

3? < 5

4? < 5

5? >= 5

?

for循環:

Python for循環可以遍歷任何序列的項目,如一個列表或一個字符串

for循環的 一般格式如下

for <variable> in <sequence>:<statement>
else:<statement>

實例:

break語句用于跳出當前循環體:

##break.py
sites = ["Baidu","Google","Runoob","Taobao"]
for site in sites:if site == "Runoob":print("cainiao!")breakprint("loop data " + site)
else:print("Having no loop data!")
print("loop end!")

運行結果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 break.py

loop data Baidu

loop data Google

cainiao!

loop end!

range()函數

如果你需要遍歷數字序列,可以使用內置的range()函數,它會生成數列,例如:

?

?

也可以使range以指定數字開始并指定不同的增量,(甚至可以是負數,有時這也叫步長)

?

??

負數:

?

?

也可以結合range()和len()函數以遍歷一個序列的索引:

?

?

?

還可以使用range()函數來創建一個列表:

?

?

?

break和continue語句及循環中的else子句

break語句可以跳出for和while循環體,如果你從for或while循環終止,任何對應的循環else塊將不執行:

#else.py
for letter in 'Runoob':if letter == 'b':break;print('the current letter : ',letter)print("the next example")var = 10
while var > 0:print('the current variable : ',var)var = var - 1if var == 5:break;
print("GOOF bye!")

運行結果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3 else.py

the current letter :? R

the current letter :? u

the current letter :? n

the current letter :? o

the current letter :? o

the next example

the current variable :? 10

the current variable :? 9

the current variable :? 8

the current variable :? 7

the current variable :? 6

GOOF bye!

?

continue語句被用來Python跳出當前循環塊的剩余語句,然后繼續下一輪循環。

循環語句可以有else子句,它在窮盡列表(for循環)或條件變為false(以while循環)導致循環終止時被執行,但循環被break終止時不執行。

下列是查詢質數的循環例子:

##prime.py
for n in range(2,10):for x in range(2,n):if n % x == 0:print(n," == ",x, '*', n//x )breakelse:print(n," is prime")

運行結果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3 prime.py

2? is prime

3? is prime

4? ==? 2 * 2

5? is prime

6? ==? 2 * 3

7? is prime

8? ==? 2 * 4

9? ==? 3 * 3

?

pass語句

Python pass是空語句,是為了保持程序結構的完整性。

pass不做任何事情,一般用作占位語句:

#pass.py
for letter in 'Runoob':if letter == 'o':passprint('execute pass block')print('the current letter : ',letter)print("Good bye!")

運行結果:

?robot@ubuntu:~/wangqinghe/python/20190826$ python3 pass.py

the current letter :? R

the current letter :? u

the current letter :? n

execute pass block

the current letter :? o

execute pass block

the current letter :? o

the current letter :? b

Good bye!

?

pass只是為了防止語法的錯誤

pass就是一條空語句,在代碼段中或定義函數時,如果沒有內容,或者就先不做任何處理,直接跳過,就可以先使用pass

?

十進制轉換:

#translate.py
while True:number = input('please input a integer(enter Q exit ):')if number in ['q','Q']:breakelif not number.isdigit():print("input error,please continue input : ")continueelse:number = int(number)print("decimal --> hexadecimal: %d -> 0x%x"%(number,number))print("decimal --> octonary: %d -> 0x%o"%(number,number))print("decimal --> binary: %d -> "%number,bin(number))

運行結果:

robot@ubuntu:~/wangqinghe/python/20190826$ python3 translate.py

please input a integer(enter Q exit ):9

decimal --> hexadecimal: 9 -> 0x9

decimal --> octonary: 9 -> 0x11

decimal --> binary: 9 ->? 0b1001

please input a integer(enter Q exit ):18

decimal --> hexadecimal: 18 -> 0x12

decimal --> octonary: 18 -> 0x22

decimal --> binary: 18 ->? 0b10010

please input a integer(enter Q exit ):q

轉載于:https://www.cnblogs.com/wanghao-boke/p/11414083.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384844.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384844.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384844.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python3迭代器和生成器

迭代器 迭代是Python最強大的功能之一&#xff0c;是訪問元素集合的一種方法。 迭代器是一個可以記住遍歷的位置的對象。 迭代器對象從集合的第一個元素開始訪問&#xff0c;直到所有的元素被訪問完結束&#xff0c;迭代器只能向前不會后退。 迭代器有兩個基本方法&#xff0c;…

Pythton3實例

計算1-100之和 #add.py n 0 sum 0 for n in range(0,101):sum n print(sum) 實現99乘法法則 #mul.py i 1 while i < 9:j 1while j < i:mut j*iprint("%d * %d %d"%(j,i,mut),end" ")j 1print(" ")i 1 運算結果: robotubuntu:~/wa…

Python3函數

函數是組織好的&#xff0c;可重復使用的&#xff0c;用來實現單一&#xff0c;或相關功能的代碼段。 函數能提高應用的模塊性&#xff0c;和代碼的重復使用率。 定義一個函數 可以定義一個由自己想要功能的函數&#xff0c;以下是簡單規則&#xff1a; l 函數代碼塊是以def關…

epoll函數

epoll是Linux下多路復用IO接口select/poll的增強版本&#xff0c;它能顯著提高程序在大量并發連接中只有少量活躍的情況下的系統CPU利用率&#xff0c;因為它會復用文件描述符集合來傳遞結果而不用迫使開發者每次等待事件之前都必須重新準備要被偵聽的文件描述符集合&#xff0…

epoll事件模型

事件模型 EPOLL事件有兩種模型&#xff1a; Edge Triggered (ET) 邊緣觸發只有數據到來才觸發&#xff0c;不管緩存區中是否還有數據。 Level Triggered (LT) 水平觸發只要有數據都會觸發。 思考如下步驟&#xff1a; 假定我們已經把一個用來從管道中讀取數據的文件描述符(RFD)…

epoll反應堆模型代碼

libevent函數庫核心思想 /*** epoll_loop.c ***/ #include<stdio.h> #include<sys/epoll.h> #include<sys/socket.h> #include<arpa/inet.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> #include<string.h> #in…

UDP廣播

廣播是在局域網之間的一對多的通信方式&#xff0c;使用的udp協議 /*** client.c ***/ #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h>#define SERVER_PORT 8000 #define MAXLINE…

UDP組播

多播(組播) 組播組可以是永久的也可以是臨時的。組播組地址中&#xff0c;有一部分由官方分配的&#xff0c;稱為永久組播組。永久組播組保持不變的是它的ip地址&#xff0c;組中的成員構成可以發生變化。永久組播組中成員的數量都可以是任意的&#xff0c;甚至可以為零。那些沒…

Python3數據結構

列表&#xff1a; Python列表是可變的&#xff0c;這是它區別于字符串數組和元組的最重要的特點。列表可以修改&#xff0c;而字符串和元組不能。 以下是Python中列表的描述方法&#xff1a; 方法 描述 list.append(x) 將元素添加到列表結尾 list.extend(L) 通過添加指定列…

sed、awk工具

ed sed意為流編輯器&#xff08;Stream Editor&#xff09;&#xff0c;在Shell腳本和Makefile中作為過濾器使用非常普遍&#xff0c;也就是把前一個程序的輸出引入sed的輸入&#xff0c;經過一系列編輯命令轉換為另一種格式輸出。sed和vi都源于早期UNIX的ed工具&#xff0c;所…

C語言正則表達式

POSIX規定了正則表達式的C語言庫函數&#xff0c;詳見regex(3)。我們已經學習了很多C語言庫函數的用法&#xff0c;讀者應該具備自己看懂man手冊的能力了。本章介紹了正則表達式在grep、sed、awk中的用法&#xff0c;學習要能夠舉一反三&#xff0c;請讀者根據regex(3)自己總結…

makefile通用版本

實際當中程序文件比較大&#xff0c;這時候對文件進行分類&#xff0c;分為頭文件、源文件、目標文件、可執行文件。也就是說通常將文件按照文件類型放在不同的目錄當中&#xff0c;這個時候的Makefile需要統一管理這些文件&#xff0c;將生產的目標文件放在目標目錄下&#xf…

Python3OS文件/方法

Python3OS文件/方法 os模塊提供了非常豐富的方法用來處理文件和目錄。 方法 描述 os.access(path,mode) 檢驗權限模式 os.chdir(path) 改變當前工作目錄 os.chflags(path,flags) 設置路徑的標記為數字標記 os.chmod(path,mode) 更改權限 os.chown(path,uid,gid) 更改…

Python3文件

open()方法 Python open()方法永于打開一個文件&#xff0c;并返回文件對象&#xff0c;并對文件進行處理過程中都需要用到這個方法&#xff0c;如果該文件無法被打開&#xff0c;則拋出OSError 注意&#xff1a;使用open()方法一定要保證關閉文件對象&#xff0c;即調用close(…

Python3輸入輸出

Python兩種輸出值的方式&#xff0c;表達式語句和print()函數。 第三種方式是使用文件對象的write()方法&#xff0c;標準輸出文件可以用sys.stdout的引用。 如果你希望輸出的形式更加多樣&#xff0c;可以使用str.fomat()函數來格式化輸出值。 如果你希望將輸出的值轉化成字符…

動態庫加載順序

1.編譯目標代碼時指定的動態庫搜索路徑&#xff1b; 2.環境變量LD_LIBRARY_PATH指定的動態庫搜索路徑&#xff1b; 3.配置文件/etc/ld.so.conf中指定的動態庫搜索路徑&#xff1b; 4.默認的動態庫搜索路徑/lib&#xff1b; 5.默認的動態庫搜索路徑/usr/lib。 轉載于:https://ww…

Python3正則表達式

正則表達式是一個特殊的字符序列&#xff0c;他能幫助你方便的檢查一個字符串是否與某種模式匹配。re.match函數 re.match嘗試從字符串的起始位置匹配一個模式&#xff0c;如果不是起始位置匹配成功的話&#xff0c;match()就返回一個none。 函數語法&#xff1a; re.match(pat…

C/C++輸入

fgets(str,n,stdin) 從鍵盤輸入一行&#xff0c;替代gets()。讀取到n-1字節時或換行符時終止&#xff0c;如果是文件的話&#xff0c;讀到文件結尾也會停止 getline(cin,str) str的類型必須是string類&#xff0c;它是C特定的字符串類&#xff0c;區別于C的char *數據類型。 ci…

strlen和sizeof的區別

C語言中沒有字符串&#xff0c;用的是字符數組來模擬字符串。 C風格的字符串時字符數組然后在末尾加0表示結尾。 在C語言中有strlen和sizeof兩個函數求字符數組的長度函數&#xff0c;他們倆的區別就是是否把最后的結束標志也加上去。 strlen是不加的&#xff0c;他表示字符串的…

shell編程練習題

求2個數之和計算1-100的和將一目錄下所有的文件的擴展名改為bak編譯當前目錄下的所有.c文件&#xff1a;打印root可以使用可執行文件數&#xff0c;處理結果: roots bins: 2306打印當前sshd的端口和進程id&#xff0c;處理結果: sshd Port&&pid: 22 5412輸出本機創建20…