Python基础2

12.运算符

   1)算数运算符

      + ,- ,* ,/除(float型),//取整,%取余,**幂(几次方,优先级高),(括号),

      5/2 结果2.5, 5//2结果为2

   2)赋值运算符

      =  -=  +=  /=  *=   //=  %=   **=

      /=(m/=2等同于m=m/2)  m//=2等同于m=m//2    m**=2等同于m=m**2(m的平方)

   3)比较运算符  

      !=  >=  <=  >  <  == ,true(正确)  false(错误)是输出的结果

      a<b<c 可直接这样表示

   4)逻辑运算符

      and  or  not  (无优先级,从左往右计算,遵循短路原则)

13.短路原则

     条件1 or 条件2:or只要有一个为真,则结果为真,

                      如果前面的条件1为真,则结果为真,后面的条件就省略不看。

     条件1 and 条件2:and只要有一个为假,则结果为假,

                      如果前面的条件1位假,则结果为假,后面的条件就省略不看。

1 not not True and True or False and False
执行结果:
True
过程分析:not not 抵消,true and true 是true,true or 条件,or短路原则,输出true

 

14.if多分支实现分数划分等级

 

 1 score=int(input("your engish score is:"))
 2 if score > 90:
 3     print("A")
 4 elif score > 80: #elif后输出只会输出一个,遇到符合条件的就会输出,并退出判断
 5     print("B")
 6 elif score > 70:
 7     print("C")
 8 elif score >= 60:
 9     print("D")
10 else:
11     print("革命尚未成功,同志还需努力")

 

15. if实现输出最大最小值

 

 1 num1=int(input("num1为:"))
 2 num2=int(input("num2为:"))
 3 num3=int(input("num3为:"))
 4 max_num=0
 5 min_num=0
 6 if num1 > num2:
 7     max_num=num1
 8     min_num=num2
 9     if max_num > num3:
10         print ("最大值为:",max_num)
11         
12     else:
13         print("最大值为:",num3)
14     if min_num>num3:
15         print("最小值为:",num3)
16     else:
17         print("最小值为:",min_num)
18 else:
19     max_num=num2
20     if max_num > num3:
21         print ("最大值为:",max_num)
22     else:
23         print("最大值为:",num3)
24     if min_num>num3:
25         print("最小值为:",num3)
26     else:
27         print("最小值为:",min_num)

 

16.while实现猜年龄

方法一:

1 age=50
2 user_input_age=int(input("plase input age:"))
3 while user_input_age!=age:
4     if user_input_age > age:
5         print("请输入一个小一点的值")
6     else:
7         print("请输入一个大一点的值")
8     user_input_age=int(input("plase input age:"))
9 print("恭喜你,猜对了")

方法二:

 1 flag=True
 2 while flag:
 3     user_input_age=int(input("plase input age:"))
 4     if user_input_age>age:
 5         print("请输入一个小一点的值 is bigger")
 6         flag =True
 7     elif user_input_age<age:
 8         print("请输入一个大一点的值 is smaller")
 9         flag =True
10     else:
11         print("you are successful")
12         flag = False

17.break 是循环满足某一条件时直接结束循环

   continue是退出当前循环,执行下一次循环

18.while输出直角三角形

方法一:

1 line=1
2 while line<=5:
3     mul=1
4     while mul<=line:
5         print("*",end="")
6         mul+=1
7     print()
8     line+=1
执行结果:
*
**
***
****
*****

方法二:

1 line2=5
2 while line2:
3     mul1=line2
4     while mul1:
5         print("#",end="")
6         mul1-=1
7     print()
8     line2-=1
执行结果:
#####
####
###
##
#

19.while 输出九九乘法口诀表

方法一:倒乘法口诀

1 factor1=9
2 while factor1>0:
3     factor2=factor1
4     while factor2>0:
5         print(str(factor1)+"*"+str(factor2)+"=",factor1*factor2,end=" ")
6         factor2-=1
7     print()
8     factor1-=1
执行结果:
9*9= 81 9*8= 72 9*7= 63 9*6= 54 9*5= 45 9*4= 36 9*3= 27 9*2= 18 9*1= 9
8*8= 64 8*7= 56 8*6= 48 8*5= 40 8*4= 32 8*3= 24 8*2= 16 8*1= 8
7*7= 49 7*6= 42 7*5= 35 7*4= 28 7*3= 21 7*2= 14 7*1= 7
6*6= 36 6*5= 30 6*4= 24 6*3= 18 6*2= 12 6*1= 6
5*5= 25 5*4= 20 5*3= 15 5*2= 10 5*1= 5
4*4= 16 4*3= 12 4*2= 8 4*1= 4
3*3= 9 3*2= 6 3*1= 3
2*2= 4 2*1= 2
1*1= 1

方法二:正乘法口诀,\t制表符(每一列都对齐),print()里面如有+连接,+左右数据类型必须一致。

1 factor1=1
2 while factor1<=9:
3     factor2=1
4     while factor2<=factor1:
5         print(str(factor2)+"*"+str(factor1)+"=",factor1*factor2,end="\t")  #\t制表符,
6         factor2+=1
7     print()
8     factor1+=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

 

 

 

 

 

 

 

 

   

posted on 2018-09-19 18:53  火龙果2425  阅读(106)  评论(0)    收藏  举报

导航