一、if判断

1、语法一:

if 条件:
    子代码块

示例代码:
sex = 'female'
age = 18
is_beautiful = True
if sex == 'female' and age >16 and age < 20 and is_beautiful:
    print("开始表白。。")

2、语法二:

if 条件:
    #条件成立时执行的子代码块
    子代码块
else:
    #条件不成立时执行的子代码块
    子代码块

示例代码:
sex = 'female'
age = 28
is_beautiful = True
if sex == 'female' and age > 16 and age < 20 and is_beautiful:
    print("开始表白。。")
else:
    print("阿姨好。。")

3、语法三:

if 条件1:
    子代码块
    if 条件2:
        子代码块

示例代码:
sex = 'female'
age = 18
is_beautiful = True
is_successful = True
if sex == 'female' and age > 16 and age < 20 and is_beautiful:
    print("开始表白。。")
    if is_successful:
        print("在一起")
    else:
        print("爱是折磨人的东西。。")
else:
    print("阿姨好。。")

4、完整的if语句

if 条件1:
    子代码块1
elif 条件2:
    子代码块2
elif 条件3:
    子代码块3
else:
    子代码块4

示例代码:
score = int(input("please input your score:"))
if score >= 90:
    print("优秀")
elif score >= 80:
    print("良好")
elif score >= 70:
    print("普通")
else:
    print("很差")

二、while循环

1、语法:

while 条件:
    循环体

2、结束循环方法一:

条件改为False,不会立即结束循环,而是在下一次循环判断条件时才生效。

示例:
tag = True
while tag:
    name = input("please input your name:")
    pwd = input("please input your password:")
    if name == 'shj' and pwd == '123':
        print("login successful")
        tag = False # 后面的代码依然会执行
    else:
        print("Invalid username and password.")
    print("end".center(20,'*'))

3、结束循环方法二:

break,一定要放在循环体内,一旦循环执行到break就会立即结束本层循环。

示例:
while True:
    name = input("please input your name:")
    pwd = input("please input your password:")
    if name == 'shj' and pwd == '123':
        print("login successful")
        break  # 后面的代码不会执行
    else:
        print("Invalid username or password.")
        continue   # 此处加continue无用
    print("end".center(20,'*'))

4、结束循环方法三:

continue,结束本次循环,直接进入下一次循环

示例:打印1,2,3,5
count = 1
while count < 6
    if count == 4:
        count += 1
        continue
    print(count)
    count += 1

5、while+else

在循环结束后,并且只有循环没有被break打断过的情况下,才会执行else的代码。

while True:
    print(1)
    break
else:   # else的代码不会执行
    print("else的代码")

tag=True
while tag:
    print(1)
    print(2)
    print(3)
    tag=False
else:  # else的代码会被执行
    print('else的代码')

6、while的嵌套循环

语法:
while 条件1:
    while 条件2:
        代码1
        代码2
        代码3

示例一:

while True:
    name=input('please input your name: ')
    pwd=input('please input your password: ')
    if name == 'shj' and pwd == '123':
        print("login successful..")
        while True:
            print("""
            0 退出
            1 取款
            2 转账
            3 查询
            """)
            choice = input("请输入您要执行的操作:")
            if choice == '1':
                print("取款")
            elif choice == '2':
                print("转账")
            elif choice == '3':
                print("查询")
            elif choice == '0':
                break
            else:
                print("您的输入有误,请重新输入。")
        break
    else:
        print('username or password error.')

示例二:
tag = True
while tag:
    name=input('please input your name: ')
    pwd=input('please input your password: ')
    if name == 'shj' and pwd == '123':
        print("login successful")
        while tag:
            print("""
            0 退出
            1 取款
            2 转账
            3 查询
            """)
            choice = input("请输入您要执行的操作:")
            if choice == '0':
                tag = False
            elif choice == '1':
                print("取款")
            elif choice == '2':
                print("转账")
            elif choice == '3':
                print("查询")
            else:
                print("您的输入有误,请重新输入")
    else:
        print("username or password error.")

三、for循环

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

循环遍历列表
l = ['a','b','c','d','e']

#用while循环完成,比较复杂
i = 0
while i < len(l):
    print(l[i])
    i += 1

# for循环简单
for x in l:
    print(x)

# 以字典为例,while实现困难,for循环简单
dic={'name':'egon','age':18,'gender':'male'}
for a in dic:
    print(a,dic[a])

# 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嵌套
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

'''

python基础——3(流程控制)的更多相关文章

  1. python基础之流程控制、数字和字符串处理

    流程控制 条件判断 if单分支:当一个“条件”成立时执行相应的操作. 语法结构: if 条件: command 流程图: 示例:如果3大于2,那么输出字符串"very good" ...

  2. python基础之流程控制(2)

    今天将是基础篇的最后一篇,咱们来补上最后一个内容,流程控制for循环 For 循环 一.为什么有for循环? for循环能做的事情,while循环全都可以实现,但是在某些情境下,for循环相对于whi ...

  3. python基础之流程控制

    流程控制之----if 流程控制,是指程序在运行时,个别的指令(或者是陈述.子程序)运行或者求值的顺序.人生道路上的岔口有很多,在每个路口都是一个选择,在每个路口加上一个标签,选择哪个就是满足哪个条件 ...

  4. python基础:流程控制案例:

    1,简述编译型与解释型的语言,且分别列出你知道的哪些语言属于编译型,哪些属于解释型. 答:简单理解编译型语言类似谷歌翻译,整篇读入整篇翻译,代表语言有C语言,解释型语言类似同   声传译,读入一行翻译 ...

  5. python基础之流程控制(1)

    一.分支结构:if 判断 1.什么要有if 判断语句? 让计算机可以像人一样根据条件进行判断,并根据判断结果执行相应的流程. 2.基本结构 单分支结构 # 单分支 if 条件1: 代码1 代码2 代码 ...

  6. python基础之流程控制(if判断和while、for循环)

    程序执行有三种方式:顺序执行.选择执行.循环执行 一.if条件判断 1.语句 (1)简单的 if 语句 (2)if-else 语句 (3)if-elif-else 结构 (4)使用多个 elif 代码 ...

  7. Python基础之流程控制for循环

    目录 1. 语法 2. for+break 3. for+continue 4. for循环嵌套 1. 语法 while循环可以对任何内容循环,但循环次数不可控 for循环基于容器类型的长度,循环次数 ...

  8. Python 基础知识----流程控制

    判断语句 循环语句 嵌套

  9. Python基础之流程控制if判断

    目录 1. 语法 1.1 if语句 1.2 if...else 1.3 if...elif...else 2. if的嵌套 3. if...else语句的练习 1. 语法 1.1 if语句 最简单的i ...

  10. Python基础之流程控制while循环

    目录 1. 语法 2. while+break 3. while+continue 4. while+else 1. 语法 最简单的while循环如下: ''' while <条件>: & ...

随机推荐

  1. first-child和last-child选择器 nth-child(n)第几个元素 nth-last-child(n)倒数第几个元素

    :first-child 和  :last-child 分别表示父元素中第一个 或者  最后一个 子元素设置样式,如上图

  2. Hdu 5459 Jesus Is Here (2015 ACM/ICPC Asia Regional Shenyang Online) 递推

    题目链接: Hdu 5459 Jesus Is Here 题目描述: s1 = 'c', s2 = 'ff', s3 = s1 + s2; 问sn里面所有的字符c的距离是多少? 解题思路: 直觉告诉我 ...

  3. 洛谷 P3327 [SDOI2015]约数个数和 || Number Challenge Codeforces - 235E

    https://www.luogu.org/problemnew/show/P3327 不会做. 去搜题解...为什么题解都用了一个奇怪的公式?太奇怪了啊... 公式是这样的: $d(xy)=\sum ...

  4. 自定义view(13)自定义属性

    1.添加attrs.xml文件 在android studio下,在res/values 下新建资源文件attrs.xml 2.添加自定义的属性 在attrs.xml中添加属性,如下.其中format ...

  5. 18.3.1获得Class对象

    package d18_3_1; /** * Java中的java.lang.Class,简单理解就是为每个java对象的类型标识的类, * 虚拟机使用运行时类型信息选择正确的执行方法,用来保存这些运 ...

  6. Xcode7.1环境下上架iOS App到AppStore 流程 转

    来自:http://www.cnblogs.com/ChinaKingKong/p/4957682.html 前言部分 之前App要上架遇到些问题到网上搜上架教程发现都是一些老的版本的教程 ,目前iT ...

  7. HAL之定时器

    一首先得对定时器的时钟与系统时钟的关系搞清楚,基本定时器的时钟来自APB1最大36MHZ.(定时器倍频值为1) 二 在STM32CubeMX中打开外设功能,时钟源选择内部:然后在配置定时器3中的参数设 ...

  8. Chrome下font-size小于12px的解决办法

    自从Chorme取消了-webkit-text-size-adjust,这个问题又变得令人烦恼起来. 好在我们可以利用-webkit-transform这个私有属性. .box{ -webkit-tr ...

  9. ES6初探——编译环境搭建

    不好意思我又要来写操作文档了,看起来更像wiki的博客(如果你想深入学习,请阅读文末列的参考资料).本文将示例如何把ES6编译成ES5. 首先,你要自行查阅什么是ES6,和ES5.javascript ...

  10. 位bit,字节byte,K,M,G(转)

      字节是由8个位所组成,可代表一个字符(A~Z).数字(0~9).或符号(,.?!%&+-*/),是内存储存数据的基本单位.1 byte = 8 bit 1 KB = 1024 bytes1 ...