语法一:if判断ide
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个空格,开始进入运行一组代码块。在这里注意中英文输入法对代码“括号、冒号、逗号”这些字符的影响,谨记必定要用英文输入法输入,否则容易出现报错。spa
语法二:if+else判断code
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 的嵌套blog
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 多个条件并过长时,可用‘\+回车’来多行显示,这里不影响语法。ip
②快捷操做,TAB键 能够进行快捷缩进,好比选中多行一块儿快捷缩进,要回退的话,按shift+tab组合键。pycharm
语法四:if+elif+elseinput
# 输入成绩判断优秀、很好、通常、不好 score = input('Please input your score:') score = int(score) if score >= 90: print('优秀') elif score >= 80: print('很好') elif score >= 70: print('通常') else: print('不好')
小练习:io
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('上班') break elif today == 'saturday' or today == 'sunday': print('出去浪') break else: print(''' 必须输入如下任意一种: monday tuesday wednesday thursday friday saturday sunday ''')
方式一:while+True/Falsefor循环
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 = False else: print('your name or password is wrong,please try again!')
方法二:while+breakevent
while True: name = input('please input your name:') pwd = input('please input your password:') if name == 'sgt' and pwd == '123': print('congratulation,region success!') break else: 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 += 1 continue else: print(nums) nums += 1
终止当前循环,直接进入下次循环。
方法四:while+else
nums = 1 while nums <= 6: if nums == 4 or nums == 5: nums += 1 continue else: 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': break elif choice == '1': print('取款...') elif choice == '2': print('转帐...') elif choice == '3': print('查询...') else: print('输入指令有误,请从新输入:') break else: 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 = False elif choice == '1': print('取款...') elif choice == '2': print('转帐...') elif choice == '3': print('查询...') else: print('指令错误,请从新输入') else: print('username or password error')
方案1、
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])
方案2、for+break
取出列表前几个数,后面的不取
nums = [11, 22, 33, 44, 55] for x in nums: if x == 44: break print(x)
方案3、for+continue
不取其中的多个数据
nums = [11, 22, 33, 44, 55] for x in nums: if x == 22 or x == 44: continue print(x)
方案4、for+else
names = ['egon', 'alexdsb', 'sb', 'dsb'] for name in names: if name == 'alexdsb': break print(name) else: print('>>>>>>>>>>>') #有break ,else下面的print不执行。 names = ['egon', 'alexdsb', 'sb', 'dsb'] for name in names: if name == 'alexdsb': continue print(name) else: print('>>>>>>>>>>>') #若是无break,else下面的print执行。
方案5、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
方案6、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 '''