Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's happening here?
#1
Lightbulb 
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.
Reply
#2
The comma is seperating the argumemts '101' and 2. If you want to know what the arguments do, read the documentation for int().
ndc85430 likes this post
Reply
#3
So it's base 2.

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

1 0 1
2 0 2
= 4

Still looking..
Reply
#4
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
>>> 
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  2 if statements happening at the same time M0TH 3 950 Nov-19-2022, 11:37 PM
Last Post: M0TH
  Why is this happening? taner7 3 2,144 Oct-14-2020, 10:11 PM
Last Post: Skaperen
  Binding not explicitly declared happening karkas 4 2,887 Aug-05-2019, 05:09 PM
Last Post: karkas
  Why this python ValueError is not happening aurquiel 2 3,051 Aug-20-2018, 07:17 PM
Last Post: aurquiel
  Something strange happening here !? TerryRitchie 4 3,916 Apr-20-2017, 07:14 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020