In [1]:
n = 1
n = 3.14
n = "hello, world"
print(n)
In [195]:
n=1
type(n)
Out[195]:
In [198]:
id(n)
Out[198]:
In [196]:
n=3.14
type(n)
Out[196]:
In [199]:
id(n)
Out[199]:
In [197]:
n="hello, world"
type(n)
Out[197]:
In [200]:
id(n)
Out[200]:
In [128]:
type(1)
Out[128]:
In [130]:
type(3.14)
Out[130]:
In [201]:
type(10000000000000000000000000000000000000)
Out[201]:
In [70]:
id(n)
Out[70]:
In [134]:
type(6.78e-5)
Out[134]:
In [135]:
type(1+2j)
Out[135]:
In [78]:
type(True)
Out[78]:
In [71]:
bool('a')
Out[71]:
In [73]:
bool('여성') == bool('남성')
Out[73]:
In [67]:
'a' == True
Out[67]:
In [68]:
'a' == False
Out[68]:
In [69]:
0 == False
Out[69]:
In [70]:
-1 == False
Out[70]:
숫자 자료형 변경
In [136]:
int(3.14)
Out[136]:
In [137]:
float(3)
Out[137]:
In [138]:
complex(3)
Out[138]:
In [139]:
int(1+2j)
In [140]:
float(1+2j)
In [116]:
2 + 3.14
Out[116]:
In [117]:
2-3.14
Out[117]:
In [118]:
2 * 3.14
Out[118]:
In [119]:
2 / 3.14
Out[119]:
In [120]:
2 // 3.14
Out[120]:
In [121]:
2 % 3.14
Out[121]:
In [122]:
3.14 ** 2
Out[122]:
In [123]:
2 > 3.14
Out[123]:
In [124]:
2 <= 3.14
Out[124]:
In [125]:
2 == 3.14
Out[125]:
In [142]:
2 != 3.14
Out[142]:
In [143]:
not 2 == 3.14
Out[143]:
In [114]:
True > False
Out[114]:
In [115]:
True == False
Out[115]:
여러 개의 비교 동시 수행 가능. 왼쪽부터 평가
In [111]:
1 < 2 < 3
Out[111]:
In [112]:
1 < 2 < 3 < 2
Out[112]:
In [103]:
True and False
Out[103]:
In [104]:
True or False
Out[104]:
In [144]:
not False
Out[144]:
In [105]:
print(n1)
n1 += 1
print(n1)
In [15]:
a='one'
b="two"
c="""three
four
five"""
In [16]:
a
Out[16]:
In [17]:
b
Out[17]:
In [18]:
c
Out[18]:
In [19]:
print(c)
In [4]:
print("Joe's Sandwich")
In [213]:
'one'[0]
Out[213]:
In [214]:
'one'[3]
In [205]:
'one'[-1] # 마지막 값
Out[205]:
In [206]:
'one'[-2]
Out[206]:
In [202]:
'one'[1:]
Out[202]:
In [204]:
# 0 <= <5
'one two three'[0:5]
Out[204]:
In [9]:
'one two three'[-5:]
Out[9]:
역순 인덱스
In [207]:
'one'[-2:]
Out[207]:
In [208]:
'one two three'[4:7]
Out[208]:
In [10]:
'one two three'[1::2]
Out[10]:
In [210]:
'python'[::2]
Out[210]:
In [211]:
'python'[::-1]
Out[211]:
In [70]:
print('안녕'+'하세요')
In [8]:
print('파이썬' + str(2))
In [5]:
print('안녕하세요' - '하세요')
In [99]:
print('참 잘했어요! '*10)
In [220]:
'a'=='b'
Out[220]:
In [221]:
'abc' == 'abc'
Out[221]:
In [20]:
'a' in 'abc'
Out[20]:
In [21]:
'd' in 'abc'
Out[21]:
In [159]:
type('파이썬')
Out[159]:
In [161]:
type(u'파이썬')
Out[161]:
In [212]:
print('안녕'+u'하세요')
In [179]:
text_str = '빅데이터를 위한 파이썬'
print(text_str[:4])
한국어와 같은 비 ASCII 문자의 경우, 유니코드가 아니면 여러 가지 문제가 있을 수 있다.
In [180]:
text_unicode = u'빅데이터를 위한 파이썬'
print(text_unicode[:4])
str을 unicode로 변환
In [64]:
text_unicode = '파이썬'.decode('utf-8')
type(text_unicode)
Out[64]:
unicode를 str으로 변환
In [65]:
text_str = u'파이썬'.encode('utf-8')
type(text_str)
Out[65]:
In [66]:
print(text_str)
In [62]:
text_str.decode('utf-8')
In [218]:
'성주' == u'성주'
Out[218]:
In [6]:
lang = '파이썬'
version = 2
print('현재 사용되는 ' + lang + ' 버전은 ' + str(version) + '.x 입니다.')
In [11]:
text_en = 'python for data'
text_en[-4:]
Out[11]:
In [13]:
text_kr = '빅데이터를 위한 파이썬'
print(text_kr[-3:])
In [63]:
text = u'빅데이터를 위한 파이썬'
print(text[-3:])
In [45]:
x = 1
y = x
y = 2
print(x)
In [50]:
numbers = [1,2,3,4,5]
type(numbers)
Out[50]:
In [51]:
NS = numbers[:]
NS
Out[51]:
In [52]:
id(NS) == id(numbers)
Out[52]:
In [53]:
NS is numbers
Out[53]:
In [54]:
NS[0]=0
print(numbers)
In [5]:
print(numbers[1])
numbers[1] = 4
print(numbers)
print(numbers[-1])
In [6]:
numbers[2:]
Out[6]:
In [7]:
numbers[2:4]
Out[7]:
In [8]:
numbers[-2:]
Out[8]:
In [45]:
# 역순으로
numbers[::-1]
Out[45]:
In [52]:
print(id(numbers))
print(id(numbers[::-1]))
numbers.reverse()
print(numbers)
다차원 자료에서 파이썬 리스트 슬라이싱의 한계
In [50]:
data_2d = [[1,2,3], [4,5,6], [7,8,9]]
data_2d[1][:2]
# 열 방향 자료 슬라이싱 ... 은 불가.
[data_2d[1][1], data_2d[2][1]]
# 한계 극복을 위해 NumPy 도입
Out[50]:
In [16]:
alist = [4,2,3]
alist.append(5)
print(alist)
In [17]:
alist.pop(0)
print(alist)
In [64]:
[1,2]+[3,4]
Out[64]:
In [18]:
[1,2]*2
Out[18]:
In [19]:
[1,2]*[1,2]
In [67]:
[1,2] * 3
Out[67]:
In [59]:
[1,2] == [3,4]
Out[59]:
In [60]:
[1,2] == [1,2]
Out[60]:
In [61]:
[1,2] == [2,1]
Out[61]:
값 비교는 첫 번째부터
In [62]:
[1,2] > [3,4]
Out[62]:
In [63]:
[1,2] < [3,4]
Out[63]:
list와 문자열
In [182]:
list('python')
Out[182]:
In [183]:
list(u'파이썬')
Out[183]:
In [191]:
import string
text = string.join(['a','b','c'])
print(text)
In [194]:
text = string.join([u'파', u'이', u'썬'], '')
print(text)
In [13]:
profile = {'name': '이성주',
'email': 'seongjoo@codebasic.co'}
print(profile['email'])
In [14]:
'twitter' in profile
Out[14]:
dict의 새 필드 추가
In [15]:
profile['twitter'] = '@LeeSeongjoo'
print(profile)
In [85]:
data = [['남자1호', '남자2호', '남자3호'],
['여자1호', '여자2호', '여자3호']]
In [87]:
for name in data[0][1:3]:
print(name)
In [88]:
for name in [data[0][1], data[1][1]]:
print(name)
In [25]:
people = [{'name': '이성주',
'email': 'seongjoo@codebasic.co',
'age': 32},
{'name': '권태형',
'email': 'taeyoung@email.com',
'age': 15},
{'name': '김경철',
'email': 'kyungchul@email.com',
'age': 20}]
# 모든 사람의 나이에 1을 더하기
for person in people:
person['age'] += 1
print(people[0])
In [26]:
tup = (1,2)
In [30]:
tup[0]
Out[30]:
In [31]:
tup[1:]
Out[31]:
In [32]:
tup[0] = 4 # 오류!
tuple unpacking
In [38]:
a,b = (1,2, 3)
In [37]:
print(a)
print(b)
In [69]:
tup = (1,2,3)
In [73]:
tup[1:]
Out[73]:
In [17]:
numbers = [1,2,3,4,5]
type(numbers)
Out[17]:
In [18]:
numbers.append(6)
print(numbers)
In [20]:
profile = {'name':'이성주'}
type(profile)
Out[20]:
In [21]:
age = profile.get('age',-1)
print(age)
In [22]:
numbers.get('age', '-1') # 오류!
In [23]:
profile.append('email') # 오류!