一、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('开始表白。。。')
else:
print('阿姨好。。。') print('other code1...')
print('other code2...')
print('other code3...') 执行结果:第一个条件通过则输出:开始表白、other code1......,第一个条件不通过则输出:阿姨好
 示例:
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...')
执行结果:开始表白、other code1.....
语法二:
if条件:
代码块1
代码块2
代码块3 else:条件不成立时执行的子代码块
代码块1
代码块2
代码块3
 示例:
sex='female'
age=18
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...') 执行结果:第一个条件通过则输出:开始表白、other code1......,第一个条件不通过则输出:阿姨好
语法三:
if条件1:
if条件2:
代码块1
代码块2
代码块3
 示例:
sex = 'female'
age = 18
is_beautiful = True
is_sucessful=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_sucessful:
print('在一起。。。')
else:
print('什么爱情不爱情的。。。。。')
else:
print('阿姨好。。。') print('other code1...')
print('other code2...')
print('other code3...')
 # 注意在代码过长的情况下可以使用\来进行分隔:
# is_beautiful=\
# True
# print(is_beautiful)
# 输出结果:True
'''
sex = 'female'
age = 18
is_beautiful = True
is_sucessful=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_sucessful:
print('在一起。。。')
else:
print('什么爱情不爱情的。。。。。')
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=int(score)
if score>=90:
print('优秀')
elif score>=80 and score<90:
print('良好')
elif score>=70 and score<80:
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')注:现这个循环是在持续运行中
 方式一:条件改为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('===>>>>') # (这个无法执行)
 方式三:
while+continue:
结束本次循环,直接进入下一次循环 count=1
while count<6: #count=1,2,3,4,
if count == 4:
continue
print(count)
count +=1 #现执行结果在4这个数进行死循环 count=1
while count<6: #count=1,2,3,4,5
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 #结束本次循环
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])
 1、for+else
names=['egon','kevin111_dsb','alex_dsb','mac_dsb']
for name in names:
if name=='kevin_dsb':
break
print(name)
else:
print('=======>') # 执行结果是正常执行完所有姓名并结束
'''
'''
names=['egon','kevin_dsb','alex_dsb','mac_dsb']
for name in names:
if name=='kevin_dsb':
break
print(name)
else:
print('=======>') # 执行结果是输出egon
'''
'''
 2、for+continue

 nums=[11,22,33,44,55]
for x in nums:
if x == 22 or x == 44:
continue
print(x) #执行结果是11、33、55
 3、for+range()

 range的用法  (cmd中执行结果)
>>> 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(1,5,2):  #range是只顾头不顾尾,2代表步长指1,1+2=3,3+2=5(不包含5所以不输出)
print(i) # 执行结果是1,3 for i in range(5) # >>>>0,1,2,3,4
print(i) # >>>>0,1,2,3,4
 4、for嵌套
for i in range(3): # >>>>i=0,1,2
for j in range(4):# >>>>j=0,1,2,3
print(i,j) # 最终执行结果: 如i=0(不变)j=0 0 0 1 0 2 0
i = 0(不变)j=1 0 1 1 1 2 1
i = 0(不变)j=2 0 2 1 2 2 2
i = 0(不变)j=3 0 3 1 3 2 3

Python编程Day4——if判断、while循环、for循环的更多相关文章

  1. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

  2. Python编程基础[条件语句if 循环语句 for,while](二)

    ython条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: if 判断条件: 执行语句……else: 执行语句…… x= ...

  3. Python编程基础:循环结构

    一.为什么要用循环 现在有一个任务,要求你输出一百遍"好好学习,天天向上!",想一想,你会怎么做? (一)老老实实的笨方法 print("第1遍写:好好学习,天天向上!& ...

  4. Python的基本语法,涵盖数据类型、循环判断、列表、map和set等

    以#开头的语句是注释 当语句以冒号“:”结尾时,缩进的语句视为代码块.一般缩进4个空格 Python程序是大小写敏感的,如果写错了大小写,程序会报错. Python的数据类型 整型 浮点型 字符串 布 ...

  5. Python基础:条件判断与循环的两个要点

    一.条件判断: Python中,条件判断用if语句实现,多个条件判断时用if...elif实现:看下面一段程序 #python 3.3.5 #test if...elif age = 20 if ag ...

  6. Python编程从入门到实践笔记——用户输入和while循环

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

  7. Python基础之if判断,while循环,循环嵌套

    if判断 判断的定义 如果条件满足,就做一件事:条件不满足,就做另一件事: 判断语句又被称为分支语句,有判断,才有分支: if判断语句基本语法 if语句格式: if 判断的条件: 条件成立后做的事 . ...

  8. Python第四天 流程控制 if else条件判断 for循环 while循环

    Python第四天   流程控制   if else条件判断   for循环 while循环 目录 Pycharm使用技巧(转载) Python第一天  安装  shell  文件 Python第二天 ...

  9. Python基础(条件判断,循环,占位符等)

    Python 自动化 系统开发用的语言和自动化脚本可以不同 学习peython可用于: 网路爬虫,数据分,web开发,人工智能,自动化运维,自动化测试,嵌入式,黑客 第三方库比较全 脚本语言:功能单一 ...

随机推荐

  1. Linux 下 Samba 服务器搭建

    初学,分享 环境和条件--- 虚拟机:VMware虚拟机 系统:Linux ubuntu 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:0 ...

  2. PM学习梳理--搭建产品架构

  3. jsp中的四个作用域,九个内置对象分别是什么?

    九大内置对象: 内置对象(又叫隐含对象),就是在jsp中,不需要创建(由服务器<容器>来创建),可以直接使用的对象. 对象 含义 类 作用域 request 请求对象 类型 javax.s ...

  4. 用python turtle实现汉诺塔的移动

    1.汉诺塔 汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下面开始按大小 ...

  5. 2019.03.28 bzoj3326: [Scoi2013]数数(数位dp)

    传送门 题意: 一个人数数,规则如下: 确定数数的进制B 确定一个数数的区间[L, R] 对于[L, R] 间的每一个数,把该数视为一个字符串,列出该字符串的所有连续子串对应的B进制数的值. 对所有列 ...

  6. 关于chrome密码保存框的神坑,这样子解决就行

  7. MIUI 10以上版本通用线刷ROOT方法

    1.高版本系统解锁 (解锁工具下载) http://www.miui.com/unlock/index.html 注意事项:登陆解锁工具的账号必须是登陆小米手机的账号 解锁步骤在解锁工具上有说明,就两 ...

  8. MySQL/Oracle索引的创建与使用

    MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 索引分单列索引和组合索引. 单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引 ...

  9. PowerShell工作流学习-5-自定义活动

    关键点: a)除了内置活动和自定义活动,还可以用C# 编写自定义活动,并将其包括在 XAML 工作流和脚本工作流中,若要将自定义活动添加到脚本工作流中,请使用 #Requires 语句的 Assemb ...

  10. switch_root vs pivot_root vs chroot【转】

    1. pivot_root can/should be used together with chroot pivot_root new_root put_old pivot_root moves t ...