当前位置: 移动技术网 > IT编程>脚本编程>Python > python04: while循环语句 break continue for in 循环

python04: while循环语句 break continue for in 循环

2018年08月29日  | 移动技术网IT编程  | 我要评论

结婚证书图片,非凡电子书论坛,桓侯再生txt

print(“hello world”)
变量 : 存储信息的,日后被调用、修改操作
常量: 固定不变的量,字母大写
命名规则:

  1. 字母数字下划线组成
  2. 不能以数字开头,不能含有特殊字符和空格
  3. 不能以保留字命名
  4. 不能以中文命名
  5. 定义的变量名应该有意义
  6. 驼峰式命、 下划线分割单词
  7. 变量名区分大小写
    a=1
    b=2
    if a<b:
    print("yes")
    print("yes")
    print("yes")
    print("yes")
    else:
    print("no")
    a=1
    b=2

if a>b:
print("yes")

elif a==b:
print("第三")

else:
print("any")

if 条件1:
自拍
elif 条件2:

else:
跳舞

单行注释

'''多行注释'''
""" 多行注释 """

input()

"abc" + "qwe"

file.py
文件的扩展名:
.py : python的程序文件
.txt : 文本文件
pdf chm html doc xml xls ppt

jpg png gif jpeg bmp
avi rmvb mp4 mkv 3gp
wmv mp3 flue mid

true 真 正确的
false 假 错误的

a
b = 100
c = 1000

if b <= a <= c:
print("true")

num number

num1 = intpu("num1:")
num2 = intpu("num2:")
num3 = intpu("num3:")

输出三个数字中的最大值/最小值

if num1>num2>num3:

num1最大

else:

num1

elif num1>num3>num2 #num1最大
elif num2>num1>num3 #num2最大
elif num2>num3>num1 #num2最大
elif num3>num2>num1 #num3最大
else

num3>num1>num2 #num3最大

num1 num2 num3

max_num =0

if num1>num2:
max_num= num1
if max_num > num3:
print("max num is",max_num)
else:
print("max num is",num3)
else:
max_num = num2
if max_num > num3:
print("max num is",max_num)
else:
print("max num is",num3)

num += 1 等价于 num = num + 1
num -= 1 等价于 num = num - 1
num = 2 等价于 num = num 2
num /= 2 等价于 num = num / 2
num //= 2 等价于 num = num // 2
num %= 2 等价于 num = num % 2
num = 2 等价于 num = num 2

and 且,并且
只有两个条件全部为true(正确)的时候, 结果才会为true(正确)

条件1 and 条件2
5>3 and 6<2 true

or 或,或者
只要有一个条件为true,则结果为ture,
5>3 or 6<2
真 或 假

not 不,雅蠛蝶

not 5>3 == false
not 5<3 == true

a>b and (c>d or (not f))

(not (not true)) or (false and (not true))

条件1 and 条件2
条件1 or 条件2
短路原则
对于and 如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式 的计算结果就一定为假,第二个条件就不会被计算

对于or
如果前面的第一个条件为真,那么这个or前后两个条件组成的表达式 的计算结果就一定为真,第二个条件就不会被计算

true or true and false

猜年龄

age = 50

user_input_age = int(input("age is :"))

if ....

while 循环

while 条件:
print("any")
print("any")

num = 1

while num<10: # 2
print(num) # 2
num+=1 # 3
if num == 9: # 3
break

num = 1

while num<=100: # num<=100 等价于 true

while num<=100: 等价于 while true:

if num%2 == 0:
print(num)
num += 1

num = 1

while num<=100:
if num%2 == 1:
print(num)
num += 1

age = 50

user_input_age = int(input("age is :"))

flag = true

while flag:
user_input_age = int(input("age is :"))
if user_input_age == age:
print("yes")
flag =false
elif user_input_age > age:
print("is bigger")
else:
print("is smaller")

print("end")

break # 终止
age = 50

user_input_age = int(input("age is :"))

flag = true

break

while true:
user_input_age = int(input("age is :"))
if user_input_age == age:
print("yes")
break
elif user_input_age > age:
print("is bigger")
else:
print("is smaller")

print("end")

continue 继续

if a>b and d<f or 5>3 and d == e:
......

while 条件:
....
else:
....

statement 语句

num = 1
while num <= 10:
num += 1
if num == 5:
break
print(num)
else:
print("this is else statement")

while 条件1:
.....
while 条件2:
....

11=1
1
2=2 22=4
1
3=3 23=6 33=9
....

print("hello world.",end="__") # \n \r\n \r

print("hello world.",end="__")

print("hello world.",end="__")

num1 = 0

while num1<=5:
print(num1,end="_")
num2 = 0
while num2<=7:
print(num2,end="-")
num2+=1

num1+=1
print() # print(end="\n")

0_0-1-2-3-4-5-6-7-

1_0-1-2-3-4-5-6-7-

height 高度
width 宽度

height = int(input("height:")) # 用户输入一个高度
width = int(input("width:")) # 用户输入一个宽度

num_height = 1
while num_height <=height:
num_width = 1
while num_width <= width:
print("#", end="")
num_width += 1
print()
num_height += 1

12345678
22345678
32345678
42345678

第一行的时候 8字符 8次循环
第二行的时候 8字符 8次循环
第三行的时候 8字符 8次循环
第四行的时候 8字符 8次循环

width = int(input("width:"))

num_width = 1

while num_width<=width:
print("#", end="")
num_width +=1
print()

num_width = 1
while num_width<=width:
print("#", end="")
num_width +=1
print()

num_width = 1
while num_width<=width:
print("#", end="")
num_width +=1
print()

num_width = 1
while num_width<=width:
print("#", end="")
num_width +=1

print("#",end="")

print("#",end="")

print("#",end="")

print("#",end="")

print()

num = 4
while num>0:
print("#", end="")
num -= 1
print()

@ ==

height = int(input("height:")) # 用户输入一个高度
width = int(input("width:")) # 用户输入一个宽度

num2 = height

num2 = height # 第一步: 赋值
while num2 > 0: # 第二步 :num2 == 2

num1 = width # 第三步: 赋值
while num1>0: # 第四部:num1==2 # 第七步:num1 = 1
print("#", end="") # 第五步: 不换行 打印一个# 第八步: 不换行 打印一个#
num1 -= 1 #第六步: num1 = 1 第九步: num1 = 0
print() # 第十步 : 只是换行
num2 -= 1 # 第十一步 : num2=1

print("1*1=",1)

"11=",1 == str(m)+""+str(n)+"=",1

m = 2
n = 2

print( str(m)+ "" + str(n) + "=" , mn )

line = 5 # 第一步 : 赋值
while line>0: # 第二部 line=5

tmp = line # 第三部 : tmp =5 tmp=4

while tmp>0: # 第四部 : tmp =5 #第七步 tmp=4 #第十部: tmp=3 第十三步 tmp=2
print("*",end="") #第五步 #第八部 #第十一部 #第十四步
tmp = tmp-1 # 第六步 tmp = 4 # 第九步 tmp=3 # 第十二步 tmp=2 第十五步 tmp= 1

print()

print(line)

line -= 1

first = 1
while first<=9:

sec = 1
while sec <= first:
print( str(sec)+""+ str(first) +"="+str(sec first), end="\t")
sec += 1

print()

first += 1

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网