{"id":24858,"date":"2021-11-02T09:54:56","date_gmt":"2021-11-02T04:24:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=24858"},"modified":"2021-11-02T20:14:27","modified_gmt":"2021-11-02T14:44:27","slug":"python-type-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-type-function-with-examples\/","title":{"rendered":"Python type() Function with Examples"},"content":{"rendered":"
type() Function in Python:<\/strong><\/p>\n The type() function returns the type of the object that was passed to it.<\/p>\n Based on the arguments passed, the type() function either returns the type of the object or returns a new type object.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n object:<\/strong> This is required. The type() function returns the type of this object if only one parameter is specified.<\/p>\n base:<\/strong> This is optional. It Describes or specifies the base classes.<\/p>\n dict:<\/strong> This is optional. Specifies the namespace containing the class definition.<\/p>\n Return Value:<\/strong><\/p>\n This function returns<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" type() Function in Python: The type() function returns the type of the object that was passed to it. Based on the arguments passed, the type() function either returns the type of the object or returns a new type object. Syntax: type(object, base, dict) Parameters object: This is required. The type() function returns the type of …<\/p>\ntype(object, base, dict)<\/pre>\n
\n
Given number = 30\r\nGiven string = \"hello this is btechgeeks\"\r\nGiven list = [1, 4, 5, 6]<\/pre>\n
The type of given number = <class 'int'>\r\nThe type of given string = <class 'str'>\r\nThe type of given list = <class 'list'><\/pre>\n
type() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 30\r\n# Give the string as static input and store it in another variable.\r\ngvn_str = \"hello this is btechgeeks\"\r\n# Give the list as static input and store it in another variable.\r\ngvn_lst = [1, 4, 5, 6]\r\n# Pass the given number as an argument to the type() function that returns the \r\n# type of the object that was passed to it.\r\n# Store it in another variable.\r\nrslt1 = type(gvn_numb)\r\n# Print the result after applying the type() function on the given number.\r\nprint(\"The type of given number = \", rslt1)\r\n# similarly do the same for the given string and list and print it.\r\nrslt2 = type(gvn_str)\r\nprint(\"The type of given string = \", rslt2)\r\nrslt3 = type(gvn_lst)\r\nprint(\"The type of given list = \", rslt3)\r\n<\/pre>\n
The type of given number = <class 'int'>\r\nThe type of given string = <class 'str'>\r\nThe type of given list = <class 'list'><\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as user input using the float(input()) function and store it in a variable.\r\ngvn_numb = float(input(\"Enter some random number = \"))\r\n# Give the string as user input using the input() function and \r\n# store it in another variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given number as an argument to the type() function that returns the \r\n# type of the object that was passed to it.\r\n# Store it in another variable.\r\nrslt1 = type(gvn_numb)\r\n# Print the result after applying the type() function on the given number.\r\nprint(\"The type of given number = \", rslt1)\r\n# similarly do the same for the given string and list and print it.\r\nrslt2 = type(gvn_str)\r\nprint(\"The type of given string = \", rslt2)\r\nrslt3 = type(gvn_lst)\r\nprint(\"The type of given list = \", rslt3)\r\n<\/pre>\n
Enter some random number = 50.5\r\nEnter some random string = good morning btechgeeks\r\nEnter some random List Elements separated by spaces = 3 2 4 1\r\nThe type of given number = <class 'float'>\r\nThe type of given string = <class 'str'>\r\nThe type of given list = <class 'list'><\/pre>\n