Python基本运算符
来源:互联网 发布:淘宝网店货源加盟代理 编辑:程序博客网 时间:2024/05/29 08:14
Python基本运算符
什么是操作符?
简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符。 Python语言支持操作者有以下几种类型。
算术运算符
比较(即关系)运算符
赋值运算符
逻辑运算符
位运算符
会员操作符
标识操作符
让我们逐一看看所有的运算符。
Python算术运算符:
假设变量a持有10和变量b持有20,则:
[ 查看示例 ]
Python的比较操作符:
假设变量a持有10和变量b持有20,则:
[ 查看示例 ]
Python赋值运算符:
假设变量持有10和变量b持有20,则:
[ 示例 ]
试试下面的例子就明白了所有在Python编程语言可供选择的赋值运算符:
#!/usr/bin/pythona =21b =10c =0c = a + bprint"Line 1 - Value of c is ", cc += aprint"Line 2 - Value of c is ", c c *= aprint"Line 3 - Value of c is ", c c /= a print"Line 4 - Value of c is ", c c =2c %= aprint"Line 5 - Value of c is ", cc **= aprint"Line 6 - Value of c is ", cc //= aprint"Line 7 - Value of c is ", c
当执行上面的程序,它会产生以下结果:
Line 1 - Value of c is 31Line 2 - Value of c is 52Line 3 - Value of c is 1092Line 4 - Value of c is 52Line 5 - Value of c is 2Line 6 - Value of c is 2097152Line 7 - Value of c is 99864
Python位运算符:
位运算符作用于位和位操作执行位。假设,如果a =60;且b =13;现在以二进制格式它们将如下:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Python语言支持下位运算符
[ 示例 ]
Python逻辑运算符:
Python语言支持以下逻辑运算符。假设变量a持有10和变量b持有20则:
[ 示例 ]
Python成员运算符:
除了前面讨论的运算符,Python成员运算符,在一个序列中成员资格的测试,如字符串,列表或元组。有两个成员运算符解释如下:
[ 示例 ]
试试下面的例子就明白了所有的Python编程语言提供会员运算符:
#!/usr/bin/pythona =10b =20list =[1,2,3,4,5];if( a in list ):print"Line 1 - a is available in the given list"else:print"Line 1 - a is not available in the given list"if( b notin list ):print"Line 2 - b is not available in the given list"else:print"Line 2 - b is available in the given list"a =2if( a in list ):print"Line 3 - a is available in the given list"else:print"Line 3 - a is not available in the given list"
当执行上面的程序它会产生以下结果:
Line 1 - a is not available in the given listLine 2 - b is not available in the given listLine 3 - a is available in the given list
Python标识运算符:
标识符比较两个对象的内存位置。两个运算符标识解释如下:
[ 查看示例 ]
试试下面的例子就明白了所有Python编程语言提供的标识运算符:
#!/usr/bin/pythona =20b =20if( a is b ):print"Line 1 - a and b have same identity"else:print"Line 1 - a and b do not have same identity"if( id(a)== id(b)):print"Line 2 - a and b have same identity"else:print"Line 2 - a and b do not have same identity"b =30if( a is b ):print"Line 3 - a and b have same identity"else:print"Line 3 - a and b do not have same identity"if( a isnot b ):print"Line 4 - a and b do not have same identity"else:print"Line 4 - a and b have same identity"
当执行上面的程序它会产生以下结果:
Line 1 - a and b have same identityLine 2 - a and b have same identityLine 3 - a and b do not have same identityLine 4 - a and b do not have same identity
Python运算符优先级
下表列出了所有运算符从最高优先级到最低。
[ 查看示例 ]
原文:http://www.yiibai.com/python/python_basic_operators.html
- Python基本运算符
- Python基本运算符
- Python基本运算符
- Python基本运算符
- python基本运算符(文摘)
- python 第一节 python基本运算符
- Python基本语法_运算符详解
- Python入门——基本运算符和基本类型
- Python学习(基本运算)
- Python变量及基本运算
- 【python】基本运算和表达式
- 实训1/6 Python基本语法、变量、运算符
- Python基础(2)-基本数据类型以及运算符介绍
- Python之运算符以及基本数据类型的objec
- Python基础一(基本类型和运算符)
- 谈python的基本数据类型、运算符以及条件控制
- Python按位运算符,字典,运算符优先级的基本总结
- Swift--基本运算符
- Python编辑器IDLE 简单操作
- 荔枝FM:异地多活IDC机房架构
- epoll原理及线程安全小结
- c++ 中 class 和 struct 的区别是什么
- 小技巧之 - __block
- Python基本运算符
- openssl如何制作自签名的CA证书,及利用CA签发证书
- Ubuntu 14.04下安装GitLab指南
- Android 四大组件 —— 活动(使用Intent 实现活动的显示跳转)
- Hibernate开发实践笔记--对象在set属性时更新了数据库问题
- ceph 修复monitor 坏掉过多时的问题
- 1.1Python语言集成环境搭建
- MAC OSX 沙盒机制
- opencv 如何输出某个pixel 的三维坐标