Imagine you are using an ATM. The ATM asks you to enter your PIN. You type your PIN, and the machine displays information on the screen.
This is an example of input and output.
Python allows you to create programs that receive information from users and display useful results.
For example, you can build a program that asks a student for their name and then displays a welcome message.
name = input("What is your name? ")
print("Welcome to Python,", name)
If the user enters Ada, the program displays:
What is your name? Ada
Welcome to Python, Ada
In this module, you will learn how to use Python's input() and print() functions to create interactive programs.
By the end of this module, you should be able to:
print() function to display information.input() function to collect information from users.Output is the information a program displays after performing an operation.
Python uses the print() function to display output on the screen.
print("Hello, welcome to Python!")
Output:
Hello, welcome to Python!
The print() function tells Python to display the message inside the brackets.
print(100)
print(250)
print(500)
Output:
100
250
500
You can use print() to display numbers without quotation marks.
You can also display information stored inside variables.
student_name = "Tunde"
student_age = 14
print(student_name)
print(student_age)
Output:
Tunde
14
Python displays the values stored in the variables.
You can display more than one value using a single print() function.
name = "Amina"
age = 13
school = "Bright Future School"
print(name, age, school)
Output:
Amina 13 Bright Future School
Python automatically places spaces between the values when you separate them with commas.
Labels make your output easier to understand.
name = "Amina"
age = 13
school = "Bright Future School"
print("Student Name:", name)
print("Age:", age)
print("School:", school)
Output:
Student Name: Amina
Age: 13
School: Bright Future School
Input is information supplied to a program by a user.
Python uses the input() function to collect information from users through the keyboard or terminal.
name = input("Enter your name: ")
print("Hello,", name)
Example Interaction:
Enter your name: Tunde
Hello, Tunde
Here is what happens:
name variable.print() function displays a greeting.subject = input("What is your favourite subject? ")
print("Your favourite subject is", subject)
Example Interaction:
What is your favourite subject? Mathematics
Your favourite subject is Mathematics
When a user enters information, Python can store it in a variable for later use.
For example:
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
print("Full Name:", first_name, last_name)
Example Interaction:
Enter your first name: Ada
Enter your last name: Okafor
Full Name: Ada Okafor
This program collects two pieces of information and uses them to display the user's full name.
One important thing to understand is that Python's input() function returns the information as a string.
A string is a type of data used to store text.
For example:
age = input("Enter your age: ")
print(type(age))
Example Interaction:
Enter your age: 15
<class 'str'>
Although the user entered the number 15, Python stores it as text because it came from the input() function.
Python provides conversion functions that allow you to change input from one data type to another.
The two common conversion functions are:
int() converts suitable input into an integer.float() converts suitable input into a decimal number.Use int() when you need a whole number.
age = int(input("Enter your age: "))
print("Next year, you will be", age + 1)
Example Interaction:
Enter your age: 14
Next year, you will be 15
The int() function converts the entered value into an integer so Python can add 1 to it.
Use float() when you need a decimal number.
price = float(input("Enter the price of an item: "))
print("Item price:", price)
Example Interaction:
Enter the price of an item: 2500.50
Item price: 2500.5
Python stores the price as a floating-point number.
After collecting information from users, you can use it to perform calculations.
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
total = number1 + number2
print("The total is:", total)
Example Interaction:
Enter the first number: 20
Enter the second number: 30
The total is: 50
The program collects two numbers, adds them together and displays the result.
math_score = float(input("Enter Mathematics score: "))
english_score = float(input("Enter English score: "))
science_score = float(input("Enter Science score: "))
average = (math_score + english_score + science_score) / 3
print("Average Score:", average)
Example Interaction:
Enter Mathematics score: 80
Enter English score: 70
Enter Science score: 90
Average Score: 80.0
Sometimes, you may want to combine text and variables in a clear and organised way.
Python provides a feature called an f-string to make this easier.
An f-string starts with the letter f before the quotation mark. Variables are placed inside curly brackets.
name = "Amina"
age = 13
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Amina and I am 13 years old.
price = 2000
quantity = 3
total = price * quantity
print(f"The total cost is โฆ{total}.")
Output:
The total cost is โฆ6000.
Python provides special characters that help you control how information appears on the screen.
The \n character moves the next text to a new line.
print("Welcome to Python\nLearn programming today")
Output:
Welcome to Python
Learn programming today
The \t character adds a tab space between pieces of information.
print("Name\tAge")
print("Amina\t13")
print("Tunde\t14")
Output:
Name Age
Amina 13
Tunde 14
The sep parameter controls the separator placed between values in a print() function.
print("Python", "Programming", "Course", sep="-")
Output:
Python-Programming-Course
By default, Python moves to a new line after a print() statement. The end parameter allows you to change this behaviour.
print("Hello", end=" ")
print("Amina")
Output:
Hello Amina
Sometimes, a user may enter text when your program expects a number.
For example, a program may ask for an age, but the user enters the word "Hello". Python cannot convert that text into an integer using int().
This causes a ValueError.
You can handle this problem using try and except.
try:
age = int(input("Enter your age: "))
print("Your age is", age)
except ValueError:
print("Please enter a valid whole number.")
If the user enters a valid whole number, the program displays it. If the user enters something that cannot be converted into an integer, the error message is displayed instead.
You are creating a simple student information program for a school. The program should ask a student to enter personal and academic information and then display the details.
Project Requirements:
name = input("Enter your full name: ")
age = int(input("Enter your age: "))
school = input("Enter your school name: ")
subject = input("Enter your favourite subject: ")
score = float(input("Enter your examination score: "))
print("\n--- STUDENT INFORMATION ---")
print(f"Name: {name}")
print(f"Age: {age}")
print(f"School: {school}")
print(f"Favourite Subject: {subject}")
print(f"Examination Score: {score}")
Example Interaction:
Enter your full name: Amina Yusuf
Enter your age: 13
Enter your school name: Bright Future School
Enter your favourite subject: Mathematics
Enter your examination score: 85
--- STUDENT INFORMATION ---
Name: Amina Yusuf
Age: 13
School: Bright Future School
Favourite Subject: Mathematics
Examination Score: 85.0
Create a Python program that asks users for their names and displays a personalised greeting.
Expected Output: Hello, Tunde! Welcome to Python programming.
Write a program that asks users to enter two numbers, adds them together and displays the answer.
Create a program that asks for the price of an item and the quantity purchased. Calculate and display the total cost.
Ask a user to enter their current age. Display how old they will be in five years.
print() must be placed inside quotation marks.print("Hello"), not print "Hello".input() returns a string.| Term | Meaning |
|---|---|
| Input | Information supplied to a computer or program. |
| Output | Information displayed or produced by a program. |
| print() | A Python function used to display information. |
| input() | A Python function used to collect information from users. |
| String | A data type used to store text. |
| int() | A function used to convert suitable values into integers. |
| float() | A function used to convert suitable values into decimal numbers. |
| f-string | A way of inserting variables and expressions into formatted text. |
| ValueError | An error that occurs when a value has an inappropriate format for an operation. |
| Validation | The process of checking whether information entered by a user is acceptable. |
In this module, you learned how Python programs receive information and display results.
You explored the input() function for collecting information and the print() function for displaying output.
You also learned how to store user input in variables, convert strings into numbers, perform calculations, use f-strings and organise output using special characters.
These skills are important because they allow you to build interactive programs that respond to information supplied by users.
Test your understanding of Module 6 by answering the following questions.
1. What is input in Python?
2. Which Python function is used to display information on the screen?
3. Which function collects information from a user?
4. What data type does input() return by default?
5. Which function converts suitable input into a whole number?
6. Which function converts suitable input into a decimal number?
7. What is the purpose of an f-string?
8. Which special character moves text to a new line?
9. What error can occur when int() tries to convert the word "Hello" into an integer?
10. Which Python structure can handle a ValueError when converting user input?
Module 7: Python Conditional Statements
In the next module, you will learn how to make Python programs take decisions.
You will explore if, elif and else statements and learn how to build programs that respond differently depending on the information provided.
For example, you will learn how to create a program that checks whether a student passed or failed an examination based on their score.