
Concepts: Integers
Representing whole numbers in Python with integers.
What are Integers?
Numbers in Python come in various types depending on the structure of the number which is created. The first type you will be learning about here are integers, known in Python as ints. Integers in math represent negative and positive non-decimal numbers, and that holds true is programming languages like Python as well.
In this exercise, you will be learning how to declare an integer value and assign it to a variable.
In addition, you will learn how to check the datatype of a value, so that you can confirm the data type of the integer value you created.
And, you will also learn how to use the int() function in Python to convert other values to integers.
Practice with Integers
Let's begin with defining a variable and assigning it an integer value:
-
Define a variable named
height_in_cmand assign it your height (in centimeters), rounded to the nearest integer. -
Print the value contained in the
height_in_cmvariable.HINT*: Click here for a hint
You should see your height in centimeters output in the terminal (as an integer):
> 183
SOLUTION*: Click here for an example solution
height_in_cm = 183 print(height_in_cm) -
Now that you have properly created a numeric integer value, the next step will be to print out the
typeof the variable to confirm it is an integer. -
To display the type of a value stored in a variable, you will be using the
type()function. Like theprint()function you used above, thetype()function is always available in Python. -
The syntax for using the
type()function is as follows:type(value_to_check) -
Using the the above information, check the type of the
height_in_cmvariable you created and confirm that it is a class of typeint.HINT*: Click here for a hint
You should see the data-type of the
height_in_cmvariable displayed in the interpreter:>
<class 'int'>SOLUTION*: Click here for an example solution
type(height_in_cm)Based on the information returned from the
type()function, you are probably curious about what the class ofintis in Python. A class in Python is like a factory that generates a specific type of object. In this case you are using a class namedintto produce integer values. There are many built in classes in Python, and we will look at more of them as the course continues.Now that you have created an integer value and confirmed it is a proper integer by using the
type()function, it is time to learn about using theint()function in Python. -
Define a new variable named
decimal_valueand assign it a value of 3.5. -
To convert the value in
decimal_valueto an integer, you will use theint()function with the following syntax:int(value_to_convert) -
Using the above information, use the
int()function to display the value ofdecimal_valuevariable after it is converted to an integer value.HINT*: Click here for a hint
You should see the integer value of the
decimal_valuevariable displayed in the interpreter:> 3
SOLUTION*: Click here for an example solution
int(decimal_value)
When the above has been completed and you are seeing the correct output in the interpreter, Congratulations! You have learned about creating integer values, converting other data-types to integers, and checking a variables data-type! You are ready to move on to the next exercise!