Python基础入门(循环控制)

#if  可以嵌套   if elif else
a = 10
b = 20
if a > b:
    print("a > b")
elif b > a:
    print("b > a")
elif a == b:
    print("a==b")
else:
    print("what the fuck!")
    
name = "arron"
oh = "shit"
if name == "arron":
    print("my name is arron")
    if oh == "shit":
        print("oh shit")
else:
    print("no,my name is kobe")
    
#可以结合 or and 
if a == b or a==1:
    print("oh ye!")
elif a != b and a == 10 and b == 20:
    print("oh no")
b > a
my name is arron
oh shit
oh no
In [9]:

#while 
count = 0
while count < 3:
    count += 1
    print("oh ye")
name = input("please input name :")
while name != "quit":
    name = input("please input name :")
    print("game over!")
oh ye
oh ye
oh ye
please input name :d
please input name :quit
game over!
In [39]:

#for
for i in range(5):
    print(i)
#嵌套
for i in range(1,3):
    for j in range(1,3):
        print(i,j)
l1 = [1,2,3]
for i in l1:
    print(i)
dic1 = {'Tom':1,'Kobe':2,'Arron':3}    
for i in dic1:
    print(i)
for i in dic1.values():
    print(i)
for i in dic1.keys():
    print(i)
0
1
2
3
4
1 1
1 2
2 1
2 2
1
2
3
Kobe
Tom
Arron
2
1
3
Kobe
Tom
Arron
In [37]:

#an = a1 + (n-1)*d
a1 = float(input("please input a num:"))
n = int(input("please input n:"))
d = float(input("please input d:"))
sum = 0
for i in range(n):
    an = a1+i*d
    sum = sum +an
print(sum)
please input a num:2
please input n:2
please input d:2
6.0
In [43]:

两个列表生成字典
#两个列表生成字典
l1 = ['a','b,','c']
l2 = [1,2,3]
dir1 = {}
for i in range(len(l1)):
    dir1[l1[i]] = l2[i]
print(dir1)
{'b,': 2, 'c': 3, 'a': 1}
In [46]:

for i,j in zip(l1,l2):
    dic[i] = j
"""
zip 语法:zip([iterable, ...])
参数说明:
iterabl -- 一个或多个迭代器;
返回值
返回元组列表。"""
dic = {}
for i,j in zip(l1,l2):
    dic[i] = j
print(dic)
{'b,': 2, 'c': 3, 'a': 1}
In [14]:

# 循环控制 break pass continue
#break 结束整个循环
a = 0
while a < 10:
    a += 1
    if(a == 5):
        break
    else:
        print(a)
#continue 每次5都没有打印出来,跳出了本次的循环
for i in range(2):
    for j in range(10):
        if j == 5:
            continue
        print(j)
#pass  占位符   保持代码完整性
1
2
3
4
0
1
2
3
4
6
7
8
9
0
1
2
3
4
6
7
8
9

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值