Python Practice Exercises: Essential Techniques for Mastering the Framework

Here’s a short practice example in Python to help you get started with the language:

  1. Open a text editor or IDE and create a new file with a .py extension, such as
  2. Add the following code to the file:
pythonCopy code# This program calculates the average of three numbers

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

average = (num1 + num2 + num3) / 3

print("The average of", num1, num2, "and", num3, "is", average)
  1. Save the file and run it from the terminal or command prompt by typing python
  2. The program will prompt you to enter three numbers. Enter any numbers you like and hit enter after each one.
  3. You should see the average of the three numbers printed to the console.

In this example, we’ve created a simple Python program that calculates the average of three numbers entered by the user. Let’s take a closer look at the code:

  • The first three lines of the code use the input function to prompt the user to enter three numbers, which are stored in the variables num1, num2, and num3.
  • The fourth line of the code calculates the average of the three numbers and stores the result in the variable average.
  • The final line of the code uses the print function to output the result to the console.

This is a simple example, but it demonstrates the basic structure of a Python program and how to use Python to interact with the user and perform simple calculations. As you continue to learn, you’ll discover new concepts and techniques for writing more complex and sophisticated programs.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *