Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python int() Function

#1
Python int() Function

Python’s built-in int(value) function converts the argument value to an integer number. For example, int('42') converts the string value '42' into the integer number 42. The int() function on a float argument rounds down to the closest integer.

Python int() Graphical Explanationhttps://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 768px) 100vw, 768px" />

Argument value A Python object to be converted into an integer number. The value object must have an __int__() method that returns the associated integer number—otherwise a TypeError will be raised.
base An optional integer argument base to define the base of the numerical system in the value argument. If you set the base, the value argument must be a string. The base argument determines how the string argument is interpreted.
Return Value int Returns an integer number after converting the input argument value using its required __int__() method for the conversion.

Input : int('42')
Output : 42 Input : int('-42')
Output : -42 Input : int(0)
Output : 0 Input : int(3.41)
Output : 3 Input : int(3.51)
Output : 3 Input : int('101', base=2)
Output : 5



But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners

Python int() Rounding


The int(x) function on a float value x rounds down to the closest integer.

>>> int(3.41)
3
>>> int(3.51)
3
>>> int(3.9999)
3

Python int() Custom Object


To allow a custom object as an input to the int(object) function, the object must implement the __int__(self) dunder method that returns an integer value. In fact, the int(object) built-in function is semantically equivalent to the object.__int__() function call.

class Car: def __int__(self): return -1 porsche = Car()
print(int(porsche))
# -1

In the example, you create a class Car and implement the __init__(self) method that always returns the integer -1. Thus, you can pass a Car object porsche into the int() function and Python doesn’t throw an exception.

Speaking of which…

Python int() Exception


If you pass an object into the int() function that doesn’t overwrite the __int__() method—for example, a list, tuple, or set—Python throws a TypeError:

class Car: None porsche = Car()
print(int(porsche))

This leads to the error message:

Traceback (most recent call last): File "C:\Users\...\code.py", line 6, in <module> print(int(porsche))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Car'

To fix the error, either pass an object that is convertible to an integer or implement your own __int__(self) method as shown previously:

class Car: def __int__(self): return -1 porsche = Car()
print(int(porsche))
# -1

Note that the same TypeError appears if you try to convert lists, sets, dictionaries, or tuples to integer values using the int() function.

Lists:

>>> int([1, 2, 3])
Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> int([1, 2, 3])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

Sets:

>>> int({1, 2, 3})
Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> int({1, 2, 3})
TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'

Dictionaries:

>>> int({'Alice': 23, 'Bob': 17})
Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> int({'Alice': 23, 'Bob': 17})
TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'

Tuples:

>>> int((1, 2, 3))
Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> int((1, 2, 3))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

Python int() base Argument


The int(value, base=10) function has an optional argument base that allows you to define the base of the numerical system in the value argument. If you set the base, the input must be a string. The base argument determines how the string argument is interpreted. For example, int('10', base=2) converts the string '10' —representing a binary number—to the integer 2.

>>> int('10', base=2)
2

Summary


Python’s built-in int(value) function converts the argument value to an integer number.

For example, int('42') converts the string value '42' into the integer number 42.

>>> int('42')
42

The int() function on a float argument rounds down to the closest integer.

>>> int(3.99)
3

Want to keep improving your Python skills? Check out our free Python cheat sheets:

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

The post Python int() Function first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...-function/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 14 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 27 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 20 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 27 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python repr() Function — A Helpful Guide with Example xSicKxBot 0 31 02-15-2021, 02:40 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 26 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 29 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] How to Send UDP Multicast in Python? xSicKxBot 0 31 02-03-2021, 01:31 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 28 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python filter() xSicKxBot 0 42 02-01-2021, 03:25 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[1.07] BO4 Zombie Trainer (Semi-Patched)
Last Post: trioxyn
Yesterday 08:14 PM
» Replies: 12
» Views: 469
HOW TO: Get all steam games. (Green Luma...
Last Post: trioxyn
Yesterday 07:28 PM
» Replies: 5
» Views: 1688
(Indie Deal) Castlevania, Pqube, Fantasy...
Last Post: xSicKxBot
Yesterday 07:15 PM
» Replies: 0
» Views: 5
Microsoft - Digital good: How to have mo...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 5
News - A Super Mario Lenticular Magnet S...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 5
News - Halo Infinite's Open World Honors...
Last Post: xSicKxBot
Yesterday 07:14 PM
» Replies: 0
» Views: 13
Blender 2.92 Released
Last Post: xSicKxBot
Yesterday 01:09 PM
» Replies: 0
» Views: 7
Fedora - Contribute at the Fedora Audio,...
Last Post: xSicKxBot
Yesterday 01:09 PM
» Replies: 0
» Views: 5
News - George Romero's Lost Movie The Am...
Last Post: xSicKxBot
Yesterday 01:09 PM
» Replies: 0
» Views: 17
[Tut] How to Read a File Without Newline...
Last Post: xSicKxBot
Yesterday 06:41 AM
» Replies: 0
» Views: 11

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020