python流程控制-条件与循环-python3笔记

简介:

1.条件语句

2.循环语句

1.条件语句:

形式:

if 判断语句 :
    执行语句1
elif 判断语句2:
    执行语句2
elif 判断语句3:
    执行语句3
#...
else:
    执行语句4

占位符 pass

意义:

if(如果) A :

    就 B(当A为True)

elif(或者) C :

    就 D(当A为False并且C为True)

else(否则) :
    就E(当A和C都为False)

#if
    a,b=1,2
    if a > b:           #if 必须接判断语句
        print(a)
    elif a == b:        #elif 后面必接判断语句,可以有多个
        print('equal')
    else:               #不能接语句
        print(b)        #可用pass函数占位
    2
##必须满足判断条件才会执行相应的语句

#input(内置函数),用于获取输入,输出为字符串
    >>> input('Please enter:')
    Please enter:a
    'a'
    >>> input('Please enter:')
    Please enter:1
    '1'

#example
    a = input('Please enter your grade: ')
    if a.isdigit() :
        a = int(a)

        if a > 90 :
            print('A')
        elif a > 80 :
            print('B')
        elif a > 60 :
            print('C')
        else :
            print('difference')
    elif len(a) == 0 :
        print('Enter blank')
    else :
        print('enter is not a number!!!')

#random 随机函数
    >>> import random
    >>> a=random.randint(1,3)     #闭区间,随机生成一个整数
    >>> a
    3
    >>> a=random.randint(1,3)
    >>> a
    1
    >>> random.random()         #随机生成0-1的浮点数
    0.5976110450434942
    >>> random.randrange(5) #随机范围,默认从0开始,也可定义(1,5),可添加步长左闭右开
    1
    >>> random.randrange(1,5)
    2
    >>> li=[1,23,4,5]
    >>> random.sample(li,2)     #从序列中随机生成一个的指定的个数
    [1, 5]
    >>> random.sample(li,1)
    [5]
    >>> random.sample(li,3)
    [1, 5, 23]
    >>> random.choice(li)       #从序列中随机生成一个数
    4

2.循环语句

1.while循环

while 判断语句A:
    执行语句B
elseprint('程序正常结束,执行else')

注意:循环要有终止条件

a=1
while a < 5 :
    a += 1
    print(a)
a=1
while a < 5 :
    print(a)
    a += 1
a=1
while a < 11 :
    if a % 2 == 0 :
        print(a)
    a += 1

2.break和continue (函数)

while True:
    break   #终止循环
    continue  #跳过本次循环

#break 会终止循环,循环不再执行
#continue是跳过本次循环,循环继续

#break 终止当前循环,循环不在执行
    >>> a = 10
    >>> while a > 4 :
        a -= 1
        if a == 5 :
            break
        print(a)    
    9
    8
    7
    6
#continue 跳过本次循环,循环继续
    >>> a = 10
    >>> while a > 4 :
        a -= 1
        if a == 5 :
            continue
        print(a)
    9
    8
    7
    6
    4

3.range(函数)

range(10) #表示0 - 9 这个范围
range(1,10) #表示 1 - 9这个范围
range(1,10,2) #表示 1 - 9这个范围,并且以步长2进行取数

>>> range(10)
range(0, 10)
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10)
range(1, 10)
>>> list(range(1,10,2))
[1, 3, 5, 7, 9]
>>> list(range(1,10,3))
[1, 4, 7]

4.for循环

for item in iterable:
    执行语句
else:
  print('程序正常结束,执行else')

#循环条件可以是任何可迭代的对象,如:序列类型,集合和字典

while,for:
相同点:循环
不同点:while需要些终止条件

>>> for i in range(10):
    print(i)    
0
1
2
3
4
5
6
7
8
9
a=5
>>> for i in range(a):
    print(i)    
0
1
2
3
4

5.嵌套循环

>>> for i in range(1,3):
    print('***',i)
    for j in range(1,3):
        print('###',j)      
*** 1
### 1
### 2
*** 2
### 1
### 2
>>> for i in range(1,3):
    for j in range(1,3):
        print('###',j)
    print('***',i)  
### 1
### 2
*** 1
### 1
### 2
*** 2
>>> for i in range(1,3):
    for j in range(1,3):
        print('###',j,end='')
    print('***',i)
### 1### 2*** 1
### 1### 2*** 2

6.else

while True:
    break
else:
    print('OK')

#for   
for item in iterable:
    break
else:
    print('OK')

""" 
只有正常结束的循环,非break结束的循环才会执行else部分
"""
>>> for i in range(5):
    if i == 3:
        break
    else:
        print('000 %s' % i)     
000 0
000 1

000 2



本文转自 zhuxtqw 51CTO博客,原文链接:http://blog.51cto.com/1054054/2082670,如需转载请自行联系原作者

相关文章
|
17小时前
|
算法 数据挖掘 数据处理
使用 Python 循环创建多个列表
在Python中,动态创建多个列表对于数据处理和算法实现十分有用。本文介绍了四种方法:1) 列表推导式,如创建偶数和奇数列表;2) 使用循环和`append()`,示例为生成斐波那契数列;3) 结合字典与循环,按条件(如正负数)分组;4) 列表生成器,用于一次性生成多组随机数列表。这些方法有助于提高代码效率和可读性。
7 1
|
1天前
|
存储 设计模式 算法
|
1天前
|
存储 索引 Python
|
5天前
|
程序员 索引 Python
Python 流程控制
Python 流程控制
16 0
|
6天前
|
Python
Python流程控制
Python流程控制
|
7天前
|
Python
python-基本类型,运算,循环
python-基本类型,运算,循环
26 0
|
8天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
16天前
|
数据采集 机器学习/深度学习 人工智能
Python环境搭建—安装Python3解释器
Python环境搭建—安装Python3解释器
33 2
|
16天前
|
数据采集 JavaScript C++
Python搭建编程环境-安装Python3解释器
Python搭建编程环境-安装Python3解释器
24 1
|
17天前
|
存储 Python
python基础篇: python中的流程控制,你都了解吗?
python基础篇: python中的流程控制,你都了解吗?
21 3