Code Monkey home page Code Monkey logo

py-one-liners's Introduction

Py-One-Liners

For cool one-liners in the Python programming language (eval() and exec() are cheating) These are by no means meant to be efficient or fast, these are just for fun and to see if it's possible!

Tricks:

  • Pretty sure this is given, but use lambdas and ternary conditionals
  • It's pretty hard to debug single line scripts, so use a handy dandy function that allows you to print a value and return it like this:
def debug(thing):
  print(thing)
  time.sleep(1)
  return thing
  • List comprehension is probably going to be your friend.
  • If you need to call a function in list comprehension you can do something like the below, which allows you to call a function in your list comprehension without messing up the contents of the finished list
[(i, print(i))[0] for in in range(10)]
  • Yes, the ; can be used in Python! A semicolon can be used to signify the end of a line. It, however, cannot be used inside sets of parentheses and brackets, but only at where the end of a line would be in regular Python. Example:
n = 2; [print(i) for i in range(n)]
  • You can have optional arguments in your lambda functions just like in regular functions which you can use for temp variables, other lambda functions, or other useful things! Example:
(lambda thing=2: print(thing))
  • Yes, you can do recursion with lambdas! Example:
f = lambda recurse, num: recurse(num-1) if num > 10 else num; f(f, 20)

py-one-liners's People

Contributors

iapetus-11 avatar sachinbhatttech avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sachinbhatttech

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.