一:for 循環
1. 簡單的說如果讓你輸出1到100之間的整數,用while該怎么實現呢?
i=0 while i<100: print(i) i+=1
看着是不是只有4行,但是有沒有更加簡單的辦法,不妨我們使用for循環來試一下
for i in range(1,101,): print(i)
是不是更加簡單,所以說我們總會選擇最簡單的方法去達到我們的目的
九九乘法表:使用while
while i <= 9: j = 1 while j <= i: print(str(j) + "*" + str(i) + "=" + str(j * i), end="\t") j += 1 print() i += 1
結果:
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
如果使用for循環

2. 輸出1到100之間的偶數
for i in range(1,101,): if i%2==0: print(i)
輸出
for i in range(1,101,2): print("loop:",i)
3. break的雙重用法
exit_flag=False for i in range(10): if i<5: continue print(i) for j in range(10): print(j) if j==6: exit_flag=True break if exit_flag: break
4...
for循環中,如果執行break,則下面的就不執行
(1) 程序: 三次登錄
# _user="sang" # _password="123" # for i in range(0,3): # username=input("Username:") # password=input("Password") # if username==_user and password==_password: # print("Welcome %s to login"% _user) # break#跳出,中斷 # else: # print("Invilid username or password")
比較:
#for循環更加簡單的比較
# _user="sang"
# _password="123"
# for i in range(0,3):
# username=input("Username:")
# password=input("Password")
# if username==_user and password==_password:
# print("Welcome %s to login"% _user)
# break#跳出,中斷
# else:
# print("Invilid username or password")
# else:
# print("執行次數太多!")_user="sang"
再次比較:
_user="sang"
_password="123"
passed_anthentication=False #anthentication認證
for i in range(0,3):
username=input("Username:")
password=input("Password")
if username==_user and password==_password:
print("Welcome %s to login"% _user)
passed_anthentication=True
break#跳出,中斷
else:
print("Invilid username or password")
if not passed_anthentication:
print("輸入次數太多!")
二:格式化輸出 基本的格式化,語法為: 格式化符號 % 格式化對象
ctrl d 直接粘貼到下一行 ctrl ? 注釋多行
name = input("Name:") age = input("Age:") job = input("Job:") salary = input("Salary:") msg = ''' ------------- info of %s ----------------- Name: %s Age : %s Job : %s Salary: %s ------------ end ---------------- ''' % (name,name,age,job,salary) print(msg)
加深比較:
name = input("Name:") age = int(input("Age:")) job = input("Job:") salary = input("Salary:") if salary.isdigit(): salary=int(salary) else: print("must input digit") exit()#退出程序 msg =''' -------------info of %s ----------------- name: %s age: %d job: %s salary: %f you will be retired in %s years ------------end---------------- '''% (name,name,age,job,salary,100-age) print(msg)
三: 列表 list
(1) a=("aaa","bbb","ccc","ddd","eee") 元組里進行的 切片:取多個元素
print(a[1:]) 取到最后面
a[1:4]取下標一到下標4的數字,包括1但是不包括4
print(a[1:-1])取到倒數第二位
print(a[1:-1:1])從左到右一個一個取
print(a[1::2])從左到右隔一個去取
(2)以下這些操作只能在列表里進行: 列表的一些操作命令
#a=["aaa","bbb","ccc","ddd","eee"]
#刪除
#a.remove("ccc") remove("內容")
#a.pop(2) pop(index)默認尾部刪除,可指定索引值
#del a[2] del a del a[index]
#a.clear()清空
#增加
#a.append("zzz")追加一個數
#a.insert(1,"ccc") a.insert(index,"內容")
#a.extend("h") 擴展
#修改
#a[2]="mmm" a[index]="新的值"
#a[1:3]=[111,222,333] a[start:end]=[a,b,c]
a=[2,5,3,9,2,7]
#a.sort() 排序,默認升序
#a.reverse() 反序
#b=a.count("2") 查找元素的個數
# b=["f","g","h"]
# b.extend(a) 連接列表
print(b)
(3)以下是一個簡單的購物車程序,
goods_list=[("iphone",6000),("book",500),("watch",2000),("coffee",100),("icecream",50)]#定義商品列表 salary=input("Your salary is:") #定義你的工資 shop_list=[] #定義購物車列表 if salary.isdigit(): salary=int(salary) while True: for i,j in enumerate(goods_list,1): print (i,">>>>>>>>>>>",j) choice=input("選擇需要購買商品的編號[退出: q]:") if choice.isdigit(): choice=int(choice) if choice>0 and choice<=len(goods_list): p_item=goods_list[choice-1] if p_item[1]<salary: salary-=p_item[1] shop_list.append(p_item) else: print("余額不足,還剩%s"%salary) print(p_item) else : print("編碼不存在") elif choice=="q": print("--------已購買如下-----------商品") for i in shop_list: print(i) print("您還有余額為:%s"%salary) break else: print("invalid input")
