A memorandum for yourself that is easy to forget.
#Python fastest grammar
#reference
#Everyone's Python 3rd Edition
# [python]Year-end Thanksgiving Day! Introducing 50 Python Tips for Beginner Escape
# http://kwatch.houkagoteatime.net/blog/2013/12/07/python-tips/
#Encoding specification
# -*- coding: utf-8 -*-
#Code break =;(semicolon)
#Module import
import moduleName
from moduleName import functionName
from math import * # import all functions
from math import e
from math import e as newName
from math import (e, pi) #If you want to import more than one, enclose them in parentheses
##Module name can be omitted by using from statement
import math
math.sin(60)
from math import sin
sin(60)
##Creating a module file
## (Script files can be used as modules as they are)
# 1. moduleName.Write a function in py
# 2. import moduleName
#I want to break a line in the middle of a line: backslash at the end of the line(\)
import urllib, poplib, sys, calendar, \
datetime, re
#literal
b = 0b1000 #Binary number
c = 0x1ff #Hexadecimal
#Bool type
True
False
# Null
None
#Symbols are case sensitive
python = 100
Python = 200
print(python, Python) #=> 100 200
#Output to standard output(With line breaks)
print("abc")
print("abc", "def") #Connect with a half-width space
print("abc", file=sys.stderr) #Standard error output
#Concatenate without line breaks
for i in range(10):
print("abc", end = ' ') #=> abc abc abc abc abc abc abc abc abc abc
#Get from standard input
s = raw_input()
#s = input() # == eval(raw_input())
#Four arithmetic operations
x += 1
x -= 1
x *= 1
x /= 1
x = 2 ** 10 #Exponentiation
x = 3 / 2 #Return value = floating point
x = 3 // 2 #Return value = integer
x = 5 % 3 #Surplus
x++ #This way of writing cannot be used
x-- #This way of writing cannot be used
#String literal
s = "abc'def"
s = 'abc"def'
s = r'\d+\s\n' #raw string(Treat as as you see it)
#String manipulation
"abcdef"[0]
"abcdef"[0] = "x" # =>error
"abcdef"[-1]
"abcdef".title()
"abcdef".lower()
"abcdef".upper()
"abcdef".endswith('f')
"abcdef".startswith('a')
"abcdef".strip()
"abcdef".count('c')
"abcdef" + "ghijkl"
"abcdef" * 3
"abcdef".replace("abc","replaced")
"abcdef".find('x') #=> -1
"abcdef".index('x') #=> ValueError
#Left justified, right justified, center justified
"abcdef".ljust(10," ")
"abcdef".rjust(10," ")
"abcdef".center(10," ")
"abc def".split(" ")
" ".join(["abc","def"])
len("abcdef")
ord("a")
chr(52)
if "a" in "abcdef":
do_something()
#String format
"{0} {1} {0}".format("Mika", "Mike")
"{name0} {name1} {name0}".format(name0="Mika", name1="Mike")
"{:.2%}".format(6260 / 12776) #% Notation
#Value exchange(Generate tuples without parentheses)
b, a = a, b
#Here document
lines = '''
in this way
Multiple lines
Can be described
If you don't want to insert line breaks\
Backticks at the end(\)With
'''
#If you want to keep the indent()use(No comma)
lines = ("aaaaaa"
"bbbbb"
"ccccc")
#Conditional operator(Ternary operator)
return "odd" if odd(x) else "even"
s = "odd" if odd(x) else "even"
#Type conversion
int("123")
hex(1023)
bin(15)
oct(7)
float("1.3")
str(123)
tuple([1,2,3])
list((1,2,3))
#Base conversion
int("0x100", 16)
int("0b1111", 2)
int("0o1777", 8)
#Bit operation
x | y # or
x & y # and
x ^ y # xor
x << y # left shift
x >> y # right shift
#function
def functionname(x):
"""
Function comment
"""
do_something()
return result
def moveTo(x,y=0): #Default value of argument
do_something()
moveTo(x=1, y=20) #Argument keyword specification
#Conditional branch
if s == "abc": # =not==
statementA()
elif s == "def":
statementB()
else:
statementC()
if min <= value <= max: #You can write comparison operators consecutively
do_something()
#Multiple return values(Unpack assignment)
def foo():
minValue = 1
maxValue = 9
return minValue, maxValue
minValue, maxValue = foo()
(minValue, #minimum value
maxValue, #Maximum value
) = foo()
#Variadic
def foo(a, b, *vals):
print(a, b, vals)
foo(1, 2, 3, 4) # => 1, 2, (3, 4)
#Variable keyword argument
def foo(a, b, **args):
print(a, b, args)
foo(1, 2, c=3, d=4) # => 1, 2, {'c': 3, 'd': 4}
#Conditional operator:== != > < <= >= not in
#Logical operator: and or xor
#switch statement is not in python
#Repeat (specified number of times)
for i in range(6): # i = 0 to 5
do_something()
for i in range(1, 10, 2): #start,End,Step i= 1,3,5,7,9
do_something()
#Loop control
continue
break
#While statement
while condition:
do_something()
#There is no do ~ while in python
for loopCounter, item in enumerate(seq): #With loop counter
print(loopCounter, item)
for no, name in zip([1, 2, 3, 4], ['name1', 'name2', 'name3', 'name4']): #Combine two sequences
print(no, name)
#Exception handling
try:
if condition:
raise Exception() #Intentionally raise an exception
except Exception as e:
#Error handling
msg = "{1}(err={2})".format(e.errno, e.strerror)
sys.stderr.write(msg)
else:
#Processing when an exception does not occur
finally:
#Post-processing (always executed)
try:
...
except Exception as e:
print e, 'error occurred'
#with statement:Make block execution efficient
#If the process fails, do not execute the block
with open(fn) as f: #If it fails here, the processing inside the block will not be executed.
for line in f :
print(line)
##Handle exceptions in the traceback module
import traceback
try:
do_something()
except:
traceback.print_exc() #Show exception
message = traceback.format_exc() #Get as a string
#list
a = [1, 2, 3, 4, 5]
a[0]
a[-1] #If it is a negative number, from the end
a[5] # => Error
#Slice: Gives the last index +1 of the element you want to retrieve
a[1:3] #=> a[1] ~ a[3 - 1]Get up to=> [2, 3]
a[2:] #=> [3, 4, 5]
a[:3] #=> [1, 2, 3]
a[2:100] #=> [3, 4, 5]
a * 2 #=> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
a + [6,7] #=> [1, 2, 3, 4, 5, 6, 7]
a[1] = "x" #=> [1, "x", 3, 4, 5, 6, 7]
del a[1] #=> [1, 3, 4, 5, 6, 7]
a.index(1)
if 1 in a:
do_something()
max(a)
min(a)
a.sort() #Be careful as it will be rewritten
a.sort(key=compareFuncName)
a.reverse() #Be careful as it will be rewritten
a.append(6) # add()is not
a.extend([6, 7, 8])
a.remove(1)
a.pop(0)
#Multidimensional array
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix[0][2]
#Tuple
# (Non-rewritable list/Memory efficiency/Can be used as a dictionary key)
t = (1, 2, 3, 4, 5)
#Others are the same as the list
#Example of usage
ipmap = {(192.168.0.1):"host1.name.com",
(192.168.0.2):"host2.name.com",}
#dictionary
dict = { "key1":"Tokyo", "key2":"Osaka" }
dict["key1"]
# .get():Even if there is no key, no error will occur and the specified value will be returned.
for word in lines.split():
wordcount[word] = wordcount.get(word, 0) + 1
dict["key1"] = "Hokkaido"
del dict["key1"]
dict.keys()
dict.values()
dict.items() # key,Returns a list of tuples of value
if "key1" in dict:
do_something()
#Synthetic
dict1.update(dict2) #Overwrite the same key
#set
s = {1, 2, 3, 4}
s2 = {5, 6}
s.remove(7) # KeyError
s.discard(7) # no error
s.add(7)
s |= {7}
if s == s2:
do_something()
if 1 in s:
do_something()
s.union(s2) # s |s2 union
s.intersection(s2) # s &s2 common set
s.difference(s2) # s -s2 difference set
s.symmetric_difference(s2) # s ^s2 symmetric difference set
# map(Convert each element of the sequence)
map1 = map(str, range(5))
#Null judgment
if x is (not) None:
do_something()
#File reading
f = open("foo.txt", "r", encoding="utf-8")
s = f.read()
line = f.readline() #1 line
lines = f.readlines() #All lines
f.close()
#File writing
f = open("foo.txt", "w", encoding="utf-8")
f.write(s)
f.writelines(seq) #Note that no line breaks are added
f.close()
#Comprehension
sq = [x ** 2 for x in range(5)]
sq #=> [0, 1, 4, 9, 16]
val = 10
[x for x in range(1,val) if val % x == 0] #=> [1, 2, 5]
filter2 = [x for x in ['cat','dog', 'bird'] if 'cat' in x]
filter2 #=> ['cat']
tz = {"GMT" : "+000", "BST" : "+100",
"EET" : "+200", "JST" : "+900"}
revtz = {value : name for name, value in tz.items()}
revtz #=> {'+200': 'EET', '+100': 'BST', '+000': 'GMT', '+900': 'JST'}
names = ['BOB', 'Burton', 'dave', 'bob']
unames = {x.title() for x in names}
unames #=> {'Bob', 'Dave', 'Burton'}
#Command to do nothing
pass
#Unit test
from Hoge import * #Test target
import unittest
class HogeClassTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def testFooMethod(self):
self.assertEqual(1, Hoge().foo())
if __name__ == "__main__":
unittest.main()
#Chord piece time measurement
import timeit
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
#Get command line
for arg in sys.argv[1:] # argv[0]=The name of the script itself
print(arg)
#Command line option analysis
from arse import OptionParser
VERSION = '1.01'
def main():
version = '%prog version ' + VERSION
usage = "usage: %prog [options] directories..."
description = 'Delete empty directories.'
parser = OptionParser(version=version, usage=usage,
description=description)
parser.add_option("-f", "--file", dest="filename",
help="read data from FILENAME")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
if options.verbose:
print "reading %s..." % options.filename
if __name__ == "__main__":
main()
#Iterator (external iterator)
# (All built-in objects can be converted to iterators)
# (It is troublesome to define a class to define it)
i = iter([1, 2, 3]) #Convert to an iterator object
next(i) #Extract the next element(StopIteration exception if the element is gone)
for line in open("test.txt"): #Process each line of the file
print(line, end=" ")
#Generator (internal generator)
# (Define iterator-like functions using functions)
#Use yield statement
def get_primes(x=2):
while True:
for i in range(2, x):
if x % i == 0:
break
else:
yield x
x += 1
i = get_primes()
for c in range(10):
print(next(i))
#Generation by generator formula
gen = (ord(s) for s in 'Python')
next(gen)
for code in (ord(s) for s in 'Python'):
print(code)
#Unit test
#TODO:Write later
#Lambda expression
lambda x: x * 10
#Block to execute only when executed in main
# (Not imported from other scripts)
if __name__ == '__main__':
do_something()
#Class definition and instantiation
class Person(SuperClassName): #Class name is uppercase
def __init__(self, name, age): #Initialization(1st argument self required)
self.name, self.age = name, age
self.address = None
bob = Person('Bob', 15)
print bob.name
bob.address = 'USA'
##Class inheritance (multiple inheritance possible)
class Person(SuperClassName):
def __init__(self, width):
super().__init__() #The call to the parent class is super()
self.width = width
#Restrictions on adding attributes (instance variables)
class Klass:
__slot__ = ['width', 'height'] #Attributes not written here cannot be added
def __init__(self):
self.width = 0
self.height = 0
i = Klass()
i.width = 1
i.c = 0 # AttributeError
#Hiding methods and attributes (encapsulation) Add _ to the beginning of the name
self._size #Inaccessible from outside the class
self.__size #Inaccessible from outside the class
## Setter / Getter(Dedicated method to modify or reference data)
##Property(Setter /Function to easily define Getter)
##Prevent rewriting from outside the class like attributes
class Prop(object):
def __init__(self):
#The name used internally is different from the property name x(Infinite loop prevention)
self.__x = 0
def getx(self): # Getter
return self.__x
def setx(self, x): # Setter
self.__x = x
#Getter for property x,Set Setter
x = property(getx, setx)
i = Prop()
i.x = 0
##Operator aura ride(redefinition)
__add__(self, other) # + (self + other)
__sub__(self, other) # -
__mul__(self, other) # *
__truediv__(self, other) # /
__floordiv__(self, other) # //
__iadd__(self, other) # +=
__isub__(self, other) # -=
__imul__(self, other) # *=
__itruediv__(self, other) # /=
__ifloordiv__(self, other) # //=
__or__(self, other) # | (or)
__and__(self, other) # & (and)
##Comparison operator aura ride
## equal, not equal, lesser than, greater than, ...
__eq__(self, other) # ==
__ne__(self, other) # !=
__lt__(self, other) # < (self < other)
__gt__(self, other) # >
__le__(self, other) # <=
__ge__(self, other) # >=
##Type conversion override
__int__(self) # int()
__bytes__(self)
__float__(self)
__str__(self)
__repr__(self) #Convert to printable character string representation
__format__(self, form_spec) # format()Redefining the format in
##Special method used in container type
__len__(self) # len()
__getitem__(self, key) # obj[key]
__setitem__(self, key, value) # obj[key] = value
__delitem__(self, key) # del obj[key]
__iter__(self) # iter() :Returns an iterator object
# (Inside__next_()Need to define a method called)
__getattr__(self, attributeName) #Does not exist in the object
#Called when an attribute is referenced
#If you want the attribute to be non-existent
# raise AttributeError
__getattrubute__(self, attributeName) #Called unconditionally__getattr__
__setattr__(self, attributeName, value) #Called when assigning
__call__(self[, args...]) #The name of the object is followed by parentheses
#Called when called as a function
__del__(self) #Destructor
__hash__(self) # hash()Returns the hash value as an integer
##Object type determination
isinstance(var, type) #Type judgment(ype= str, int, float, ...)
##Get attributes (including methods)
s = "abcde"
s.find("cd")
getattr(s, "find")("cd") # find()Call the method
#Standard library
##Get data from web server
from urllib import request
src = request.urlopen('http://www.python.org/').read()
from urllib import request, parse
url = 'http://www.python.org/somefile.zip'
filename = parse.urlparse(url)[2].split('/')[-1]
request.urlretrieve(url, filename)
##Ordered dictionary
from collections import OrderedDict
od = OrderedDict()
##Dictionary with default value
from collections import defaultdict
animals = [('Cat', 'Maine Coon'),
('Cat', 'Abyssinian'),
('dog', 'Pug'),
('dog', 'Siberian husky'),
('dog', 'bulldog') ]
dd = defaultdict(list) #A dictionary with an empty list as the initial value
for k, v in animals :
dd[k].append(v)
dd # => defaultdict(<class 'list'>, {'Cat': ['Maine Coon', 'Abyssinian'], 'dog': ['Pug', 'Siberian husky', 'bulldog']})
##Assistance in adding elements to a sorted list
import bisect
##Handle date and time
import datetime
datetime.date.today() #today
datetime.datetime.now() #Current date and time
datetime.datetime(2014, 3, 2, 16, 32, 00)
datetime.time(16, 32, 00)
datetime.date(2014, 3, 2)
d = datetime.date(2014, 3, 2)
d.year #2014
d.month # 3
d.day # 2
d.weekday() # 6:Daily day
d.strftime('%Y/%m/%d %H:%M:%S') # '2014/03/02 00:00:00'
d = datetime.strptime('2014/03/02 00:00:00', '%Y/%m/%d %H:%M:%S')
###Date and time difference(Comparison and multiplication / division are also possible)
delta = datetime.timedelta(days=5)
d1 = datetime.date(2014, 2, 6)
d2 = d1 + delta
print(d2) # 2014-02-11
d2 = datetime.date(2014, 3, 2)
d1 = datetime.date(2014, 2, 6)
d2 = datetime.date(2014, 3, 2)
delta = d2 - d1
print(delta) # 24 days, 0:00:00
##Get information about the calendar
import calendar
###Day of the week and number of days in the specified year and month
(days, first_weekday) = calendar.monthrange(2014, 2)
print(days, first_weekday) #5 28
calendar.month(2014, 2) #Output calendar
##Regular expressions
import re
re.findall(r'[abcde]', 'abcdefg') # ['a', 'b', 'c', 'd', 'e']
re.split(r',', 'ab,cde,fg') # ['ab', 'cde', 'fg']
re.sub(r'a', 'b', 'abcde') # 'bbcde'
match = re.search(r'a', 'abcdea')
print(match)
match = re.match(r'a', 'abcdea')
print(match)
for match in re.finditer(r'[a-z]+', 'This is a pen.'):
print(match.group(0), end =' ') # his is a pen
s = 'abcd,efgh,ijkl'
r = re.compile(r'[abcde]', re.IGNORECASE | re.MULTILINE | re.DOTALL)
r.findall(s)
##Get and operate system parameters
import sys
sys.argv[0] #The running script itself
sys.argv[1:] #Command line parameters
sys.exit(0)
sys.getdefaultencoding()
##Acquire and operate OS-dependent information such as file systems and processes
import os
os.getenv(key, default=None)
os.chdir(path)
os.getcwd()
os.remove(path)
os.rename(src, dest)
os.mkdir(path, mode)
os.mkdirs(path, mode) #Create a path along the way
os.rmdir(path)
os.removedirs(path) #Delete in the middle
os.path.exists(path)
os.path.getsize(path)
os.path.isfile(path)
os.path.isdir(path)
os.path.join(path, path2, ...)
dirname, filename = os.path.split(path)
path = r'c:\users\default\desktop.ini'
os.path.dirname(path) #Directory name
os.path.basename(path) #file name
path_without_ext, ext = os.path.splitext(path) #extension
##Math module
import math
math.pi
mati.sqrt(2)
mati.sin(60)
mati.cos(60)
mati.tan(60)
##random number
import random
random.seed(value) #Random seed
x = random.random() # 0 <= x <= 1
y = random.randint(1, 10) #Integer from 1 to 10
###Walk across the hierarchy
for dirpath, dirnames, filenames in os.walk(path, topdown=True, oneerror=None):
do_something()
###process/Execution of external command
exit_status = os.system(command) #Run the process and wait until it finishes(Returns an exit code)
os.startfile(path) #Windows Start command
import commands
stdout_text = commands.getoutput(command) #Run the process and wait until it finishes(Returns standard output)
IPython installation
>pip install ipython
Launch IPython
>ipython
Launch IPython Notebook
>ipython notebook --inline
Recommended Posts