a=-11.37
type(a)
#输出
float
abs(-10)
#输出
10
a=[1,2,3]
b=[4,5,6]
d=list(zip(a,b))
print(d)
[(1,4),(2,5),(3,6)]
del object #删除一个变量
my_list = [1, 2, 3, 4, 5]
del my_list[2] # 删除索引为2的元素,即3
print(my_list) # 输出 [1, 2, 4, 5]
l = ['d', 'b', 'a', 'f', 'd']print("l.count('d') -->", l.count('d'))
输出结果:
l.count('d') --> 2
`oct()`函数的返回值是一个字符串,表示转换后的八进制数。需要注意的是,转换后的字符串前面会带有`0o`前缀,表示这是一个八进制数。如果不需要前缀,可以使用字符串切片操作去掉前两个字符,即oct_num[2:]
dec_num = 123 # 十进制数
oct_num = oct(dec_num) # 转换为八进制数
print(oct_num) # 输出:0o173
print(oct_num)[2:] #输出:173
num = 255
hex_num = hex(num)
print(hex_num) # 输出:0xff
print(hex_num[2:]) #输出: ff
‘连接符’.join([str1,str2,str3]):拼接字符串
a = 'www'
b = 'baidu'
c = 'com'
new_str = '.'.join([a,b,c])
print(new_str) #输出结果: www.baidu.com
l = ['d', 'b', 'a', 'f', 'd']print("l.index('d') -->", l.index('d'))
输出结果:
l.index('d') --> 0
l = ['d', 'b', 'a', 'f', 'd']l.remove('d')print("l -->", l)
输出结果:
l --> ['b', 'a', 'f', 'd']
l = ['d', 'b', 'a', 'f', 'd']
l.sort()
print('l -->', l)
输出结果:
l --> ['a', 'b', 'd', 'd', 'f']
降序排列:.sort(reverse=True)
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
my_list.sort(reverse=True)
print(my_list)
输出结果:
[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
sorted([36,5,-12,9,-21])
#[-21,-12,5,9,36]
sorted([36,5,-12,9,-21], reverse=True)
#[36,9,5,-12,-21]
sorted([36,5,-12,9,-21], key=abs)
#[5,9,-12,-21,36](不会改变原来的值)
l = ['d', 'b', 'a', 'f', 'd']lc = l.copy()print('lc -->', lc)
输出结果:
lc --> ['d', 'b', 'a', 'f', 'd']
filter()接收一个函数和一个序列。filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
def func(x):
return x % 2 == 0 #返回一个bool值
a = [1,2,3,4,5,6,7,8]
print(list(filter(func,a))) #要显示序列,先转化为list
f = lambda x : x * x
print(f(5))
max(1,2,4,6,8,9,7,5,4,3) #返回:9
min(3.14,9,5,7,4) #返回: 3.14
pow(2,3) #输出:8
pow(2.5,3) #输出:15.625
round(4.775)
输出:5
round(4.775,2)
输出:4.78
range(5)
[0, 1, 2, 3, 4]
range(2, 6)
[2, 3, 4, 5]
range(0, 10, 2)
[0, 2, 4, 6, 8]
range(10, 0, -2)
[10, 8, 6, 4, 2]
默认空格为分隔符,如对分隔符有要求则再括号内填写
math.fabs(-10)
输出:10.0
math.ceil(4.5)
输出:5
math.floor(4.9)
输出: 4
print(math.factorial(5))
输出:20
import random
fruits = ['apple', 'banana', 'orange', 'pear']
random_fruit = random.choice(fruits)
print(random_fruit)
输出:orange
import random
random.choice(range(1,100)) #从1-100中随机生成一个数
输出:87
import random
#生成0~9之间的随机整数
num1 = random.randrange(10)
print(num1)
# 生成1~10之间的随机整数
num2 = random.randrange(1, 11)
print(num2)
# 生成1~10之间的随机偶数
num3 = random.randrange(2, 11, 2)
print(num3)
# 生成10~1之间的随机整数
num4 = random.randrange(10, 0, -1)
print(num4)
import random
random.randint(0, 100)
import random
# 生成一个0-1之间的随机浮点数
rand_num = random.random()
print(rand_num)
注:random.shuffle()用于将一个列表中的元素打乱顺序,值得注意的是使用这个方法不会生成新的列表,只是将原列表的次序打乱。
import random
x=[1,8,6,5,4,7,5]
random.shuffle(x)
print(x)
输出:[7, 6, 1, 5, 4, 5, 8]
import random
random.uniform(1,100)
索引 |
属性 |
值 |
0 |
tm_year(年) |
如:1945 |
1 |
tm_mon(月) |
1 ~ 12 |
2 |
tm_mday(日) |
1 ~ 31 |
3 |
tm_hour(时) |
0 ~ 23 |
4 |
tm_min(分) |
0 ~ 59 |
5 |
tm_sec(秒) |
0 ~ 61 |
6 |
tm_wday(周) |
0 ~ 6 |
7 |
tm_yday(一年内第几天) |
1 ~ 366 |
8 |
tm_isdst(夏时令) |
-1、0、1 |
localtime() 表示当前时间,返回类型为 struct_time 对象,示例如下所示:
import time
t = time.localtime()
print('t-->', t)
print('tm_year-->', t.tm_year)
print('tm_year-->', t[0])
输出结果:
t--> time.struct_time(tm_year=2019, tm_mon=12, tm_mday=1, tm_hour=19, tm_min=49, tm_sec=54, tm_wday=6, tm_yday=335, tm_isdst=0)
tm_year--> 2019
tm_year--> 2019
函数(常量) |
说明 |
time() |
返回当前时间的时间戳 |
gmtime([secs]) |
将时间戳转换为格林威治天文时间下的 struct_time,可选参数 secs 表示从 epoch 到现在的秒数,默认为当前时间 |
localtime([secs]) |
与 gmtime() 相似,返回当地时间下的 struct_time |
mktime(t) |
localtime() 的反函数 |
asctime([t]) |
接收一个 struct_time 表示的时间,返回形式为:Mon Dec 2 08:53:47 2019 的字符串 |
ctime([secs]) |
ctime(secs) 相当于 asctime(localtime(secs)) |
strftime(format[, t]) |
格式化日期,接收一个 struct_time 表示的时间,并返回以可读字符串表示的当地时间 |
sleep(secs) |
暂停执行调用线程指定的秒数 |
altzone |
本地 DST 时区的偏移量,以 UTC 为单位的秒数 |
timezone |
本地(非 DST)时区的偏移量,UTC 以西的秒数(西欧大部分地区为负,美国为正,英国为零) |
tzname |
两个字符串的元组:第一个是本地非 DST 时区的名称,第二个是本地 DST 时区的名称 |
import time
print(time.time())
print(time.gmtime())
print(time.localtime())
print(time.asctime(time.localtime()))
print(time.tzname)
# strftime 使用
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
import re
pattern = r'hello'
string = 'hello world'
match_obj = re.match(pattern, string)
if match_obj:
print(match_obj.group())
#输出结果为:`hello`
import re
pattern = r'world'
string = 'hello world'
search_obj = re.search(pattern, string)
if search_obj:
print(search_obj.group())
#输出结果为:`world`
import re
pattern = r'\d+'
string = 'hello 123 world 456'
match_list = re.findall(pattern, string)
print(match_list)
#输出结果为:`['123', '456']`
import re
pattern = r'\d+'
string = 'hello 123 world 456'
new_string = re.sub(pattern, '666', string)
print(new_string)
#输出结果为:`hello 666 world 666`
import re
pattern = r'\s+'
string = 'hello world'
split_list = re.split(pattern, string)
print(split_list)
#输出结果为:`['hello', 'world']`
if not os.path.isdir(path):
os.mkdir(path)
#通常结合isdir函数一起用
if os.path.exists("baidu.html"):
os.remove("baidu.html")
#如果存在baidu.html文件,则删除该文件
if not os.path.isdir(path):
ok.mkdir(path)
#这里用if语句判断当前目录之下是否存在名为'path'的文件夹,如果不存在,则创建一个
举例说明(删除一个存有若干张图片的文件夹):
def remove_dir(path):
if os.path.isdir(path): #判断指定路径是否为该目录
for file in os.listdir(path): #遍历该目录中的文件列表
remove_dir(os.path.join(path, file)) #运用递归的方法,依次删除该目录下的文件以及子目录
os.rmdir(path) #删除该路径
else:
os.remove(path) #删除文件
if os.path.exists("tupian"): #判断是否存在,存在即删除,不存在则不管
remove_dir("tupian")
file = open(filename, mode)
filename:文件名
mode:是打开文件的模式
常用的模式有:
content = file.read()
`content`是读取到的文件内容
file.write(content)
其中,`content`是要写入的内容。
file.close()