Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this tutorial, I will show you how to print numbers in Python using different techniques, such as simple number printing, printing with formatting, and printing numbers within loops.
You can use the print()
function to print a number directly.
# Print an integer print(42) # Print a float print(3.14159)
You can use formatted string literals (f-strings), introduced in Python 3.6, or the str.format()
method to format and print numbers.
num = 42 pi = 3.14159 # Print numbers with f-strings print(f"The integer is: {num}") print(f"The float is: {pi:.2f}")
str.format()
:num = 42 pi = 3.14159 # Print numbers with str.format() print("The integer is: {}".format(num)) print("The float is: {:.2f}".format(pi))
You can use loops, such as for
or while
loops, to print a sequence of numbers.
for
loop with the range()
function:# Print numbers from 1 to 5 for i in range(1, 6): print(i)
while
loop:# Print numbers from 1 to 5 num = 1 while num <= 5: print(num) num += 1
These examples demonstrate how to print numbers in Python using various techniques, such as simple number printing, printing with formatting, and printing numbers within loops. Choose the appropriate method depending on your specific use case and requirements.
How to display floating-point numbers in Python:
num = 3.14159 print(f"Formatted Float: {num:.2f}")
Printing numbers with print() in Python:
print()
function.num = 42 print("Number:", num)
Print positive and negative numbers in Python:
positive_num = 10 negative_num = -5 print("Positive:", positive_num, "Negative:", negative_num)
Displaying numbers using f-strings in Python:
num = 123 print(f"Formatted Number: {num}")
Printing numbers with format() method in Python:
format()
method.num = 456 print("Formatted Number: {}".format(num))
How to print complex numbers in Python:
complex_num = 2 + 3j print("Complex Number:", complex_num)
Print binary representation of numbers in Python:
num = 8 print("Binary Representation:", bin(num))
Formatting numbers with commas in Python:
large_num = 1000000 print("Formatted Number:", "{:,}".format(large_num))
Print numbers with leading zeros in Python:
num = 7 print("Number with Leading Zeros:", "{:03}".format(num))
Display hexadecimal numbers in Python:
num = 255 print("Hexadecimal Representation:", hex(num))
Python scientific notation print:
num = 0.00005 print("Scientific Notation:", "{:e}".format(num))
Print numbers with a specific precision in Python:
num = 3.14159 print("Formatted Precision:", "{:.3f}".format(num))
Print numbers in exponential form in Python:
num = 1000000 print("Exponential Form:", "{:e}".format(num))
Formatting numbers with locale in Python:
locale
module to format numbers based on the system's locale.import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') num = 1234567.89 print("Formatted with Locale:", locale.format_string("%0.2f", num))
Printing octal numbers in Python:
num = 64 print("Octal Representation:", oct(num))
Print numbers with specific width in Python:
num = 42 print("Formatted Width:", "{:5}".format(num))
How to print large numbers with underscores in Python:
large_num = 100_000_000 print("Large Number with Underscores:", large_num)
Print numbers as Roman numerals in Python:
def int_to_roman(num): val = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ] syb = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] roman_num = '' i = 0 while num > 0: for _ in range(num // val[i]): roman_num += syb[i] num -= val[i] i += 1 return roman_num num = 2023 print("Roman Numeral:", int_to_roman(num))