python学习笔记-3.1python运算符和if判断
来源:互联网 发布:mysql压缩包下载地址 编辑:程序博客网 时间:2024/09/21 11:18
1. python 运算符
要点
算术运算符 + - * / %(取模,返回除法的余数) **(求幂,返回x的y次幂) //(取整除,返回商的整数部分)
比较(关系)运算符 == != <> >< >= <=
赋值运算符 = += -= = /= %= *= //=
逻辑运算符 and和 or或 not非
and和 真真为真,其他为假 or或 假假为假,其他未真 not非 取反
位运算符 & | ^ - << >>
&按位与运算符:参与运算的两个值,如果连个相应都未1,则该位的结果为1,否则为0
|按位或运算符:只要对应的二个二进制有一个为1,结果位就为1 ^按位异或运算符,两个二进制不同为1 -按位取反运算符,对数据的每个二进制位取反,即把1变为0,把0变为1,在一个有符号二进制数的补码形式 << 左移 >> 右移
成员运算符
in指定的序列中找到值返回True,否则返回False。 not in指定的序列中没有找到值返回True,否则返回False
身份运算符
is 是否一样,判断两个标识符是不是引用自一个对象 is not 是否不一样,判断两个标识符是不是引用自不同对象
运算符优先级
第一等级** 第二等级*/ // % 第三等级+- 第四等级>><< 第五等级& 第六等级^| 第七等级<= => >< 第八等级 == != 第九等级 = += -= *= /= **= 第十等级 is is not 第十一等级 in not in 第十二等级 not or and
1.1算术运算符
a=10b=4c=0c=a+bc=a%bc=a**bc=a//bprint c
1.2 比较(关系)运算符
if (a==b): print 'yes'else: print 'no'if (a!=b): print 'yes'else: print 'no'if (a>=b): print 'yes'else: print 'no'if (a<=b): print 'yes'else: print 'no'
1.3赋值运算符
a = 10b = 4c = 12c += ac += bc -= ac -= bc *= ac /= bc %= bc **=ac //= bprint c
1.4 逻辑运算符
a = Trueb = Falsec = 12if (a and b): print 'yes'else: print 'no'if (a or b): print 'yes'else: print 'no'if not(a and b): #取反 print 'yes'else: print 'no'
1.5 位运算符 ,二进制运算
a = 10b = 9c = 0#c = a & b#c = a | b#c = a^b#c = -ac = a<<2c = a>>2print c
1.6 成员运算符
list = [1,2,3,4,5,6,7]if (a in list): print 'yes'else: print 'no'if (a not in list): print 'yes'else: print 'no'
1.7身份运算符
a = 10b = 9c = 0if (a is b): print 'yes i is'else: print 'no not'if (a is not b): print 'yes i is'else: print 'no not'd = (1+2)*3/2print d
2.if 判断
用户登录案例
##创建一个用户名#coding=utf-8username = 'admin'password = 'xulaoshi123456'user_input = raw_input('请输入你的用户名:')pass_input = raw_input('请输入密码:')if username == user_input and password == pass_input: print '欢迎登录 %s'%user_input #%s是字符串变量,user_input变量else: print'登录失败,%s用户名或密码错误'%user_input##新增访客用户username = 'admin'password = 'xulaoshi123456'user_input = raw_input('请输入你的用户名:')pass_input = raw_input('请输入密码:')if username == user_input and password == pass_input: print '欢迎登录 %s'%user_input #%s是字符串变量,user_input变量elif user_input == 'guest': print'登录成功,但是你只有只读权限'else: print'登录失败,%s用户名或密码错误'%user_input
0 0
- python学习笔记-3.1python运算符和if判断
- 【学习笔记】Python条件判断-If语句
- 【python学习笔记】运算符和表达式
- python笔记4_运算符和判断
- Python学习笔记(运算符)
- Python学习笔记-运算符
- 【python学习笔记】python if和for的多种写法
- python 学习笔记 if语句
- Python学习笔记之IF
- 【Python语法笔记】if else 流程判断
- 【python学习笔记二】对象和类型,运算符
- Python学习笔记_判断和循环
- 【Python学习笔记】条件判断和input
- python学习笔记 条件判断和循环
- python学习总结---if-else判断
- python--if条件判断
- Python的if-判断
- python if判断
- SpringMVC有用归纳
- Delphi 10.2 Tokyo 下载激活
- android 大文件分割上传(分块上传)
- 自己实现shadowsocks搭建
- qt中文乱码解释
- python学习笔记-3.1python运算符和if判断
- java学习笔记3——操作运算符
- (六)简单排序-选择排序
- Appdelegate跳转到指定tabbaritem
- javamail实现简单邮件发送
- datatables+json+ajax以json输出和删除
- Android listview实现分组
- 小学生图解排序算法:④希尔排序
- CentOS 7上安装nsq,并配置系统服务、开机启动