python基礎for迴圈和while迴圈(十)

2021-09-24 02:11:28 字數 973 閱讀 3197

while:迴圈 

a = 10

while a > 0:

print(a)

print('結束')

for迴圈:

a = '12345'

for item in a:

print(item)

b = [1, 2, 3, 4]

for item in b:

print(item)

c = ('a', 'b', 'c', 'd')

for item in c:

print(item)

d =

# 列印出字典的key和value

for a,b in d.items():

print("{}-{}".format(a,b))

e =

# for a, b in d.items():

# print('{}={}'.format(a, b))

# for item in e:

# print(item)

# 列印1到20的數字

for item in range(1, 20):

print(item)

# 表示從1到20每間隔4個數列印一次

for item in range(1, 20,4):

print(item)

迴圈的關鍵字:

# break

# 表示0-9都可以列印出來,左關右開

for i in range(10):

print(i)

if i % 2 != 0:

break

# continue

for i in range(10):

if i % 2 == 0:

continue

print(i)

python 迴圈和列表基礎

二.列表 一.while迴圈 1.迴圈計算 迴圈一般操作 1 找數學規律重複的工作 2 用迴圈語句完成 計算0 100所有數字累加之和 sum 0 i 0 while i 100 sum sum i i 1 print sum 5050 計算 0 100 之間所有數字的累計求和結果 sun num ...

Python基礎 條件和迴圈

4.1 if語句 if expression expr true suite 只有條件表示式expression結果為真時才執行expr true suite 塊,否則繼續執行緊跟在該 塊後面的語句。單個if語句中的expression條件表示式可以通過布林操作符and,or,not實現多重條件判斷...

Python基礎 條件判斷和迴圈

age 20 if age 18 print your age is age print adult else print youth your age is 20 adult注意 python 的縮排規則.具有相同縮排 被視為 塊,上面的3 4 行就構成了乙個 塊 縮排請嚴格按照python的習慣...