Define Integer Literals (Solution)
00:09 This is the Python shell, which you can use as a playground to quickly test out ideas by running short code snippets before saving them in a file. I’m going to dock this window on the left side of the screen so that I have some space on the right where I can open another window with an empty file ready for editing.
00:27 I like to have a split screen with everything I need on a single workspace like this to avoid losing focus or switching between different programs. You’ll also notice that I use a lot of keyboard shortcuts to make things a little quicker. While your shortcuts may be slightly different than mine, you can always find them in the menu at the top if you’re interested in what they are.
I’ll paste the exercise instructions as a Python string into the code editor for a quick reference. As you can see, I’ve already turned each sentence into a separate bullet point, which we’re going to tackle one by one. Right off the bat, it says, write a program which indicates that you should save your code as a Python script, which I’ll do now, and name the file
IDLE assumes we are creating a file with Python source code, so it automatically appends the
.py extension to the file name unless told otherwise. You can see the path to your file in the title bar of the window.
We’re going to create two variables named
num2. It’s customary to define each variable on a separate line in Python for better readability, even though it’s technically possible to squash them onto a single line, which can sometimes be justified. A quick reminder: you can define a variable by specifying its name, such as
num1, followed by the assignment operator, which is the equal sign (
=) in Python, and finally, the value. According to the second bullet point, both variables are supposed to have the same integer value of 25 million, but they’re written using two alternative literal forms.
02:09 Let’s use the integer literal with underscores for the first variable and one without the underscores for the second variable. You can place underscores anywhere in your literals as long as they don’t appear in the front or at the end. In other words, there must be some digits on both sides of each underscore, and these underscores are completely optional because Python ignores them.
Now the last point in this exercise is about printing both variables in order to reveal their current values. Printing boils down to calling the built-in
print() function with each variable as an argument. So
The two numbers that appear on the screen are the result of calling the
print() function twice with different arguments. Notice that despite using alternative literal forms that we assign to the variables, Python prints them the same way.
03:35 You see, from Python’s perspective, both variables contain exactly the same numerical value. Using underscores in integer literals only makes a difference to programmers like you who read the source code. All right, the next exercise will continue our exploration of numeric literals in Python.
Become a Member to join the conversation.