从零学Python之入门(四)运算

yipeiwu_com6年前Python基础

Python的运算符和其他语言类似

(我们暂时只了解这些运算符的基本用法,方便我们展开后面的内容,高级应用暂时不介绍)

数学运算

复制代码 代码如下:

>>>print 1+9        # 加法

>>>print 1.3-4      # 减法

>>>print 3*5        # 乘法

>>>print 4.5/1.5    # 除法

>>>print 3**2       # 乘方    

>>>print 10%3       # 求余数

判断

判断是真还是假,返回True/False

复制代码 代码如下:

>>>print 5==6               # =, 相等

>>>print 8.0!=8.0           # !=, 不等

>>>print 3<3, 3<=3          # <, 小于; <=, 小于等于

>>>print 4>5, 4>=0          # >, 大于; >=, 大于等于

>>>print 5 in [1,3,5]       # 5是list [1,3,5]的一个元素

还有is, is not等, 暂时不深入)


逻辑运算

True/False之间的运算

复制代码 代码如下:

>>>print True and True, True and False      # and, “与”运算, 两者都为真才是真

>>>print True or False                      # or, "或"运算, 其中之一为真即为真

>>>print not True                           # not, “非”运算, 取反

可以和上一部分结合做一些练习,比如:

复制代码 代码如下:

>>>print 5==6 or 3>=3

总结

数学 +, -, *, /, **, %

判断 ==, !=, >, >=, <, <=, in

逻辑 and, or, not

相关文章

python3使用urllib示例取googletranslate(谷歌翻译)

复制代码 代码如下:#!/usr/bin/env python3# -*- coding: utf-8 -*-# File Name : gt1.py# Purpose :# Creat...

Random 在 Python 中的使用方法

Random 在 Python 中的使用方法

1.random.random(): 会随机生成0-1之间的小数 例如: 2.random.uniform(min,max): 会随机生成 min - max 之间的小数,其中min...

解决sublime+python3无法输出中文的问题

Tools -> Build System -> Build New System { "cmd": ["/usr/local/bin/python3", "-u",...

python实现自主查询实时天气

python实现自主查询实时天气

本文实例为大家分享了python实现自主查询实时天气的具体代码,供大家参考,具体内容如下 用到了urllib2 json  很简单的一个应用 如下 获取城市编号 #cod...

Python中常见的异常总结

一、异常错误    a、语法错误 错误一: if 错误二: def  text:       pass...