Skip to content

starkizard/PythonIsKewl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Python is Kewl! 😎

Exploring Kewl stuff with Python 🐍

Python can do a lot of wonderful stuff, Here is just a repo , showing all the cool stuff you can do with Python.

(I'll be adding stuff as i see)

So, Here we go,

Note: All the examples are tested on Python 3.5.2 interactive interpreter, and they should work for all the Python versions unless explicitly specified before the output.

Table of Contents

▶ Quines

s='s=%r;print(s%%s)';print(s%s)

💡 Explanation:

So, this python code is a quine, i.e. The output is the program itself. It can be called a quine if it's not an empty program , and is self sustained. (No file reading/writing operations). Imagine our DNA coded like this and it self replicates , Try it online!

The code has two parts , Data and the output.

DATA part

s='s=%r;print(s%%s)'

OUTPUT producing part

print(s%s)

The Variable s is assigned the above string. the %r provides a repr() version of the string that's passed as a format specifier. Basically if %s was used, it doesn't display the quotation marks, but %r does. %%s is used to escape the %s and to indicate that %s is not a format specifier. Then we print the string s and pass s to %r which equates to repr(s). thus printing the whole code. This shows the beauty of code replication. Imagine if some code like this was present in DNA and it uses this to replicate.

▶ Introns

t='';s='t=input()or t;print(f"t={repr(t)};s={repr(s)};exec(s)#{t}")';exec(s)#

💡 Explanation:

So, this python code is an Intron, i.e. it is a quine in itself but can GROW by mutation of some kind. Weirded out? I was too...

Check this out. The code in itself is a quine, i.e. if you provide an empty input to it, (a newline) it'll output the program itself. But, if you provide some input, It'll will produce a modified intron that has the string in it (Yes, it still remains a quine and you can run the output again!)

INPUT

BINOD

OUTPUT

t='BINOD';s='t=input()or t;print(f"t={repr(t)};s={repr(s)};exec(s)#{t}")';exec(s)#BINOD

Kewl, isn't it? Now you can take the Output and run it again!

CODE

t='BINOD';s='t=input()or t;print(f"t={repr(t)};s={repr(s)};exec(s)#{t}")';exec(s)#BINOD

INPUT

Demoknight tf2

OUTPUT

t='Demoknight tf2';s='t=input()or t;print(f"t={repr(t)};s={repr(s)};exec(s)#{t}")';exec(s)#Demoknight tf2

The code mutated again!

What if, you supplied the original intron into the intron itself?? Why don't you Try it Yourself?

▶ Polyglots

#if 0
print("Demoknight tf2, but in Python3")
#endif
#if 0
""" "
#endif
#include<bits/stdc++.h>
using namespace std;
int main(){
    cout << "Demoknight tf2, but in C++\n";
    return 0;
}
#if 0
" """
#endif

💡 Explanation:

What Language is this coded in? Python? C? C++? All three in fact. How?

This is exploitation of some neat tricks taking advantage of #if and #endif directives in C/C++ and they fact # is used to start a comment in Python. A Python interpreter will ignore all code except the pyython code and a C/C++ compiler will ignore all code except the C/C++ code. Kewl! isn't it? I have also made a tool that you can use to generate your own Python-C/C++ Polyglots very quickly. Use this

Releases

No releases published

Packages

No packages published