基础知识:if判断、while循环、for循环
阅读目录
今日学习内容
1、if 判断(流程控制的一种)
写重复的代码是程序员最不耻的行为,所以我们需要while循环和for循环 ,_!
2、while循环(条件循环)
3、for循环(迭代器循环)
==========================================================
一、if 判断
语法一:if判断
--------------------------------------------------------------------注:如果你对python感兴趣,我这有个学习Python基地,里面有很多学习资料,感兴趣的+Q群:895817687
-------------------------------------------------------------------sex = 'female'
age = 32
is_beautiful = True
if sex == 'female' and age == 18 and is_beautiful:print('Begin your love words:')
print('another code1')
print('another code2')
if判断使用注意事项:if条件紧跟着一个冒号:+回车,pycharm会自动进入到下一行并缩进4个空格,开始进入运行一组代码块。在这里注意中英文输入法对代码“括号、冒号、逗号”这些字符的影响,谨记一定要用英文输入法输入,不然容易出现报错。
语法二:if+else判断
sex = 'female'
age = 18
is_beautiful = True
if sex == 'female' and 16 < age <24 and is_beautiful:print('Begin your love words.')
else:print('Say nothing...')
语法三:if+else 的嵌套
sex = 'female'
age = 18
is_beautiful = True
is_success = True
if sex == 'female' and 16 < age <24 and is_beautiful:print('Begin your love words.')if is_success:print('HaHaHa')else:print('I hate love,go away!')
else:print('Say nothing...')
tip:①and 多个条件并过长时,可用‘+回车’来多行显示,这里不影响语法。
②快捷操作,TAB键 可以进行快捷缩进,比如选中多行一起快捷缩进,要回退的话,按shift+tab组合键。
语法四:if+elif+else
# 输入成绩判断优秀、很好、一般、很差
score = input('Please input your score:')
score = int(score)
if score >= 90:print('优秀')
elif score >= 80:print('很好')
elif score >= 70:print('一般')
else:print('很差')
小练习:
练习一:用户登陆验证
name = input('请输入您的用户名:')
pwd = input('请输入您的密码:')
if name == 'sgt' and pwd == '123':print('登陆成功')
else:print('用户名或密码错误')
根据用户输入内容打印其权限
sgt = 'spueradministrator'
sgf = 'systemadministrator'
shr = 'administrator'
sqs = 'guest'
name = input('please input your username:')
if name == 'sgt':print('你好,超级管理员!')
elif name =='sgf':print('你好,系统管理员!')
elif name == 'shr':print('你好,管理员!')
elif name == 'sqs':print('你好,宾客!')
上班或出去浪
while True:today = input('今天是星期几?')if today == 'monday' or today == 'tuesday' or today == 'wednesday'\or today == 'thursday' or today == 'friday':print('上班')breakelif today == 'saturday' or today == 'sunday':print('出去浪')breakelse:print('''必须输入以下任意一种:mondaytuesdaywednesdaythursdayfridaysaturdaysunday''')
二、while循环
方式一:while+True/False
tag = True
while tag:name = input('please input your name:')pwd = input('please input your password:')if name == 'sgt' and pwd == '123':print('congratulation,region success!')tag = Falseelse:print('your name or password is wrong,please try again!')
方法二:while+break
while True:name = input('please input your name:')pwd = input('please input your password:')if name == 'sgt' and pwd == '123':print('congratulation,region success!')breakelse:print('your name or password is wrong,please try again!')
break是终止本层while循环。
方法三:while+continue
nums = 1
while nums <= 6:if nums == 4 or nums == 5:nums += 1continueelse:print(nums)nums += 1
终止当前循环,直接进入下次循环。
方法四:while+else
nums = 1
while nums <= 6:if nums == 4 or nums == 5:nums += 1continueelse:print(nums)nums += 1
else:print('前面代码无break,else 条件成立,否则else不执行。')
方法五:while的嵌套
示例一:
while True:name = input('please input your username:')pwd = input('please input your password:')if name == 'egon' and pwd == '123':print('login successful')while True:print('''0_退出1_取款2_转账3_查询 ''')choice = input('请输入您要执行的操作:')if choice == '0':breakelif choice == '1':print('取款...')elif choice == '2':print('转账...')elif choice == '3':print('查询...')else:print('输入指令有误,请重新输入:')breakelse:print('username or password error!')
改进示例二:
tag = True
while tag:name = input('please input your username:')pwd = input('please input your password:')if name == 'egon' and pwd == '123':print('login successful')while tag:print('''0_退出1_取款2_转账3_查询 ''')choice = input('请输入您要执行的代码:')if choice == '0':tag = Falseelif choice == '1':print('取款...')elif choice == '2':print('转账...')elif choice == '3':print('查询...')else:print('指令错误,请重新输入')else:print('username or password error')
三、for 循环 (其强大之处在于循环取值)
方案一、
for循环取值列表:
取值如果用while循环:
l = ['a', 'b', 'c', 'd', 'e']
i = 0
while i < len(l): #这里的len(l)表示计算列表l的长度(也就是索 #引数) print(l[i]) i += 1 如果我们使用for循环取值:
l = ['a', 'b', 'c', 'd', 'e']
for x in l: #这里的x in l 是直接将l列表里的数据取出关联 print(x) #到x,不用额外赋值。
for循环取值字典:
info = {'sgt': 'me', 'sgf': 'brother', 'age': 18, 'sun': 'shr'}
for x in info:print(x, info[x])
方案二、for+break
取出列表前几个数,后面的不取
nums = [11, 22, 33, 44, 55]
for x in nums:if x == 44:breakprint(x)
方案三、for+continue
不取其中的多个数据
nums = [11, 22, 33, 44, 55]
for x in nums:if x == 22 or x == 44:continueprint(x)
方案四、for+else
names = ['egon', 'alexdsb', 'sb', 'dsb']
for name in names:if name == 'alexdsb':breakprint(name)
else:print('>>>>>>>>>>>')#有break ,else下面的print不执行。names = ['egon', 'alexdsb', 'sb', 'dsb']
for name in names:if name == 'alexdsb':continueprint(name)
else:print('>>>>>>>>>>>')
#如果无break,else下面的print执行。
方案五、for+range()
range的用法:
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(1,5)
[1, 2, 3, 4]
>>> range(1,5,2)
[1, 3]
>>> range(1,1)
[]
>>>
正规格式为range(1,5,2)
表示:从1开始计数,每计数一次递增2,所以结果是[1,3]
如果括号里只有一个数,代表(0,5,1),即从0开始计数,递增数为1.
示例:
for x in range(5):print(x)#结果:
#0
#1
#2
#3
#4
方案六、for循环嵌套
for x in range(3):for y in range(3):print(x, y)
'''
结果
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
'''
基础知识:if判断、while循环、for循环相关推荐
- Python基础之day02-if判断与while,for循环
Python基础之day02-if判断与while,for循环 文章目录 Python基础之day02-if判断与while,for循环 一.比较与关系运算符 二.if-elif-else语句 三.i ...
- C++入门基础知识[5]——判断语句
C++入门基础知识[5]--判断语句 原创不易,路过的各位大佬请点个赞 C++入门基础知识--判断语句 C++入门基础知识[5]--判断语句 9.判断语句 9.1 判断语句 9.2 判断语句 9.3 ...
- 《Python基础知识-4判断和循环语句》
1 判断(分支)语句 1.1 if 语句 1)简单 if 语句 在Python中,if 语句就是用来判断的,格式如下: if 要判断的条件:条件成立时,要做的事情... 例如: In [1]: ...
- C语言随机比大小循环,C语言基础知识之(三):循环、随机数
循环结构: 循环:当满足某个特定条件的情况下,重复执行一段代码. 作用:减少重复冗余的代码,增加代码的可读性,易于维护 重点:各循环语句中循环条件的执行顺序. While循环 Do-While循环 F ...
- Java基础知识之跳转语句、循环标号多级break跳出实现、Random库
文章目录 一.跳转语句continue 二.跳转语句break 三.循环标号多级break 四.Random库 一.跳转语句continue 1.基本说明 在Java中的continue和其他编程语言 ...
- python基础知识~ 等值判断和码
简介 :等值判断和编码 一 第一部分 1 == 比较 数字字符串->True 元组,列表,字典->FALSE 2 is 比较 数字(-5-256 ...
- 基础知识—条件判断语句-switch语句
if语句是单一分支选择的,C++又提供了一种多分支选择的switch语句. switch语句的一般表现形式 switch(表达式) { case 表达式常量1: 语句1: break; case 表达 ...
- 基础知识—条件判断语句-if条件类型的语句
if关键字的条件判断语句 形式 if(表达式){语句} 表达式的运算结果应该为真和假,若为真则执行{语句},若为假则跳过. else语句 与if语句连用的语句 形式为 if(表达式) 语句1: els ...
- 【java基础知识】判断字符串不为空
if (str != null || !"".equals(str.trim())) {//则字符串不为空或空格 } 转载自:https://blog.csdn.net/xiazu ...
- 计算机基础知识的判断题,计算机基础知识判断题(12)
1. 在一个单元格输入公式后,若相邻的单元格中需要进行同类型计算,可利用公式的自动填充.( √ ) 2. SUM函数用来对单元格或单元格区域所有数值求平均的运算.( × ) 3. Excel2 ...
最新文章
- 电缆的选择及载流量的计算,超实用~
- 设置select默认值
- VGA timing information
- pytorch dataset dataloader_PyTorch(五)——数据的加载和预处理
- redhat Nginx 安装
- DOA——ESPRIT算法
- Assign the task HDU - 3974(线段树+dfs建树+单点查询+区间修改)
- const指针和指向const对象的指针
- yoga11rt系统刷linux,【攻略贴】联想Yoga“一秒”变身安卓平板,Win8 Andriod双系统刷机攻略出炉!...
- 自己动手写Docker系列 -- 5.1实现容器的后台运行
- 3ds Max Graphic Device Error 怎么解决(设置问题)
- mysql是用啥语言写的_mysql源码是什么语言
- c++实现解释器模式完整源代码
- 20135202闫佳歆-期中总结
- webstorm最新版激活破解
- python教程贪吃蛇_python实现贪吃蛇小游戏
- Excel 文字转拼音
- Android studio使用SVN
- erlang 虚机CPU 占用高排查
- 计算机专业英语被动语态举例,高考英语各种时态被动语态总结
热门文章
- plsql清完表需不需要提交事务_分布式基础-分布式事务
- autolisp统计相同元素个数_统计学习基础知识
- 零基础带你快速入门consul-难道consul还能这样用?
- springboot配置templates直接访问
- 二分法(三种基本模版)
- 《数据库系统实训》实验报告——存储过程
- Swagger 2——@ApiOperation注解、@ApiModel注解、@ApiImplicitParams注解、@ApiImplicitParam注解无效解决方案
- Kuangyeye and hamburgers
- mysql router 8.0.11_MySQL Router8
- I春秋—— Crypto Write up(一)