基础知识:if判断、while循环、for循环
閱讀目錄
今日學習內容
1、if 判斷(流程控制的一種)
寫重復的代碼是程序員最不恥的行為,所以我們需要while循環(huán)和for循環(huán) ,_!
2、while循環(huán)(條件循環(huán))
3、for循環(huán)(迭代器循環(huán))
==========================================================
一、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個空格,開始進入運行一組代碼塊。在這里注意中英文輸入法對代碼“括號、冒號、逗號”這些字符的影響,謹記一定要用英文輸入法輸入,不然容易出現(xiàn)報錯。
語法二: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
# 輸入成績判斷優(yōu)秀、很好、一般、很差 score = input('Please input your score:') score = int(score) if score >= 90:print('優(yōu)秀') elif score >= 80:print('很好') elif score >= 70:print('一般') else:print('很差')小練習:
練習一:用戶登陸驗證
根據(jù)用戶輸入內容打印其權限
sgt = 'spueradministrator' sgf = 'systemadministrator' shr = 'administrator' sqs = 'guest' name = input('please input your username:') if name == 'sgt':print('你好,超級管理員!') elif name =='sgf':print('你好,系統(tǒng)管理員!') 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循環(huán)
方式一: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循環(huán)。
方法三:while+continue
nums = 1 while nums <= 6:if nums == 4 or nums == 5:nums += 1continueelse:print(nums)nums += 1終止當前循環(huán),直接進入下次循環(huán)。
方法四:while+else
nums = 1 while nums <= 6:if nums == 4 or nums == 5:nums += 1continueelse:print(nums)nums += 1 else:print('前面代碼無break,else 條件成立,否則else不執(zhí)行。')方法五: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('請輸入您要執(zhí)行的操作:')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('請輸入您要執(zhí)行的代碼:')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 循環(huán) (其強大之處在于循環(huán)取值)
方案一、
for循環(huán)取值列表:
取值如果用while循環(huán): l = ['a', 'b', 'c', 'd', 'e'] i = 0 while i < len(l): #這里的len(l)表示計算列表l的長度(也就是索 #引數(shù)) print(l[i]) i += 1 如果我們使用for循環(huán)取值: l = ['a', 'b', 'c', 'd', 'e'] for x in l: #這里的x in l 是直接將l列表里的數(shù)據(jù)取出關聯(lián) print(x) #到x,不用額外賦值。for循環(huán)取值字典:
info = {'sgt': 'me', 'sgf': 'brother', 'age': 18, 'sun': 'shr'} for x in info:print(x, info[x])方案二、for+break
取出列表前幾個數(shù),后面的不取
nums = [11, 22, 33, 44, 55] for x in nums:if x == 44:breakprint(x)方案三、for+continue
不取其中的多個數(shù)據(jù)
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不執(zhí)行。names = ['egon', 'alexdsb', 'sb', 'dsb'] for name in names:if name == 'alexdsb':continueprint(name) else:print('>>>>>>>>>>>') #如果無break,else下面的print執(zhí)行。方案五、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) [] >>> 正規(guī)格式為range(1,5,2) 表示:從1開始計數(shù),每計數(shù)一次遞增2,所以結果是[1,3] 如果括號里只有一個數(shù),代表(0,5,1),即從0開始計數(shù),遞增數(shù)為1.示例:
for x in range(5):print(x)#結果: #0 #1 #2 #3 #4方案六、for循環(huán)嵌套
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循环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基础知识:IDE集成开发环境(pycha
- 下一篇: 基础知识:数字、字符串、列表 的类型及内