注:个人笔记,并不详细,仅供参考。
Python 变量类型
对象
Python 对象特征
所有的Python 对象都拥有三个特性:
特性 | 说明 | 例子 |
---|---|---|
身份 | 每一个对象都有一个唯一的身份,标识自己, 任何对象的身份可以使用内建函数id() 来得到 | id(username) |
类型 | 决定了该对象可以保存什么类型的值, 可以进行什么样的操作,以及遵循什么样的规则。 用内建函数type() 查看对象的类型 | type(username) |
值 | 对象表示的数据项 | print(username) |
Python 对象属性
某些Python 对象有属性、值或相关联的可执行代码,用点.
标记法来访问属性。最常用的属性是函数和方法,不过有一些Python 类型也有数据属性。含有数据属性的对象包括(但不限于):类、类实例、模块、复数和文件。
>>> username.
userName.capitalize( userName.isdigit( userName.rfind(
userName.casefold( userName.isidentifier( userName.rindex(
userName.center( userName.islower( userName.rjust(
userName.count( userName.isnumeric( userName.rpartition(
userName.encode( userName.isprintable( userName.rsplit(
userName.endswith( userName.isspace( userName.rstrip(
userName.expandtabs( userName.istitle( userName.split(
userName.find( userName.isupper( userName.splitlines(
userName.format( userName.join( userName.startswith(
userName.format_map( userName.ljust( userName.strip(
userName.index( userName.lower( userName.swapcase(
userName.isalnum( userName.lstrip( userName.title(
userName.isalpha( userName.maketrans( userName.translate(
userName.isascii( userName.partition( userName.upper(
userName.isdecimal( userName.replace( userName.zfill(
>>> username.lower()
'zs'
>>> userName
'ZS'
>>>
对象身份比较
对象可以通过引用被赋值到另一个变量,因为每个变量都指向同一个(共享的)数据对象,只要任何一个引用发生改变,该对象的其它引用也会随之改变。
>>> x = 1
>>> y = x
>>> id(x)
4318726800
>>> id(y)
4318726800
>>>
数字
基本数字类型
关键字 | 解释 | 例子 |
---|---|---|
int | 有符号整数 | 自然数 |
bool boolean | 布尔值 | True:1 False:0 |
float | 浮点数 | π |
进制间的数字表示方式
Python 默认以十进制数显示,可以用其他方法表示数字:
进制 | 表达 | 实例 |
---|---|---|
10 进制 | 默认表达方式 | 11 |
2 进制 | 数字以0b 或0B 开头 | 0b11 |
8 进制 | 数字以0o 开头 | 0o11 |
16 进制 | 数字以0x 或0X 开头 | 0x11 |
>>> 11
11
>>> 0b11
3
>>> 0o11
9
>>> 0x11
17
>>>
位运算符
位运算符 | 功能 |
---|---|
num1 << num2 | num1 左移num2 位 |
num1 >> num2 | num1 右移num2 位 |
num1 & num2 | num1 与num2 按位与 |
num1 ^ num2 | num1 异或 num2 |
num1 | num2 | num1 与num2 按位或 |
~num | 单目运算,对数的每一位取反 |
>>> 2 >> 2
0
>>> 2 << 2
8
>>>
数字类型函数
函数 | 说明 |
---|---|
int() | 将字符串转化为数字 |
long() | |
float() | |
complex() | |
abs() | 返回给定参数的绝对值 |
divmod() | 内建函数把除法和取余运算结合起来,返回一个包含商和余数的元组 |
pow() | 进行指数运算 |
round() | 用于对浮点数进行四舍五入运算 |
仅用于整数的函数
函数 | 说明 |
---|---|
hex() | 10 进制数转换为字符串形式的16 进制数 |
oct() | 10 进制数转换为字符串形式的8 进制数 |
bin() | 10 进制数转换为字符串形式的2 进制数 |
>>> hex(11)
'0xb'
>>> oct(11)
'0o13'
>>> bin(11)
'0b1011'
>>>
字符串
字符串的定义
Python 中字符串被定义为引号之间的字符集合。Python 支持使用成对的单引号或双引号,无论单引号,还是双引号,表示的意义相同。Python 还支持三引号(三个连续的单引号或者双引号),可以用来包含特殊字符;Python 不区分字符和字符串。
# 07 - 字符串的定义.py
username = 'ZS'
username = "ZS"
username = '''ZS'''
username = """ZS"""
print(type(username))
字符串拼接
字符串连接的时候可以使用 +
或者 *
。
使用 +
号可以将多个字符串拼接在一起。或者使用 *
号可以将一个字符串重复多次输出。
# 08 - 字符串拼接.py
username = "ZS"
print("Welcome, " + username * 5)
二狗子问题:
>>> 2 + 'dog'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> str(2) + 'dog' # str() 可以将数字转换成字符串
'2dog'
>>>
单个字符操作
单个字符操作 | 例子 |
---|---|
将字符转化成ASCII 码 | ord(‘a’) |
将ASCII 码转化位字符 | chr(97) |
与字符串相关的函数
函数名 | 作用 |
---|---|
len() | 用于返回给定对象的长度或项目数 |
str() | 用于将其他数据类型转换为字符串类型 |
ord() | 返回一个整数,表示给定字符的 Unicode 编码 |
chr() | 用于将一个整数转换为一个对应的 ASCII 字符 |
# a = "hello, world!"
# print(len(a)) # len()函数用于返回给定对象的长度或项目数
# b = "123"
# print(str(b)) # str() 用于将其他数据类型转换为字符串类型。
# c = 'A'
# print(ord(c)) # ord() 返回一个整数,表示给定字符的 Unicode 编码。
d = 65
print(chr(d)) # chr() 用于将一个整数转换为一个对应的 ASCII 字符
字符串操作
字符串操作 | 实例 |
---|---|
比较运算符: 字符串按照字符的ASCII 码值进行比较。 | ‘b’ < ‘bool’ ‘b’ > ‘abcd’ |
角标访问: 利用偏离量获取字符串中的字符。 | ‘ZS’[1] |
切片:取子串 str[n:m],n,m 均代表偏移量,m > n,[n,m) str[::n],每n 个字母一组,取第一个字母 str[::-1],字符串反向 | ‘phpinfo()’[1:3] ‘phpinfo()’[::3] ‘phpinfo()’[::-1] |
成员关系: 字符串A 是否是字符串B 的子串 | ‘Z’ in ‘ZS’ ‘z’ not in ‘ZS’ |
格式化操作符
格式化字符 | 转换方式 |
---|---|
%s | 字符串 |
%d | 10 进制数 |
%e | 科学计数法 “%e” %10000000000 |
%f | 浮点数 |
辅助指令 | 作用 |
数值 | 定义宽度或小数点精度,例如:%5s,%.3f |
- | 左对齐 |
+ | 正负数前面显示符号 “%+d” %-10 |
# 字符串格式化输出.py
students = [
{"name":"ZS", "age":24},
{"name":"Lilei", "age":25},
{"name":"Hanmeimei", "age":23}
]
print("name: %10s, age: %10d" % (students[0]["name"], students[0]["age"]))
print("name: %10s, age: %10d" % (students[1]["name"], students[1]["age"]))
print("name: %-10s, age: %-10d" % (students[2]["name"], students[2]["age"]))
format 格式化
Python2.6 开始,新增了一种格式化字符串的函数str.format()
,它增强了字符串格式化的功能。基本语法是通过{}
和 :
来代替以前的%
。format 函数可以接受不限个参数,位置可以不按顺序。
>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
'hello world'
>>> "{0} {1}".format("hello", "world") # 设置指定位置
'hello world'
>>> "{1} {0} {1}".format("hello", "world") # 设置指定位置
'world hello world'
字符串常用内建函数
常用内建函数 | 说明 | 示例 |
---|---|---|
s.strip() | 删除字符串实例字符串两端的空白 | ’ ZS '.strip() |
s.replace(sub,rep) | 搜索s 字符串中的sub, 并替换为rep | ‘zs’.replace(‘e’,‘E’) |
s.split© | 分割字符串,并返回一个列表, 可以指定分隔符c,默认为空白。 | ‘127.0.0.1’.split(‘.’) |
s.center(width) | 返回一个原字符串居中, 并使用空格填充至长度width 的新字符串 | “ZS”.center(11) |
s.upper() | 转换字符串实例中的小写字母为大写 | ‘zs’.upper() |
s.lower() | 转换字符串实例中的大写字母为小写 | ‘ZS’.lower() |