Python exercises for beginners with solutions

Venus 169 Published: 12/05/2024

Python exercises for beginners with solutions

I'm happy to provide you with some Python exercises for beginners along with their solutions. Here are a few:

Exercise 1: Printing Hello World

Task: Write a Python program that prints "Hello, World!" on the screen.

Solution:

print("Hello, World!")

Exercise 2: Adding Numbers

Task: Ask the user to input two numbers and then add them together. Display the result.

Solution:

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))

result = num1 + num2

print(f"The sum is: {result}")

Exercise 3: Calculating Average

Task: Ask the user to input three numbers and then calculate their average.

Solution:

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

num3 = float(input("Enter third number: "))

average = (num1 + num2 + num3) / 3

print(f"The average is: {average}")

Exercise 4: Checking if a Number is Odd or Even

Task: Ask the user to input a number and then check if it's odd or even.

Solution:

num = int(input("Enter a number: "))

if num % 2 == 0:

print(f"{num} is an even number.")

else:

print(f"{num} is an odd number.")

Exercise 5: Counting Characters

Task: Ask the user to input a sentence and then count the number of characters (letters, digits, or special characters) in the sentence.

Solution:

sentence = input("Enter a sentence: ")

count = len(sentence)

print(f"The total character count is: {count}")

Exercise 6: Calculating Perimeter

Task: Ask the user to input the length and width of a rectangle, then calculate its perimeter.

Solution:

length = float(input("Enter the length: "))

width = float(input("Enter the width: "))

perimeter = 2 * (length + width)

print(f"The perimeter is: {perimeter:.2f}")

Exercise 7: Playing with Strings

Task: Ask the user to input a sentence, then print out the first letter of each word.

Solution:

sentence = input("Enter a sentence: ")

words = sentence.split()

for i in range(len(words)):

print(f"Word {i+1}: {words[i][0]}")

These exercises cover basic concepts like variables, data types, user input, control structures (if-else), functions, and string manipulation. I hope this helps you get started with Python programming!

How do people practice Python?

People can practice Python in a variety of ways to improve their coding skills and become proficient in the language. Here are some common methods:

Coding Challenges: Websites like LeetCode, HackerRank, Codewars, and Codeforces offer a range of challenges that test various aspects of programming, including data structures, algorithms, and problem-solving.

Personal Projects: Working on personal projects is an excellent way to apply Python skills to real-world scenarios. This can include building games, simulating systems, or even automating tasks at home or work. Online Courses: Online platforms like Coursera, edX, and Udemy offer a wide range of courses covering various aspects of Python programming. These courses often come with assignments and quizzes to test understanding. Practice Exercises: Websites like Project Euler, Rosalind, and Pythagoras provide exercises that focus on specific areas, such as number theory, graph algorithms, or string manipulation. Books and Tutorials: There are numerous books and tutorials available online that cover Python basics, intermediate topics, and advanced concepts. These resources often come with examples and exercises to work through. Pair Programming: Working with a partner can be an effective way to learn and improve skills. This can involve finding a coding buddy or joining online communities like GitHub or Reddit's r/learnpython. Code Review: Participating in code review sessions with peers or experienced programmers can help identify areas for improvement, receive feedback on coding style, and learn best practices. Participating in Coding Communities: Joining online forums like Stack Overflow, Reddit's r/learnpython, or Python subreddit, and participating in discussions and sharing knowledge with others is an excellent way to stay motivated and inspired. Real-World Applications: Applying Python skills to real-world projects can be a fantastic way to learn and improve. This might involve working on personal projects, contributing to open-source projects, or even taking on freelance work. Code Golfing: Code golfing involves writing the shortest possible code that solves a given problem. This exercise helps improve coding efficiency, readability, and creativity.

By practicing these methods, individuals can develop their Python skills, build confidence in their abilities, and become proficient programmers.

Additional Tips:

Consistency is key: Set aside time each week to practice, even if it's just for 30 minutes. Consistency helps build habits and improve learning. Focus on understanding: Don't just memorize code snippets or try to solve problems quickly. Instead, focus on understanding the underlying concepts and principles. Learn by doing: Practice is the best way to learn Python (or any programming language). Try solving real-world problems or building projects that interest you. Don't be afraid of mistakes: Everyone makes mistakes when learning a new language. The key is to learn from those errors and move forward.

Remember, practice is essential to mastering Python. With persistence and dedication, you can improve your coding skills and become proficient in this popular programming language.