python基础知识--5条件判断及循环if/if-else/range/for/while

1.if执行语句
#%%

if condition:
    expression
other_expression


#%%

# 当布尔类型为 0 None 空值的时候,返回False,其他情况返回True

#%%

a = 0

#%%

if a:
print('条件成立')
print('if执行完成')

#%%



2.if-else执行语句
#%%

if condition:
    true_expressions
else:
    false_expressions
other_expressoin


#%%


a = 1

#%%

b = 2

#%%



#%%

if a == b:
print('a 与 b 的值是一样的')
else:
print('a 与 b 的值不一样')

print('if-else 执行结束')

#%%



#%%

if a > b:
print('a 的值大于 b的值')
else:
print('a 的值小于 b的值')

print('if-else 执行结束')

#%%


a = 1

#%%

b = 2

#%%

if a == b:
print('a 等于 b')
print('if执行完毕')



if a<b:
print('a 小于b')
print('if执行结束')

#%%

3.if-else的不足之处

# input() 函数会接受输入数据,返回为 string 类型。


num = input('请输入数字:')

num


type(num)


num = int(num)


num


type(num)


weather= 1 春天

weather= 2 夏天

weather= 3 秋天

weather= 4 冬天


weather = input('请输入数字')


weather = int(weather)



if weather == 1:
print('春天')
else:
if weather == 2:
print('夏天')
else:
if weather == 3:
print('秋天')
else:
if weather == 4:
print('冬天')
else:
print('请输入1-4的正整数')



# 1.嵌套太多,一不留神,容易出bug
# 2.代码看起来很乱,不符合Python优雅风格


4.完善 if-else 的不足之处
#%%

if weather == 1:
print('春天')
else:
if weather == 2:
print('夏天')
else:
if weather == 3:
print('秋天')
else:
if weather == 4:
print('冬天')
else:
print('您输入的数字有误')

#%%

# 1. 嵌套太多
# 2. 代码看起来很乱,不符合Python优雅的特点



if - elif - else

#%%

if condition1:
expression1

elif condition2:
expression2

elif condition3:
expression3

else:
expression

#%%

# elif 必须在if语句后面
# 可书写多个elif进行条件判断
# 如果有一个条件成立,执行相应代码后,跳过整个if语句
# 如果条件都不成立,则执行else语句内容,退出整个if语句
# else 需要和if语句搭配使用,提高逻辑严谨性


weather= 1 春天

weather= 2 夏天

weather= 3 秋天

weather= 4 冬天

#%%

# if - elif - else

#%%

weather = input('请输入数字:')

#%%

weather = int(weather)

#%%



#%%

if weather == 1:
print('春天')
elif weather == 2:
print('夏天')
elif weather == 3:
print('秋天')
elif weather == 4:
print('冬天')
else:
print('请输入1-4正整数')



if weather == 1:
print('春天')
else:
if weather == 2:
print('夏天')
else:
if weather == 3:
print('秋天')
else:
if weather == 4:
print('冬天')
else:
print('您输入的数字有误')


# 使用 if - elif - else 优点:
# 1.代码了更少;
# 2.逻辑清晰,少了很多层嵌套。


5.FOR循环
#%% md

# Python循环: for 循环 / while 循环


for循环,循环/遍历一个可迭代对象,主要是用来遍历 序列(列表、字符串、元祖)、字典和集合

#%%

for 循环变量 in 可迭代对象:
<语句1>
else:
<语句2>

#%%

# 循环变量,代表着当前可迭代对象里的某个元素。
# 这里的循环变量,大家可以自定义,建议取一些方便记忆的名称
# else 也可与 for循环进行搭配,当for循环遍历完成后,执行else里面的内容。


words = 'I am lemon'

for word in words:
print(word,end = '')

运行结果:
I am lemon


fruits_list = ['banana', 'mango','lemon']

#%%

for i in fruits_list:
print(i)

运行结果:
banana
mango
lemon
#

fruits_dict = {'banana':5,'mango':1,'lemon':7}

for key in fruits_dict.keys():
print(key)
运行结果:
banana
mango
lemon

for value in fruits_dict.values():
print(value)
运行结果:
5
1
7

#%%

for key,value in fruits_dict.items():
print(key,value)
运行结果:
banana 5
mango 1
lemon 7



complex_list = [['banana', 'mango','lemon'],[1,2,3]]

#%%

for x in complex_list:
for y in x:
print(y)
运行结果:

banana mango lemon 1 2 3
#%%


6.RANGE()函数用法

# 需求:将 0-9 循环遍历


num = [0,1,2,3,4,5,6,7,8,9]

for i in num:
print(i)
运行结果:
0
1
2
3
4
5
6
7
8
9

#%%

# 需求:将 0-100 循环遍历

#%%


range(start,end[, step])



# start: 计数从 start 开始。默认是从 0 开始。例如range(10)等价于range(0,10);
# end: 计数到 end 结束,但不包括 end。例如:range(0,10) 是[0,1,2,3,4,5,6,7,8,9] 没有10
# step:步长,默认为1。例如:range(0,10) 等价于 range(0,10,1)

#%%

# 根据我们设定的规则,生成一个序列。生成的序列,可供for循环遍历

#%%

# 需要注意的是,range里面只能是整型类型

#%%

for i in range(10.0):
print(i)
运行结果:

TypeError Traceback (most recent call last)

<ipython-input-21-5ded3258cf00> in <module>() ----> 1 for i in range(10.0): 2 print(i)

TypeError: 'float' object cannot be interpreted as an integer



for 循环变量名 in range(start,end[, step]):
<执行语句1>
else:
<执行语句2>



for i in range(0,11,1):
print(i)
else:
print('执行完毕')
运行结果:
0
1
2
3
4
5
6
7
8
9
10
执行完毕

# 需求:将 0-100 循环遍历
for i in range(0,101,1):
print(i)
else:
print('执行完毕')

#%%

# 需求2:在0-9之间,0 2 4 6 8

#%%

for i in range(0,10,2):
print(i,end= ' ')
运行结果:
0 2 4 6 8 
7.For循环与continue、break语句的使用(一)

# 在循环中,根据需求情况,如果需要跳出循环,可以使用 continue break 关键字

# continue 是结束本次循环,进入下一次循环遍历
# break 是结束当前循环,不再执行当前循环所有代码


fruits = ['apple','lemon','pear','lemon','banana','grape','banana','apple']

# 需求1: 不打印lemon
# 需求2: 遇到grape,停止遍历

for i in fruits:
if i == 'lemon':
continue
if i == 'grape':
break
print(i,end= ' ')
运行结果:
apple pear banana 
#%%
8.For循环与continue、break语句的使用(二)

# continue 是结束本次循环,进入下一次循环遍历
# break 是结束当前循环,不再执行当前循环下所有代码

a = [['A', 'B', 'C'], [1,2,3]]

# 需求1: 不打印B


for x in a:
for y in x:
print(y,end= ' ')
运行结果:

A B C 1 2 3
#
for x in a:
for y in x:
if y == 'B':
continue
print(y,end= ' ')
运行结果:
A C 1 2 3 

# 疑问?遍历B的时候,使用break关键字跳出程序,会打印 123吗?

#%%

for x in a:
print(x)
运行结果:
['A', 'B', 'C']
[1, 2, 3]

#%%

for x in a:
if 'B' in x:
break
for y in x:
if y == 'B':
break
print(y,end= ' ')

#%%


9.FOR语句与内置迭代函数

Python 内置了4种常用迭代函数,有:

1. enumerate(seq) # 编号迭代
2. sorted(seq) # 排序迭代
3. reversed(seq) # 翻转迭代
4. zip(seq1,seq2....) # 并行迭代

seq 为 可遍历/可迭代的对象,如列表、字符串、元组

# enumerate()

# 编号迭代,在迭代的时候既返回序列中的编号(默认从0开始),又返回序列中的元素
# 需要两个循环变量,分别接收编号和元素的值

a = 'abcd'

for index,value in enumerate(a):
print(index,value)

运行结果:
0 a
1 b
2 c
3 d
# sorted()  排序迭代

# for循环变量的时候, 默认先遍历序列中较小的值,后遍历序列中较大的值。

# 可迭代对象中的元素,需要是可排序的同类数据


a = [1,5,4,2]

for i in sorted(a):
print(i)

运行结果:

1 2 4 5
#%%

for i in sorted(a,reverse= True):
print(i)
运行结果:
5
4
2
1

# reversed() 翻转迭代

# 将可迭代对象中的元素,从尾到头,进行遍历
# 不是大小排序

a = [1,5,4,2]
for i in reversed(a):
print(i)
运行结果:
2
4
5
1
# zip()

# 同时遍历可迭代对象中,同一序号元素。

# 如果元素长度不一致,只遍历到最短的序列长度

x = [1,2]

y = [3,4]

z = [5,6,7]

for a,b,c in zip(x,y,z):
print(a,b,c)
运行结果:

1 3 5 2 4 6


原文地址:https://www.cnblogs.com/tester007/p/13860987.html