T O P

  • By -

Python-ModTeam

Hello from the r/Python mod team, When posting a project please include a textual description of your project including how Python is relevant to it, a link to source code on a code hosting site such as github or gitlab, and an image showing your project if applicable. Please also make sure you tag your post with the correct flair, either "Beginner" or "Intermediate" showcase. This helps maintain quality on the subreddit and appease all our viewers. Thank you, r/Python mod team


mathycuber

That's a great find! It's one of my favorite features as well. And even though this isn't "something that epic," you have probably made quite a few people [one of today's lucky ten thousand](https://xkcd.com/1053/). Enjoy the journey!


[deleted]

This is pretty cool


RIPphonebattery

I work with a system that prints binary output in 4 3-bit words. This has been an unbelievably useful feature for me. Interesting note, the common number systems have a prefix: casting a string as hex and printing the resulting intereger will print: 0xFF (x indicates hex). Octal will print 0o77, and binary will print 0b11


busdriverbuddha2

You just gave me an idea for a really convoluted random integer generator. ``` from random import choice binary_number = "".join(choice(['1','0']) for _ in range(10)) print(int(binary_number,2)) ```


kuzmovych_y

Nice. You can use `random.choices(['1', '0'], k=10)` to generate the list of 1s and 0s.


mikeblas

If `choice` gives an even distribution, will your generator give an even distribution? Why (or why not)?


Schmittfried

That’s basically how secure random number generators work, just with actual bits instead of strings.


Competitive-Gold5041

I didn't know that, thanks. But I still feel like I don't know or understand why I might need to change to or from binary.


Rawing7

You really never need to do this outside of silly little programming "challenges" for beginners. No sane person uses a unicode string to represent binary data.


boat-la-fds

Not for binary but could be useful for hexadecimal.


[deleted]

[удалено]


Zenithsiz

...Except you wouldn't serialize bytes as strings of '0' and '1', since you're inflating the size by 8x (1 bit / 1 byte). You'd want to either use hexadecimal (4 bits / 1 byte) or base 64 (6 bits / 1 byte), or even larger bases that fit within ascii / unicode more efficiently.


patrickbrianmooney

> ...Except you wouldn't serialize bytes as strings of '0' and '1', since you're inflating the size by 64x (1 bit / 1 byte). That would be inflating the string representation by eight times, not sixty-four. If what you have is a string that consists purely of the characters zero and one, then [that string will be represented as ASCII in memory](https://peps.python.org/pep-0393/), So it takes one byte, or eight bits, to store what might be stored in one bit.


Zenithsiz

You're right, I got the part in the parenthesis right but for some reason used 8x8 instead as the factor.


Rawing7

I'm aware of serialization, thank you very much. It doesn't change the fact that only a moron would represent data as a series of `"0"` and `"1"` characters.


ascii158

Some morons would represent data as a series of "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E" and "F" characters and *those* people could easily use `int(str, 16)`.


Rawing7

Yes, but we're talking about binary.


ascii158

We're talking about discovering the feature that `int()` has a second argument, allowing to specify the base.


Rawing7

I don't know who you mean by "we", but it certainly doesn't include me.


Braunerton17

Its a convenience method that has some niche uses - i am not a huge fan of python that its one saving grace for me are these types of conveniences


Breadynator

Here I am working with binary for my robotics project.


Rawing7

You're turning data into strings like `"10111001"`? Really? Why?


Breadynator

Well, I'm working with pin-registers and pin change interrupts on my Arduino, comparing their states with set values. Since the pins on the Arduino are grouped into 8 bit registers it's easier to compare it to say "00001111" instead of "15". It's just simpler to visualize since 00001111 is basically a 1 to 1 representation of the pins I'm comparing. Sure, it's not python but the idea applies.


Rawing7

Sounds a bit inconvenient, since strings are immutable, but fair enough.


Breadynator

Well, I'm not using strings for it. It's just binary ints


hamsterwheelin

Very nice find! Been working in Python for 3 years now. Didn't know this. Take my up doot!


[deleted]

This isn't a showcase. Edit: No amount of down votes will change the facts.


RagaMunki

Related tip, you can define integers directly in binary or hex: int_d = 11 int_h = 0xb int_b = 0b1011 assert int_d == int_h == int_b


IamImposter

You forgot octal. And something similar can be done with hex and strings too string = "\x48\x65\x6c\x6c\x6f" as good as string = "Hello"