学习周记1

 

本周对Python进行了基础的学习:

1.程序结构:顺序结构、选择结构和循环结构。

a8ba6391f9494cec9e7095fa5a235294.pngb3e5d3b72a3c4e2eaaaccbe5328fa6e4.png6f3ee99dc8744246be5a96e1eb190364.png

 (1)if 语句: if 表达式:

                            语句块

例:

num = int(input('输入一个整数'))
if num%3==2 and num%5==3 and num%7==2:
    print(num)

(2)if...else 语句 :if 表达式:    

                                     语句块1

                                  else:      

                                     语句块2

例:

num =int(input('输入一个整数:'))
if num%4==0 and num%100!=0 or num%400==0:
    print("leap year")
else:
    print("common year")

 (3)if...elif...else 语句: if 表达式1:

                                                 语句块1

                                         elif 表达式2:      

                                                语句块2

                                         elif  表达式3:      

                                                语句块3 ...      

                                                语句块n

 例:

score = int(input('输入一个考试成绩:'))
if score>=0 and score<=59:
    print("E")
elif score>=60 and score<=69:
    print("D")
elif score>=70 and score<=79:
    print("C")
elif score>=80 and score<=89:
    print("B")
elif score>=90 and score<=100:
    print("A")

(4)if 语句的嵌套

if  表达式1:  

    if  表达式2:        

           语句块1      

      else:            

           语句块 2

例:

jiu = int(input('输入一个酒精含量:'))
if jiu<20:
    print("不构成酒驾")
else:
    if jiu>=20 and jiu<80:
        print("酒驾")
    else:
        print("醉驾")

例2:

word = str(input('输入一个单词:'))
if word == 'mo':
    print('monday')
elif word == 'tu':
    print('tuesday')
elif word == 'we':
    print('wednesday')
elif word == 'th':
    print('thurday')
elif word == 'fr':
    print('friday')
elif word == 'sa':
    print('saturday')
elif word == 'su':
    print('sunday')
else:
    print('error')

(5)while循环:通过一个条件来控制是否要继续反复执行循环体中的语句。

while  条件表达式:          

         循环体

(6)for循环:一个依次重复执行的循环。

for 迭代变量 in 对象:      

         循环体

迭代变量用于保存读取出的值 对象为要遍历或迭代的对象 循环体为一组被重复执行的语句

(7)range()函数 Python内置函数,用于生成一系列连续的整数,多用于for循环语句中。 range(start,end,step)

例:实现1到100的累加

a = 0
for i in range(1,101):
    a=a+i
print(a)
a=0
i=1
while i <101:
    a=a+i
    i+=1
print(a)

(8)for 循环嵌套

         for 迭代变量1 in 对象1:      

                 for 迭代变量2 in 对象2:              

                         循环体2        

                循环体1

例:

#打印九九乘法表
for i in range(1,10):
    for j in range(1,i+1):
        print(str(j)+"x"+str(i)+"="+str(i*j)+"\t",end='')
    print('')

(9)在while循环中套用for循环

while 条件表达式:        

        迭代变量 in 对象:            

                 循环体2        

        循环体1

(10)在for循环中套用while循环

for 迭代变量 in 对象:        

        while 条件表达式:              

                 循环体2        

        循环体 1

(11)break 语句

while 条件表达式1:    

         执行代码          

         if 条件表达式2:              

                break

条件表达式2用于判断何时调用break语句跳出循环。

(12)continue 语句 :终止本次循环而提前进入下一次循环中。

while  条件表达式1:        

         执行代码      

         if  条件表达式2:                

                continue

(13)pass语句表示空语句。 不作任何事情,一般起到占位作用。

作业一:

#逢7拍手
total = 99
# import random
# a = random.randint(0,10)
# print("从这开始", a )
for num in range(1,100):
    if num % 7 == 0 :
        print(num)
        continue
    else:
        str1 = str(num)
        if str1.endswith('7'):
            print(num)
            continue
    total-=1
print("total:" , total)

作业二:

# 地铁
per_num = int(input('per_num:'))
sta_num = int(input('sta_num:'))
if 0<sta_num<=5:
    money=per_num*3
    print(money)
elif 6<=sta_num<=9:
    money=per_num*8
    print(money)
elif sta_num>=10:
    money=per_num*9
    print(money)
elif sta_num<=0 or per_num<=0:
    print('error')

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值