Python Reference
Python Basics: Data Science ![]() |
importing data ![]() |
python 3 (pt 1 of 2) ![]() |
python 3 (pt 2 of 2) ![]() |
Numpy Basics ![]() |
Numpy ![]() |
Matplotlib ![]() |
|
Pandas Basics ![]() |
Pandas (1 of 2) ![]() |
Pandas (2 of 2) ![]() |
Pandas ![]() |
%pdb on
auto-activate debugger on exception
%debug
starts the debugger after an errror
%xmode Verbose
sets error reporting to verbose
%xmode Plain
sets error reporting back to default
There are many more available commands for interactive debugging than we've listed here; the following table contains a description of some of the more common and useful ones:
Command | Description |
---|---|
list |
Show the current location in the file |
h(elp) |
Show a list of commands, or find help on a specific command |
q(uit) |
Quit the debugger and the program |
c(ontinue) |
Quit the debugger, continue in the program |
n(ext) |
Go to the next step of the program |
<enter> |
Repeat the previous command |
p(rint) |
Print variables |
s(tep) |
Step into a subroutine |
r(eturn) |
Return out of a subroutine |
For more information, use the help
command in the debugger, or take a look at ipdb
's online documentation.
from https://www.python.org/dev/peps/pep-3101/ and https://pyformat.info/
'1, 2, 3' '{0}, {1}, {2}'.format(1, 2, 3)
'1, 2, 3' '{}, {}, {}'.format(1, 2, 3)
'1, 2, 3' '{value1}, {value2}, {value2}'.format(value1=1, value2=2, value3=3)
'second' '{[1]}'.format(['first', 'second', 'third'])
'<stdin>' '{.name}'.format(sys.stdin)
'7 9' '{[a]} {[a]}'.format({'a':7},{'a':9})
'7 9' '{[1][0]} {[1][2]}'.format([[4,5,6],[7,8,9]])
'9' '{[1][2]}'.format([[4,5,6],[7,8,9]])
'9' '{0[1][2]}'.format([[4,5,6],[7,8,9]])
'9' '{[a][1][2]}'.format({'a':[[4,5,6],[7,8,9]]}) property accessing
'9' '{a[1][2]}'.format(**{'a':[[4,5,6],[7,8,9]]}) property accessing
'9' '{a[1][2]}'.format(a=[[4,5,6],[7,8,9]]) property accessing
'ba' '{:.2}'.format('bar')
'x ' '{:5}'.format('x') implicit left
'x ' '{:<5}'.format('x') explicit left
' x ' '{:^4}'.format('x') even # chars
' x ' '{:^5}'.format('x') odd # chars
' x' '{:>5}'.format('x') left padding
'x____' '{:_<5}'.format('x') w/chars left
'__x__' '{:_^5}'.format('x') w/chars center
'____x' '{:_>5}'.format('x') w/chars right
'_b_' '{:_^3.1}'.format('bar') w/ truncation
'33.1' '{:.3}'.format(33.14159)
'33.141' '{:.3f}'.format(33.14159)
' 3.14' '{:10.2f}'.format(3.14159)
' +3.14' '{:+10.2f}'.format(3.14159)
' -3.14' '{:10.2f}'.format(-3.14159)
' -3.14' '{:+10.2f}'.format(-3.14159)
'__-3.14___' '{:_^10.2f}'.format(-3.14159) implicit sign -
'__-3.14___' '{:_^+10.2f}'.format(-3.14159) explicit sign -
'__+3.14___' '{:_^+10.2f}'.format(3.14159) explicit sign +
'00+3.14000' '{:0^+10.2f}'.format(3.14159) works with 0's
'+ 23' '{:=+10d}'.format(23) padding after sign
'1.53' '{!s}'.format(1.53) str()
"'foo'" '{!r}'.format('foo') repr
'd9f' '{:x}'.format(3487) hex
'D9F' '{:X}'.format(3487) hex upper
'0xd9f' '{:#x}'.format(3487) hexadecimal w/prefix
'0xD9F' '{:#X}'.format(3487) hexadecimal w/prefix upper
'1100100' '{:b}'.format(100) binary
'0b1100100' '{#:b}'.format(100) binary w/ prefix
'100' '{:d}'.format(100) decimal
'1,000' '{:,}'.format(1000) decimal with commas
'144' '{:o}'.format(100) octal
'0o144' '{:#o}'.format(100) octal w/ prefix
'1.0e-2' '{:e}'.format(0.01) exponent
'1.0E-2' '{:E}'.format(0.01) exponent upper E
'66.000000%' '{:%}'.format(0.66) percentage
'd' '{:c}'.format(100) character
'1' '{:g}'.format(1) general
'1' '{:g}'.format(1.)
'1.2345' '{:g}'.format(1.2345)
'1' '{:n}'.format(1) general with locale-based separator
'1' '{:n}'.format(1.)
'1.2345' '{:n}'.format(1.2345)
'1' '{:}'.format(1) None, like g, but prints at least 1 digit
'1.0' '{:}'.format(1.)
'1.2345' '{:}'.format(1.2345)
'1.000000' '{:f}'.format(1) fixed point
'1.000000' '{:f}'.format(1.)
'1.234500' '{:f}'.format(1.2345)
'__+3.14___' '{a[1][0]:_^+10.2f}'.format(a=[[],[3.14159]]) combinations
'_+314.16%_' '{a[1][0]:_^+10.2%}'.format(a=[[],[3.14159]]) combinations
' x ' '{:{align}{width}}'.format('x', align='^', width='10') as params
'xy = 2.7' '{:{prec}} = {:{prec}}'.format('xyz', 2.7182, prec='.2') as params
'2001-02-03 04:05' '{:%Y-%m-%d %H:%M}'.format(datetime(2001, 2, 3, 4, 5))
class Scare(object):
def __format__(self, format):
if (format == 'boo'): return "AHHH!"
return 'zzzzz'
'AHHH!' '{:boo}'.format(Scare())
'zzzzz' '{:notboo}'.format(Scare())
import random
random.randint(0, 5) # 0|1|2|3|4|5
random.randrange(0, 5) # 0|1|2|3|4
random.randrange(0, 5, 2) # 0|2|4
random.random() # [0,...,1) num between 0 and 1, excluding 1
list(range(0,5)) # [0,1,2,3,4]
list(range(0,5,2)) # [0,2,4]