python logo

python tricks


Python hosting: Host, run, and code Python in the cloud!

Dive into these awesome Python tricks! Whether you’re a novice or seasoned developer, you’ll find these Python 3 tips and tricks invaluable. Not only do they simplify your coding routine, but some also add a bit of fun to your programming experience.

Highlighted Offer:
Elevate your Python skills with the Python Programming Bootcamp: Go from zero to hero.

Start a Simple HTTP Web Server in Python
Did you know you can set up an HTTP Server in Python within seconds? Here’s how:

For Python2:

python -m SimpleHTTPServer

For Python3:

python -m http.server

After initiating, simply visit http://127.0.0.1:8000/. Your browser will display the current directory’s content.

Uncover a Hidden Python Poem
Unravel a delightful poem by Tim Peters using this one-liner:

import this

XKCD Web Comic Easter Egg in Python
Access an XKCD comic straight from your Python environment. Try:

import antigravity

Effortlessly Combine Arrays using Zip in Python
Merging two arrays? Use zip for a straightforward solution:

b = [[1, 2, 3, 4], [6, 5, 4, 3]]
zip(*b)
[(1, 6), (2, 5), (3, 4), (4, 3)]

Reversing a List Made Simple
No need to craft a separate function to reverse a list. Python has got you covered:

b = [1,2,3,4,5]
b.reverse()
print(b)
[5, 4, 3, 2, 1]

Reverse a String with Python’s Slicing Feature
Get the reversed version of any string effortlessly:

s = "Hello world"
s = s[::-1]
print(s)
'dlrow olleH'

Swap Variables in Python without Temp Variables
Python elegantly lets you swap values without the fuss of using temporary variables:

a = 1
b = 3
b,a = a,b
print(a)
print(b)
3
1

Enjoyed these Python tricks? Feel free to share yours in the comments. Together, we make the Python community richer!

Recommended for You:
Take your Python journey to the next level with the Python Programming Bootcamp: Go from zero to hero.

Navigation:
← Back | Next →






Leave a Reply:




Arishem Sun, 05 Jul 2015

Hello Frank!
I wanted to know in which order should I study the tutorials in your website(I mean,after Beginner)

Frank Sun, 05 Jul 2015

Hi, after beginner tutorials you could learn:
Flask (web apps)
GUI programming (pick one of the modules)
Databases

I will create intermediate and more tutorials, stay tuned :-)