Python 重要提示和技巧

发布时间:2022-07-24

每个 Python 开发人员都应该知道的 10 个基本提示和技巧

了解编程语言的技巧和窍门对于所有开发人员来说都是非常有帮助的。Python 是最受欢迎的编程语言之一,掌握一些基本的技巧可以节省大量时间并提升编码能力。以下是每个 Python 开发人员都应该知道的十个基本技巧。

1. 从列表元素创建单个字符串

我们可以使用 join() 函数将列表中的所有元素合并为一个字符串。

GivenList = ["Hello", "Python", "Developers!", "Welcome", "to", "JavaTpoint"]
print(" ".join(GivenList))

输出:

Hello Python Developers! Welcome to JavaTpoint

2. 使用枚举

在 Python 中,可以使用枚举来检查变量在函数中首次出现的次数。

class EnumExample:
    Hello, Python, Developers, Welcome, to, JavaTpoint, tutorial, of, Python = range(9)
print("Occurrence of JavaTpoint: ", EnumExample.JavaTpoint)
print("Occurrence of Hello: ", EnumExample.Hello)
print("Occurrence of Python: ", EnumExample.Python)
print("Occurrence of Welcome: ", EnumExample.Welcome)

输出:

Occurrence of JavaTpoint: 5
Occurrence of Hello: 0
Occurrence of Python: 8
Occurrence of Welcome: 3

3. 打印导入模块的路径

可以使用 print() 函数打印导入模块的文件路径。

import socket
import numpy
import os
print(socket)
print(numpy)
print(os)

输出:

<module 'socket' from 'C:\\Users\\Manish\\lib\\socket.py'>
<module 'numpy' from 'C:\\Users\\Manish\\lib\\site-packages\\numpy\\__init__.py'>
<module 'os' from 'C:\\Users\\Manish\\lib\\os.py'>

4. 打印列表中出现次数最多的元素

使用 max()count() 函数查找列表中出现次数最多的元素。

GivenList = [24, 21, 27, 29, 17, 23, 29, 34, 67, 23, 21, 29, 19, 63, 29, 27, 35, 21, 29]
print("Most occurred element in the given list: ", max(set(GivenList), key=GivenList.count))

输出:

Most occurred element in the given list: 29

5. 打印给定字符串的次数

使用 "字符串名称 * n" 语法打印字符串 n 次。

GivenString = "Welcome to JavaTpoint, Python developers!"
n = 4
print("Given string for n number of times: ")
print(GivenString * n)

输出:

Given string for n number of times:
Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!Welcome to JavaTpoint, Python developers!

6. 两个可变数字的交换

可以使用元组解包交换两个变量的值。

m = 24
n = 26
print("m before swapping: ", m)
print("n before swapping: ", n)
m, n = n, m
print("m after swapping: ", m)
print("n after swapping: ", n)

输出:

m before swapping: 24
n before swapping: 26
m after swapping: 26
n after swapping: 24

7. 使用比较运算符链

可以使用比较运算符链在一次比较中比较多个值。

num = 31
Result1 = 35 > num > 30
Result2 = 17 > num < 35
print(Result1)
print(Result2)

输出:

True
False

8. 反转给定的字符串

使用切片操作 [::-1] 来反转字符串。

GivenString = "Welcome to JavaTpoint Python Developers!"
print("Given String in program: ", GivenString)
print("Reverse of Given string in program is: ", GivenString[::-1])

输出:

Given string in program: Welcome to JavaTpoint Python Developers!
Reverse of Given string in program is: !srepoleveD nohtyP tniopTavaJ to emocleW

9. 从单个函数返回多个值

一个函数可以返回多个值,并通过解包赋值给多个变量。

def multival():
    return 24, 25, 31, 43, 37, 29, 39, 23
j, k, l, m, n, o, p, q = multival()
print(j, k, l, m, n, o, p, q)

输出:

24 25 31 43 37 29 39 23

10. 检查异序词

异序词是字母相同但顺序不同的两个单词。可以通过以下两种方式检查:

a. 不导入外部模块

def CheckAnagram(mkr1, mkr2):
    return sorted(mkr1) == sorted(mkr2)
print("Words are anagrams: ", CheckAnagram('Python', 'yPotnh'))
print("Words are anagrams: ", CheckAnagram('JavaTpoint', 'poijTtavaG'))

输出:

Words are anagrams: True
Words are anagrams: False

b. 导入外部模块

from collections import Counter
def CheckAnagram(mkr1, mkr2):
    return Counter(mkr1) == Counter(mkr2)
print("Words are anagrams: ", CheckAnagram('Python', 'yPotnh'))
print("Words are anagrams: ", CheckAnagram('JavaTpoint', 'poijTtavaG'))

输出:

Words are anagrams: True
Words are anagrams: False