Code Monkey home page Code Monkey logo

pythoniskewl's Introduction

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

pythoniskewl's People

Contributors

starkizard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.