Python if 语句小结

4.4Python课程总结

导入方式:

第一种:import 模块名称

第二种:from 模块名称 import 导入的对象

​ or

​ from 模块名称 import * 导入模块内的所有对象

if语句

单分支

if 条件表达式:

​ 当条件满足时执行

双分支

if 条件表达式:

​ 当条件满足时执行的内容

else:

​ 当条件不满足时执行的内容

多分支

if 条件表达式:

​ 条件满足时执行的内容

elif 条件表达式:

​ 在不满足上一句的基础上条件满足时执行的内容

else:

​ 当以上条件都不满足时执行的内容

上课练习:

遍历

from 省市县数据 import pros
for i in pros:
    c=i.get('city')
    print(i.get('name'))
    for s in c:
        print("           ",s.get('name'))
        e=s.get('districtAndCounty')
        for l in e:
            print("                         "+l)
            

if语句

判断是否为奇数
e = int(input("请输入一个数"))
if(e%2==1):
    print(e,"是奇数")
else:
    print(e,"是偶数")

#判断月份是哪个季度
yf=int(input("请输入月份"))
if(yf<=3and yf>0):
    print(yf,"月是第一季")
elif(yf<=6):
    print(yf,"月是第二季")
elif(yf<=9):
    print(yf,"月是第三季")
elif(yf<=12):
    print(yf,"月是第四季")
else:
    print("只有12个月")


#判断当月有多少天
ln=int(input("请输入月份"))
if ln<=12 and ln>0:
    if ln < 8 and ln != 2:
        if ln % 2 == 1:
            c = 31;
        else:
            c = 30;
    else:
        if ln % 2 == 0:
            c = 31

        else:
            c = 30
    if ln == 2:
        c = 28
    print(ln, "月有", c, "天")
else:print("您输入的月份不规范!")
#判断闰年
nf=int(input("请输入年份"))
if nf%4==0 or nf%100!=0 and nf%400==0:
    print(nf,"是闰年")
else:
    print(nf,"不是闰年")
判断月份有多少天完整版

nf=int(input("请先输入年份:"))
if nf%4==0 and nf%100!=0 or nf%400==0:
    ln = int(input("请输入月份"))
    if ln <= 12 and ln > 0:
        if ln < 8 and ln != 2:
            if ln % 2 == 1:
                c = 31;
            else:
                c = 30;
        else:
            if ln % 2 == 0:
                c = 31

            else:
                c = 30
        if ln == 2:
            c = 29
        print(ln, "月有", c, "天")
    else:
        print("您输入的月份不规范!")
else:
    ln = int(input("请输入月份"))
    if ln <= 12 and ln > 0:
        if ln < 8 and ln != 2:
            if ln % 2 == 1:
                c = 31;
            else:
                c = 30;
        else:
            if ln % 2 == 0:
                c = 31

            else:
                c = 30
        if ln == 2:
            c = 28
        print(ln, "月有", c, "天")
    else:
        print("您输入的月份不规范!")

统计出现次数
names=["大大龙","大大龙","范家的","龙范子","范家的"]
set01 = set(names)
dict01 = {}
for item in set01:
    dict01.update({item:names.count(item)})
print(dict01)

求1-100的和
a=0
for i in range(0,101,1):
    a+=i
print(a)

打印**:
for i in range(0,3):
    for y in range(0,5):
        print("*",end=' ')
    print(" ")

for i in range(1,10):
    for j in range(1,i+1):
        print(j,"*",i,"=",(i*j),"    ",end='')
    print(" ")

猜数字:
c=0
print("猜1-10000之间的数字")
sz=int(input("猜数字"))
import random
a = random.randint(0,10000)
while sz!=a:
    if sz > a:
        print("数字大了")
    elif sz < a:
        print("数字小了")
    sz = int(input("猜数字"))
    c+=1
print("恭喜你",c,"次就猜对了!")

打星星
for i in range(0,3):
    for y in range(0,i+1):
        print("*",end=' ')
    print(" ")

求阶乘
p=1
jc=int(input("求阶乘"))
if jc!=0:
    for i in range(1, jc+1):
        p *= i
    print(jc, "的阶乘是", p)
else:print(jc,"的阶乘是",p)

你可能感兴趣的:(Python if 语句小结)