大家好~我是恰恰!好久不见啦~不知道最近大家的Python学习的怎么样啦?在别人看不到的日子里,大家也都要努力学习哦,悄悄学习Python,然后惊艳所有人!
大家学习Python估计都知道,经常写代码的大佬肯定都有自己私藏的非常好用的代码段,所以我就想着,要是能搜集一些这样的代码段给大家,岂不是很好,所以我就来啦~
最近也会有很多小伙伴说,为什么大家都在学Python呢,这里我想告诉大家!
1,Python的前景非常好:Guido龟叔表示:他打算在2022年10月发布3.11版本时将快CPython的速度提高1倍。在接下来的四年里,他的目标是将CPython的速度提高到原来的5倍。
2,Python 的用法非常简洁、灵活:它的扩展库也很丰富,可以满足非常多复杂场景的需求,能够替代非常多的手工操作。
3,Python跨平台性非常好:无论是在 macOS 和 Windows 间如何切换,不用修改任何一行代码,就可以让已经写好的程序直接在新的平台上运行。
总之,在职场总有人不需要加班就能完成老板布置的工作任务,那么你想要是你会Python,这个人就是你啦!
所以,这里整理的18个常用的Python代码段请果断收藏起来,如果觉得足够好用记得分享给你身边的朋友和同事哟~
1.交换两个变量的值
num_1, num_2 = 666, 999
# 一行代码搞定交换两个变量的值
num_1, num_2 = num_2, num_1
print(num_1, num_2)
输出:
999 666
Process finished with exit code 0
2.查找对象使用的内存
import sys
slogan = "今天你学python了么?"
size = sys.getsizeof(slogan)
print(size)
输出:
100
Process finished with exit code 0
3.反转字符串
slogan = "今天你学习python了么?"
# 一行代码搞定字符串的反转
new_slogan = slogan[::-1]
print(new_slogan)
输出:
?么了nohtyp习学你天今
Process finished with exit code 0
4.检查字符串是否为回文
# 定义一个判断字符串是否是回文的函数
def is_palindrome(string):
return string == string[::-1]
示例:调用判断函数来进行判断slogan是否是回文字符串
slogan = "今天你学python了么?"
_ = is_palindrome(slogan)
print(_)
输出:
False
Process finished with exit code 0
5.将字符串列表合并为单个字符串
slogan = ["今", "天", "你", "学", "python", "了", "么", "?"]
# 一行代码搞定将字符串列表合并为单个字符串
real_slogan = "".join(slogan)
print(real_slogan)
输出:
今天你学python了么?
Process finished with exit code 0
6.查找存在于两个列表中任一列表存在的元素
# 定义一个函数用来查找存在于两个列表中任一列表存在的元素
def union(list1, list2):
return list(set(list1 + list2))
示例:调用该函数用来查找存在于两个列表中任一列表存在的元素
list1, list2 = [5, 2, 0], [5, 2, 1]
new_list = union(list1, list2)
print(new_list)
输出:
[0, 1, 2, 5]
Process finished with exit code 0
7.打印N次字符串
slogan = "今天你学python了么?"
new_slogan = 11*slogan
print(new_slogan)
输出:
今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?今天你学python了么?
Process finished with exit code 0
8.链式比较
number = 100
print(98<number<102)
输出:
True
Process finished with exit code 0
print(100==number<102)
输出:
True
Process finished with exit code 0
9.单词大小写
slogan = "python happy"
# 一行代码搞定单词大小写转换
print(slogan.upper())
# 一行代码搞定单词首字母大写
print(slogan.capitalize())
# 一行代码搞定将每个单词的首字母转为大写,其余小写
print(slogan.title())
输出:
PYTHON HAPPY
Python happy
Python Happy
Process finished with exit code 0
10.统计列表中元素的频率
from collections import Counter
numbers = [1, 1, 3, 2, 4, 4, 3, 6]
# 一行代码搞定求列表中每个元素出现的频率
count = Counter(numbers)
print(count)
输出:
Counter({1: 2, 3: 2, 4: 2, 2: 1, 6: 1})
Process finished with exit code 0
11.判断字符串所含元素是否相同
from collections import Counter
course = "python"
new_course = "ypthon"
count_1, count_2 = Counter(course), Counter(new_course)
if count_1 == count_2:
print("两个字符串所含元素相同!")
输出:
两个字符串所含元素相同!
Process finished with exit code 0
12.将数字字符串转化为数字列表
string = "666888"
numbers = list(map(int, string))
print(numbers)
输出:
[6, 6, 6, 8, 8, 8]
Process finished with exit code 0
13.使用enumerate() 函数来获取索引-数值对
string = "python"
for index, value in enumerate(string):
print(index, value)
输出:
0 p
1 y
2 t
3 h
4 o
5 n
Process finished with exit code 0
14.代码执行消耗时间
import time
start_time = time.time()
numbers = [i for i in range(10000)]
end_time = time.time()
time_consume = end_time - start_time
print("代码执行消耗的时间是:{}".format(time_consume))
输出示例:
代码执行消耗的时间是:0.002994537353515625
Process finished with exit code 0
15.比较集合和字典的查找效率
import time
number = 999999
# 生成数字列表和数字集合
numbers = [i for i in range(1000000)]
digits = {i for i in range(1000000)}
start_time = time.time()
# 列表的查找
_ = number in numbers
end_time = time.time()
print("列表查找时间为:{}".format(end_time - start_time))
start_time = time.time()
# 集合的查找
_ = number in digits
end_time = time.time()
print("集合查找时间为:{}".format(end_time - start_time))
输出:
列表查找时间为:0.060904741287231445
集合查找时间为:0.0
Process finished with exit code 0
16.字典的合并
info_1 = {"apple": 13, "orange": 22}
info_2 = {"爆款写作": 48, "跃迁": 49}
# 一行代码搞定合并两个字典
new_info = {**info_1, **info_2}
print(new_info)
输出:
{'apple': 13, 'orange': 22, '爆款写作': 48, '跃迁': 49}
Process finished with exit code 0
17.随机采样
import random
books = ["爆款写作", "这个世界,偏爱会写作的人", "写作七堂课", "越书写越明白"]
# 随机取出2本书阅读
reading_book = random.sample(books, 2)
print(reading_book)
输出:
['这个世界,偏爱会写作的人', '越书写越明白']
Process finished with exit code 0
18.判断列表中元素的唯一性
# 定义一个函数判断列表中元素的唯一性
def is_unique(list):
if len(list) == len(set(list)):
return True
else:
return False
# 调用该函数判断一个列表是否是唯一性的
numbers = [1, 2, 3, 3, 4, 5]
_ = is_unique(numbers)
print(_)
输出:
False
Process finished with exit code 0
最近我有点懈怠了,O(∩_∩)O哈哈~争取在接下来的日子里,能多给大家分享关于Python的有用的知识哦~觉得有用的话,可以分享给自己的同学同事哦~也希望大家终有一日能成为Python大神!