the methode "methode1" is automatically inherites from Student class. Input function in Python ------------------------- n = input('Please enter a number') remember that input() function retuens a str not a number Use cases for sets in Python ----------------------------- we use sets in python or convert a list to set using the set() function mostly to benifit from the intersection or union functions in sets. Context manager in Python -------------------------- in opening and closing files: with open('filename','rw') as file: file_content = file.read() Impoting in python ------------------- we can use either ways: import module module.function() from module import function function() Using sqlite with python
# Created: 2017-06-10 # # Copyright (C) 2017 Taishi Matsumura # import platform import matplotlib.pyplot as plt import module import cythonized_module plt.close('all') N = 1000 s = module.StopWatch() s.start() module.function(N) s.lap() cythonized_module.function_cy(N) s.lap() cythonized_module.function_cy_cdef(N) s.stop() x = xrange(3) y = [s.laps[1][0], s.laps[2][0], s.laps[3][0]] labels = ['Pure python', 'Cython', 'Cython\n+cdef'] text = ''' Task 1 ({0} loops) Platform : {1} Version : {2} CPU : {3}
print f.read() 自定义 #this goes in mystuff.py def function1(): print "You are using function1" #this just a variable in the .py file target = "xxx" \\ import mystuff: mystuff.function1()#调用函数 print mystuff.target#调用变量 \\ from mystuff import function1: function1()#调用函数 print target#调用变量 ''' #三种方法从某个东西里获取它的内容 #dict style print dict[key] -> value #module style import module print module.variable -> variable module.function(argument) #class style instance = class() print instance.attribute -> attribute instance.function(argument)
return 0 print greater_less_equal_5(4) print greater_less_equal_5(5) print greater_less_equal_5(6) #================================================================================= # functions def function_name(input): return something # modules import module # display the result of the use print module.function() # importing only the specific function, to be used w/t module. from module import function from module import * # for all functions; avoid, b/c overwrite existing ones #--------------------------------------------------------------------------------- # anonymous functions lambda x: x % 3 == 0 # same as def by_three(x): return x % 3 == 0 # lambda + filter = do it for each element in languages = ["HTML", "JavaScript", "Python", "Ruby"]
# ---------------------------------------------- modules.py -> module/package/library <collection of functions> def function() return <expr> if __name__ == '__main__': <test code> --------------------------------------- import module module.function() # ---------------------------------------------- Special functions: map(), filter(), zip() Lambda function: fobj = lambda x,y : x + y Special modules: datetime -> now(), strftime(), t1 + t2, <format strings> functools -> reduce() itertools -> permutation(), combination() collections -> Counter() operator -> itemgetter() # ----------------------------------------------
import module module.function(arguments) from module import function function(arguments)
# reload, from, 대화형 테스트 from module import function function(1, 2, 3) from imp import reload reload(module) from imp import reload import module reload(module) function(1, 2, 3) from imp import reload import module reload(module) from module import function # 또는 포기하고 module.function() 사용 function(1, 2, 3)