对于所有 Python 开发人员或任何其他语言开发人员来说,了解我们正在学习的编程语言的技巧和窍门总是令人着迷的。众所周知,Python 是开发人员最喜欢的编程语言之一。因此,在本教程中,我们带来了每个 Python 开发人员都应该知道的 Python 中的基本提示和技巧。
我们将在这里讨论的 Python 技巧或提示将使我们不必编写许多行代码,并且将节省大量时间。这些 Python 技巧也将有助于我们在与其他程序员竞争时提高我们的编码游戏并提升我们的能力。
以下是每个 Python 开发人员都应该知道的十个基本 Python 技巧和诀窍:
我们可以简单地使用“”从给定列表的所有元素中创建一个字符串用 join()函数在打印语句里面加上 list 变量。因此,通过这种方式,我们可以很容易地从列表格式给出的多个数据元素中获得单个字符串数据格式。
示例:
# Given data elements in list format
GivenList = ["Hello", "Python", "Developers!", "Welcome", "to", "JavaTpoint"]
# printing single string using "." With join() function
print(" ".join(GivenList))
输出:
Hello Python Developers! Welcome to JavaTpoint
在 Python 中,我们可以简单地使用枚举来检查变量在给定函数中首次出现的次数。我们只需要在带有“.”的 print 语句中使用带有函数名的单词运算符打印该变量在函数中第一次出现的次数。
示例:
# define a class for Enums
class EnumExample:
Hello, Python, Developers, Welcome, to, JavaTpoint, tutorial, of, Python = range(9)
# printing first number of occurrences of the given variable
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
如果我们需要打印我们在程序中导入的 Python 模块的文件目录或路径,那么我们只需要简单地使用 print 语句中的模块名称,文件目录就会在输出中打印出来。
示例:看下面的 Python 程序:
# Importing modules in the program
import socket
import numpy
import os
# Printing the file directory of module imported in program
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'>
我们给出了一个列表,其中有许多元素,并且其中有许多元素被重复了不止一次。现在,如果我们想打印列表中出现最多的元素的数量,这与从给定的数字数据中查找统计模式相同。我们使用 max()和 count 函数来获得发生次数最多的元素的结果。
示例:
# A list with the number of elements in it
GivenList = [24, 21, 27, 29, 17, 23, 29, 34, 67, 23, 21, 29, 19, 63, 29, 27, 35, 21, 29]
# Printing most occurred number or element in list
print("Most occurred element in the given list: ", max(set(GivenList), key = GivenList.count))
输出:
Most occurred element in the given list: 29
我们只需在打印语句中使用“字符串名称* n”语法,就可以在输出中打印给定的字符串“n”次。它将在输出中连续打印给定的字符串 n 次。
示例:
# Define a string and n number
GivenString = "Welcome to JavaTpoint, Python developers!"
n = 4
# Printing string multiple times
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!
我们还可以进行两个变量数字的就地交换,这样我们就可以在程序中使用它们的交换值。
示例:看看下面的 Python 程序:
# Define two number variables
m = 24
n = 26
print("m before swapping: ", m)
print("n before swapping: ", n)
# In-place swapping variables
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
我们可以使用比较运算符链在一次比较中比较给定的变量值和多个值。
示例:
# Defining a number variable
num = 31
# Chaining comparison operators on num variable
Result1 = 35 > num > 30
Result2 = 17 > num < 35
# Printing result of comparison
print(Result1)
print(Result2)
输出:
True
False
有时,我们有一个给定的字符串变量,我们可能不得不打印或使用该字符串的逆序。因此,我们应该知道打印给定字符串的反向格式的最简单方法。
示例:看看下面的 Python 程序:
# Define a string variable
GivenString = "Welcome to JavaTpoint Python Developers!"
print("Given String in program: ", GivenString)
# Printing reverse of string in the output
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
我们只需使用一个 print 语句,就可以从给定的单个函数中打印多个值或元素。在程序中编写多行代码会为我们节省很多时间。
示例:
# Define a default functions
def multival():
return 24, 25, 31, 43, 37, 29, 39, 23
# Defining multiple values from multival() function
j, k, l, m, n, o, p, q = multival()
# Printing multiple values in single statement
print(j, k, l, m, n, o, p, q)
输出:
24 25 31 43 37 29 39 23
异序词是两个不同单词中所有字母都相同的单词,但是单词的字母在单词中的排列顺序不同。我们可以检查给定的两个单词是否是一对异序词。
我们可以通过使用以下两种方法来执行检查异序词的操作:
a .不在程序中导入外部模块:
请看下面的 Python 程序示例:
# A default function to check anagram word logic
def CheckAnagram(mkr1, mkr2):
return sorted(mkr1) == sorted(mkr2) # logic
# Checking anagram words with default function
print("Words are anagrams: ", CheckAnagram('Python', 'yPotnh'))
print("Words are anagrams: ", CheckAnagram('JavaTpoint', 'poijTtavaG'))
输出:
Words are anagrams: True
Words are anagrams: False
b .通过在程序中导入外部模块:
# Importing counter from collection module
from collections import Counter
# A default function to check anagram word logic
def CheckAnagram(mkr1, mkr2):
return Counter(mkr1) == Counter(mkr2) # logic
# Checking anagram words with default function
print("Words are anagrams: ", CheckAnagram('Python', 'yPotnh'))
print("Words are anagrams: ", CheckAnagram('JavaTpoint', 'poijTtavaG'))
输出:
Words are anagrams: True
Words are anagrams: False
原创文章,作者:XK4SX,如若转载,请注明出处:https://www.506064.com/n/126802.html