Your first Python program: input to output.
A variable is a name that refers to a value. A simple Python program can collect values, calculate a result and display an answer.
Read a name
name = input("What is your name? ")
print("Hello,", name)input() returns text. The variable name holds that text, and print() displays the greeting.
Calculate an area
length = float(input("Length in metres: "))
width = float(input("Width in metres: "))
area = length * width
print("Area:", area, "square metres")float() converts a valid numerical string into a number that can include a decimal part. Multiplication uses the * symbol.
Think about invalid input
This small example assumes the user enters valid, positive numbers. Text such as “five” will cause a conversion error. A production program needs input validation and a helpful way to retry.
Use meaningful names
Prefer total_score over x when the variable represents a total score. Python names are case-sensitive, so score and Score are different names.
Practice
- Run the area example with length 5 and width 4. The result should be 20.
- Change it to calculate perimeter using
2 * (length + width). - Predict the result before running it.
Keep exploring
More practical guides ↗These introductory guides support learning and do not replace an institution’s approved syllabus.
