세상돌아가는 최첨단 이야기

Python 에서 알아야 할 것들.. 본문

python 프로그래밍 팁

Python 에서 알아야 할 것들..

Overmars 2021. 12. 9. 23:05
반응형

dictionary: dic = {'name':'Suk', 'phone':'0119993323', 'birth': '1224'}, a = {1: 'hi'}, a = { 'a': [1,2,3]}

defaultdict: a subclass of dict Class which makes dictionary.

>>> from collections import defaultdict
>>> int_dict = defaultdict(int)
>>> int_dict['Key']
0
>>> int_dict['Key2'] = 'test'
>>> int_dict['Key3']
0
>>> int_dict
defaultdict(<class 'int'>, {'Key': 0, 'Key2': 'test', 'Key3': 0})
>>> int_dict['Key4']
0
>>> int_dict


abstract class:

  • 추상클래스란 미구현 추상메소드를 한개 이상 가지며, 자식클래스에서 해당 추상 메소드를 반드시 구현하도록 강제합니다.  출처: https://wikidocs.net/16075   자식클래스를 생성할 때 super의 추상 메소드를 구현 하지 않으면 TypeError 발생

 

all function : returns true if all the elements of a given iterable( List, Dictionary, Tuple, set, etc) are True else it returns False. It also returns True if the iterable object is empty. :: Syntax: all(iterable)

numpy allows you to multiply two arrays withouot a for loop..
>>> import numpy as np
>>> in_arr1 = np.array([[2, -7, 5], [-6, 2, 0]])
>>> in_arr2 = np.array([[0, -7, 8], [5, -2, 9]])
>>> out_arr = np.multiply(in_arr1, in_arr2)
>>> out_arr
array([[  0,  49,  40],
       [-30,  -4,   0]])
np.dot(a, b)

{x : x*x for x in range(1,100)}

>>> {x : x*x for x in range(1,100)}
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225, 16: 256, 17: 289, 18: 324, 19: 361, 20: 400, 21: 441, 22: 484, 23: 529, 24: 576, 25: 625, 26: 676, 27: 729, 28: 784, 29: 841, 30: 900, 31: 961, 32: 1024, 33: 1089, 34: 1156, 35: 1225, 36: 1296, 37: 1369, 38: 1444, 39: 1521, 40: 1600, 41: 1681, 42: 1764, 43: 1849, 44: 1936, 45: 2025, 46: 2116, 47: 2209, 48: 2304, 49: 2401, 50: 2500, 51: 2601, 52: 2704, 53: 2809, 54: 2916, 55: 3025, 56: 3136, 57: 3249, 58: 3364, 59: 3481, 60: 3600, 61: 3721, 62: 3844, 63: 3969, 64: 4096, 65: 4225, 66: 4356, 67: 4489, 68: 4624, 69: 4761, 70: 4900, 71: 5041, 72: 5184, 73: 5329, 74: 5476, 75: 5625, 76: 5776, 77: 5929, 78: 6084, 79: 6241, 80: 6400, 81: 6561, 82: 6724, 83: 6889, 84: 7056, 85: 7225, 86: 7396, 87: 7569, 88: 7744, 89: 7921, 90: 8100, 91: 8281, 92: 8464, 93: 8649, 94: 8836, 95: 9025, 96: 9216, 97: 9409, 98: 9604, 99: 9801}
>>> 

 

what does a generator return?
generator: iterator를 생성해주는 함수, 함수안에 yield 키워드를 사용함

>>> def test_generator():

... print('yield 1 전') ... yield 1

... print('yield 1과 2사이')

... yield 2

... print('yield 2와 3사이')

... yield 3

... print('yield 3 후')


tuple

  • tuple(튜플)은 불변한 순서가 있는 객체의 집합입니다.
  • list형과 비슷하지만 한 번 생성되면 값을 변경할 수 없습니다. 추가는 가능함.

t = (1, "korea", 3.5, 1)

t * 2  => (1, 'korea', 3.5, 1, 3, 5, 1, 'korea', 3.5, 1, 3, 5)

>>> a = ((1 ,2) , (3,4), (5,9))  # 튜플 속에 튜플이 포함될 수 있다. 

>>> a[2]

(5, 9)

  • tuple(튜플)변환 - tuple(iterable한객체)로 tuple(튜플)로 변형할 수 있습니다.
>>> tuple([1, 7, 5, 3, 9])
(1, 7, 5, 3, 9)
>>> tuple("abcde")
('a', 'b', 'c', 'd', 'e')


linear equations (1차 방정식) numpy dot()

numpy array 를 곱할 때 사용한다.  a, b가 1차원 행열(vector) 라면, 각 자리 수끼리 곱하여 전 부 더한다. 

a = np.array([1, 2, 3]) b= np.array([2, 3, 4])  numpy.dot(a, b) => 20

 

 

반응형