Image

Anyone Can Code.





Image


type()



Jyotirmay Zamre, Sammarth Kumar



You can verify the type of any object in Python using the type() function. It comes built-in and you just need to call it with the object as an arguement. An arguement is simply a value we pass into a function, upon which the function executes the code statements within it.

Let's create different kinds of objects to test our type() function.


    x = 35 #int
    y = False #bool
    z = [3,4,5] #list
    a = (3,6,7) #tuple
    b = {'name':'qp','year' : 2020} #dict
    c = None #None
    d = 0.34 #float
    e = 4 + 5j #complex
    f = "hello" #str

    print(type(x),
        type(y),
        type(z),
        type(a),
        type(b),
        type(c),
        type(d),
        type(e),
        type(f),
        sep = "\n"
        )
                      
<class 'int'> <class 'bool'> <class 'list'> <class 'tuple'> <class 'dict'> <class 'NoneType'> <class 'float'> <class 'complex'> <class 'str'>

Whoa! We used some new kinds of code here! Well, not really. The program is actually quite simple, it's just a bit longer than what you have seen so far. Let's break it down once. We can split the print() statement into lines and the interpreter will read it the same way as it would if it were in a single line.

This is a cool trick you can use to make your code more readable. Otherwise, a cluster of type() functions looks daunting, while this looks simple and elegant.

We use sep to separate each component inside the print statement with a string of our choice. Here, we separated each element by a new line using the \n escape sequence. All of this will be covered in detail when we discuss everything you can do with print() and input().


That's it for this tutorial! Click Next to access the next tutorial!

❮ Back

Next ❯









Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar