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

Number print in Python

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.

  • Simple number printing:

You can use the print() function to print a number directly.

# Print an integer
print(42)

# Print a float
print(3.14159)
  • Printing numbers with formatting:

You can use formatted string literals (f-strings), introduced in Python 3.6, or the str.format() method to format and print numbers.

  • Using f-strings:
num = 42
pi = 3.14159

# Print numbers with f-strings
print(f"The integer is: {num}")
print(f"The float is: {pi:.2f}")
  • Using 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))
  • Printing numbers within loops:

You can use loops, such as for or while loops, to print a sequence of numbers.

  • Using a for loop with the range() function:
# Print numbers from 1 to 5
for i in range(1, 6):
    print(i)
  • Using a 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.

  1. How to display floating-point numbers in Python:

    • Description: Displaying floating-point numbers with a specified precision.
    • Code:
      num = 3.14159
      print(f"Formatted Float: {num:.2f}")
      
  2. Printing numbers with print() in Python:

    • Description: Basic printing of integers or floating-point numbers using the print() function.
    • Code:
      num = 42
      print("Number:", num)
      
  3. Print positive and negative numbers in Python:

    • Description: Printing positive and negative numbers.
    • Code:
      positive_num = 10
      negative_num = -5
      print("Positive:", positive_num, "Negative:", negative_num)
      
  4. Displaying numbers using f-strings in Python:

    • Description: Using f-strings to format and print numbers.
    • Code:
      num = 123
      print(f"Formatted Number: {num}")
      
  5. Printing numbers with format() method in Python:

    • Description: Formatting numbers using the format() method.
    • Code:
      num = 456
      print("Formatted Number: {}".format(num))
      
  6. How to print complex numbers in Python:

    • Description: Printing complex numbers.
    • Code:
      complex_num = 2 + 3j
      print("Complex Number:", complex_num)
      
  7. Print binary representation of numbers in Python:

    • Description: Printing binary representation of integers.
    • Code:
      num = 8
      print("Binary Representation:", bin(num))
      
  8. Formatting numbers with commas in Python:

    • Description: Adding commas to format large numbers.
    • Code:
      large_num = 1000000
      print("Formatted Number:", "{:,}".format(large_num))
      
  9. Print numbers with leading zeros in Python:

    • Description: Displaying numbers with leading zeros.
    • Code:
      num = 7
      print("Number with Leading Zeros:", "{:03}".format(num))
      
  10. Display hexadecimal numbers in Python:

    • Description: Printing numbers in hexadecimal format.
    • Code:
      num = 255
      print("Hexadecimal Representation:", hex(num))
      
  11. Python scientific notation print:

    • Description: Printing numbers in scientific notation.
    • Code:
      num = 0.00005
      print("Scientific Notation:", "{:e}".format(num))
      
  12. Print numbers with a specific precision in Python:

    • Description: Setting precision for floating-point numbers.
    • Code:
      num = 3.14159
      print("Formatted Precision:", "{:.3f}".format(num))
      
  13. Print numbers in exponential form in Python:

    • Description: Printing numbers in exponential form.
    • Code:
      num = 1000000
      print("Exponential Form:", "{:e}".format(num))
      
  14. Formatting numbers with locale in Python:

    • Description: Using the locale module to format numbers based on the system's locale.
    • Code:
      import locale
      locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
      num = 1234567.89
      print("Formatted with Locale:", locale.format_string("%0.2f", num))
      
  15. Printing octal numbers in Python:

    • Description: Displaying numbers in octal format.
    • Code:
      num = 64
      print("Octal Representation:", oct(num))
      
  16. Print numbers with specific width in Python:

    • Description: Setting a specific width for printed numbers.
    • Code:
      num = 42
      print("Formatted Width:", "{:5}".format(num))
      
  17. How to print large numbers with underscores in Python:

    • Description: Using underscores to improve readability of large numbers.
    • Code:
      large_num = 100_000_000
      print("Large Number with Underscores:", large_num)
      
  18. Print numbers as Roman numerals in Python:

    • Description: Converting integers to Roman numerals.
    • Code:
      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))