python学习之路,基础篇-条件语句
python常用语句
python语言的常用语句包含:赋值语句、判断语句(分支语句)、循环语句、注释语句和其他常用语句。
1、赋值语句
赋值语句是python语言中最简单、最常用的语句,通过赋值语句可以定义变量并为其赋初始值。
#!/usr/bin/env python # -*- coding:utf-8 -*- a = "Hello World !" b = 100 print(a) print(b) #输出结果: Hello World ! 100
2、条件分支语句
条件分支语句指当指定表达式取不同的值时,程序运营的流程也会发生相应的分支变化,Python提供的条件分支语句包含if 语句、else语句和elif语句。
2.1 if 语句
Python条件语句是通过一条或多条语句的执行结果(True或False)来决定执行的代码块。
python程序语言指定任何非0和非空(null)值为True,0或者null为False;
if语句是最常用的一种条件分支语句,其基本语法结构如下:
if 条件表达式: 语句块
else:
语句块 #只有当"条件表达式"等于True时,执行"语句块"
if语句的流程图如下图所示:

- if条件判断语句,其中“判断条件”成立时(非0),则执行后面的语句,而执行的内容可以为多行,以缩进来区分表示同一范围;
- 当需要多重判断时,可以使用elif语句继续判断;
- 条件不成立可以使用else语句来判断;
1 #!/usr/bin/python 2 #-*- coding: UTF-8 -*- 3 if 条件判断: 4 语句块 5 elif 条件判断: 6 语句块 7 elif 条件判断: 8 语句块 9 elif 条件判断: 10 语句块 11 else: 12 语句块
if 语句可以嵌套使用,也就是说在<语句块>中还可以使用if语句。
if语句实例:
#!/usr/bin/env python # -*- coding:utf-8 -*- a = 100 b = 1000 if a < b: print("a 小于b") if a == 100: print("a 等于100") if b == 1000: print("b 等于1000")
if语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
由于python不支持switch语句,所以多个条件判断,只能使用elif语句来实现,如果判断需要多个条件需同时判断时,可以使用and(与)、or(或)来判断条件是否成功。
- and(与):表示只有两个条件同时成立时,才为True;
- or(或):表示两个条件中有一个城里,就为True;
- 当if有多个条件时,可使用括号来区分判断的先后顺序,括号中的判断优先执行;
- “and"和"or"的优先级低于">"、"<"、“=”等判断符号,即">"和"<"等在没有括号的情况下会比"and"和“or”要优先判断;
if条件判断语句示例:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 6 user = "accp" 7 passwd = "accp#123" 8 9 while True: 10 username = input("Please input your name:") 11 password = input("Please input your password:") 12 if username != user or password != passwd: 13 print("Your username or password is incorrect. Please re-enter !") 14 else: 15 print("Welcome %s login!"%(username)) 16 break
多层条件判断语句示例:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 product_list = [('iphone',5800),('ipad',3280),('macbook pro',15880),('iwatch',2580),('car',10800),('Starbucks Mocha',41)] 6 shopping_list = [] 7 8 while True: 9 salary = input("Please input your salary:") 10 if salary.isdigit(): 11 salary = int(salary) 12 while True: 13 for index,item in enumerate(product_list): 14 print(index,item) 15 user_choice = input("Please enter the product you want to buy: ") 16 if user_choice.isdigit(): 17 user_choice = int(user_choice) 18 if user_choice < len(product_list) and user_choice >= 0: 19 p_item = product_list[user_choice][1] 20 if p_item <= salary: 21 shopping_list.append(product_list[user_choice]) 22 salary -= p_item 23 print("Your buy \033[32;1m%s\033[0m,Your balance is \033[32;1m%s\033[0m"%(product_list[user_choice],salary)) 24 elif p_item > salary: 25 print("Your balance is insufficient \033[31;1m%s\033[0m, please choose to buy the product"%(p_item)) 26 elif user_choice > len(product_list): 27 print("\033[31;1mIf you do not want to buy the product, please choose to buy the product\033[0m") 28 elif user_choice == 'q': 29 print("Your buy \033[32;1m%s\033[0m,Your balance is \033[32;1m%s\033[0m"%(shopping_list,salary)) 30 exit("\033[32;1mWelcome to you next time!\033[0m") 31 32 else: 33 print("\033[31;1mThe parameter you entered is wrong, Product number must be a number!\033[0m") 34 35 36 else: 37 print("\033[31;1mThe parameter you entered is wrong, Salary must be a number!\033[0m") 38 continue
浙公网安备 33010602011771号