最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

20個解決日常編程問題的Python代碼分享

 更新時間:2023年01月21日 08:46:40   作者:快學Python  
在這篇文章中,主要和大家分享了20個Python代碼片段,以幫助你應對日常編程挑戰(zhàn)。文中的示例代碼講解詳細,感興趣的小伙伴可以跟上小編一起了解一下

使用這些有用的 Python 代碼片段提升你的編程技能,在本文中,我將分享 20 個 Python 代碼片段,以幫助你應對日常編程挑戰(zhàn),你可能已經(jīng)知道其中一些片段,但其他片段對你來說,有可能是新的。我們現(xiàn)在開始吧。

1. 簡單的 HTTP Web 服務器

# Simple HTTP SERVER
import socketserver
import http.server
PORT = 8000
handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), handler) as http:
    print("Server Launch at Localhost: " + str(PORT))
    http.serve_forever()
# Type in http://127.0.0.1:8000/ in your webbrowser

2.單行循環(huán)List

# 單行循環(huán)List
mylist = [10, 11, 12, 13, 14]
print([i * 2 for i in mylist]) # [20, 22, 24, 26, 28]
print([i * 5 for i in mylist]) # [50, 55, 60, 65, 70]

Output:

3.更新字典

# Update Dictionary
mydict = {1: "Python", 2: "JavaScript", 3: "Csharp"}
mydict.update({4: "Dart"})
print(mydict) # {1: 'Python', 2: 'JavaScript', 3: 'Csharp', 4: 'Dart'}

4.拆分多行字符串

# Split Multi Lines String
string = "Data \n is encrpted \n by Python"
print(string)

splited = string.split("\n")
print(splited)

Output:

5. 跟蹤列表中元素的頻率

# Track Frequency 
import collections
def Track_Frequency(List):
    return dict(collections.Counter(List))
print(Track_Frequency([10, 10, 12, 12, 10, 13, 13, 14]))

Output:

6. 不使用 Pandas 讀取 CSV 文件

# Simple Class Creation
import csv
with open("Test.csv", "r") as file:
    read = csv.reader(f)
    for r in read:
        print(row)
# Output
# ['Sr', 'Name', 'Profession']
# ['1', 'Haider Imtiaz', 'Back End Developer']
# ['2', 'Tadashi Wong', 'Software Engineer']

7. 將列表壓縮成一個字符串

# Squash list of String
mylist = ["I learn", "Python", "JavaScript", "Dart"]
string = " ".join(mylist)
print(string) # I learn Python JavaScript Dart

8. 獲取列表中元素的索引

# 獲取列表中元素的索引
mylist = [10, 11, 12, 13, 14]
print(mylist.index(10))
print(mylist.index(12))
print(mylist.index(14))

運行結果:

9. Magic of *arg

# Magic of *arg
def func(*arg):
    num = 0
    for x in arg:
        num = num + x
    print(num) # 600
func(100, 200, 300)

運行結果:

10. 獲取任何數(shù)據(jù)的類型

# Get Type of Any Data
data1 = 123
data2 = "Py"
data3 = 123.443
data4 = True
data5 = [1, 2]
print(type(data1)) # <class 'int'>
print(type(data2)) # <class 'str'>
print(type(data3)) # <class 'float'>
print(type(data4)) # <class 'bool'>
print(type(data5)) # <class 'list'>

11.修改打印功能

# 修改打印功能
print("Top Programming Languages are %r, %r and %r" % ('Py', 'Js', 'C#'))
# Output
# Top Programming Languages are 'Py', 'Js' and 'C#'

12. 字符串去大寫

# 字符串去大寫
data1 = "KuaiXue"
data2 = "Python"
data3 = "Kx Python"
print(data1.lower())
print(data2.lower()) 
print(data3.lower())

運行結果:

13. 更快捷的變量交換方式

# Quick Way to Exchange Variables
d1 = 25
d2 = 50
d1, d2 = d2, d1
print(d1, d2) # 50 25

14. 分色打印

# Print with Seperation
print("Py", "Js", "C#", sep="-") # Py-Js-C#
print("100", "200", "300", sep="x") # 100x200x300

15. 獲取網(wǎng)頁 HTML 數(shù)據(jù)

# First Install Request with pip install requests
import requests
r = requests.get("https://www.baidu.com/")
print(r)

運行結果:

16. 獲取數(shù)據(jù)占用的內(nèi)存

# Get Memory taken by data
import sys
def memory(data):
    return sys.getsizeof(data)
print(memory(100)) # 28
print(memory("Pythonnnnnnn")) # 61

17. 簡單的類創(chuàng)建

# Simple Class Creation
class Employee:
    def __init__(self, empID):
        self.empID = empID
        self.name = "Haider"
        self.salary = 50000

    def getEmpData(self):
        return self.name, self.salary
emp = Employee(189345)
print(emp.getEmpData()) # ('Haider', 50000)

18. 字符串乘法器

# String Multiplier
# Normal way 
for x in range(5):
    print("C#")

# Good way
print("C# "*5) # C# C# C# C# C#

19.進行鏈式比較

# Chain Comparison
a = 5
print(1 == a < 2) # False
print(2 < 3 < 6 > a) # True

20. 數(shù)字化整數(shù)值

# Digitizing
integer = 234553
digitz = [int(i) for i in str(integer)]
print(digitz) # [2, 3, 4, 5, 5, 3]

到此這篇關于20個解決日常編程問題的Python代碼分享的文章就介紹到這了,更多相關Python解決編程問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Django REST framework 視圖和路由詳解

    Django REST framework 視圖和路由詳解

    這篇文章主要介紹了Django REST framework 視圖和路由詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • 在Pytorch中使用樣本權重(sample_weight)的正確方法

    在Pytorch中使用樣本權重(sample_weight)的正確方法

    今天小編就為大家分享一篇在Pytorch中使用樣本權重(sample_weight)的正確方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Keras實現(xiàn)DenseNet結構操作

    Keras實現(xiàn)DenseNet結構操作

    這篇文章主要介紹了Keras實現(xiàn)DenseNet結構操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 使用Python編寫一個Lisp語言的解釋器

    使用Python編寫一個Lisp語言的解釋器

    這篇文章主要為大家詳細介紹了如何使用Python編寫一個簡單的Lisp語言的解釋器,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • 獲取Django項目的全部url方法詳解

    獲取Django項目的全部url方法詳解

    這篇文章主要介紹了獲取Django項目的全部url方法詳解,小編覺得挺不錯的,這里分享給大家,供需要的朋友參考。
    2017-10-10
  • 淺談matplotlib中FigureCanvasXAgg的用法

    淺談matplotlib中FigureCanvasXAgg的用法

    這篇文章主要介紹了淺談matplotlib中FigureCanvasXAgg的用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 16中Python機器學習類別特征處理方法總結

    16中Python機器學習類別特征處理方法總結

    類別型特征(categorical?feature)主要是指職業(yè),血型等在有限類別內(nèi)取值的特征。在這篇文章中,小編將給大家分享一下16種類別特征處理方法,需要的可以參考一下
    2022-09-09
  • 關于python環(huán)境變量如何配置

    關于python環(huán)境變量如何配置

    這篇文章主要介紹了關于python環(huán)境變量如何配置,當我們在自己電腦上同時安裝了python2.x和python3.x版本的解釋器的時候,就需要對環(huán)境變量的配置進行一定的修改,需要的朋友可以參考下
    2023-04-04
  • 詳解Python如何根據(jù)給定模型計算權值

    詳解Python如何根據(jù)給定模型計算權值

    這篇文章將通過一個簡單的例子,為大家展示Python如何根據(jù)給定的模型結構來計算和提取權值,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-11-11
  • Python的collections模塊中的OrderedDict有序字典

    Python的collections模塊中的OrderedDict有序字典

    字典是無序的,但是collections的OrderedDict類為我們提供了一個有序的字典結構,名副其實的Ordered+Dict,下面通過兩個例子來簡單了解下Python的collections模塊中的OrderedDict有序字典:
    2016-07-07

最新評論

罗平县| 若尔盖县| 兴义市| 阿荣旗| 育儿| 清丰县| 舟曲县| 全椒县| 威海市| 舒城县| 湖州市| 石台县| 策勒县| 神农架林区| 太湖县| 德化县| 静乐县| 乐至县| 阳谷县| 鸡西市| 时尚| 宜城市| 金堂县| 阳城县| 姚安县| 武冈市| 乾安县| 周至县| 南漳县| 子洲县| 大丰市| 大理市| 游戏| 金华市| 西华县| 北宁市| 上饶县| 和田市| 张北县| 高淳县| 桂东县|