對於所有 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/zh-hant/n/126802.html