python入门第二天

逻辑运算符中有一个短路原则

continue and break的区别

1 num=0
2 while num<=10:
3     num+=1
4     if num==3:
5         break
6     print(num)

输出结果

1
2
1 num=0
2 while num<=10:
3     num+=1
4     if num==3:
5         continue
6     print(num)

输出结果

1
2
4
5
6
7
8
9
10
11

print输入不换行

1 print("你好,世界!")
2 print("你好,世界!")
3 print("你好,世界!")

输出结果:

================= RESTART: F:/python从入门到放弃/5.31/continue.py =================
你好,世界!
你好,世界!
你好,世界!
>>> 

print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end=""

1 print("你好,世界!",end="")
2 print("你好,世界!",end="")
3 print("你好,世界!",end="")
1 print("你好,世界!",end="__")
2 print("你好,世界!",end="__")
3 print("你好,世界!",end="__")
1 print("你好,世界!",end="##")
2 print("你好,世界!",end="##")
3 print("你好,世界!",end="##")
1 print("你好,世界!",end=" ")
2 print("你好,世界!",end=" ")
3 print("你好,世界!",end=" ")

输出结果:

================= RESTART: F:/python从入门到放弃/5.31/continue.py =================
你好,世界!你好,世界!你好,世界!
>>> 
================= RESTART: F:/python从入门到放弃/5.31/continue.py =================
你好,世界!__你好,世界!__你好,世界!__
>>> 
================= RESTART: F:/python从入门到放弃/5.31/continue.py =================
你好,世界!##你好,世界!##你好,世界!##
>>> 
================= RESTART: F:/python从入门到放弃/5.31/continue.py =================
你好,世界! 你好,世界! 你好,世界! 
>>> 

同一行显示多条语句

Python可以在同一行中使用多条语句,语句之间使用分号(;)分割,以下是一个简单的实例:

1 #!/usr/bin/python3
2 
3 import sys; x = 'runoob'; sys.stdout.write(x + '\n')

执行以上代码,输出结果为:

runoob

 例:

 1 #!/usr/bin/python3
 2 
 3 height=int(input("请输入高度值:"));width=int(input("请输入宽度值:"));num_height=1;
 4 while num_height<=height:  
 5     num_width=1
 6     while width>=num_width:
 7         print(num_width,end='')
 8         num_width+=1
 9 
10     num_height+=1
11     print()#换行

执行以上代码,输出结果为:

================= RESTART: F:/python从入门到放弃/5.31/continue.py =================
请输入高度值:5
请输入宽度值:6
123456
123456
123456
123456
123456
>>> 

猜你喜欢

转载自www.cnblogs.com/Mengchangxin/p/9114856.html
今日推荐