本章大纲
对象
变量
变量操作
对象
一切(万物)皆为对象
常见对象-数字
整数
python 语言里 一切数据都是对象
1,2,3
-1,-345345
python 2 :int ---long(长整数)
32bit -2的31次方-----2的31次方----1
64bit -2的63次方-----2的63次方----1
python 2 :只有int,没有long
浮点数float
3.14
3.0
-3.2
精度--限制(小数点后面16位)
数字--运算
加法
Air ~ % python # 进入python编译器
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+1 # 输入运算公式
3
减法
Air ~ % python # 进入python编译器
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2-1 # 输入运算公式
1
乘法
Air ~ % python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2*3
6
>>> 2*6
12
除法
取商
Python2: 9/4 结果2
Python3: 9/4 结果2.25 / 运算符 总是得到小数
Python3: 9//4 结果2
Air ~ % python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9/4
2.25
>>> 9//4
2
Air ~ % python2
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
Type "help", "copyright", "credits" or "license" for more information.
>>> 9/4
2
>>> 9//4
2
取余数:python2和3都是一样
Air ~ % python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 9%4
1
Air ~ % python2
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
Type "help", "copyright", "credits" or "license" for more information.
>>> 9%4
1
![](https://img.haomeiwen.com/i9622367/1b4db140b96fc7c8.png)
小数结果(这里python2得到小数结果的方法)
Air ~ % python2
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
Type "help", "copyright", "credits" or "license" for more information.
>>> 9.0%4
2.25
次方
Air ~ % python2
Python 2.7.16 (default, Jun 5 2020, 22:59:21)
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**4
16
Air ~ % python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2**4
16
练习--pycharm中python3数字运算
print(type(100)) # type查看类型
print(2/6) # 除不尽,精度--限制:小数点后16位
print(round(2/6,4)) # round取精度,保留小数点后4位
print(round(3.1415987,4)) # round取精度,保留小数点后4位,round可以进行四舍五入
print(2+1)
print(2-1)
print(2*3)
print(9/4)
print(9//4)
print(9.0/2)
print(2**4) # 打印2的4次方
数字--混合运算
混合运算
print(4*2+1)
print(4*(2+3))
print(4.0*(118+988)/45)
print(2.0*(118+988)+34/2-2)
有括号先算括号内的,再算括号外的乘除,最后算加减
字符串
只要引号引起来的都是字符串,左右两边的引号要一致
# 字符串
print(type('my name is xiaoming'))
print(type("my name is xiaoming"))
print(type("""my name is xiaoming"""))
print(type('''my name is xiaoming'''))
print(type('my name is' + 'xiaoming'))
表达式
# 前面的都是表达式 3+1--算数表达式,不同类型不能运算
# 算数操作(mathematical operations)表达式(expression)
# + - * / % ** 都是算数操作符
# 表达式是会产生一个值的一段代码
# 因为表达式可被求值,所以他可以写在赋值语句等号的右侧
print('hello' + 2) # 先入为主,加号前面的是字符串,所以此处+ 是拼接符,代码运行报类型错误:TypeError: must be str, not int,将‘hello’改成int型数字或将‘2’改成str就不报错了
print('hello' + '2')
print('hello' + 'world!')
print(3 + 2)
执行多行语句
多行语句对齐
空行没有关系
顺序执行
多行语句时,只运行一行数据:选中要运行的语句“右键”,点击“Execute Selection in Python Console”
打开python解释器:Tools -> Python or Debug Console
总结
数字对象
字符串对象
所有的数据类型都是对象
对象时非常泛义的概念
一切(包含函数、类、模块、方法)都是对象
对象的产生
解释器执行到相应的代码,内存中的变化
字面量
像这种从代码表达直接可以产生python对象的叫字面量(Literal)
比如:3,3.14,“hello”
变量
# 变量
welcome = ',欢迎您的到来' # 变量本质就是一个对象的名字
print('王校长' + welcome)
print('李校长' + welcome)
变量命名--组成:字母 +、数字 、下划线
--一般以字母(大写,小写均可)开头,数字、下滑线不能开头
--大小写敏感
--不能与关键字相同 if = 1, for = 1
--不要与内置函数相同 print = 1
# 什么是关键字:解释器有特定意义的名字
# 打印关键字
import keyword
print(keyword.kwlist)
python 不需要声明类型,赋什么类型就是什么类型
# 同一个变量不能赋多个值,如果一个变量赋两个值,只有最后的值生效,因为前面的值被认为是无用的,就被清除了,占用的空间也被释放了
Name = '苏东坡'
Name = '李商隐'
print(Name)
变量--存储
没有任何变量引用的对象,会被python解释器清除
被认为是无用的
释放内存空间
何时清除,一定的策略
请大家画三张图,分别表示下面三句代码执行时的内存指向情况
a = 3 # 图1
b = a # 图2
a = 4 # 图3
b = a # 图4
注意:python中任何变量赋值,都不会修改变量原来指向对象的值,而是将变量 指向一个新的对象而已,如果原来的对象,有其他变量指向它,也不回改变其他变量指向
a = 3
b = a
a = 4
b = a
print(a,b)
变量操作
# 变量操作
b = 1
a = b
a = a + 1
a += 1 # 等于 a = a + 1
a = a - 1
a -= 1 # 等于 a = a - 1
a *= 5 # 等于 a = a * 5
a /= 2 # 等于 a = a / 2
网友评论