That: Python Hidden Code
- Easter Eggs
- Geek
- Idioms
- Less Known Feature
- Don't Do It
- Command Line
- Extend Python
- One Liner
- Quotes
- Jokes
- Tools
- Guides
import this
2. Open Python xkcd link in browser
import antigravity
from __future__ import braces
import __hello__
if x == 1: #{
print('hello')
#}
ĂĽnĂŹcode = 'hello'
x = False
y = x or 5 # if x is False, y will hold the default value 5.
debug and log('debug message.') # if debug is True, then the message will be logged.
status_headers[:] = [status, headers]
bottle.run(app, port=8891) #, debug=True, reloader=True)
while ...:
print(hello)
You can use exponents instead of typing long floating point numbers.
total = 1e4 # 10000.0
import codecs
codecs.encode('abc', 'rot13') # or 'rot-13'
str.encode
doesn't support rot13
.
utf8
, utf-8
and utf_8
and even utf+_+8
is one thing.
>>> 5 + 5
>>> _
10
This only works in interactive interpreter.
class Hello:
__name = 'Python'
Hello._Hello__name # accessing private property __name
greet = 'Hello'
greet[::-1]
nums = [1, 2, 2, 3]
set(nums)
This will produce an unordered set.
To convert it back to a list you can do the following, although the order will still be lost:
nums = [1, 2, 2, 3]
list(set(nums))
list = ['hello', 'world']
for index, value in enumerate(list):
print(index, value)
# 1 hello
# 2 world
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
or
# coding: uft8
We can use any name instead of self in class defination.
class Hello:
def __init__(this, name):
this.name = name
if __name__ == '__main__':
while True:
print('hello')
else:
print('not hello')
new_str_type = type('new_str_type', (str,), {})
list = [1, 2]
dict = {'name': 'hello'}
def hello(one, two, name=None):
pass
hello(*list, **dict)
try:
int(input)
except: # instead use except TypeError:
pass
Be careful: Mutable default parameters may not behave the way you think.
def hello(items=[]):
items.append('hello')
return items
first = hello()
second = hello() # you will get ['hello', 'hello'] here
If you expect a new object every time, use None
as the default instead:
def hello(items=None):
if items is None:
items = []
items.append('hello')
return items
first = hello()
second = hello() # now you get ['hello']
python -m "calendar" 2015 9
For options: python -m "calendar" --help
python -m http.server --bind 127.0.0.1 8080
For options: python -m http.server --help
output = print
multiline("""Hello, How are you?
Hello, How are you?
Hello, How are you?""") # the function will remove the indent.
This one is fake yet.
[1, 2, 3] | add
if x == 1:
goto .end
Licensed under MIT.