收藏!長文!從Python小白到大牛,要走的路這裡都有面向項目的學習是學習編碼的最佳方法。Python是當今最需求的語言,為了幫助您學習它,以下是一些您可以探索的最重要的Python項目:Python遊戲Python圖像編程CIFAR10在Python中使用TensorFlow
開始看吧,和從開始到放棄說再見
俗話說的好,沒吃過豬肉還沒見過豬跑?Python雖然對大多數小白來說,可能是從入門到放棄的過程。探究起來,可能初入門的同學沒見到過Python美麗的全景,一直埋頭寫hello world太多了,喪失了對Python的愛才是放棄的主要原因吧。
在本文中,將用真實的代碼給你展示從小白到大牛Python項目之旅。只要你敢看,我就敢寫。開始吧!
只要你敢看,就敢讓你成大牛
您將學習如何按以下順序創建這些Python項目:
Python簡介如何使用Python創建項目?我們可以用Python進行哪些項目?Python初學者項目:使用Python的Hangman遊戲中級Python項目:在Python中數據可視化高級Python項目:使用Python進行機器學習結論下面就開始這次從小白的大牛的Python代碼盛宴。
Python簡介
Python是一種高級的,面向對象的,解釋性的程式語言,已經引起了全世界的關注。Stack Overflow發現其38.8%的用戶主要在其項目中使用Python。Python又名為Guido Van Rossum的開發人員創建。
Python一直很容易學習和掌握。它非常適合初學者,並且語法非常易於閱讀和遵循。這無疑使我們所有人都開心,令人驚奇的是python在全球擁有數百萬快樂的學習者!
根據該網站的調查,Python的流行度在2018年超過了C#–就像2017年超過了PHP。在GitHub平臺上,Python超過了Java,成為第二大使用的程式語言,與2017年相比,Python發出的拉取請求多40%在2016年。
這使 Python認證成為最受歡迎的編程認證之一。
適用於初學者的Python項目| Python專案範例
如何使用Python創建項目?
這個問題的答案非常簡單明了。這一切都始於學習Python的基礎知識和所有基礎知識。基本上,這是一個衡量指標,可以了解您使用Python的舒適程度。
下一步的主要步驟是查看基本的簡單代碼,以熟悉代碼中的語法和邏輯流程。這是非常重要的一步,也為以後的發展奠定了堅實的基礎。
現實生活中的Python?
在此之後,您絕對應該查看python在現實生活中的用途。這將在找出為什麼首先要學習Python的過程中扮演重要角色。
如果不是這種情況,那麼您將了解項目,並可以為項目實施某些策略,您可以考慮自己開始。
其次肯定是要研究可以解決當前Python知識的項目。深入研究Python將有助於您在每個階段進行自我評估。
項目基本上用於解決眼前的問題。如果您喜歡為各種簡單和複雜的問題提供解決方案,那麼您絕對應該考慮從事Python項目。
在完成幾個項目後,您將比精通python更近一步。這很重要,因為您將能夠自發地將所學到的內容簡單地編寫為自己編寫計算器程序,直至幫助實現人工智慧。
我們可以用Python進行哪些項目?
我們可以根據學習者的技能水平將Python項目分為初學者,中級和高級項目。
Python入門級項目
Python子手遊戲與Python使用Pygame的蛇遊戲使用Python的科學計算器使用Python Flask的產品目標網頁使用Python的URL縮短器Python中級項目
使用Python進行網頁爬取探索性數據分析在Python中使用Kivy的Pong遊戲使用Python Flask / Django Web框架的登錄系統鐵達尼號數據的生存預測Python高級項目
使用OpenCV Python進行面罩檢測使用Python進行語音識別使用Python進行文字轉語音Python中的聊天機器人使用Selenium的Web瀏覽器自動化讓我們從檢查Python項目的第一級開始。
Python初學者項目:使用Python的猜詞遊戲
我們可以考慮的最好的初學者項目是Hangman遊戲。我敢肯定,閱讀此Python Projects博客的大多數人在您生命中的某個時間點都曾玩過猜詞。簡單地說,這裡的主要目標是創建一個「猜詞」遊戲。聽起來很簡單,但是您需要注意某些關鍵事項。
用戶需要能夠輸入字母猜測。還應該對可以使用的猜測次數設置一個限制。繼續將剩餘的次數通知用戶。這意味著您將需要一種獲取單詞以進行猜測的方法。讓我們保持簡單,並使用文本文件作為輸入。文本文件包含我們必須猜測的單詞。
您還將需要一些函數來檢查用戶是否實際輸入了單個字母,檢查輸入的字母是否在隱藏的單詞中(如果是,顯示了多少次),列印字母,以及一個計數器變量來限制猜測。
Python基礎起步
Python項目要記住的關鍵概念:
隨機變數布爾型輸入輸出整數串長度列印代碼:
Hangman
from string import ascii_lowercasefrom words import get_random_word def get_num_attempts(): """Get user-inputted number of incorrect attempts for the game.""" while True: num_attempts = input( 'How many incorrect attempts do you want? [1-25] ') try: num_attempts = int(num_attempts) if 1 <= num_attempts <= 25: return num_attempts else: print('{0} is not between 1 and 25'.format(num_attempts)) except ValueError: print('{0} is not an integer between 1 and 25'.format( num_attempts)) def get_min_word_length(): """Get user-inputted minimum word length for the game.""" while True: min_word_length = input( 'What minimum word length do you want? [4-16] ') try: min_word_length = int(min_word_length) if 4 <= min_word_length <= 16: return min_word_length else: print('{0} is not between 4 and 16'.format(min_word_length)) except ValueError: print('{0} is not an integer between 4 and 16'.format( min_word_length)) def get_display_word(word, idxs): """Get the word suitable for display.""" if len(word) != len(idxs): raise ValueError('Word length and indices length are not the same') displayed_word = ''.join( [letter if idxs[i] else '*' for i, letter in enumerate(word)]) return displayed_word.strip() def get_next_letter(remaining_letters): """Get the user-inputted next letter.""" if len(remaining_letters) == 0: raise ValueError('There are no remaining letters') while True: next_letter = input('Choose the next letter: ').lower() if len(next_letter) != 1: print('{0} is not a single character'.format(next_letter)) elif next_letter not in ascii_lowercase: print('{0} is not a letter'.format(next_letter)) elif next_letter not in remaining_letters: print('{0} has been guessed before'.format(next_letter)) else: remaining_letters.remove(next_letter) return next_letter def play_hangman(): """Play a game of hangman. At the end of the game, returns if the player wants to retry. """ # Let player specify difficulty print('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() # Randomly select a word print('Selecting a word...') word = get_random_word(min_word_length) print() # Initialize game state variables idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False # Main game loop while attempts_remaining > 0 and not word_solved: # Print current game state print('Word: {0}'.format(get_display_word(word, idxs))) print('Attempts Remaining: {0}'.format(attempts_remaining)) print('Previous Guesses: {0}'.format(' '.join(wrong_letters))) # Get player's next letter guess next_letter = get_next_letter(remaining_letters) # Check if letter guess is in word if next_letter in word: # Guessed correctly print('{0} is in the word!'.format(next_letter)) # Reveal matching letters for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: # Guessed incorrectly print('{0} is NOT in the word!'.format(next_letter)) # Decrement num of attempts left and append guess to wrong guesses attempts_remaining -= 1 wrong_letters.append(next_letter) # Check if word is completely solved if False not in idxs: word_solved = True print() # The game is over: reveal the word print('The word is {0}'.format(word)) # Notify player of victory or defeat if word_solved: print('Congratulations! You won!') else: print('Try again next time!') # Ask player if he/she wants to try again try_again = input('Would you like to try again? [y/Y] ') return try_again.lower() == 'y' if __name__ == '__main__': while play_hangman(): print()
2. Words.py
"""Function to fetch words.""" import random WORDLIST = 'wordlist.txt' def get_random_word(min_word_length): """Get a random word from the wordlist using no extra memory.""" num_words_processed = 0 curr_word = None with open(WORDLIST, 'r') as f: for word in f: if '(' in word or ')' in word: continue word = word.strip().lower() if len(word) < min_word_length: continue num_words_processed += 1 if random.randint(1, num_words_processed) == 1: curr_word = word return curr_word輸出如下:
後續
目前,了解了如何處理諸如Hangman之類的初學者項目,對其進行一些增強,然後開始下一個中級Python項目。
收藏!從Python小白到大牛,要走的路這裡都有(中級篇)
收藏!從Python小白到大牛,要走的路這裡都有(高級篇)