python 条件分支与循环
一、if判断:
语法一:
if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3
示例:
sex='female'
age=18
is_beautiful=True if sex == 'female' and age > 16 and age < 20 and is_beautiful:
print('开始表白。。。') print('other code1...')
print('other code2...')
print('other code3...')
示例
语法二:
if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3
else:
# 条件不成立时执行的子代码块
代码1
代码2
代码3
sex='female'
age=38
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
sex='female'
age=18
is_beautiful=True
is_successful=True
height=1.70 if sex == 'female' and age > 16 and age < 20 and is_beautiful \
and height > 1.60 and height < 1.80:
print('开始表白。。。')
if is_successful:
print('在一起。。。')
else:
print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.')
else:
print('阿姨好。。。') print('other code1...')
print('other code2...')
print('other code3...')
示例
语法四:
if 条件1:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
elif 条件3:
代码1
代码2
代码3
.......
else:
代码1
代码2
代码3
示例:
如果成绩 >= 90,那么:优秀 如果成绩 >= 80且 < 90, 那么:良好 如果成绩 >= 70且 < 80, 那么:普通 其他情况:很差
''' score = input('please input your score: ') # score='100'
score = int(score) if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')
示例
二、while循环
语法:
while 条件:
代码1
代码2
代码3
while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '':
print('login successful')
else:
print('username or password error')
示例
结束while循环的两种方式 方式一:条件改为False,
在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效 tag=True
while tag:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '':
print('login successful')
tag=False
else:
print('username or password error') print('===>')
方式二:while+break
break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环 while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('username or password error') print('===>>>>>')
print('===>>>>>')
2.1、while+continue:结束本次循环,直接进入下一次循环
# 示例一
count=1
while count < 6: #count=6
if count == 4:
count += 1
continue print(count)
count+=1 # 示例二:
while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '':
print('login successful')
break
else:
print('username or password error')
# continue # 此处加continue无用
2.2、while else
while + else: while 条件:
代码1
代码2
代码3
else:
在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码 tag=True
while tag:
print(1)
print(2)
print(3)
# tag=False
break
else:
print('else的代码')
2.3、while嵌套
#语法
while 条件1:
while 条件2:
代码1
代码2
代码3 示例一:
while True:
name=input('please input your name: ')
pwd=input('please input your password: ') if name == 'egon' and pwd == '':
print('login successful')
while True:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '':
break
elif choice == '':
print('取款。。。')
elif choice == '':
print('转账。。。')
elif choice == '':
print('查询')
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 == 'egon' and pwd == '':
print('login successful')
while tag:
print("""
0 退出
1 取款
2 转账
3 查询
""")
choice=input('请输入您要执行的操作:') #choice='1'
if choice == '':
tag=False
elif choice == '':
print('取款。。。')
elif choice == '':
print('转账。。。')
elif choice == '':
print('查询')
else:
print('输入指令错误,请重新输入')
else:
print('username or password error')
示例二
三、for循环
1 迭代式循环:for,语法如下 for i in range(10): 缩进的代码块 2 break与continue(同上)
3 循环嵌套
for i in range(1,10):
for j in range(1,i+1):
print('%s*%s=%s' %(i,j,i*j),end=' ')
print()
九九乘法表
python 条件分支与循环的更多相关文章
- python3.4学习笔记(十) 常用操作符,条件分支和循环实例
python3.4学习笔记(十) 常用操作符,条件分支和循环实例 #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 print(d) #1.25 c //= 8 #用两个 ...
- Python - 条件控制、循环语句 - 第十二天
Python 条件控制.循环语句 end 关键字 关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下: Python 条件语句是通过一条或多条语句的执行结果(True 或 ...
- Python——条件语句及其循环
条件语句及其循环 一. 条件语句 在条件语句中可以使用以下所有的运算符: 算术运算符:+.-.*././/.%.** 关系运算符:>.<.==.<=.>=.!= 测试运算符:i ...
- C#(三)基础篇—方法,递归,条件分支,循环,三元操作符
C# 本随笔为个人复习巩固知识用,多从书上总结与理解得来,如有错误麻烦指正 2020-12-03 1.方法 static void Main(string[] args) { float Sum(fl ...
- Python(四) 分支、循环、条件与枚举
一.什么是表达式 表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列 二.表达式的优先级 三.表达式优先级练习 优先级同级 从左往右计算 1 or 2 a ...
- 初学python(print使用、条件分支、循环、模块引用)
import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...
- python条件判断与循环
条件判断 1.python缩进规则: 如果if语句判断是True,就把缩进的语句执行了,否则,什么也不做,比如: age=20 if age >= 18: print('your age is' ...
- Python条件控制与循环语句
1. 条件控制 # if-elif-else结构 age = 12 if age < 4: price = 0 elif age < 18: price = 5 else: price = ...
- Python条件判断和循环,range()函数
条件判断经常使用if语句进行判断,表达方式为:if 条件语句: :elif:else if...用于执行第一条不满足if的判断,继续执行其它的判断.比如一个简单的if判断 Python3取消 ...
随机推荐
- Mybatis入门之增删改查
Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...
- 9个Console命令
九个Console命令,让js调试更简单 By.cllgeek 一.显示信息的命令 1: <!DOCTYPE html> 2: <html> 3: <head> 4 ...
- JavaScript函数继承
在ES6中有了继承,使用extends关键字就能实现.但这里讲的讲的不是这种,而是ES6之前的几种实现继承的方式. (一)原型继承 ECMAScript中将原型链作为实现继承的主要方法.其基本思想是利 ...
- MySQL 常用指令小结
l 创建数据库:CREATE DATABASE table_name; l 删除数据库:DROP DATABASE table_name; l 展示数据库:SHOW DATABASE; l 选 ...
- Ubuntu系统分配存储空间的建议以及给Ubuntu系统根目录扩容方法(从20GB追加100GB)
当初准备装双系统时,也思考了很久分配多少空间给Ubuntu16.04系统,查了许多资料,大多意思是‘/’目录总共给20GB,其他的给/home.网上资料推荐的大多跟这篇文章一样:https://blo ...
- centos后台运行Python
在服务器上,为了退出终端,程序依然能够运行,需要设置程序在后台运行. 关键的命令:nohup *基本用法:进入要运行的py文件目录前 nohup python -u test.py > tes ...
- 多线程控制工具类--倒计时器CountDownLatch的使用(模仿火箭发射)
package com.thread.test.Lock; import java.util.Random; import java.util.concurrent.CountDownLatch; i ...
- Python总结(一)
从大学开始,就对python有了兴趣,毕业设计就是用python做的一个新闻爬取和关键字提取的程序.然而,毕业之后由于一直没有从事python相关的开发,所以就一直没有再使用,一直停留在偶尔看一些资料 ...
- 二维数组中的查找[by Python]
题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...