Python practice programs
Python practice programs
I'm happy to provide you with some Python practice programs! Here are a few:
Program 1: Guessing Game
In this program, the user is prompted to guess a random number between 1 and 100. The program will keep track of the user's guesses and let them know if their guess is too high or too low.
import random
number_to_guess = random.randint(1, 100)
guesses = 0
while True:
user_guess = int(input("Guess a number between 1 and 100: "))
guesses += 1
if user_guess == number_to_guess:
print(f" Congratulations! You guessed the number in {guesses} attempts.")
break
elif user_guess > number_to_guess:
print("Too high! Try again.")
else:
print("Too low! Try again.")
Program 2: Hangman
In this program, the user is prompted to guess a random word. For each incorrect guess, a part of a hangman figure will be drawn. If the user guesses the word correctly before the hangman figure is complete, they win.
import random
word_list = ["apple", "banana", "cherry"]
word_to_guess = random.choice(word_list)
guessed_word = "-" * len(word_to_guess)
hangman_figure = ["_______",
"| |",
"| O |",
"| /||",
"| / |"]
guesses = 0
correct_letters = []
while True:
print(hangman_figure[guesses // 5])
print(guessed_word)
user_guess = input("Guess a letter: ")
if len(user_guess) != 1:
print("Please guess one letter at a time.")
continue
if user_guess in correct_letters:
print("You already guessed that letter. Try again!")
continue
if user_guess not in word_to_guess:
guesses += 1
if guesses // 5 == len(hangman_figure) - 1:
print(hangman_figure[-1])
print(f"Game over! The word was {word_to_guess}.")
break
else:
continue
for i in range(len(word_to_guess)):
if word_to_guess[i] == user_guess:
guessed_word = list(guessed_word)
guessed_word[i*2:(i+1)*2] = user_guess
correct_letters.append(user_guess)
if all(letter in guessed_word for letter in word_to_guess):
print(f" Congratulations! You guessed the word {word_to_guess}.")
break
Program 3: Rock, Paper, Scissors
In this program, the user is prompted to play a game of rock, paper, scissors against the computer. The program will generate a random choice and let the user know if they win or lose.
import random
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
user_choice = input("Enter your choice (rock/paper/scissors): ")
if user_choice not in choices:
print("Invalid input. Please try again!")
else:
if user_choice == computer_choice:
print(f"Both players chose {user_choice}. It's a tie!")
elif (user_choice == "rock" and computer_choice == "scissors") or
(user_choice == "paper" and computer_choice == "rock") or
(user_choice == "scissors" and computer_choice == "paper"):
print(f"You chose {user_choice} and the computer chose {computer_choice}. You win!")
else:
print(f"You chose {user_choice} and the computer chose {computer_choice}. The computer wins!")
These are just a few examples of what you can do with Python. I hope they help you get started with your practice programs!
How do I practice coding in Python?
Here's a detailed guide on how to practice coding in Python:
1. Start with the Basics: Before diving into complex projects, make sure you have a solid grasp of the basics. Practice writing simple programs that cover variables, data types, control structures (if-else statements, for loops), functions, and modules.
2. Online Resources: Utilize online resources to hone your skills:
Codecademy: Codecademy offers an interactive Python course with exercises and projects. Python.org: The official Python documentation provides tutorials, examples, and a language reference. W3Schools: W3Schools has a comprehensive Python tutorial covering syntax, variables, functions, and more.3. Practice with Exercises: Websites like:
LeetCode: LeetCode offers a wide range of Python problems, from easy to challenging. HackerRank: HackerRank provides coding challenges in various domains, including Python. Project Euler: Project Euler is a platform for mathematical and computational programming puzzles.4. Real-World Projects: Apply your knowledge to real-world projects:
Web Development: Create a simple web application using Flask or Django. Data Analysis: Work with datasets from sources like Kaggle, UCI Machine Learning Repository, or Google Sheets. Game Development: Use libraries like Pygame or Panda3D to create games.5. Join Online Communities: Engage with other programmers on:
Reddit (r/learnpython): A community dedicated to learning and discussing Python. Stack Overflow: Ask and answer questions related to programming in general. Python Subreddit: Discuss various Python-related topics, share resources, and get help.6. Read Others' Code: Study open-source projects on platforms like:
GitHub: Explore popular Python repositories, such as Requests or Scikit-learn. Bitbucket: Look at code for projects like Scrapy or Pyomo.7. Participate in Hackathons: Join online hackathons or participate in local events to collaborate with others and develop your skills under time pressure.
8. Read Books and Documentation: Supplement your learning with books on Python programming, such as:
"Python Crash Course" by Eric Matthes "Automate the Boring Stuff with Python" by Al Sweigart9. Take Online Courses: Enroll in courses on platforms like:
Coursera: "Python for Everybody" by Charles Severance (University of Michigan) edX: "Introduction to Computer Science using Python" (Harvard University)10. Reflect and Review: Regularly reflect on your progress, identify areas for improvement, and review what you've learned.
By following these steps, you'll be well on your way to mastering the basics of Python programming and developing a solid foundation for more advanced topics. Happy coding!