def f(): # Expressions 1 3.0e-12 "String" 'String' """String""" '''String''' "''''" '""""' "" '' """""" '''''' "a" 'b' """c""" '''d''' """\ """ "\ " "\\\'\"" "\a\b\f\n\r\t\v" "\000\777\00\77\0\7" "\778\788\888" "\x00\xff" b'hello' b"world" 'hello' "world" b'\n' br'\n' "\u0000\u1000" "\U00000000" "\U00001000" 'ಠ_ಠ' '''a''' '''b''' '''a\''' b''' (2) {} [] [3] [4,5,6] [7,8,9,] [x for y in z] [u for v in w if c] (x for y in z) 1,2 a if b else c lambda a: 0 lambda *a: 0 lambda: 0 def g(): yield 1,2 a = yield b, yield d > e <= f is not g != h in i j + k - l / m * n % o // p | q ^ r & s << t >> u v and w or x y()(z)(a,b)(c,)[d][::][e::][::f][g::h][i:j:][k:l:m][n:][o:][p:q][:][:,r][s,t] del u[:,v] del w[x,y] a(b=c)(d,e=f)(*g)(**h)(i,j=k,*l,**m) f(x for y in z) (x for y in z for a in b if c) [x for y in z for a in b if c] (x for y in z if a if b) [x for y in z if a for y in z if a] {x:y for z in w} {x for y in z} not n +-o (1,2) (1,2,) () {a:1,b:2} {b:3,c:4,} {a:1} {} {x,y,z} # Statements 1 1; 1;2 1;2; # Indent problems in editor... while p: break while q: continue r = s t = u = v (*w,) = y,*z,a = b t += u v # expr... while w: return while x: return y while z: return a,b while c: return d,e, del a del a,b, del a[b][c] while f: raise g while f: raise while f: raise g from h while h: pass assert i assert i,i global gj global gk,gl,gm la = 1 lb = 1 lc = 1 ld = 1 def h(): nonlocal la nonlocal lb,lc,ld with a, b, c: pass with d as e, f as g, h: pass if a: 1 if a: 1 else: 2 if a: 1 elif b: 2 if a: 1 elif b: 2 elif c: 3 else: 4 try: pass finally: pass try: pass except p: pass except q: pass except: pass else: pass finally: pass try: pass except: pass finally: pass try: pass except t: pass except u: pass else: pass while v: pass while w: pass else: pass for a in b: pass for c in d: pass else: pass for e,f in g,h: pass def i(): pass def j(k,l,m): pass def j(n,o,p,): pass def q(r,*s): pass def t(u,v=w): pass def t(x,y=z,): pass def a(**b): pass def c(d,**e): pass def f(*,g=h): pass def m(h,i=j,*,k=l,m,**n):pass @k def l(): pass @m() @n(o) @p.q(r,s=t,*u,**v) def w(x,y=z,*a,**b): pass class q: pass class r(s,t): pass class u(v,w,): pass class x(y,z=a,*b,**c): pass class d(): pass @k class l(): pass @m() @n(o) @p.q(r,s=t,*u,**v) class w(x,y=z,*a,**b): pass import a,b as c,d.e as f from c.d import (e,f) from ... import g as h, i
""" # FIXME: Some comment about why this function is crap but still in production. import inner_imports if inner_imports.are_evil(): # Explains why we have this if. # In great detail indeed. x = X() return x.method1() # type: ignore # This return is also commented for some reason. return default # Explains why we use global state. GLOBAL_STATE = {"a": a(1), "b": a(2), "c": a(3)} # Another comment! # This time two lines. class Foo: """Docstring for class Foo. Example from Sphinx docs.""" #: Doc comment for class attribute Foo.bar. #: It can have multiple lines. bar = 1 flox = 1.5 #: Doc comment for Foo.flox. One line only. baz = 2
import a def b(): return 2 print b() print a()
import numpy as np a = np.arange(1,10).reshape (3,3) b = np.arange(10,19).reshape (3,3) c = np.dot(a,b) print(c) e = np.sum(c, axis = 0) print(e) Create two arrays a and b, stack these two arrays vertically use the np.dot and np.sum to calculate totals row a = np.arange(1,5).reshape (2,2) b = np.arange(5,9).reshape (2,2) c = np.dot(a,b) print(c) e = np.sum(c, axis = 1) print(e) In two arrays a ( 1,2,3,4,5) and b ( 4,5,6,7,8,9) – remove all repeating items present in array b import numpy as np arrays = [[1,2,3,4,5], [4,5,6,7,8,]] unique_numbers = np.unique(arrays) print(unique_numbers) Get all items between 5 and 10 from a and b and sum them together arrays = [[1,2,3,4,5], [4,5,6,7,8,]] unique_numbers = np.unique(arrays) sum_together = np.sum(unique_numbers[4:9]) print(unique_numbers[4:9], sum_together)
class C(a(b)): d = 1 def e(self): f = c + 1 return f
""" # FIXME: Some comment about why this function is crap but still in production. import inner_imports if inner_imports.are_evil(): # Explains why we have this if. # In great detail indeed. x = X() return x.method1() # type: ignore # This return is also commented for some reason. return default # Explains why we use global state. GLOBAL_STATE = {'a': a(1), 'b': a(2), 'c': a(3)} # Another comment! # This time two lines. class Foo: """Docstring for class Foo. Example from Sphinx docs.""" #: Doc comment for class attribute Foo.bar. #: It can have multiple lines. bar = 1 flox = 1.5 #: Doc comment for Foo.flox. One line only.
class like: def life(self): #object is passed as a argument print('make code easy') l=like() l.life() #make code easy ## class with in class class a: def lie(self): #object is passed as a argument print('make code easy') class b: def life(self): print('python code') s=a() s.lie() #s.life() o/p 'a' object has no attribute 'life' k=s.b() k.life() #python code #**************************************************************************** """constructor --used for initailisation purpose --it is called at the time of object creation --default constructor -- with out arguments --parameterized construction -- with passing parameters """
f() except Spam as err: p() try: f() except Spam: g() except Eggs: h() except (Vikings+Marmalade) as err: i() try: a() except Spam: b() except Spam2: c() finally: g() try: a() except: b() else: c() try: a() except: b() else: c() finally: d()
assert False if '##__import__': ''' Backend for the `import` statement. Import module with name that cannot be variable, e.g. hyphens in executables: test_mod = __import__('test-mod') ''' if 'Magic methods dont work': try: assert a() == 'a.__call__()' except TypeError: pass else: assert False if '##submodules': if 'Submodule vs attribute': # *never define in your __init__ file an attribute which has the same name as a module* import d.a assert d.a.a == 'd.a' #TODO1
#파이썬에서는 import라고 하면 뒤에 불러올 파이썬 파일을 찾음!! import a import B #import a, B라고 하는 걸 더 권장 print(a.a()) print(B.B()) #yunjin이라는 모듈에서 a 함수만 불러오고 싶을 땐? from yunjin import a print(a()) #이렇게 가져오면 yunjin을 명시할 필요X #아예 모듈을 가져오면 명시해야 하지만... from yunjin import B as print_B #B를 print_B라는 이름으로 가져오고 싶다. print(print_B()) import a as print_a # 파일 이름인 모듈 a를 print_a라는 함수 이름으로 쓰고 싶다 print(print_a.a()) #from 없이 import만 있으면 모듈을 가져오는 것이고, from과 함께 있으면 함수만 가져오는 것! #import가 가져오는 건 모듈일 수도, 함수일 수도 있다!
# from __future__ import division # def_op('STOP_CODE', 0) # ignore # def_op('POP_TOP', 1) a() # def_op('ROT_TWO', 2) (a, b) = (b, a) # def_op('ROT_THREE', 3) (a, a, a) = (a, a, a) # def_op('DUP_TOP', 4) exec 1 # def_op('ROT_FOUR', 5) a[2:4] += 'abc' # def_op('NOP', 9) # ignore # def_op('UNARY_POSITIVE', 10) + a # def_op('UNARY_NEGATIVE', 11) - a # def_op('UNARY_NOT', 12) not a
def f(): # Expressions 1 3.0e-12 "String" "String" """String""" """String""" "''''" '""""' "" "" """""" """""" "a" "b" """c""" """d""" """\ """ "\ " "\\'\"" "\a\b\f\n\r\t\v" "\000\777\00\77\0\7" "\778\788\888" "\x00\xff" b"hello" b"world" "hello" "world" b"\n" br"\n" "\u0000\u1000" "\U00000000" "\U00001000" "ಠ_ಠ" """a""" """b""" """a''' b""" (2) {} [] [3] [4, 5, 6] [7, 8, 9] [x for y in z] [u for v in w if c] (x for y in z) 1, 2 a if b else c lambda a: 0 lambda *a: 0 lambda: 0 def g(): yield 1, 2 a = yield b, yield d > e <= f is not g != h in i j + k - l / m * n % o // p | q ^ r & s << t >> u v and w or x y()(z)(a, b)(c)[d][::][e::][::f][g::h][i:j:][k:l:m][n:][o:][p:q][:][:, r][s, t] del u[:, v] del w[x, y] a(b=c)(d, e=f)(*g)(**h)(i, j=k, *l, **m) f(x for y in z) (x for y in z for a in b if c) [x for y in z for a in b if c] (x for y in z if a if b) [x for y in z if a for y in z if a] {x: y for z in w} {x for y in z} not n +-o (1, 2) (1, 2) () {a: 1, b: 2} {b: 3, c: 4} {a: 1} {} {x, y, z} ... # Statements 1 1 1 2 1 2 # Indent problems in editor... while p: break while q: continue r = s t = u = v (*w,) = y, *z, a = b t += u v # expr... # Allowed assign/augassign/del/generator targets... a = b (c, d) = e [f, g] = h [i, *j, k] = l m.n = o p[q] = r s[t:u:v] = w [] = x while w: return while x: return y while z: return a, b while c: return d, e del a del a, b, del a[b][c] while f: raise g while f: raise while f: raise g from h while h: pass assert i assert i, i global gj global gk, gl, gm la = 1 lb = 1 lc = 1 ld = 1 def h(): nonlocal la nonlocal lb, lc, ld with a, b, c: pass with d as e, f as g, h: pass if a: 1 if a: 1 else: 2 if a: 1 elif b: 2 if a: 1 elif b: 2 elif c: 3 else: 4 try: pass finally: pass try: pass except p: pass except q: pass except: pass else: pass finally: pass try: pass except: pass finally: pass try: pass except t: pass except u: pass else: pass while v: pass while w: pass else: pass for a in b: pass for c in d: pass else: pass for e, f in g, h: pass def i(): pass def j(k, l, m): pass def j(n, o, p): pass def q(r, *s): pass def t(u, v=w): pass def t(x, y=z): pass def a(**b): pass def c(d, **e): pass def f(*, g=h): pass def m(h, i=j, *, k=l, m, **n): pass def f(a: b, c: d = e, *f: g, h: i, j: k = l, **m: n): pass @k def l(): pass @m() @n(o) @p.q(r, s=t, *u, **v) def w(x, y=z, *a, **b): pass class q: pass class r(s, t): pass class u(v, w): pass class x(y, z=a, *b, **c): pass class d: pass @k class l: pass @m() @n(o) @p.q(r, s=t, *u, **v) class w(x, y=z, *a, **b): pass import a, b as c, d.e as f from c.d import e, f from ... import g as h, i
def f(): # constants a = None + False + True + ... a = 0 a = 1000 a = -1000 # constructing data a = 1 b = (1, 2) c = [1, 2] d = {1, 2} e = {} f = {1:2} g = 'a' h = b'a' # unary/binary ops i = 1 j = 2 k = a + b l = -a m = not a m = a == b == c m = not (a == b and b == c) # attributes n = b.c b.c = n # subscript p = b[0] b[0] = p # slice a = b[::] # sequenc unpacking a, b = c # tuple swapping a, b = b, a a, b, c = c, b, a # del fast del a # globals global gl gl = a del gl # comprehensions a = (b for c in d if e) a = [b for c in d if e] a = {b:b for c in d if e} # function calls a() a(1) a(b=1) a(*b) # method calls a.b() a.b(1) a.b(c=1) a.b(*c) # jumps if a: x else: y while a: b while not a: b # for loop for a in b: c # exceptions try: while a: break except: b finally: c # with with a: b # closed over variables x = 1 def closure(): a = x + 1 x = 1 del x # import import a from a import b from a import * # raise raise raise 1 # return return return 1
""" # FIXME: Some comment about why this function is crap but still in production. import inner_imports if inner_imports.are_evil(): # Explains why we have this if. # In great detail indeed. x = X() return x.method1() # type: ignore # This return is also commented for some reason. return default # Explains why we use global state. GLOBAL_STATE = {"a": a(1), "b": a(2), "c": a(3)} # Another comment! # This time two lines. class Foo: """Docstring for class Foo. Example from Sphinx docs.""" #: Doc comment for class attribute Foo.bar. #: It can have multiple lines. bar = 1 flox = 1.5 #: Doc comment for Foo.flox. One line only.
print(line) import sys print(sys.path) #sys.path.append(r'C:\pythontraining\lib') print(line) import addmodule as a print(a.msg) print(a.add(10, 20)) print(line) from addmodule import msg, add print(msg) print(add(10, 20)) print(line) from addmodule import msg as m, add as a print(m) print(a(10, 20)) print(line) from addmodule import * print(msg) print(add(10, 20)) print(line) import project.net.addmodule as a print(a.msg) print(a.add(10, 20)) print(line)
def f(): x = (yield 1) # Attribute ref x = a.b # Subscript - basic, slice, long slide, ellipsis, tuple x = a[0] x = a[0:1], a[0:], a[:1] x = a[0:1:2], a[0:1:], a[0::2], a[:1:2], a[0::], a[:0:], a[::0], a[::] x = a[...] x = a[0, 1:2, 3:4:5, ...] # Call - arg, kwarg, arglist, kwarglist a(a, b=2, *c, **d) # Pow x = a**b # Invert, negate, pos x = ~a, -b, +c # mul, div, mod x = a * b x = a / b x = a % b # add, sub x = a + b x = a - b
class C(a(b)): d = 1
def f(): # Expressions 1 3.0e-12 "String" 'String' """String""" '''String''' "''''" '""""' "" '' """""" '''''' "a" 'b' """c""" '''d''' """\ """ "\ " "\\\'\"" "\a\b\f\n\r\t\v" "\000\777\00\77\0\7" "\778\788\888" "\x00\xff" b'hello' b"world" 'hello' "world" b'\n' br'\n' "\u0000\u1000" "\U00000000" "\U00001000" 'ಠ_ಠ' '''a''' '''b''' '''a\''' b''' (2) {} [] [3] [4,5,6] [7,8,9,] [x for y in z] [u for v in w if c] (x for y in z) 1,2 a if b else c lambda a: 0 lambda *a: 0 lambda: 0 def g(): yield 1,2 a = yield b, yield d > e <= f is not g != h in i j + k - l / m * n % o // p | q ^ r & s << t >> u v and w or x y()(z)(a,b)(c,)[d][::][e::][::f][g::h][i:j:][k:l:m][n:][o:][p:q][:][:,r][s,t] del u[:,v] del w[x,y] a(b=c)(d,e=f)(*g)(**h)(i,j=k,*l,**m) f(x for y in z) (x for y in z for a in b if c) [x for y in z for a in b if c] (x for y in z if a if b) [x for y in z if a for y in z if a] {x:y for z in w} {x for y in z} not n +-o (1,2) (1,2,) () {a:1,b:2} {b:3,c:4,} {a:1} {} {x,y,z} ... # Statements 1 1; 1;2 1;2; # Indent problems in editor... while p: break while q: continue r = s t = u = v (*w,) = y,*z,a = b t += u v # expr... # Allowed assign/augassign/del/generator targets... a = b (c,d) = e [f,g] = h [i,*j,k] = l m.n = o p[q] = r s[t:u:v] = w [] = x while w: return while x: return y while z: return a,b while c: return d,e, del a del a,b, del a[b][c] while f: raise g while f: raise while f: raise g from h while h: pass assert i assert i,i global gj global gk,gl,gm la = 1 lb = 1 lc = 1 ld = 1 def h(): nonlocal la nonlocal lb,lc,ld with a, b, c: pass with d as e, f as g, h: pass if a: 1 if a: 1 else: 2 if a: 1 elif b: 2 if a: 1 elif b: 2 elif c: 3 else: 4 try: pass finally: pass try: pass except p: pass except q: pass except: pass else: pass finally: pass try: pass except: pass finally: pass try: pass except t: pass except u: pass else: pass while v: pass while w: pass else: pass for a in b: pass for c in d: pass else: pass for e,f in g,h: pass def i(): pass def j(k,l,m): pass def j(n,o,p,): pass def q(r,*s): pass def t(u,v=w): pass def t(x,y=z,): pass def a(**b): pass def c(d,**e): pass def f(*,g=h): pass def m(h,i=j,*,k=l,m,**n):pass def f(a:b, c:d=e, *f:g, h:i, j:k=l, **m:n): pass @k def l(): pass @m() @n(o) @p.q(r,s=t,*u,**v) def w(x,y=z,*a,**b): pass class q: pass class r(s,t): pass class u(v,w,): pass class x(y,z=a,*b,**c): pass class d(): pass @k class l(): pass @m() @n(o) @p.q(r,s=t,*u,**v) class w(x,y=z,*a,**b): pass import a,b as c,d.e as f from c.d import (e,f) from ... import g as h, i
assert f() == [0, 1, 2, 3, 4, 5, 6, 7, 8] assert a == 8 # nested function scopes def f(method, arg): def cb(ev): return method(ev, arg) return cb def g(*z): return z a = f(g, 5) b = f(g, 11) assert a(8) == (8, 5) assert b(13) == (13, 11) # nonlocal and global x = 0 def f(): x = 1 res = [] def g(): global x return x res.append(g()) def h(): nonlocal x return x
keras.__version__ Let's start by loading the fashion MNIST dataset. Keras has a number of functions to load popular datasets in `keras.datasets`. The dataset is already split for you between a training set and a test set, but it can be useful to split the training set further to have a validation set: fashion_mnist = keras.datasets.fashion_mnist (X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data() The training set contains 60,000 grayscale images, each 28x28 pixels: X_train_full.shape Each pixel intensity is represented as a byte (0 to 255): X_train_full.dtype Let's split the full training set into a validation set and a (smaller) training set. We also scale the pixel intensities down to the 0-1 range and convert them to floats, by dividing by 255. X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255. y_valid, y_train = y_train_full[:5000], y_train_full[5000:] X_test = X_test / 255. You can plot an image using Matplotlib's `imshow()` function, with a `'binary'` color map: plt.imshow(X_train[0], cmap="binary") plt.axis('off') plt.show() The labels are the class IDs (represented as uint8), from 0 to 9: y_train
Import module with name that cannot be variable, e.g. hyphens in executables: test_mod = __import__('test-mod') ''' if '##circular import': """ Modules are only evaluated once in import. So this can lead cannot import errors. """ if 'Magic methods dont work': try: assert a() == 'a.__call__()' except TypeError: pass else: assert False if '##submodules': if 'Submodule vs attribute': # *never define in your __init__ file an attribute which has the same name as a module* import d.a assert d.a.a == 'd.a' #TODO1
assert f()==[0,1,2,3,4,5,6,7,8] assert a==8 # nested function scopes def f(method, arg): def cb(ev): return method(ev, arg) return cb def g(*z): return z a = f(g,5) b = f(g,11) assert a(8) == (8, 5) assert b(13) == (13, 11) # nonlocal and global x = 0 def f(): x = 1 res = [] def g(): global x return x res.append(g()) def h(): nonlocal x return x
def f(): # constants a = None + False + True a = 0 a = 1000 a = -1000 # constructing data a = 1 b = (1, 2) c = [1, 2] d = {1, 2} e = {} f = {1: 2} g = 'a' h = b'a' # unary/binary ops i = 1 j = 2 k = a + b l = -a m = not a m = a == b == c m = not (a == b and b == c) # attributes n = b.c b.c = n # subscript p = b[0] b[0] = p b[0] += p # slice a = b[::] # sequenc unpacking a, b = c a, *a = a # tuple swapping a, b = b, a a, b, c = c, b, a # del fast del a # globals global gl gl = a del gl # comprehensions a = (b for c in d if e) a = [b for c in d if e] a = {b: b for c in d if e} # function calls a() a(1) a(b=1) a(*b) # method calls a.b() a.b(1) a.b(c=1) a.b(*c) # jumps if a: x else: y while a: b while not a: b a = a or a # for loop for a in b: c # exceptions try: while a: break except: b finally: c while a: try: break except: pass # with with a: b # closed over variables x = 1 def closure(): a = x + 1 x = 1 del x # import import a from a import b from a import * # raise raise raise 1 # return return return 1
def c(): print("-----c------") a()