r
“r”是“raw”的简写。意思是“未加工的,原料”。在python字符串前面,表示“按原样输出字符串”,也就是说字符串里的元素,原来什么样子,还是什么样子,不变。python不会去对一些符号转义。
print('My name is Dave')
My name is Dave
print('My name \n is Dave')
My name
is Dave
print(r'My name \n is Dave')
My name \n is Dave
upper?lower?
大小写转换
a='学习Python'
print(a.upper())
学习PYTHON
b='学习PYTHON'
print(b.lower())
学习python
count
统计在字符串/列表/元组中某个字符出现的次数,可以设置起始位置或结束位置。
c='hello world,very good'
print(c.count('o'))
4
字符串格式化
name = input('请输入你的名字:')
age = input('请输入你的年龄:')
subject = input('请输入科目:')
print('My name is {},My age is {},I like {} ;'.format(name,age,subject))
My name is Dave,My age is 25,I like Python ;
列表 List
list1 = ['a','b','c','d']
list2 = [10,20,30]
list3 = list3 = ['English','Math','Music','Chemistry','Chinese','History','Biology']
list2.append(list3)
print(list2)
[10, 20, 30, ['English', 'Math', 'Music', 'Chemistry', 'Chinese', 'History', 'Biology']]
list4=[list1,list2,list3]
print(list4)
[['a', 'b', 'c', 'd'], [10, 20, 30, ['English', 'Math', 'Music', 'Chemistry', 'Chinese', 'History', 'Biology']], ['English', 'Math', 'Music', 'Chemistry', 'Chinese', 'History', 'Biology']]
#打印list3下表为2,3,4的元素
print(list3[2:4])
['Music', 'Chemistry', 'Chinese']
#打印list3最后一位元素
print(list3[-1])
Biology
#打印最后一个元素里面的倒数第三个元素
print(list4[-1][-3])
Chinese
#list 末尾追加元素
list1.append("北京")
list1.append("上海")
print(list1)
['a', 'b', 'c', 'd', '北京', '上海']
#list 第3个位置添加一个4倍的元素
list1.insert(2,'hello' * 4)
print(list1)
['a', 'b', 'hellohellohellohello', 'c', 'd', '北京', '上海']
#获取list 元素的个数
print(len(list1))
7
#删除下表为2的元素
list2.remove(30)
print(list2)
[10, 20, ['English', 'Math', 'Music', 'Chemistry', 'Chinese', 'History', 'Biology']]
list2.clear()
print(list2)
[]
#反向列表中元素
list.reverse()
集合
集合是无序的
#集合:
set1 = {'apple','pear','banage','grape'}
print(set1)
{'grape', 'banage', 'apple', 'pear'}
set1.add('hello')
print(set1)
{'pear', 'hello', 'apple', 'banage', 'grape'}
#遍历循环
for x in set1:
print(x)
#检查是否存在
print('hello' in set1)
True
set1.remove('apple')
print(len(set1))
set1.clear()
isnumeric()方法
Python isnumeric() 方法检测字符串是否只由数字组成。
random.choice()
可以从序列中获取一个随机元素;比如随机点名系统;
students = []
students.append('张三')
students.append('李四')
students.append('王五')
students.append('赵六')
print(random.choice(students))
Python 集合(数组)
Python 编程语言中有四种集合数据类型:
列表(List)是一种有序和可更改的集合。允许重复的成员。
元组(Tuple)是一种有序且不可更改的集合。允许重复的成员。
集合(Set)是一个无序和无索引的集合。没有重复的成员。
字典(Dictionary)是一个无序,可变和有索引的集合。没有重复的成员。
#生成一个0到1的随机数
print(random.random())
# 产生 1 到 10 的一个整数型随机数
print(random.randint(1,10))
# 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数
print(random.uniform(1.1,5.4))
# 从序列中随机选取一个元素
print(random.choice("hello world"))
# 生成从1到100的间隔为2的随机整数
print(random.randrange(1,100,2))
# 将序列a中的元素顺序打乱
a=[1,2,3,4,5,6]
random.shuffle(a)
print(a)
datetime
print(datetime.now())
2022-07-28 16:21:34.918248
print(datetime.today())
2022-07-28 16:21:34.918248
print(date.today())
2022-07-28
print(datetime.strftime(datetime.now(), '%y-%m-%d %I:%M:%S'))
22-07-28 04:21:34
os
import os
1. os.getcwd() # 获取当前文件路径
2. os.listdir(path) # 获取path下所有文件,以列表返回
3. os.mkdir(path) # 创建路径
4. os.rename(old_name,new_name) # 重命名
5. os.remove(str) #删除文件夹
6. os.path.isdir(path) #判断路径是不是文件夹
7. os.path.isfile(path) # 判断path是不是文件
8. os.path.exists(path) # 判断path是否存在
os.path.join
路径拼接
a = os.path.join('aa','bb','cc')
print(a)
aa\bb\cc
json.dump()和json.dumps()的区别
json.dumps() 是把python对象转换成json对象的一个过程,生成的是字符串。
json.dump() 是把python对象转换成json对象生成一个fp的文件流,和文件相关。