通过练习题来学习条件语句
1,使用while循环输出1 2 3 4 5 6 8 9 10
n=1
while n<11:
if n=7:
pass
else:
print(n)
n=n+1
print('----end----')
2,求出1-100所有数的和
n=1
sum=0
while n<101:
sum=sum+n
n=n+1
print sum
3,输出1-100内的所有的奇数
n=1
while n<101:
if n %2==0:
pass
else:
print n
n=n+1
print('---end---')
4,输出1-100内的所有的偶数
n=1
while n<101:
if n %2==0:
print n
else:
pass
n=n+1
print('---end---')
5,求1-2+3-4+5....99的所有数的和
n=1
s=0
while n<100:
temp = n %2
if temp == 0:
s=s-n
else:
s=s+n
n=n+1
print s
6,变量名:
可以是字母数字下划线 PS:数字不能开头,不能是关键字,最好不要和Python内置的东西重复
7,条件语句:
a.
n1 = input('>>>')
if "alex" == "wy":
n2 = input('>>>')
if n2 == "确认":
print('wy SB')
else:
print('wy DB')
else:
print('error')
注意:
n1 = "wy" 赋值
n1 == 'wy' 比较,
b.
if 条件1:
pass
elif 条件2:
pass
elif 条件3:
pass
else:
pass
print('end')
c. 条件1
and or
if n1 == "sy" or n2 == "sy!23":
print('OK')
else:
print('OK')
PS:
pass 代指空代码,无意义,仅仅用于表示代码块
8,基本数据类型:
字符串 - n1 = "sy" n2 = 'root' n3 = """eric""" n4='''tony'''
数字 - age=21 weight = 64 fight = 5
加减乘除等:
字符串:
加法:
n1 = "sy"
n2 = "sb"
n4 = "db"
n3 = n1 + n2 + n4
# "sysbdb"
乘法:
n1 = "sy"
n3 = n1 * 10
数字:
n1 = 9
n2 = 2
n3 = n1 + n2
n3 = n1 - n2
n3 = n1 * n2
n3 = n1 / n2
n3 = n1 % n2
n3 = n1 ** n2
出题:
11 12 13 ...
num = 12
n = num % 2
if n == 0:
print('偶数')
else:
print('奇数')
9. 循环
死循环
while 1==1:
print('ok')