{"id":24792,"date":"2021-11-02T09:47:58","date_gmt":"2021-11-02T04:17:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=24792"},"modified":"2021-11-05T20:36:36","modified_gmt":"2021-11-05T15:06:36","slug":"python-int-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-int-method-with-examples\/","title":{"rendered":"Python int() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python id() Method with Examples<\/a> The int() function converts the given value to an integer number.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n value:<\/strong> It is a number or string that can be converted into an integer number.<\/p>\n base:<\/strong> The number format is represented by a number. 10 is the default value.<\/p>\n Return Value:<\/strong><\/p>\n The int() method gives:<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/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 Similarly, check it out for other values<\/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 Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python id() Method with Examples int() Method in Python: The int() function converts the given value to an integer number. Syntax: int(value, base) Parameter Values: value: It is a number or string that can be converted into an integer number. base: The number format is represented by a …<\/p>\n
\nint() Method in Python:<\/strong><\/p>\nint(value, base)<\/pre>\n
\n
Given Number = 35.5<\/pre>\n
The given number's{ 35.5 } Integer number = 35<\/pre>\n
Given Value = 'D'\r\nGiven base = 16<\/pre>\n
The given value's { D } Integer number with given base{ 16 } = 13<\/pre>\n
int() Method 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_numbr = 35.5\r\n# Pass the given number as an argument to the int() function to get the\r\n# the given numbers's Integer number.\r\n# Store it in another variable.\r\nrslt = int(gvn_numbr)\r\n# Print the given number's Integer Number.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Integer number = \", rslt)\r\n<\/pre>\n
The given number's{ 35.5 } Integer number = 35<\/pre>\n
How does int() work with decimal, octal, and hexadecimal values?<\/h5>\n
\n
# Give the value as static input and store it in a variable.\r\ngvn_valu = '16'\r\n# Give the base as static input and store it in another variable.\r\ngvn_base = 8\r\n# Pass the given value, base as the arguments to the int() function to get the\r\n# Integer number of a given value.\r\n# Store it in another variable.\r\nrslt = int(gvn_valu, gvn_base)\r\n# Print the Integer number of a given value.\r\nprint(\"The given value's {\", gvn_valu,\r\n \"} Integer number = \", rslt)\r\n<\/pre>\n
The given value's { 16 } Integer number = 14<\/pre>\n
gvn_valu = 'D'\r\ngvn_base = 16\r\nrslt = int(gvn_valu, gvn_base)\r\nprint(\"The given value's {\", gvn_valu,\r\n \"} Integer number = \", rslt)\r\n<\/pre>\n
The given value's { D } Integer number = 13<\/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_numbr = float(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the int() function to get the\r\n# the given numbers's Integer number.\r\n# Store it in another variable.\r\nrslt = int(gvn_numbr)\r\n# Print the given number's Integer Number.\r\nprint(\"The given number's{\", gvn_numbr,\r\n \"} Integer number = \", rslt)\r\n<\/pre>\n
Enter some random number = 250\r\nThe given number's{ 250.0 } Integer number = 250<\/pre>\n
How does int() work with decimal, octal, and hexadecimal values?<\/h5>\n
\n
# Give the value as user input using the input() function and store it in a variable.\r\ngvn_valu = input(\"Enter some random value = \")\r\n# Give the base as user input using the int(input()) function and store it in another variable.\r\ngvn_base = int(input(\"Enter some random number = \"))\r\n# Pass the given value, base as the arguments to the int() function to get the\r\n# Integer number of a given value.\r\n# Store it in another variable.\r\nrslt = int(gvn_valu, gvn_base)\r\n# Print the Integer number of a given value.\r\nprint(\"The given value's {\", gvn_valu,\r\n \"} Integer number = \", rslt)\r\n<\/pre>\n
Enter some random value = B\r\nEnter some random number = 16\r\nThe given value's { B } Integer number = 11<\/pre>\n
\n