5 Fun Python Easter Eggs
HTML-код
- Опубликовано: 9 июн 2024
- Here are 5 fun Python easter eggs that you should know about if you love Python.
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
00:00 Learning Python made simple
00:05 Intro
00:23 Easter egg #1
01:07 Easter egg #2
01:43 Secret challenge
02:22 Easter egg #3
04:16 Easter egg #4
04:46 Easter egg #5
05:08 Why is this here?
06:10 Summing it up
You can see the source code for the `this' module at `this.__file__'. It has purposefully bad code to demonstrate what not to do. That's part of the poetry that a lot of people miss.
And it contains the challenge solution
Just by looks it seems to be Ceasar Cipher. The decoder table is provided in the same module. Here is a one-liner to recover the text
''.join([this.d.get(c, c) for c in this.s])
THE braces one made my day
If only it worked 😤
it ruined mine
@@replikvltyoutube3727 It worked back in 2.5.
same😂
I kinda didn't get it
5:48 geohasing is another one of xkcd comics which is like a random location generator you can go hiking to.
It's a play on words with GeoCaching and hashing. The algorithm name is also a joke, because Randall Munroe is the creator of xkcd.
And Munroe is the surname of the creator of xkcd, Randall Munroe, and as he invented the algorithm in the geohashing comic it's naturally named after him.
426th xkcd
I had no idea python HAD easter eggs. I'm so glad I watched your video, because I learned something new.
Me: bracing myself for him typing var: str = "..."
The first Easter egg is an example script for the Freeze utility that comes with Python. The instructions for Freeze uses the `hello.py` file so you can test it out.
Per the Github repo for Python:
```
Freeze make it possible to ship arbitrary Python programs to people
who don't have Python. The shipped file (called a "frozen" version of
your Python program) is an executable, so this only works if your
platform is compatible with that on the receiving end (this is usually
a matter of having the same major operating system revision and CPU
type).
The shipped file contains a Python interpreter and large portions of
the Python run-time. Some measures have been taken to avoid linking
unneeded modules, but the resulting binary is usually not small.
```
Originally the Hello script would print `Hello World` just by importing the module. Starting in Python 3.11 it uses the `main()` function.
I love these Easter Egg videos. I end up falling down a rabbit hole and learning a lot more than just the Easter egg.
__hello__ was added in python 3.11
Not long ago
1. Fellas, I need your help to conclude a debate once and for all : the correct way to print "Hello World!" is without a comma? or "Hello, World!" with a comma ?
2. I did not know any of these. I'm kinda new to python.
3. I'm gonna mess with an entire codebase by importin braces everywhere lmaooo
No comma, it's part of the fun.
I think what you meant was:
No comma it's part of the fun.
Neither is correct, because "world" isn't someone's name (to my knowledge :D) and should be lowercased. Other than that, I'm pretty sure, that with comma is grammatically correct, but almost no one uses it there.
@@callbettersaul _Hello World_ is a title tho
@@ahmoinno
Python provides another module, *codecs*, that decodes this.s:
Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32
import this
...[poem elided]....
import codecs as c
print(c.decode(this.s, 'rot13'))
...[decoded poem elided]...
So, we see that rot13 is one of the many batteries included in Python.
You can pretty much do the same thing with instance and static methods without importing anything; print(this.s.translate(str.maketrans(this.d))). But it's good to know anyways :)
These are great! thanks
[ 2:13 ]: this is actually easy it's just the alphabet shifted and can be solved vary easily
You dont need to do that since the dictionary to translate it already exists in the module, "this.d", and the raw string is, "this.s"
@@shikanokonokokoshitantan that dictionary is actually generated on runtime meaning it was not hardcoded
@@SolathPrime Then that means that the module hardcoded, ofc I am not saying that value is defined WITH the module I am saying that its defined WITH the STRING that is PRINTED when you import this, using the dictionary you can decode this.s using a simole for loop or using a list comprehension.
Stop fighting, it's as simple as writing "print(this.s.translate(str.maketrans(this.d)))"
And also, I'd like to mention that the method used to rotate the alphabet is ROT13, which rotates the alphabet 13 places.
damn thanks for telling me how i can print hello world! i was actually struggling for a bit with it had to type it out 100x times and misspelled it a lot
1:48 i have translated the message, it's just caesar chiper. it returns:
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
IT'S THE SAME THING
Yeah, iirc the cipher text is what is stored in thr source code so that the easter egg is somewhat hidden. But it already decodes it for you when you run it. So the d and s aren't additional easter eggs, they are implementation details.
instead of _hello_ why not just use constant for the string?
lmao i have modified built-in json library and call it antigrav (dont question why) and uploaded to pypi
i discovered antigravity is a thing only now now i feel like i made a cheap copy
Broh, may I please know which software do you use to create your very good Thumbnails and icons??
Thank you
That antigravity thing reminded me of a video I just watched about MS Office macro viruses. Who thought it was a good idea to give a programming language the ability to open a browser window?
Eyy second again!
Geohashing is reference to another xkcd webcomic
What is a datedow, and why is it needed?
how did I not know any of these? (other than this, everyone knows that)
what IDE/text editor is this?
PyCharm IDE for Python Developers
the "this.d" is the decoding table for "this.s"
inline solution: print(''.join([this.d[i] if i in this.d else i for i in this.s]))
i loved solving this
Huh all good but the first one
Now we need help to print hello world 😢
c# coders would flip at the braces thing.. oh wait, don't i know a bit of c#- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
print(''.join([this.d.get(x,x) for x in this.s])) -- is there a more pythonic way to do it?
yes there is.
''.join(map(this.d.get, this.s, this.s)
I will leave it to you to decide about the comprehension versus map, but at least remove the explicit list comprehension if you stick with something like that. You can remove both square braces, and unless I am missing something it will still work. What you would doing instead is called a generator expression. Generally, you need to use parentheses around a generator expression, but there are sometimes when you can omit them because they would just be duplicative. Duplicative. Passing a generator expression as a single parameter to a function is one such time.
also
import this
import codecs
print(codecs.decode(this.s, 'rot13'))
That's interesting, I didn't know it was encoded using ROT13!
`print(this.s.translate(str.maketrans(this.d)))`
1:28 I have a shortcut to this on my desktop.
Las Malvinas son argentinas!!!
early
Almost decodes the this.s message correctly. The only problem is that when you import this, you immediately get the text, so when you run this on the command line, it prints it out twice.
python -c 'from this import d, s; j = d; j["
"] = "
"; j[" "] = " "; print("".join([j[x] for x in list(s) if x.isalpha() or x == "
" or x == " "]))'
Is much better to use this.s.translate and str.maketrans like this;
import this
print(this.s.translate(str.maketrans(this.d)))
You can make it shorter by importing s and d directly, but the point is that you can use the dictionary and the translate method to do the work.