{"id":7423,"date":"2020-12-10T15:47:48","date_gmt":"2020-12-10T10:17:48","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=7423"},"modified":"2020-12-10T15:52:37","modified_gmt":"2020-12-10T10:22:37","slug":"type-function-in-python-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/type-function-in-python-with-examples\/","title":{"rendered":"type() function in Python with Examples"},"content":{"rendered":"\n<p>As we know, Python does not allow us to declare variables with the datatype mentioned. Python auto allocate the data type to the variable with respect to the data that we put inside the variable. There is a built-in function called type that returns the datatype of the variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of type function:<\/h2>\n\n\n\n<p><strong>Parameters<\/strong>: type(object)<\/p>\n\n\n\n<p><strong>Object:<\/strong> This is a mandatory parameter. If this is the only parameter passed to type(), it will return you the parameter type.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-pale-blue-background-color has-background\"><tbody><tr><td>my_list = [&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;, &#8220;D&#8221;]<br>my_tuple = (&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;, &#8220;D&#8221;)<br>my_dict = {&#8220;A&#8221;:&#8221;a&#8221;, &#8220;B&#8221;:&#8221;b&#8221;, &#8220;C&#8221;:&#8221;c&#8221;, &#8220;D&#8221;:&#8221;d&#8221;}<br>my_set = {&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;, &#8216;D&#8217;}<br><br>str_list = &#8220;Hello World&#8221;<br>number = 50<br>pi_value = 3.14<br>complex_num = 3j+10<br><br>print(&#8220;The type is : &#8220;,type(my_list))<br>print(&#8220;The type is : &#8220;,type(my_tuple))<br>print(&#8220;The type is : &#8220;,type(my_dict))<br>print(&#8220;The type is : &#8220;,type(my_set))<br>print(&#8220;The type is : &#8220;,type(str_list))<br>print(&#8220;The type is : &#8220;,type(number))<br>print(&#8220;The type is : &#8220;,type(pi_value))<br>print(&#8220;The type is : &#8220;,type(complex_num))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/E26ioTx_z-FH4rP-6WPOF6_XWidaQot9JzJll3GPcs8gU3GerkCqoqy9AU9k0cSBYsudvPs-XS_bXWw0PnCde-LDNRvB81i4gYKEQq7lUygUFGSx2BDqo5NGFSSpG5-ziEBPVwfFfTxpTodVdw\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example: Using type() for class object.<\/strong><\/h2>\n\n\n\n<p>When we call the type function with input as an object of a class, it returns the class type along with the name of the class.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>class car:<br>color = &#8216;blue&#8217;<br><br>a = car()<br>print(type(a))<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/nPFSPdyepAyyrSN2iODZUPETgc0ztQc3_BfUYyReLrGOsXJy9ZD486b4Jwc7Qjyzy9VPd3W--c9Ch2Uwo6891zIi0Ygm5dUFBca4CkDfe7kGUsV2QOvyLn5T8hfGyLX-5jUW50l-ZQ2a_PPHpg\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we know, Python does not allow us to declare variables with the datatype mentioned. Python auto allocate the data type to the variable with respect to the data that we put inside the variable. There is a built-in function called type that returns the datatype of the variable. Syntax of type function: Parameters: type(object) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7428,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-7423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=7423"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7428"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=7423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=7423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=7423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}