python 1 liners

   1 # Palindrome Python One-Liner
   2 phrase.find(phrase[::-1])
   3 
   4 # Swap Two Variables Python One-Liner
   5 a, b = b, a
   6 
   7 # Sum Over Every Other Value Python One-Liner
   8 sum(stock_prices[::2])
   9 
  10 # Read File Python One-Liner
  11 [line.strip() for line in open(filename)]
  12 
  13 # Factorial Python One-Liner
  14 reduce(lambda x, y: x * y, range(1, n+1))
  15 
  16 # Performance Profiling Python One-Liner
  17 python -m cProfile foo.py
  18 
  19 # Superset Python One-Liner
  20 lambda l: reduce(lambda z, x: z + [y + [x] for y in z], l, [[]])
  21 
  22 # Fibonacci Python One-Liner
  23 lambda x: x if x<=1 else fib(x-1) + fib(x-2)
  24 
  25 # Quicksort Python One-liner
  26 lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]])
  27 
  28 # Sieve of Eratosthenes Python One-liner
  29 reduce( (lambda r,x: r-set(range(x**2,n,x)) if (x in r) else r), range(2,int(n**0.5)), set(range(2,n)))

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source