Here’s a short practice example in Python to help you get started with the language:
- Open a text editor or IDE and create a new file with a
.py
extension, such aspractice.py
. - 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)
- Save the file and run it from the terminal or command prompt by typing
python practice.py
. - The program will prompt you to enter three numbers. Enter any numbers you like and hit enter after each one.
- 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 variablesnum1
,num2
, andnum3
. - 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.