Python学习之路—————day04

今日内容:

1、 循环语句

1.1 if判断

1.2 while循环

1.3 for循环

一、if判断

语法一:

if 条件

代码块1

代码块2

代码块3

# 例:

sex='female'

age=18

height=1.70

weight=50

is_beautiful=True

if sex=='female' and age > 16 and age < 20 and is_beautiful:

print('开始表白。。')

'''

'''

法二:

if 条件:

代码块1

代码块2

代码块3

else:

代码块1

代码块2

代码块3

'''

'''

sex='female'

age=18

height=1.70

weight=50

is_beautiful=True

if sex=='female' and age > 16 and age < 20 and is_beautiful:

print('开始表白。。')

else:

print('阿姨好....')

print('other code1')

print('other code2')

print('other code3')

'''

'''

语法三:

if 条件1:

if 条件2

代码1

代码2

代码3

代码4

else:

'''

'''

sex = 'female'

age = 18

height = 1.85

weight = 100

is_beautiful = True

is_sucess=True

if sex == 'female' and age > 16 and age < 20 \

and is_beautiful and height < 1.80 and height>1.60:

print('开始表白。。')

if is_sucess:

print('在一起')

else:

print('byebye')

else:

print('阿姨好....')

print('other code1')

print('other code2')

print('other code3')

'''

'''

语法四:

if 条件1:

代码1

代码2

代码3

代码4

elif 条件2:

代码1

代码2

代码3

代码4

elif 条件3:

代码1

代码2

代码3

代码4

else:

代码1

示例:

如果:成绩>=90,那么:优秀

如果成绩>=80且<90,那么:良好

如果成绩>=70且<80,那么:普通

其他情况:很差

score = input('请输入分数》》》')

score = int(score)

if score > 90:

print('优秀')

elif score >=80 :

print('良好')

elif score >=70 :

print('普通')

else:

print('很差')

'''

二、while循环'''

语法:

while 条件:

代码1

代码2

代码3

'''

'''

结束循环的方式:

方式1:在条件改为FALSE时不会立即结束循环,

而是在下次条件判断是结束

tag = True

while tag:

name = input('please input your name')

pwd = input('please input your password')

if name == 'king' and pwd == '123':

print('login sucessful')

tag = False

else:

print('username or password err0')

print('>>>>>')# 循环结束后依然会执行

'''

'''

方式2:while+break

break 一定要放在循环体内部,一旦循环结束下面将不执行

'''

'''

while True:

name = input('please input your name')

pwd = input('please input your password')

if name == 'king' and pwd == '123':

print('login sucessful')

break

else:

print('username or password err0')

print('>>>>>')

print('>>>>>')

'''

'''

方式三:

while+continue:结束本次循环,直接进入下次循环

'''

'''

示例1:

count = 1

while count < 6:# count=6

if count == 4:

count +=1

continue

print(count)

count+=1

'''

# 示例2:

'''

while True:

name = input('please input your name')

pwd = input('please input your password')

if name == 'king' and pwd == '123':

print('login sucessful')

break

else:

print('username or password err0')

'''

'''

while + else

while 条件:

代码1

代码2

else:

在循环结束后,并且只有在while在没break打断的情况下才会执行

'''

'''

while True:

name = input('please input your name: ')

pwd = input('please input your password: ')

if name == 'egon' and pwd == '123':

print('login successful')

break

else:

print('username or password error')

print('===>>>>>')

print('===>>>>>')

'''

三、for循环

# for循环的强大之处在于循环取值

l=['a','b','c','d','e']

# i=0

# while i < len(l):

# print(l[i])

# i+=1

# for x in l: # x='b'

# print(x)

# dic={'name':'egon','age':18,'gender':'male'}

# for x in dic:

# print(x,dic[x])

#for + break

# nums=[11,22,33,44,55]

# for x in nums:

# if x == 44:

# break

# print(x)

#for + continue

# nums=[11,22,33,44,55]

# for x in nums:

# if x == 22 or x == 44:

# continue

# print(x)

#for + else

# names=['egon','kevin1111_dsb','alex_dsb','mac_dsb']

#

# for name in names:

# if name == 'kevin_dsb':

# break

# print(name)

# else:

# print('======>')

#for+ range()

'''

# range的用法

>>> range(1,5)

[1, 2, 3, 4]

>>> for i in range(1,5):

... print(i)

...

1

2

3

4

>>> range(1,5,1)

[1, 2, 3, 4]

>>> range(1,5,2) # 1 3

[1, 3]

'''

# for i in range(5): # 0 1 2 3 4

# print(i)

#for嵌套

for i in range(3):

for j in range(4):

print(i,j)

for i in [0,1,2]: # i=1

for j in [0,1,2,3]: # j=1

print(i,j)

'''

0 0

0 1

0 2

0 3

1 0

1 1

1 2

1 3

2 0

2 1

2 2

2 3

'''

上一篇:21副GIF动图让你了解各种数学概念(转。太强大了)


下一篇:OpenShift中的持续交付