An integer is a whole number with no decimal places. The quickest way to create an integer in Python is by writing an integer literal consisting of digits that appear literally in your code. For example, typing
42 in IDLE creates an integer.
You can check the type of such a literal, which Python refers to as
int. Because integers are whole numbers, they don’t come with a fractional part, so as soon as you include the decimal point in your literal, you no longer create an integer.
Even if the fractional part of your literal is equal to
0, like in the example, the result will be a floating-point number, which you’ll learn about in the next lesson. On the other hand, an integer literal can include the
- (minus) sign.
01:01 This creates a negative integer number. Additionally, you can delimit groups of multiple digits by placing a single underscore character anywhere in your literal to make a big number easier to read.
It makes no difference to Python whether you use the underscores or not, but writing a number with them is arguably more readable for humans. So far, you’ve only seen decimal literals consisting of the familiar ten digits,
9. However, occasionally you might want to express a number using a different numeral system.
Because computers use binary and sometimes a few other numeral systems, Python lets you create integers using those alternatives. By prefixing your integer literal with one of the few supported system bases, you can change how many digits you want to use. For example, the number 42 can be expressed as
101010 in the binary system.
02:55 It’s fair to say that you will rarely need to use integer literals other than the default decimal one. Nevertheless, they can sometimes be useful. Using integer literals is best when you know your numbers up front and want to embed them in your code in a literal form. However, numbers often come as strings from the users who type them on their keyboard.
From an earlier video course, you might remember that you can convert a string to an integer by calling the built-in
int() function in Python. Notice the quotes around
"42", which define a string literal.
What you get back is the corresponding in integer value, denoted without the quotes. Again, you can verify the type of the return value, which happens to be
int. By default, the
int() function assumes that you will supply a string consisting of decimal digits
If you’d like to choose a different base for the numeral system, then you can optionally pass a second argument to the function after a comma. In this case, you are creating an integer number, 42, from a string of binary digits,
As you can see, the
int() function allows for converting values from other data types to integers, which you might need to perform some calculations on numbers rather than strings. When you call
int() without passing any value as an argument, it will always return
0 as the result.
04:55 All right. Now you know how to create whole numbers in Python using integer literals, which you can represent with different numeral systems, including the decimal, binary, hexadecimal, and octal systems.
In this case, they’re all different representations of the same value. You also know about the
int() function, which return
0 when you call it without any arguments. Otherwise, it will convert whatever data type you supply, such as a string literal, to the corresponding integer number.
Become a Member to join the conversation.