码迷,mamicode.com
首页 > 其他好文 > 详细

运算符及控制流程

时间:2018-12-29 17:22:37      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:style   比较运算符   3.1   end   alt   流程   分享   技术   close   

py3中只有input

py2中有raw_input 和 input

  raw_input和py3中的input完全一样,输入的全被存成字符串类型

  input:要求必须输入一个明确的数据类型,输入什么类型就存成什么类型

 

1、算数运算符

+ 加

- 减

*乘

/除

//取整数部分

%求余

**  求幂运算

 

技术分享图片
# int,float=>数字类型
# print(10 + 3.1)
# print(10 / 3)
# print(10 // 3)
# print(10 % 3)
# print(10 ** 2)

# +与*(了解)
# msg1=‘hello‘
# msg2=‘world‘
# print(msg1 + msg2)
# print(msg1*10)

# l1=[‘a‘,‘b‘]
# l2=[‘c‘,‘d‘]
# print(l1 + l2)
# print(l1*3)
View Code
2、赋值运算
技术分享图片
# 2 赋值运算符
# 增量赋值
# age=18
# age+=1 # age=age + 1
# print(age)

# age=18
# age*=3 #age=age*3
# print(age)

# 交叉赋值
# x=11
# y=22

# temp=x
# x=y
# y=temp
# x,y=y,x
# print(x,y)

# 链式赋值
# x=10
# y=x
# z=y

# x=y=z=10
# print(x,y,z)

# 解压赋值
# 1. 列表的解压赋值
l=[egon,18,male,33333]
# a=l[0]
# b=l[1]
# c=l[2]
# d=l[3]

# a,b,c,d=l
# print(a,b,c,d)

# a,b,c,d=l

# salaries=[1.1,2.2,3.3,4.4,5.5]
# a,b,_,_,_=salaries
# a,b,*_=salaries
# *_,a,b=salaries
# a,*_,b=salaries
# print(a,b,)

# 2. 字典的解压赋值
# dic={‘aaa‘:1,‘bbb‘:2,‘ccc‘:3}
# x,y,z=dic
# print(x,y,z)
View Code

3、逻辑运算符

技术分享图片
# 3 逻辑运算符
# and: 左右两个条件必须同时成立,最终结果才为True
# print(10 < 3 and 3 == 3)

# or: 左右两个条件只要有一个成立,最终结果就为True
# print(10 < 3 or 3 == 3)
# print(10 < 3 or 3 < 3)

# not:将紧跟其后的条件结果取反
# print(not 10 > 3)
# print(not 10 < 3 or 3 < 3)

# res=(10 > 3 and 3 == 1) or ((4 < 3 and True) or (not 1 > 2 or 3 > 2))

# print(res)
View Code

4、比较运算符

# 4 比较运算符
# ==
#!=
# print(10 != 3)

 

 

5、控制流程
#
语法一: # if 条件: # 代码1 # 代码2 # 代码3 # gender=‘female‘ # age=18 # is_beautiful=True # # if gender == ‘female‘ and age > 16 and age < 20 and is_beautiful: # print(‘开始表白。。。。‘) # # print(‘其他代码‘) #语法二: # if 条件: # 代码1 # 代码2 # 代码3 # else: # 代码1 # 代码2 # 代码3 # gender=‘female‘ # age=26 # is_beautiful=True # # if gender == ‘female‘ and age > 16 and age < 20 and is_beautiful: # print(‘开始表白。。。。‘) # else: # print(‘阿姨好‘) # # print(‘其他代码‘) #语法三: # if 条件1: # if 条件2: # 代码1 # 代码2 # 代码3 # gender=‘female‘ # age=18 # is_beautiful=True # is_successfull=True # # if gender == ‘female‘ and age > 16 and age < 20 and is_beautiful: # print(‘开始表白。。。。‘) # if is_successfull: # print(‘在一起,,,‘) # else: # print(‘逗你玩呢。。。‘) # else: # print(‘阿姨好‘) # # print(‘其他代码‘) # #语法四: # 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(your score: ) score=int(score) if score >= 90: print(优秀) elif score >= 80: print(良好) elif score >= 70: print(普通) else: print(很差)

 

运算符及控制流程

标签:style   比较运算符   3.1   end   alt   流程   分享   技术   close   

原文地址:https://www.cnblogs.com/Hale-wang/p/10196522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!