对于所有Python开发人员或任何其他语言开发人员来说,了解我们正在学习的编程语言的技巧和诀窍总是令人着迷的。众所周知,Python是开发人员中最受欢迎的编程语言之一。因此,在本教程中,我们将介绍每个Python开发人员都应该知道的Python的基本技巧和诀窍。

Python的10个基本技巧和诀窍

我们将在这里讨论的Python的技巧或诀窍将帮助我们节省很多代码行,并节省大量时间。这些Python技巧是诀窍,也将帮助我们提高我们的编程水平,使我们在与其他程序员竞争时更有竞争力。

以下是每个Python开发人员都应该知道的十个基本Python技巧和诀窍的列表:

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

我们可以通过在print语句中使用列表变量的join()函数和"."来从给定列表的所有元素创建单个字符串。因此,我们可以轻松地从列表格式中的多个数据元素获取单个字符串数据格式。

示例:

# Given data elements in list format  
GivenList = ["Hello", "Python", "Developers!", "Welcome", "to", "javatiku"]   
# printing single string using "." With join() function  
print(" ".join(GivenList))   

输出:

Hello Python Developers! Welcome to javatiku

2. Python使用枚举:

在Python中,我们可以使用枚举来检查变量在首次出现的函数中出现的次数。我们只需在print语句中使用函数名称和函数名来打印该变量的第一次出现的次数。

示例:

# define a class for Enums  
class EnumExample:   
    Hello, Python, Developers, Welcome, to, javatiku, tutorial, of, Python = range(9)  
# printing first number of occurrences of the given variable  
print("Occurrence of javatiku: ", EnumExample.javatiku)   
print("Occurrence of Hello: ", EnumExample.Hello)   
print("Occurrence of Python: ", EnumExample.Python)  
print("Occurrence of Welcome: ", EnumExample.Welcome)  

输出:

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

3. 打印导入模块的路径:

如果我们需要在程序中打印Python模块的文件目录或路径,那么我们只需简单地使用模块名称即可,它将在输出中打印文件目录。

示例: 查看以下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'>

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

我们给出了一个包含多个元素的列表,并且其中有许多元素重复多次。现在,如果我们想打印在列表中出现最多的元素的数量,并且它与统计数据中的找到模式的数据相同,我们可以使用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

5. 打印给定字符串多次:

我们可以通过在print语句中使用'String Name * n'语法来在输出中简单地多次打印给定字符串'n'次。它将连续n次打印给定的字符串。

示例:

# Define a string and n number  
GivenString = "Welcome to javatiku, 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 javatiku, Python developers!Welcome to javatiku, Python developers!Welcome to javatiku, Python developers!Welcome to javatiku, Python developers!

6. 两个变量数字的原地交换:

我们还可以原地交换两个变量数字,以便在程序中使用它们的交换值。

示例: 查看以下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

7. 使用比较运算符的链:

我们可以使用比较运算符链来在单个比较中将给定变量与多个值进行比较。

示例:

# 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

8. 打印给定字符串的反向:

有时,我们有一个给定的字符串变量,我们可能需要打印或使用该字符串的反向顺序。因此,我们应该知道打印给定字符串的反向格式的最简单方法。

示例: 查看以下Python程序:

# Define a string variable  
GivenString = "Welcome to javatiku 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 javatiku Python Developers!
Reverse of Given string in program is: !srepoleveD nohtyP tniopTavaJ to emocleW

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

我们可以使用一个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

10. 检查字谜单词:

字谜单词是指两个不同单词中所有字母相同,但单词的字母以不同的顺序排列在单词中。我们可以检查给定的两个单词是否是字谜单词对。

我们可以执行检查字谜单词的操作,使用以下两种方法之一:

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('javatiku', '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('javatiku', 'poijTtavaG'))  

输出:

Words are anagrams:  True
Words are anagrams:  False

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程