분류 전체보기
-
13. 반복문 제어하기Python/Grammar 2022. 12. 8. 01:42
반복문을 제어하는 방법은 break와 continue를 사용하는 것이다. break를 사용해 반복문의 제어 흐름을 중단하고 루프를 빠져나오거나, continue를 사용해 반복문의 제어 흐름은 유지하되 코드 실행만 건너뛸 수 있다. # 1. break로 반복문 끝내기 1-1. while에서 break로 반복문 끝내기 i = 0 while True: # 무한 루프 생성 i += 2 if i == 8: # i가 8일 때 print(f"repeated {i // 2} times. escape the loop.") break # 반복문(무한 루프)을 끝냄 # 실행 결과 repeated 4 times. escape the loop. 1-2. for에서 break로 반복문 끝내기 for i in range(10): ..
-
10. if 조건문으로 특정 조건일 때 코드 실행하기Python/Grammar 2022. 12. 2. 10:32
# 1. if 조건문 사용하기 if 조건문은 의사코드(pseudo code)의 일종이다. 의사코드란 실제 프로그래밍 언어가 아닌 사람의 언어로 프로그래밍 언어를 표현한 것이다. 예를 들어 'x = 10'이라는 파이썬 코드를 '변수 x에 10 할당'과 같이 한글로 의사코드를 표현할 수 있다. 1-1. if 조건문의 기본 형태와 실행 흐름 : if 조건문은 if 조건식 다음줄에 들여쓰기 후 조건식이 만족할 때 실행할 코드를 넣는다. 조건식이 만족할 때 실행할 코드를 if 본문(if body)이라고 부른다. 1-2. if 조건문을 사용할 때 주의할 점 들여쓰기 : if 조건문 다음줄부터 형성되는 코드블럭(if body) 안에서는 같은 깊이로 들여쓰기를 해줘야 한다. IDLE 파이썬 셸에서는 자동 들여쓰기가 되..
-
9. 딕셔너리 만들기Python/Grammar 2022. 12. 2. 10:21
# 1. 딕셔너리 만들기 딕셔너리는 말 글대로 사전에서 단어를 찾듯이 키를 찾으면 값을 가져올 수 있다고 해서 딕셔너리라는 명칭이 붙었다. 딕셔너리를 만드는방법은 여러 가지가 있지만 가장 기본적인 방법으로 중괄호 안에 키: 값을 넣은 {키1: 값1, 키2: 값2, ...} 형태로 만든다. 각 키와 값은 콤마로 구분해주며, 키와 값은 1:1 대응되어 하나의 키에는 값을 하나만 지정할 수 있다. 따라서 키와 값을 "키-값 쌍(key-value pair)"이라고 부른다. 참고로 딕셔너리와 같은 키-값 쌍 형태의 자료형을 Hash, Hash Map, Hash Table 등으로 부르며 REDIS 저장기법 중 HSET의 H도 해시를 의미한다. 1-1. 빈 딕셔너리 만들기 : 빈 딕셔너리를 만들려면 중괄호 안에 아무..
-
8. 시퀀스 자료형 사용하기Python/Grammar 2022. 12. 1. 13:01
# 1. 시퀀스 자료형의 공통기능 사용하기 파이썬에서는 리스트, 튜플, range, 문자열처럼 값이 연속적으로 이어진 자료형을 '시퀀스(sequence)'라고 한다. 참고로 시퀀스 자료형에는 위 4가지 자료형 이외에도 byte, bytearray가 있다. ※ 시퀀스 자료형을 만든 객체를 '시퀀스 객체'라고 하며, 시퀀스 객체에 들어있는 각 값을 '요소(element)'라고 한다. 1-1. 특정 값이 있는지 확인하기 : 시퀀스 객체에 in 연산자를 사용했을 때 특정 값이 있으면 True, 없으면 False를 반환한다. (not in 은 반대) 값 in 시퀀스객체 값 not in 시퀀스 객체 >>> a = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] >>> 0 in a True >..
-
7. 리스트와 튜플 사용하기Python/Grammar 2022. 12. 1. 12:51
# 1. 리스트 만들기 변수에 값을 저장할 때 대괄호로 묶어주면 리스트가 되고, 리스트 안의 각 요소(element)는 콤마로 구분해준다. 1-1. 리스트에 여러가지 자료형 저장하기 : 리스트는 문자열, 정수, 실수, 불 등 모든 자료형을 저장할 수 있으며, 여러 가지 자료형을 섞어서 저장해도 된다. >>> a = [1, 1.0, 'hello', True] >>> a [1, 1.0, 'hello', True] >>> type(a[2]) >>> type(a[3]) 1-2. range를 사용해 리스트 만들기 list(range(횟수)): 지정한 횟수(0~지정한 횟수 -1)만큼 숫자를 생성 list(range(시작, 끝)): 시작으로 지정한 숫자부터 끝으로 지정한 숫자 -1까지 연속된 숫자를 생성 list(r..
-
6. 문자열 사용하기Python/Grammar 2022. 11. 30. 13:07
#1. 문자열 사용하기 1-1. 작은따옴표 한개씩으로 묶어 문자열 만들기 : 문자열 안에 큰 따옴표를 사용하고 싶은 경우 작은따옴표로 묶인 문자열 안에서 작은따옴표를 단독으로 사용할 수 없다. 작은따옴표로 묶었을 때 작은따옴표를 문자열 안에 포함시키고 싶다면 \'와 같이 역슬래쉬 뒤에 작은 따옴표를 붙여서 사용한다. 큰 따옴표 안에 큰 따옴표를 사용하고 싶을 때도 마찬가지이다. 문자열로 된 변수를 표현할 경우에도 작은따옴표를 사용한다. >>> 'Python isn\'t difficult' "Python isn't difficult" 1-2. 큰따옴표 한개씩으로 묶어 문자열 만들기 문자열 안에 작은 따옴표를 사용하고 싶은 경우 문장을 표현할 경우 >>> s = "Python isn't difficult" ..
-
5. 불과 비교, 논리 연산자 알아보기Python/Grammar 2022. 11. 30. 13:03
#1. 불과 비교 연산자 사용하기 1-1. 두 값이 서로 같은지 다른지 판단하기 ==: 두 값이 같으면 True, 두 값이 다르면 False 반환 !=: 두 값이 다르면 True, 두 값이 같으면 False 반환 >>> 1 == 1.0 True 1-2. 두 객체가 서로 같은지 다른지 판단하기 is: 두 객체가 같으면 True, 두 객체가 다르면 False 반환 is not: 두 객체가 다르면 True, 두 객체가 같으면 False 반환 >>> 1 is 1.0 # 1은 정수 객체, 1.0은 실수 객체 False 1-3. 부등호 사용하기 >>> 10 > 1 # 초과 True >>> 10 >> 10 >= 10 # 이상 True >>> 11 >> True and True True >..
-
4. 출력 방법 알아보기Python/Grammar 2022. 11. 29. 13:00
# 1. 값을 여러 개 출력하기 1-1.sep 파라미터 사용하기 : 값 사이에 공백이 아닌 다른 문자를 넣고 싶을 때 print의 'sep' 파라미터에 문자나 문자열을 지정해주면 된다. sep은 '구분자' seperator를 의미한다. print(값1, 값2, sep='문자 또는 문자열') print(변수1, 변수2, sep='문자 또는 문자열') >>> print(1, 2, 3, sep=', ') # sep에 콤마와 공백 지정 1, 2, 3 >>> print(1, 2, 3, sep='\n') # sep에 개행 문자(\n) 지정 1 2 3 >>> print(4, 5, 6, sep=',') # sep에 콤마만 지정 4,5,6 >>> print('Hello', 'Python', sep='') # sep에 빈..