Python复习总结(1)

Shypython-learn-notes

1. python 数据类型

1.1 变量

1.1.1 算术运算符

- 加减乘除+-*/
- 取余、取整、取绝对值 %//abs()
- 最小、最大值 min()max()
- 复数 complex(re,im)
- 取共轭 c.conjugate()
- 返回商和余数 divmod(x,y)  

1.1.2 布尔运算符

-  小于、大于 <>
- 等于、不等于 ==!= 
- 与、或、非 andornot

1.1.3 赋值运算符

- a=a+b  is  a+=b
- a=a-b  is  a-=b
- a=a*/b  is  a*/b
- a=a**(//)b  is  a**(//)=b

1.1.4 位运算符

- 与或 &|  
- 异或、取反 ^~ 
- 左位移、右位移  <<>>

1.1.5 转义符

- 续行符 \
- 反斜杠符号 \\
- 引号 \'
- 响铃 \a
- 退格 \b
- 转义 \e
- 空 \000
- 换行 \n
- 纵向制表符 \v
- 横向制表符 \t
- 回车 \r
- 换页 \f
- 八进制 \oyy
- 十六进制 \xyy

1.2 字符串(不可变类型)

1.2.1 切片操作

# 当索引为正数时,从0开始,当索引为负数时,从-1开始(从右往左)
- newstr = s[a:b:c]  从索引a开始到b ,每隔c取一个值,左开右闭
- newstr = s[0:]  
- newstr = s[:]  和上面的式子等价
- newstr = s[::-1] 实现字符串的逆序

1.2.2 字符串运算及方法

str = 'I LOVE PYTHON!!'
- 标准化字符串 r'str' 或者 repr(str)
- x in s 子字符串 
- s1 + s2 字符串连接 
- s*n 字符串副本的拼接 
- s[i] 字符串索引 
- str.index('s') 获得字符串s字符的索引位置
- len(s) 字符串长度 
- ord(str) 字符串的编码 
- chr(number) 返回某个编码得到的字符 
- str.spilt(', ') 字符串的分割 ,返回值是一个列表
- chr.join(list)  字符串编码的连接 , " ".join(list)
str = "www.runoob.com"
- print(str.upper())  把所有字符中的小写字母转换成大写字母
- print(str.lower())  把所有字符中的大写字母转换成小写字母
- print(str.capitalize()) 把第一个字母转化为大写字母,其余小写
- print(str.title())  把每个单词的第一个字母转化为大写,其余小写 

1.2.3深入研究字符串的方法

str.find(x)  返回x的第一个字符出现的索引位置
str.count(x)  返回x出现的次数
str.replace('top','bot')  返回一个修改的副本
str.spilt()
tabel = str.maketrans('xyz','uvw') ; str.translate(table)  返回一个映射后的副本
str.strip()  返回字符串的一个副本,并且消除前后空格

1.3 列表(可变类型)

1.3.1 list的内置方法

lst = [1,3,a,[4,5],6]
- list.append(x)  在尾部增加一个元素
- list.insert(x,i)  在索引i处添加一个元素
- list.index(x)  获得元素x的索引
- list.remove(x)  删除列表的原色
- list.pop(i)  弹出索引为i的元素并在列表中删除它
- list.clear() 清楚列表
- list.count(x) 返回列表x出现的次数
- list.sort() 对列表直接排序, 区别于排序函数 sorted()
- list.reverse() 对列表进行反转
- len(list)
- for item in list:   对列表的遍历

1.3.2 列表和字符串的相互转化

1. str >>>list 

str1 = "12345"
list1 = list(str1)
print list1

str2 = "123 sjhid dhi"
list2 = str2.split() #or list2 = str2.split(" ")
print list2

str3 = "www.google.com"
list3 = str3.split(".")
print list3

输出为:
['1', '2', '3', '4', '5']
['123', 'sjhid', 'dhi']
['www', 'google', 'com']

2. list >>>str
str4 = "".join(list3)
print str4
str5 = ".".join(list3)
print str5
str6 = " ".join(list3)
print str6
输出为:
wwwgooglecom
www.google.com
www google com

1.3 元组类型(不可变类型)

1.3.1 元组的运算及操作

# 元组为不可修改的字符串
tuple = (2019,'a',(b,c),'science')

1.3.2 元组与列表的转换

tuple = tuple(list)
list  = list(touple)

1.4 集合类型(消除关系重复元素)

myset = ['nature','science']
set.add(x)
set.remove(X)
set.discard(X)
set.clear()
set.pop()
len(set)
in / not in
set.issubset(set2)  判断set是否是set2的子集,返回bool类型
set.isuperset(set2)  
set.union(set2)  计算并集
set.intersection(set2)  计算交集
set.difference(set2)  计算差集
set.symmetric_difference(set2)  计算对称差集

1.5 字典类型(键值对)

mydict = {'a':1,'b':2,'c':3}
len(dict)
str(dict)
dict('a')  访问字典中键为a的值
dict.clear()
dict.items() 以列表形式返回可遍历的(键,值)元素数组
dict.keys() 以列表形式返回一个字典中的所有键
dict.values() 以字典形式返回一个字典中的所有值

2. 语句类型

2.1 if 语句

if <条件>pass
elif <条件>pass
else:
    pass

2.2 while语句

while <条件>pass
# 死循环
while 1:
    pass

2.3 for 语句

for item in list:
    pass

# 在for循环中使用内置函数 range()
range(a,b,c)  返回一个数字区间的所有整数

# 简单的冒泡排序算法S
for i in range(len(n)-1):
    for j in range(len(n)-i-1):
        if n[j] > n[i]:
            n[j], n[j+1] = n[j+1], n[j]

# 在for循环中使用内置函数zip()
zip(x,y)  将多序列生成一个新的序列,每个序列的元素以元组形式存储数据

for t1,t2 in zip(x,y):
    print(t1,t2)

2.4 控制语句

break  跳出循环
continue  终止当前一次循环、继续进行下一次循环
pass 什么都不做,保持结构完整性
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值