Numbers

What Are Python Numbers?

  • In Python, there are three numeric types:
    1. int: Integer, a whole number without decimals.
    2. float: Floating point number, a number with decimals.

Creating Numeric Variables:

  • Variables of numeric types are created when you assign a value to them.

    python
    x = 1    # int
    y = 2.8  # float
    

Checking the Type:

  • Use the type() function to verify the type of any object.

    python
    print(type(x))   
    print(type(y)) 
    
    • Output: <class 'int'>
    • Output: <class 'float'>

Integer:

  • Integers are whole numbers without decimals, of unlimited length.

    python
    x = 1
    y = 35656222554887711
    z = -3255522
    
    print(type(x))  
    print(type(y))  
    print(type(z))  
    • Output: <class 'int'>
    • Output: <class 'int'>
    • Output: <class 'int'>

Float:

  • Floats are numbers with one or more decimals and can also be scientific numbers with an "e" to indicate the power of 10.

    python
    x = 1.10
    y = 1.0
    z = -35.59
    
    print(type(x))  
    print(type(y))  
    print(type(z))  
    
    x = 35e3
    y = 12E4
    z = -87.7e100
    
    print(type(x))  
    print(type(y))  
    print(type(z))  
    • Output: <class 'float'>
    • Output: <class 'float'>
    • Output: <class 'float'>
    • Output: <class 'float'>
    • Output: <class 'float'>
    • Output: <class 'float'>

Summary:

  • Python supports three numeric types: integers, floats, and complex numbers. You can convert between these types and generate random numbers using the random module. Practice creating and working with numeric variables to understand their properties and uses!