
PYTHON — Define Integer Literals in Python
Technology, like art, is a soaring exercise of the human imagination. — Daniel Bell
In Python, you can define integer literals using two alternative forms: with or without underscores. The underscore character can be placed anywhere within the numeric value to visually group similar digits together. Here’s a step-by-step guide to defining integer literals in Python.
First, let’s create a Python script file, exercise1.py, and define two variables: num1 and num2. Both variables will have the same integer value of 25 million, but they will be written using different literal forms.
Here’s how you can define them in your Python script:
num1 = 25_000_000
num2 = 25000000In the above code, num1 is defined using underscores within the literal form, while num2 is defined without underscores. You can place underscores anywhere within your literals as long as they don't appear in the front or at the end. These underscores are completely optional because Python ignores them. However, they can be helpful in visually grouping similar digits together for better readability.
Finally, to reveal the current values of both variables, you can use the built-in print() function:
print(num1)
print(num2)When you run the script, you’ll see that Python prints both variables the same way, despite using alternative literal forms. This is because from Python’s perspective, both variables contain exactly the same numerical value. The use of underscores in integer literals only makes a difference to programmers who read the source code.
After saving the file and running it, you will see the output with the values of num1 and num2.
These exercises provide a practical understanding of how to define integer literals in Python and the optional use of underscores within these literals for better readability. This knowledge is essential for working with large numeric values in Python.
