Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Concepts: Integers

check

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:

  1. Define a variable named height_in_cm and assign it your height (in centimeters), rounded to the nearest integer.

  2. Print the value contained in the height_in_cm variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

    You should see your height in centimeters output in the terminal (as an integer):

    > 183
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      height_in_cm = 183
      print(height_in_cm)
    
  3. Now that you have properly created a numeric integer value, the next step will be to print out the type of the variable to confirm it is an integer.

  4. To display the type of a value stored in a variable, you will be using the type() function. Like the print() function you used above, the type() function is always available in Python.

  5. The syntax for using the type() function is as follows:

      type(value_to_check)
    
  6. Using the the above information, check the type of the height_in_cm variable you created and confirm that it is a class of type int.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

    You should see the data-type of the height_in_cm variable displayed in the interpreter:

    > <class 'int'>
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      type(height_in_cm)
    

    Based on the information returned from the type() function, you are probably curious about what the class of int is 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 named int to 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 the int() function in Python.

  7. Define a new variable named decimal_value and assign it a value of 3.5.

  8. To convert the value in decimal_value to an integer, you will use the int() function with the following syntax:

    int(value_to_convert)
    
  9. Using the above information, use the int() function to display the value of decimal_value variable after it is converted to an integer value.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

    You should see the integer value of the decimal_value variable displayed in the interpreter:

    > 3
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
      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!