收藏!长文!从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_lowercase
from 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项目。