Define Integer Literals (Solution)

00:00 I have IDLE already open in the default interactive mode, which you can tell by the prompt consisting of the three right-angle brackets (>>>).

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.

00:48 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 exercise1.

01:13 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.

01:28 We’re going to create two variables named num1 and 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.

02:34 They can be helpful in visually grouping similar digits together like in this exercise. Arguably, the first literal value is easier to read than the second one.

02:45 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 print(num1) and print(num2).

03:02 You can now save the file and run it in the interactive window on the left. On my computer, I can reload the module by hitting F5 on my keyboard.

03:12 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:27 You can verify this by explicitly calling print() with num1 and num2 in the interactive Python shell on the left.

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.