条件语句
1.顺序结构
按照python的语句的顺序依次输出
s='hello,python!'
print(s)
hello,python!
2.if条件语句
if条件语句的结构和函数定义相同;一个语句后面跟着一个缩进的语句体,语句数目没有要求,保证有一条语句,当需求存在没有语句的时候,在这种情况下可以使用return0,
x=6
if x% 2 == 0:
print('x为偶数')
else:
print('x为基数')
x为偶数
def LeapYear(year):
if year % 4 == 0 and year % 100!= 0:
print("是润年")
elif year % 400 == 0:
print("大闰年")
else:
print("平年")
LeapYear(2004)
LeapYear(1111)
LeapYear(8888)
是润年
平年
是润年
条件嵌套
eg:判断a和b的大小
a=5
b=7
if a<b:
print('a小于b')
else:
if a>b:
print('a大于b')
else:
print('a等于b')
a小于b
循环语句
while循环
a=0
while a < 10:
print(a,end='')
a=a+1
0123456789
eg:
x=1
while x==1:
y=input("输入你内心的数")
print("你输入的数字是:",y)
输入你内心的数1314
你输入的数字是: 1314
eg:
count=4
while count <6:
print(count,"小于6")
count =count+1
else:
print(count,"大于等于6")
4 小于6
5 小于6
6 大于等于6
for循环
在for循环中,循环的条件是循环条件中是否存在元素,存在时,判断为真,当当中没有元素时,会跳出循环
eg:
for i in range(10):
if i % 2 ==1:
print('奇数',i)
奇数 1
奇数 3
奇数 5
奇数 7
奇数 9
嵌套循环
1.while循环嵌套while循环
while判断条件1:
while判断条件2:
python循环2
python循环1
2.for循环嵌套for循环
for 变量1 in 序列 1:
for 变量2 in 序列2:
python语句2
python语句1
3 while循环嵌套for循环
while判断条件:
for 变量 in 序列 :
python语句2
python语句1
4 for循环嵌套while循环
for 变量 in 序列 :
while判断条件:
python语句2
python语句1
for i in range(1,10):
for j in range(i,i+1):
print("%d*%d=%2d"%(j,i,j*i),end='')
print("")