▶ 자연과학/▷ Python

【Python】 파이썬 기초 코드

초록E 2018. 6. 9. 10:08

파이썬 기초 코드

 

추천글 : 【Python】  파이썬 목차


알고리즘 중심의 머신러닝 가이드, 스티븐 마슬랜드, 제이펍, 2016.

 

코드 1.

 

a = 3
b = 2
print type(a/2) # <type 'int'>
print type(a/2.0) # <type 'float'>
a = b
b = 3
print a // 2
print 'a' + 'd' # ad
print 1 < a < 3 # True

 

코드 2.

 

newlist = [3, 2, [5, 4, 3], [2, 3, 2]]
print newlist[0] # 3
print newlist[2][1] # 4
print newlist[-2] # [5, 4, 3]
print newlist[1:3] # [2, [5, 4, 3]]
print newlist[0:4:2] # [3, [5, 4, 3]]
print newlist[::-1] # [[2, 3, 2], [5, 4, 3], 2, 3]
blist = newlist[::]
blist[2][2] = 100
print newlist # [3, 2, [5, 4, 100], [2, 3, 2]]
import copy
clist = copy.deepcopy(newlist)
clist[2][2] = 3
print newlist # [3, 2, [5, 4, 100], [2, 3, 2]]
print clist # [3, 2, [5, 4, 3], [2, 3, 2]]
newlist.append([2, 7])
print newlist # [3, 2, [5, 4, 100], [2, 3, 2], [2, 7]]
print newlist.count(2) # 1
newlist.remove([5, 4, 100])
print newlist # [3, 2, [2, 3, 2], [2, 7]]
newlist.extend(clist)
print newlist # [3, 2, [2, 3, 2], [2, 7], 3, 2, [5, 4, 3], [2, 3, 2]]
print newlist.index([2, 3, 2]) # 2
newlist.insert(1, [1, 8])
print newlist # [3, [1, 8], 2, [2, 3, 2], [2, 7], 3, 2, [5, 4, 3], [2, 3, 2]]
newlist.pop(7)
print newlist # [3, [1, 8], 2, [2, 3, 2], [2, 7], 3, 2, [2, 3, 2]]
newlist.reverse()
print newlist # [[2, 3, 2], 2, 3, [2, 7], [2, 3, 2], 2, [1, 8], 3]
newlist.sort()
print newlist # [2, 2, 3, 3, [1, 8], [2, 3, 2], [2, 3, 2], [2, 7]]

 

코드 3.

 

months = {'Jan':31, 'Feb':28, 'Mar':31}
print months.keys() # ['Jan', 'Mar', 'Feb']
print months.values() #[31, 31, 28]
print months.items() #[('Jan', 31), ('Mar', 31), ('Feb', 28)]
print range(4) # [0, 1, 2, 3]
print range(5, -3, -2) # [5, 3, 1, -1]
for i in range(2, 3):
	print i # 2

 

코드 4.

 

def pythagoras(x,y):
	h = pow(x**2+y**2, 0.5)
	""" pow(x, 0.5) returns the root of x """
	return h

print pythagoras(3, 4) # 5.0

 

코드 5.

 

newlist = [3, 2, 1]
print map(lambda x:pow(x,3)+7, newlist) # [34, 15, 8]
print filter(lambda x:x>=2, newlist) # [3, 2]
double = lambda x : 2*x
print double(3) # 6

 

코드 6. 

 

try:
	print 4/2 # 2
	print 1/0
	print 6/2
except ZeroDivisionError:
	print "Divisor must not be 0" # Divisor must not be 0
except TypeError:
	print "They must be numbers"
except:
	print "Something unspecified went wrong"
else:
	print "Everything worked"

 

코드 7.

 

import numpy as np
print np.arange(5) # [0 1 2 3 4]
print np.ones((1, 5)) # [[1. 1. 1. 1. 1.]]
print np.zeros(4) # [0. 0. 0. 0.]
print np.eye(3) # [[1. 0. 0.]\n [0. 1. 0.]\n [0. 0. 1.]]
print np.eye(3,4) # [[1. 0. 0. 0.]\n [0. 1. 0. 0.]\n [0. 0. 1. 0.]]
print np.linspace(3, 9, 4) # [3. 5. 7. 9.]
print np.r_[1:4, 0, 4] # [1 2 3 0 4]
print np.r_[2, 1:7:3] # [2 1 4]
print np.r_[2, 1:7:3j] # [2. 1. 4. 7.]

 

코드 8.

 

import numpy as np
a = np.arange(6).reshape(3,2)
print a # [[0 1]\n [2 3]\n [4 5]]
print np.ndim(a) # 2
print np.size(a) # 6
print np.shape(a) # (3, 2)
print np.transpose(a) #[[0 2 4]\n [1 3 5]]
print a[::-1] #[[4 5]\n [2 3]\n [0 1]]
print np.min(a) # 0
print np.min(a) # 5
print np.sum(a) # 15
print np.ravel(a) # [0 1 2 3 4 5]
b = np.copy(a)
a = a.reshape(2, 3)
print b #[[0 1]\n [2 3]\n [4 5]]

 

입력: 2018.06.09 10:08