View
Calculate
print(15/7) # 나눗셈, 2
print(15//7) # 몫, 2
print(15%7) # 나머지, 1
print(2**3) # 제곱근, 8
Variable Type
Type
- String
- Boolean
- Integer
- Float
v = 1
print(type(v)) # int
Tuple, Dictionary, Set
Tuple
수정이 안되는 엘리먼트 집합 자료형
# define
tuple = ()
tupleA = tuple()
tupleA = (1, 2)
tupleB = (3, 4)
print( tupleA(0) ) # 1, 인덱스 접근 가능
tupleA + tupleB # (1, 2, 3, 4)
tupleA * 2 # (1, 2, 1, 2)
# use case
## switch x, y without temporary variable
x = 1, y = 2
x, y = y, x
## return two varible
function1(x, y)
return x+y, x*y
## List to Tuple, Tuple to List
tupleA = tuple( [1,2,3] )
listA = list( (1,2,3) )
Dictionary
수정이 가능한 key, value 형태의 데이터 셋
dic = {}
dic = dic()
dic = {'key1': 'value1', 'key2', 'value2'}
# get
dic['key1'] # value1
# add or edit
dic['key3'] = 'value3'
# delete
del dic['key3']
# functions
dic.items() # get tuple (key, value)
dic.keys() # get key array
dic.values() # get value array
Set
집합연산을 지원하는 순서와 중복이 없는 데이터 셋. 순서가 없기때문에 인덱스로 접근 불가
setA = {1, 2, 3}
setA = set()
setA = set(1)
setA = set(1, 2, 3) # 한 개 이상일 경우 { } 사용해야 함
# use
## check contains
'apple' in setA # false
1 in setA # true
## operation
setA = {1, 2, 3}
setB = {3, 4}
setA & setB # {3}, 교집합
setA | setB # {1, 2, 3, 4}, 합집합
setA - setB # {1, 2}, 차집합
setA ^ setB # {1, 2, 4}, xor
## list의 중복 제거
list(set(listA))
input, print
name = input("type your input : ") # kim
print(name) # kim
print("Hello, ", name) # Hello, kim
print("Hello, {0}, {1}".format(name, "woo")) # Hello, kim, woo
print("Hello, {1}, {0}".format(name, "woo")) # Hello, woo, kim
print("Hello, {0}, {0}".format(name, "woo")) # Hello, kim, kim
v = 0.126
print( format(v, ".2f") ) # 0.13
print( format(v, ".4f") ) # 0.1260
# %s : string, %c : character, %d : int, %f : float
String
str = """
Hello,
World!
"""
str = "Hello, World!"
len(str) # 13
str.count("l") # 3
str.find("e") # 1
str.replace("Hello", "Hi") # Hi, World!
str[0] # H
str[0:2] # He
str[:2] # He
str[0:-2] # Hello, Wor
str.split(',') # ["Hello", " World!"]
str = " strip "
str.strip() # strip
Array
arr = [1, "a", 3.5]
print( arr[2] ) # 3.5
print( arr[-1] ) # 3.5
print( arr[1:3] ) # ["a", 3.5]
print( arr.append(3) ) # [1, "a", 3.5, 3]
print( arr.remove("a") ) # [1, 3.5, 3]
del arr[1]
print( arr ) # [1, 3]
arr.insert(1, 5)
print( arr ) # [1, 5, 3]
arr.sort()
print( arr ) # [1, 3, 5]
arr.reverse()
print( arr ) # [5, 3, 1]
For, While, If
#for
for element in ['a', 'b', 'c', 'd']:
print(element) # a, b, c, d
for element in range(3) # or range(0:3)
print(element) # 0, 1, 2
# while
while true:
print("Hello World")
break;
# if elif else
if n == 1:
print('n is 1')
elif n == 2:
print('n is 2')
else:
print('n is ', n)
Class
# define
class People():
id = 0
name = ""
def hello(self, args2):
print("arg1 : ", arg1)
return "Hello, " + self.name
reply