Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† PYTHON FUNDAMENTALS LEVEL 1 PROFESSIONAL ยท Lesson 7 of 9

Module Six

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
7
Module 6: Python Input and Output Module 6: Python Input and Output

MODULE 6: PYTHON INPUT AND OUTPUT

Course: Python Fundamentals Level 1 Professional
Module: 6 of 8
Level: Beginner

1. Introduction to Python Input and Output

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.

  • Input is information you provide to a computer.
  • Output is information a computer displays or produces.

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.

2. Learning Objectives

By the end of this module, you should be able to:

  • Explain the meaning of input and output.
  • Use the print() function to display information.
  • Use the input() function to collect information from users.
  • Store user input in variables.
  • Understand why input values are usually strings.
  • Convert user input into numbers.
  • Display multiple values in a single output.
  • Build a simple interactive Python program.

3. Understanding Output in Python

Output is the information a program displays after performing an operation.

Python uses the print() function to display output on the screen.

Example 1: Displaying a Message

print("Hello, welcome to Python!")

Output:

Hello, welcome to Python!

The print() function tells Python to display the message inside the brackets.

Example 2: Displaying Numbers

print(100)
print(250)
print(500)

Output:

100
250
500

You can use print() to display numbers without quotation marks.

Example 3: Displaying Variables

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.

4. Displaying Multiple Values

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.

Using Labels in Output

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

5. Understanding Input in Python

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.

Example 1: Asking for a Name

name = input("Enter your name: ")

print("Hello,", name)

Example Interaction:

Enter your name: Tunde
Hello, Tunde

Here is what happens:

  1. Python displays the question.
  2. The user enters their name.
  3. Python stores the answer inside the name variable.
  4. The print() function displays a greeting.

Example 2: Asking for a Favourite Subject

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

6. Storing User Input in Variables

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.

7. Understanding the Data Type of User Input

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.

Important: If you want to perform mathematical calculations with numbers entered by users, you must convert the input into the correct numeric data type.

8. Converting User Input into Numbers

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.

8.1 Using int()

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.

8.2 Using float()

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.

9. Performing Calculations with User Input

After collecting information from users, you can use it to perform calculations.

Example: Adding Two Numbers

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.

Example: Calculating a Student's Average Score

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

10. Using f-Strings to Display Output

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.

Example:

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.

Using f-Strings for Calculations

price = 2000
quantity = 3

total = price * quantity

print(f"The total cost is โ‚ฆ{total}.")

Output:

The total cost is โ‚ฆ6000.

11. Controlling Output with Special Characters

Python provides special characters that help you control how information appears on the screen.

11.1 New Line Character (\n)

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

11.2 Tab Character (\t)

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

11.3 The sep Parameter

The sep parameter controls the separator placed between values in a print() function.

print("Python", "Programming", "Course", sep="-")

Output:

Python-Programming-Course

11.4 The end Parameter

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

12. Handling Invalid Numeric Input

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.

Remember: Input validation helps make programs more reliable and easier for people to use.

13. Practical Project: Student Information System

Project Description

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:

  • Ask the student to enter their full name.
  • Ask for their age.
  • Ask for their school name.
  • Ask for their favourite subject.
  • Ask for their examination score.
  • Display all the information using an organised output.

Sample Solution

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

14. Practical Activities

Activity 1: Personal Greeting Program

Create a Python program that asks users for their names and displays a personalised greeting.

Expected Output: Hello, Tunde! Welcome to Python programming.

Activity 2: Simple Addition Calculator

Write a program that asks users to enter two numbers, adds them together and displays the answer.

Activity 3: Shopping Bill Calculator

Create a program that asks for the price of an item and the quantity purchased. Calculate and display the total cost.

Activity 4: Age Calculator

Ask a user to enter their current age. Display how old they will be in five years.

15. Common Mistakes to Avoid

  • Forgetting quotation marks: Text displayed directly with print() must be placed inside quotation marks.
  • Forgetting parentheses: Write print("Hello"), not print "Hello".
  • Ignoring input data types: Remember that input() returns a string.
  • Adding text to numbers incorrectly: Convert input to an integer or float before performing mathematical calculations.
  • Using invalid numbers: Enter valid numeric values when a program expects a number.
  • Using incorrect variable names: Make sure you use the same variable name when storing and displaying information.

16. Key Terms to Remember

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.

17. Module Summary

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.

18. Knowledge Check

Test your understanding of Module 6 by answering the following questions.

1. What is input in Python?

  1. Information supplied to a program
  2. Information deleted from a program
  3. A computer virus
  4. A programming error

2. Which Python function is used to display information on the screen?

  1. input()
  2. print()
  3. type()
  4. int()

3. Which function collects information from a user?

  1. print()
  2. float()
  3. input()
  4. str()

4. What data type does input() return by default?

  1. Integer
  2. Boolean
  3. Float
  4. String

5. Which function converts suitable input into a whole number?

  1. int()
  2. print()
  3. input()
  4. type()

6. Which function converts suitable input into a decimal number?

  1. str()
  2. float()
  3. print()
  4. input()

7. What is the purpose of an f-string?

  1. To delete variables
  2. To stop a program
  3. To insert variables and expressions into formatted text
  4. To install Python

8. Which special character moves text to a new line?

  1. \t
  2. \n
  3. \b
  4. \r

9. What error can occur when int() tries to convert the word "Hello" into an integer?

  1. SyntaxError
  2. NameError
  3. ValueError
  4. IndexError

10. Which Python structure can handle a ValueError when converting user input?

  1. try and except
  2. print and input
  3. for and while
  4. class and object
Click here to reveal the answers
  1. A - Input is information supplied to a program.
  2. B - print() displays information on the screen.
  3. C - input() collects information from a user.
  4. D - input() returns a string by default.
  5. A - int() converts suitable values into integers.
  6. B - float() converts suitable values into decimal numbers.
  7. C - An f-string inserts variables and expressions into formatted text.
  8. B - \n moves text to a new line.
  9. C - ValueError can occur when a value cannot be converted into the requested type.
  10. A - try and except can handle conversion errors.

19. What Is Next?

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.

Congratulations! You have completed Module 6 of Python Fundamentals Level 1 Professional. Practise the activities and make sure you understand how to collect, convert and display information before moving to the next module.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’