Block 지정
- Indentation - level 당 4 spaces
조건문
1. 조건지정(Boolean 값)
1) False
- False, None, 0, "", {}, (), []
2) True
- True,
- False가 아닌 기타의 모든 것
2. 조건실행
- if, elif, else
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var = 100 if var<200: print("Expression value is less than 200") if var == 150: print("Which is 150") elif var == 100: print("Which is 100") elif var < 50: print("Expression value is less than 50") else: print("Could not find true expression") # Expression value is less than 200 # Which is 100 | cs |
3. 비교연산자
==
<.<
<=,>=
!=
is와 is not
in과 not in
4. Equality 연산자 : is
1 2 3 4 5 6 7 | x = y = [1, 2, 3] z = [1, 2, 3] print(x == y) # True print(x == z) # True print(x is y) # True print(x is z) # False | cs |
5. Membership: in
1 2 | print('a' in 'apple') # True print('b' in 'apple') # False | cs |
6. String 비교
1 2 | print('apple' < 'banana') # True print([1, 2] < [2, 1]) # True | cs |
Loop
1. while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | x = 1 while x <= 10: print(x) x += 1 # 1 # 2 # 3 # 4 # 5 # 6 # 7 # 8 # 9 # 10 | cs |
2. for loop
code block 수행을 for each element of a set(or sequence, or other iterable object)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | words = ('Think like a man of action and act like man of thought').split() for word in words: print(word) # Think # like # a # man # of # action # and # act # like # man # of # thought | cs |
3. Loop에서 나오는 방법
1) break
2) continue
- while 에서는 True 이기 때문에, break를 걸어줘야함
1 2 3 4 | word = input('Enter a word: ') while word: print('the word was '+ word) word = input('Enter a word: ') | cs |
1 2 3 4 | while True: word = input('Enter a word: ') if not word: break print('The word was '+ word) | cs |
1. pass()
2. exec()
직접 system 명령을 수행할 수 있음
보안 issue가 있기 때문에, 가급적 사용안함
3. eval()
1 2 3 4 | print(eval(input('Enter an arithmetic expression: '))) # Enter an arithmetic expression: 5+13 # 18 |
4.
Range
range(start, end, step)
1 2 | print(range(1, 10, 3)) # range(1, 10, 3) print(list(range(1, 10, 3))) # [1, 4, 7] | cs |
Dictionary에 대한 iteration
1 2 3 4 5 6 7 8 9 10 | numbers = {1:'one', 2:'two', 3:'three', 4:'four', 5:'five'} for key in numbers: print(key, 'corresonds to', numbers[key]) # 1 corresonds to one # 2 corresonds to two # 3 corresonds to three # 4 corresonds to four # 5 corresonds to five | cs |
iteration 관련 utilities
- Parallel iteration
1 2 3 4 5 6 7 8 9 | animals = ['cat', 'dog', 'rabbit'] eat = ['fish', 'meat', 'carrot'] for i in range(len(animals)): print(animals[i], 'eats', eat[i]) # cat eats fish # dog eats meat # rabbit eats carrot | cs |
- zip
1 2 3 4 5 | animals = ['cat', 'dog', 'rabbit'] eat = ['fish', 'meat', 'carrot'] print(list(zip(animals, eat))) # [('cat', 'fish'), ('dog', 'meat'), ('rabbit', 'carrot')] | cs |
- enumerate
1 2 3 4 5 6 7 8 9 | animals = ['cat', 'dog', 'rabbit'] eat = ['fish', 'meat', 'carrot'] for i, j in enumerate(animals): print(i, j) # 0 cat # 1 dog # 2 rabbit | cs |
- reverse, sort
- sorted(), reversed()
1 2 3 4 5 6 7 8 9 | # reverse, sort; in-place change # sorted(), reversed() print(sorted([4, 3, 2, 1, 5])) # [1, 2, 3, 4, 5] print(sorted('hello')) # ['e', 'h', 'l', 'l', 'o'] print(''.join(reversed('hello'))) # olleh | cs |
List Comprehension(짱짱 좋은 기능이랩)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | print([x*x for x in range(10)]) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # 3으로 나누어지는 x로 print([x*x for x in range(10) if x % 3==0]) # [0, 9, 36, 81] print([(x, y) for x in range(3) for y in range(2)]) # [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] result = [] for x in range(3): for y in range(2): result.append((x,y)) print(result) # [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] | cs |
1 2 3 4 5 | boys = ['chris', 'bob', 'arnold'] girls = ['alice', 'bernice', 'clarice'] print([b+':'+g for b in boys for g in girls if b[0]==g[0]]) # ['chris:clarice', 'bob:bernice', 'arnold:alice'] | cs |
기타
1. pass()
2. exec()
직접 system 명령을 수행할 수 있음
보안 issue가 있기 때문에, 가급적 사용안함
3. eval()
1 2 3 4 | print(eval(input('Enter an arithmetic expression: '))) # Enter an arithmetic expression: 5+13 # 18 | cs |
4. assert
위험요소를 미리!!! checkpoint로 활용
맞으면 별일 없이 넘어가고, 아니면 error
1 2 3 4 5 6 | age = 10 assert 0 <age <100 age = -5 assert 0 < age <100 #AssertionError | cs |
5.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | x, y, z = 1, 2, 3 print(x, y, z) # 1 2 3 x, y = y, x print(x, y, z) # 2 1 3 x = 2 x += 3 print(x) # 5 somename ='son' somename += 'jiyoung' print(somename) # sonjiyoung print(somename*5) #s onjiyoungsonjiyoungsonjiyoungsonjiyoungsonjiyoung | cs |
'Data > python·알고리즘' 카테고리의 다른 글
[python 기초] python의 함수에 대하여 (0) | 2018.06.28 |
---|---|
[python 기초] python의 module에 대하여 (0) | 2018.06.28 |
[python 기초] python의 dictionary에 대하여 (0) | 2018.06.28 |
[python 기초] python의 String에 대하여 (0) | 2018.06.27 |
[python 기초] python의 tuple에 대하여 (0) | 2018.06.27 |
댓글