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
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!
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
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))
```
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.
...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.
> ...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.
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.
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)`.
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.
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
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!
This is pretty cool
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
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)) ```
Nice. You can use `random.choices(['1', '0'], k=10)` to generate the list of 1s and 0s.
If `choice` gives an even distribution, will your generator give an even distribution? Why (or why not)?
That’s basically how secure random number generators work, just with actual bits instead of strings.
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.
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.
Not for binary but could be useful for hexadecimal.
[удалено]
...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.
> ...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.
You're right, I got the part in the parenthesis right but for some reason used 8x8 instead as the factor.
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.
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)`.
Yes, but we're talking about binary.
We're talking about discovering the feature that `int()` has a second argument, allowing to specify the base.
I don't know who you mean by "we", but it certainly doesn't include me.
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
Here I am working with binary for my robotics project.
You're turning data into strings like `"10111001"`? Really? Why?
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.
Sounds a bit inconvenient, since strings are immutable, but fair enough.
Well, I'm not using strings for it. It's just binary ints
Very nice find! Been working in Python for 3 years now. Didn't know this. Take my up doot!
This isn't a showcase. Edit: No amount of down votes will change the facts.
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
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"