Simple Example Python Programs for Practice [Beginners ]

Here are some simple Python programs for practice:

  1. Hello, World!:
print("Hello, World!")Code language: Python (python)
  1. Simple Calculator:
def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y != 0:
        return x / y
    else:
        return "Cannot divide by zero."

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

print("Addition:", add(num1, num2))
print("Subtraction:", subtract(num1, num2))
print("Multiplication:", multiply(num1, num2))
print("Division:", divide(num1, num2))Code language: Python (python)
  1. Guess the Number:
import random

secret_number = random.randint(1, 100)
attempts = 0

while True:
    guess = int(input("Guess the number (1-100): "))
    attempts += 1

    if guess == secret_number:
        print(f"Congratulations! You guessed the number in {attempts} attempts.")
        break
    elif guess < secret_number:
        print("Try a higher number.")
    else:
        print("Try a lower number.")Code language: Python (python)
  1. Simple To-Do List:
todo_list = []

while True:
    print("1. Add task")
    print("2. View tasks")
    print("3. Quit")
    
    choice = int(input("Enter your choice: "))

    if choice == 1:
        task = input("Enter the task: ")
        todo_list.append(task)
        print("Task added!")
    elif choice == 2:
        print("Tasks:")
        for index, task in enumerate(todo_list, start=1):
            print(f"{index}. {task}")
    elif choice == 3:
        print("Goodbye!")
        break
    else:
        print("Invalid choice. Please try again.")Code language: Python (python)
  1. Check Prime Number:
def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            return False
    return True

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

if is_prime(number):
    print(f"{number} is a prime number.")
else:
    print(f"{number} is not a prime number.")Code language: Python (python)

These simple Python programs cover various concepts like basic I/O, conditionals, loops, functions, and list manipulation. They should provide a good starting point for practice. Feel free to modify them or add more features to make them more interesting and challenging!

How do I practice Python programs?


Practicing Python programs is a great way to improve your programming skills. Here are some effective steps and tips to practice Python programs:

  1. Understand the Basics: Ensure you have a good understanding of Python’s fundamentals, such as variables, data types, conditionals, loops, functions, and basic I/O (input/output).
  2. Start with Simple Programs: Begin with small and straightforward programs. For example, programs that perform basic arithmetic operations, print patterns, or manipulate lists.
  3. Online Coding Platforms: Use online coding platforms like LeetCode, HackerRank, or Codeforces to access a wide range of coding challenges and exercises. These platforms often have problems categorized by difficulty, making it easier to gradually increase the complexity.
  4. Solve Real-World Problems: Try solving real-world problems using Python. This could involve automating repetitive tasks, processing data files, or building small utility scripts.
  5. Work on Projects: Take up small projects that interest you. Projects could be creating a web scraper, building a calculator, developing a simple game, or designing a basic website. Working on projects helps you apply Python to practical scenarios and reinforces your learning.
  6. Read Code: Study and analyze code written by other developers. You can find code on open-source platforms like GitHub. Reading and understanding different coding styles and techniques will broaden your understanding of Python.
  7. Practice Regularly: Consistency is key. Set aside dedicated time for practicing Python regularly. Even a few minutes each day can be more beneficial than infrequent long sessions.
  8. Debugging Skills: Practice debugging your programs. Learning how to find and fix errors is crucial for becoming a proficient programmer.
  9. Optimize Your Solutions: After solving a problem, try optimizing your code for efficiency. This could involve reducing time complexity, using built-in Python functions, or employing better algorithms.
  10. Collaborate and Discuss: Join online coding communities or forums where you can collaborate with other programmers and discuss coding challenges. This will expose you to different perspectives and approaches.
  11. Read Python Documentation: Familiarize yourself with Python’s official documentation. It contains comprehensive information on Python’s standard library and language features.
  12. Learn from Tutorials and Courses: There are numerous online tutorials and courses that cater to learners of all levels. Utilize these resources to gain deeper insights into Python programming.
  13. Practice Code Reviews: If possible, participate in or initiate code reviews with your peers. Reviewing and discussing each other’s code helps identify potential improvements and promotes better coding practices.
  14. Set Goals: Set specific programming goals for yourself. For example, solve a certain number of problems per week or complete a project within a specific timeframe.
  15. Stay Curious and Persistent: Programming requires patience and a curious mindset. Don’t get discouraged by challenges, and keep exploring new concepts and techniques.

Remember, the key to becoming proficient in Python programming is practice and consistency. Start with simple programs and gradually tackle more complex challenges as you gain confidence. Happy coding!

Read More;

  • Yaryna Ostapchuk

    I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

    View all posts

Leave a Comment