美文网首页
(2)变量类型、运算和字符串编码

(2)变量类型、运算和字符串编码

作者: 故城东 | 来源:发表于2018-09-26 17:57 被阅读0次

python是弱语言类型,不需要显式的声明变量,不需要提前声明变量的类型,python中没有常量

>>>a = 1
>>>print(a)
1

1 保存在内存中

a 是指针,指针中存的是1在内存中的地址,使用a的时候可以访问到内存中的1

>>>a = 1
>>>print(a)
1
>>>id(1)        #查看内存中的地址
4480297984
>>>id(a)        #a 和 1在内存中的地址相同
4480297984
>>>a is 1       #内存中地址相同,is的结果为True
True

变量类型

>>>a = 1
>>>type(a)
<class 'int'>           #整形
>>>b = 1.0
>>>type(b)
<class 'float'>         #浮点型
>>>e = 1 + 1j
>>>type(e)
<class 'complex'>       #复数
>>>c = "abc"
>>>type(c)
<class 'str'>           #字符串
>>>d = b"abc"
>>>type(d)
>>><class 'bytes'>      #bytes类型
>>>l = []
>>>type(l)
<class 'list'>          #列表
>>>t = (1,2)
>>>type(t)
>>><class 'tuple'>      #元祖
>>>s = set([1,1,1,2])
>>>type(s)
<class 'set'>           #set
>>>s = {1,2,3,4}
>>>type(s)
<class 'set'>           #set也可以这样定义
>>>fs = frozenset([1,1,1,2])
>>>type(fs)
<class 'frozenset'>     #frozenset
>>>True
>>>type(True)
<class 'bool'>          #bool
>>>False
>>>type(False)
<class 'bool'>
>>> class P:pass        #类
... 
>>>p = P()              #类的实例化
>>>type(P)
<class 'type'>          #类定义
>>>type(p)
<class '__main__.P'>    #类实例

运算

>>>a = 1
>>>b = 2
>>>a + b        #加
3
>>>a - b        #减
-1
>>>a * b        #乘
2
>>>a / b        #除
0.5
>>>a // b       #整除
0
>>>a % b        #取余
1
>>>a ** b       #乘方
1
>>>pow(2,3)     #乘方
8
>>>import math      #引入math模块
>>>math.sqrt(b)     #开方
1.4142135623730951  
>>>math.floor(1/2)  #地板除
0
>>>math.ceil(1/2)   #天花板除
1
>>>round(0.3)   #四舍五入
0
>>>round(0.8)
1

字符串转换

python3:str类型是unicode字符,只能被encode(),简称ue
>>>s = "abc"
>>>type(s)
<class 'str'>       #unicode
>>>type(s.encode("utf-8"))
<class 'bytes'>
>>>type(s.encode("utf-8").decode("utf-8"))
<class 'str'>

python2:str类型是bytes字符,只能被decode()
>>> s = "abc"
>>> type(s)
<type 'str'>        #bytes
>>> type(s.decode("utf-8"))
<type 'unicode'>
>>> type(s.decode("utf-8").encode("utf-8"))
<type 'str'>

函数

>>>abs(-1)      #绝对值函数
1
>>>max([1,2,3]) #最大值函数,参数可以是1个序列或多个值,返回最大值
3
>>>min([1,2,3]) #最小值函数,参数可以是1个序列或多个值,返回最小值
>>>pow(2,3)     #乘方,2个参数就是乘方
8
>>> pow(2,3,5)  #乘方取余,3个参数就是前2个数乘方再和第3个数取余
3
>>>round(0.3)   #四舍五入,1个参数就是该参数四舍五入
0
>>>round(0.8)
1
>>> round(2.555556,3) #四舍五入,2个参数就是第1个参数保留第2个参数位四舍五入
2.556
>>>import math      #引入math模块
>>>math.sqrt(b)     #开方,返回平方根
1.4142135623730951  
>>>math.floor(1/2)  #地板除
0
>>>math.ceil(1/2)   #天花板除
1

相关文章

  • (2)变量类型、运算和字符串编码

    python是弱语言类型,不需要显式的声明变量,不需要提前声明变量的类型,python中没有常量 1 保存在内存中...

  • Python3 个人学习记录

    Python3 学习记录 1.数据类型与变量 整数 浮点数 字符串 布尔型 常量 运算符 2.字符串与编码 字符串...

  • Python基础

    数据类型与变量 字符串在内存中的表示(数字变量同) 字符串和编码 1.获取字符整数ord('A') 2.将编码转换...

  • Python学习笔记(1)

    Python (1)数据类型和变量 (2)字符串和编码 (3)使用List和tuple (4)条件判断 (5)循环...

  • 基本数据类型和复杂数据类型的转化和运算归纳

    一、基本类型的运算 1、字符串之间的运算 2、数字类型之间,数字类型与字符串类型之间的运算 二、复杂类型的运算 三...

  • JavaScript

    快速入门 基本语法 语法 数据类型和变量 Number 字符串 '' '''' 布尔值 &&||! 比较运算...

  • JavaScript散乱(一)

    变量和计算 js中使用typeof能得到哪些类型? 强制类型转换 可能引起强制类型转换的操作 字符串拼接 ==运算...

  • PHP基础语法

    一、php的编码格式 1、php代码引入: 2、变量的定义:变量前面加$符号 二、PHP数据类型 字符串、整型、浮...

  • [LN_08] Shell编程-变量声明、运算符、变量测试

    目录结构 一、declare命令 Shell变量为弱类型,默认为字符串类型;若要参与运算,则需要声明变量类型 de...

  • Python基础入门:从变量到异常处理(1天)--阿里云天池

    变量、运算符与数据类型和位运算 2 变量、运算符与数据类型 2.1注释 1.在python中,#表示注释,作用于整...

网友评论

      本文标题:(2)变量类型、运算和字符串编码

      本文链接:https://www.haomeiwen.com/subject/ozjjoftx.html