The print() function is not limited to displaying text. It can also display numbers such as integers and decimal numbers.
Unlike text, numbers are written without quotation marks. Python recognizes them as numeric values and displays them directly.
Output
When Python sees a number without quotes, it treats it as a numeric data type instead of text.
Note
Numbers should not be enclosed in quotation marks unless you want Python to treat them as text.
Printing Integer Numbers
An integer is a whole number that does not contain a decimal point. Integers can be positive, negative, or zero.
Output
Integers are commonly used to represent values such as age, marks, quantity, or the number of students.
Real World Scenario
A shopping application may store the number of items in a cart as an integer because you cannot have half an item in your cart.
Printing Decimal Numbers
A floating point number, often called a float, is a number that contains a decimal point.
Output
Decimal numbers are useful when working with values such as prices, temperatures, distances, or percentages.
Performing Calculations Inside print()
The print() function can display the result of mathematical expressions.
Python first evaluates the expression and then prints the final result.
Addition
Output
Subtraction
Output
Multiplication
Output
Division
Output
Modulus
The modulus operator (%) returns the remainder after division.
Output
Exponent
The exponent operator (**) calculates the power of a number.
Output
Pro Tip
The print() function can display the result of any valid mathematical expression. You do not need to calculate the answer yourself.
Printing Variables That Store Numbers
Variables can store numeric values, and the print() function displays the value stored inside them.
Output
Variables make your programs flexible because the value can change without modifying every print() statement.
Printing Text and Numbers Together
You can display text and numbers together by separating them with commas.
Output
You can also display multiple values in a single statement.
Output
Python automatically inserts a space between each value separated by commas.
Real World Scenario
An online examination system may display output like "Student: Priya Score: 87" by combining text and variables in a single print() statement.
Numbers Inside Quotes
Numbers written inside quotation marks are treated as text instead of numeric values.
Output
Although the output looks the same, Python internally treats "100" as a string, not as a number.
Compare the following examples.
Output
Output
In the second example, Python joins the two strings instead of performing addition.
Caution
Always remove quotation marks when you want to perform mathematical calculations.
Common Mistakes
Writing Numbers Inside Quotes
Incorrect code
Output
Correct code
Output
Forgetting Commas While Printing Text and Numbers
Incorrect code
Correct code
Using Undefined Variables
Incorrect code
Output
Always create a variable before printing it.
Stop
If Python cannot find a variable, it immediately raises a NameError.
Best Practices
Write numbers without quotation marks when they are used for calculations.
Use meaningful variable names such as age, marks, or salary.
Combine text and variables to create readable output.
Avoid hardcoding values repeatedly. Store them in variables whenever possible.
Use mathematical expressions inside print() whenever you need quick calculations.
Great Job
You have learned how to print integers, decimal numbers, mathematical expressions, variables, and mixed text with numbers using Python's print() function.
Exercise
1.Print the numbers 100, 250, and 500.
2.Print the result of 45 + 55.
3.Print the result of 18 × 6.
4.Create a variable named age with the value 25 and print it.
5.Create a variable named marks with the value 96 and print "Marks:" followed by the variable.
6.Print the result of 100 ÷ 4.
7.Print your name and age in a single print() statement.
Challenge
Write a Python program that displays the following output.