Python期末复习:基础+数据结构

  • 合法的标识符定义规则

·  以字母或下划线开头

  • 标识符必须以字母(大写或小写)或下划线 _ 开头。

·  后续字符可以是字母、数字或下划线

  • 后续字符可以是字母(大写或小写)、数字(0-9)或下划线 _。

·  区分大小写

  • Python 中的标识符是区分大小写的。例如,myVar 和 myvar 是不同的标识符。

·  不能是 Python 的关键字

  • 标识符不能是 Python 的关键字。关键字是 Python 语言预定义的标识符,具有特殊意义。可以使用 keyword 模块查看所有的关键字。

  • 数据类型转化

一下是常见的转化数据类型的函数

int() 、float()、str()、list()、tuple()、set()

  • 运算顺序

·  圆括号:()

  • 用于分组表达式,改变默认的运算优先级。

·  指数:**

  • 例子:2 ** 3 等于 8。

·  按位取反按位求补一元加号一元减号:~ + -

  • 例子:-5 表示负数 5。

·  乘法除法取模取整除:* / % //

  • 例子:5 * 3 等于 15,10 / 2 等于 5.0,10 % 3 等于 1,10 // 3 等于 3。

·  加法减法:+ -

  • 例子:5 + 3 等于 8,5 - 3 等于 2。

·  按位左移按位右移:<< >>

  • 例子:2 << 2 等于 8,8 >> 2 等于 2。

·  按位与:&

  • 例子:5 & 3 等于 1(在二进制中 0101 & 0011 结果为 0001)。

·  按位异或:^

  • 例子:5 ^ 3 等于 6(在二进制中 0101 ^ 0011 结果为 0110)。

·  按位或:|

  • 例子:5 | 3 等于 7(在二进制中 0101 | 0011 结果为 0111)。

·  比较运算符:< <= > >= != ==

  • 例子:5 < 3 等于 False,5 == 5 等于 True。

·  赋值运算符:= += -= *= /= %= //= **= &= |= ^= >>= <<=

  • 例子:a = 5,a += 3 等价于 a = a + 3。

·  标识运算符:is is not

  • 例子:a is b,检查两个对象是否引用自同一个内存地址。

·  成员运算符:in not in

  • 例子:a in b,检查 a 是否在 b 中。

·  逻辑与:and

  • 例子:True and False 等于 False。

·  逻辑或:or

  • 例子:True or False 等于 True。

·  条件表达式:if else

  • 例子:a if condition else b,如果 condition 为 True,则结果为 a,否则为 b。

结合性(Associativity):

有些运算符具有相同的优先级,结合性决定了它们的计算顺序。

从左到右

    • 大多数运算符是从左到右结合的,例如:+, -, *, /, %, <<, >>, &, ^, |, <, >, <=, >=, ==, !=, and, or

从右到左

    • 一些运算符是从右到左结合的,例如:=, +=, -=, *=, /=, %=, //= , **, is, is not, in, not in, if else
  • 常见的数据结构

列表list:

append方法:直接修改原始列表,不会返回新的列表

Extend方法:在列表的后面添加列表,不会返回新的列表,和上面的区别就在于Extend一次性可以添加很多个

Insert 方法:在指定位置插入一个对象

Index 方法:返回指定数值的下标,如果没有就报错

Count(x):表示x在列表元素中出现的次数

Sort和reverse:一个是从小到大排序一个是从大到小的排序

Id()返回对象的标识符,因该是哈希值(并不是判断两个是否等值,其实可以理解成比较对象的地址)
列表是可变的,就是在对列表进行修改后不会创建新的列表,而是在内存中直接修改

不可变的对象是创建了一个副本之后在进行操作的,所以引用的对象的哈希值会改变

字符串string:


单引号和多引号的作用是一样的,而三引通常标识多行

可以有和C语言一样的转义字符

可以通过加法合并

可以通过乘法扩展长度

还可以通过切片访问

在类型转化方面只能转化成和自己同级或者比自己高级的
比如说float只能转向float,而int能够转化成int或者float

字符换占位符

:s = "im a bad gay %s %d"%("sd",12)

或者
 

s = "im a bad gay %s %d"
print(s%("sd",12))

可以在%s的中间上一些转移字符,来实现格式化操作,比如说%-10d标识向左空10格对齐

string = 'nishi shenm gui %10d' % 12
str1 = 'nishi shenm gui %-10.3f shenm' % 12.325125
print(string)
print(str1)

输出结果 

nishi shenm gui         12
nishi shenm gui 12.325     shenm

可以用过format进行格式化操作:

可以直接共变量名,可以直接用字典,但是应该是字典中必须有的关键字

site = {"shenw":"sd","wanzha":"sd"}
s = "woshi:{wanzha}".format(**site)
print(s)

输出

woshi:sd

字符串在format形式的表示下可以是下面几种格式

age, name = 25, 'Caroline'
print('{0}is {1} years old.'.format(name, age))  # 按照顺序访问
print('{0:.3f} and {1:.3f} are decimals.'.format(1 / 3, 20 + 1 / 3))  # 保留小数
print('{0:_^11}is a 11 length.'.format(name))  # 在空白处添加空格
print('{first} is as {second}.'.format(first=name, second='Wendy'))  # 指定名称
print('My name is .{0.name}'.format(open('out.txt', 'w')))  # 获取文件长度
print('My name is {0:8}.'.format('Fred'))  # 指定宽度
Carolineis 25 years old.
0.333 and 20.333 are decimals.
_Caroline__is a 11 length.
Caroline is as Wendy.
My name is .out.txt
My name is Fred    .

字符串的一些相关函数:

s = "hello world"
s.casefold()  # 首字母大写
print(s)
s.title()  # 每个单词的首字母大写
print(s)
s.lower()
s.upper()
s.swapcase()# 大小写呼唤
s = 'str'
print(s.center(7, "*"))
print(s.ljust(7,"*"))
print(s.rjust(7,"*"))
print(s.zfill(7))
print(s)

输出结果:

**str**
str****
****str
0000str
str

进程已结束,退出代码0

s = '''hello world i love you
yes'''
print(s.split())
print(s.split('l', 2))  # 从左往右分
print(s.rsplit('l', 2))  # 从右往左分
print(s.splitlines())  # 按照行分,里面有bool类型的参数表示是否保留分隔符
print('****'.join(s.split('l'))) #替换字符,如果没有就不会被替换
['hello', 'world', 'i', 'love', 'you', 'yes']
['he', '', 'o world i love you\nyes']
['hello wor', 'd i ', 'ove you\nyes']
['hello world i love you', 'yes']
he********o wor****d i ****ove you
yes

进程已结束,退出代码0
s = '''hello world i love you'''
print(s.find('h'))  # 扎到相应的下标
print(s.rfind('h'))  # 从右往左查找
print(s.find('s'))  # 如果没有就变成-1
print(s.index('h'))  # 和find效果一样,只是没有找到的时候会报错
print(s.count('h'))  # 找到字符的个数
0
0
-1
0

进程已结束,退出代码0
string = "hello oo"
print(string.replace('o', 'y', 1))
str1 = "*** 111 *\t**"
print(str1.strip('*'))  # 去除左右两边的指定字符
print(str1.strip('*').strip())
print(str1.lstrip('*'))  # 在左边替换
print(str1.rstrip('*'))  # 在右边替换
print(str1)
print(str1.expandtabs(5))  # 将换行符换成空格,里面的数字代表了换成几个
helly oo
 111 *	
111 *
 111 *	**
*** 111 *	
*** 111 *	**
*** 111 * **

进程已结束,退出代码0

字符串的判断方法 

startwith

s = 'hello world'
print(s.startswith('hello'))
print(s.startswith('hello', 3, 7))  # 这里可以设置检查的开始和结束位置
Ttue
False

 endwith

s = 'hello world'
print(s.endswith('world', 5, 10))  # 和startwith的语法一样
False

isXXXX

s = 'hello world'
print(s.isalpha())  # 是否全是字母
print(s.isdigit())  # 是否全是数字和字母
print(s.isalnum())  # 是否全是数字
print(s.isspace())  # 判断是不是空格
print(s.islower())  # 判断是不是小写字母
print(s.istitle())  # 判断开头是不是大写的
False
False
False
False
True
False

字符串的性质 

         字符串是不可变的,当对字符串进行修改的时候就会产生一个新的字符串

元组

 性质
        

        输出的时候总是有括号

        输入的时候元组是可以没有括号

        不可变

        可以嵌套,也可以是任意类型

        创建的元组不定长,一旦创建后就不可以修改长度

        在创建只有一个元素的元组时需要在元素后面添加逗号,防止被当成是运算符

        

s = (10,)

 元组运算
 

可以用加号来连接元组,乘法来增加长度

x = (1, 2, 3)
y = ('123', '456')
print(x + y)
print(3*x)
print(y*3)
print(1 in x)
(1, 2, 3, '123', '456')
(1, 2, 3, 1, 2, 3, 1, 2, 3)
('123', '456', '123', '456', '123', '456')
True

字典

x = {'woshishei':12}

        性质

         

         不可变类型

         不能用列表作为关键字,因为列表随时可能更改

         关键字必须互不相同

        实际应用

        

        

x = {'woshishei': 12, 'nishi': 4568}
print(x.keys())
print(x.values())
print(x.items())
for i in x.items():
    print(i[1])
for i in x:
    print(i)
dict_keys(['woshishei', 'nishi'])
dict_values([12, 4568])
dict_items([('woshishei', 12), ('nishi', 4568)])
12
4568
woshishei
nishi

这里注意,如果直接用的for in迭代只是去除其中的key 

 集合

          性质

                无序,不重合

                分为可变集合和不可变集合

           作用

                    去重:将列表变成集合就自动去重了

                   检查两个集合之间的交并性质

a = {1, 2, 3, 4, 5, 6, 7}
b = {2, 3, 4, 5, 6, 7, 8}
print(a | b)  # union
print(a & b)  # 交集
print(a - b)  # 差集
print(a ^ b)  # 对称差集

{1, 2, 3, 4, 5, 6, 7, 8}
{2, 3, 4, 5, 6, 7}
{1}
{1, 8}

  • 15
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值