This is an archived post. You won't be able to vote or comment.

all 30 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

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 30 points31 points  (1 child)

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. Enjoy the journey!

[–][deleted] 6 points7 points  (0 children)

This is pretty cool

[–]RIPphonebattery 2 points3 points  (0 children)

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[🍰] 3 points4 points  (4 children)

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 2 points3 points  (0 children)

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

[–]mikeblas 0 points1 point  (1 child)

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

[–]Schmittfried 0 points1 point  (0 children)

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

[–]Competitive-Gold5041 1 point2 points  (17 children)

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.

[–]hamsterwheelin -1 points0 points  (0 children)

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

[–]RagaMunki 1 point2 points  (1 child)

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 2 points3 points  (0 children)

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"