Python Forum

Full Version: What's happening here?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm not understanding what's happening here?

print ( int('101', 2))
# Equals 5
However...

print ( int('101))
# Equals 101
I guess I don't know what the comma is doing.


Thanks much.
The comma is seperating the argumemts '101' and 2. If you want to know what the arguments do, read the documentation for int().
So it's base 2.

This is wrong I'm sure but I'm reading..

1 0 1
2 0 2
= 4

Still looking..
It's only simple maths
>>> int('101', 2) == 1 * 2**2 + 0 * 2**1 + 1 * 2**0
True
>>> int('101') == 1 * 10**2 + 0 * 10**1 + 1 * 10**0
True
>>>