python学习暂时笔记-20180131

一、逻辑运算——短路原则

and or

and 前面第一个结果为false,后面的不管计算结果都为false

or 前面第一个结果为True,后面的不管计算结果都为True

二、python中“\r”是回车,“\n”是换行,“\t”是制表符:代表在不使用表格的情况下在垂直方向按列对齐文本

1 print("hello world")
2 print("hello world")
3 print("hello world")

此代码输出结果如下:

1 hello world
2 hello world
3 hello world

如何将输出结果变为一行,代码如下:

1 print("hello world",end=" ")
2 print("hello world",end=" ")
3 print("hello world",end=" ")

此代码输出结果如下:

1 hello world hello world hello world

 三、运用while输出99乘法表

具体代码方法1如下:

 1 a = 0
 2 b = 0
 3 while a < 9:
 4     a += 1
 5     while b < 9:
 6         b += 1
 7         c = a * b
 8         #\t为制表符,end=让输出在一行上
 9         print(a,"x",b,"=",c,"\t",end="")
10         if a == b:
11             #默认会到下一行,等价于print()或者print(end="\n")
12             print("")
13             b = 0
14             break

 

输出结果如下:

1 1 x 1 = 1     
2 2 x 1 = 2     2 x 2 = 4     
3 3 x 1 = 3     3 x 2 = 6     3 x 3 = 9     
4 4 x 1 = 4     4 x 2 = 8     4 x 3 = 12     4 x 4 = 16     
5 5 x 1 = 5     5 x 2 = 10     5 x 3 = 15     5 x 4 = 20     5 x 5 = 25     
6 6 x 1 = 6     6 x 2 = 12     6 x 3 = 18     6 x 4 = 24     6 x 5 = 30     6 x 6 = 36     
7 7 x 1 = 7     7 x 2 = 14     7 x 3 = 21     7 x 4 = 28     7 x 5 = 35     7 x 6 = 42     7 x 7 = 49     
8 8 x 1 = 8     8 x 2 = 16     8 x 3 = 24     8 x 4 = 32     8 x 5 = 40     8 x 6 = 48     8 x 7 = 56     8 x 8 = 64     
9 9 x 1 = 9     9 x 2 = 18     9 x 3 = 27     9 x 4 = 36     9 x 5 = 45     9 x 6 = 54     9 x 7 = 63     9 x 8 = 72     9 x 9 = 81

 方法二代码如下:

1 a = 1
2 while a <=9:
3     b = 1
4     while b <=a:
5         print(str(b)+"*"+str(a)+"="+str(b*a),'\t',end="")
6         b +=1
7     print()
8     a +=1

输出结果为:

1 1*1=1     
2 1*2=2     2*2=4     
3 1*3=3     2*3=6     3*3=9     
4 1*4=4     2*4=8     3*4=12     4*4=16     
5 1*5=5     2*5=10     3*5=15     4*5=20     5*5=25     
6 1*6=6     2*6=12     3*6=18     4*6=24     5*6=30     6*6=36     
7 1*7=7     2*7=14     3*7=21     4*7=28     5*7=35     6*7=42     7*7=49     
8 1*8=8     2*8=16     3*8=24     4*8=32     5*8=40     6*8=48     7*8=56     8*8=64     
9 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-01-31 21:46  加州牛肉面  阅读(112)  评论(0编辑  收藏  举报

导航