Python/Grammar
-
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에 빈..
-
3. 변수와 입력 사용하기Python/Grammar 2022. 11. 29. 12:56
# 1. 변수 만들기 파이썬에서는 변수를 만들기 위해 '=(assignment operator: 할당연산자)' 을 사용해 값에 변수를 할당하며, 변수 이름을 지정할 때는 다음과 같은 규칙을 따라야 한다. 💡 변수 naming 규칙 - 영문 문자와 숫자를 사용할 수 있다. - 대소문자를 구분한다. - 문자부터 시작해야 하며 숫자부터 시작하면 안 된다. - _(밑줄 문자)로 시작할 수 있다. - 특수 문자(+, -, *, /, $, @, &, % 등)는 사용할 수 없다. - 파이썬의 키워드(if, for, while, and, or 등)는 사용할 수 없다. 1-1. 변수의 자료형 알아내기 type(변수) >>> x = 100 >>> type(x) >>> y = 'Hello, Python!' >>> type(..
-
1. 기본 문법Python/Grammar 2022. 11. 28. 22:43
# 세미콜론 한 줄에 여러 구문을 사용할 때 세미콜론을 붙인다. 대부분의 프로그래밍 언어들은 구문이 끝날 때 세미콜론을 붙여야하지만 파이썬은 세미콜론을 붙이지 않아도 문법 에러가 발생하지 않는다. EX) print('Hello'); print('Python') # 공백 파이썬 코딩 스타일 가이드(PEP 8)에서는 공백 4칸으로 규정하고 있어 공백 4칸을 사용하는 것이 좋으나 공백 2칸, 탭 1칸을 사용해도 잘 동작한다. 단, 같은 블록은 들여쓰기 칸 수가 같아야 하고 공백과 탭 문자를 섞어쓰면 안 된다. # 코드 블록 파이썬은 들여쓰기를 기준으로 코드 블록을 구성한다. EX) count = 0 # 코드 블록 1 while count < 10: # 코드 블록 1 count += 1 # 코드 블록 2 pri..
-
2. 숫자 계산하기Python/Grammar 2022. 11. 28. 12:52
# 정수 계산하기 사칙연산하기: + - * / >>> 10 + 1 11 >>> 10 / 2 5.0 # 파이썬3은 몫으로 완전히 나누어떨어져도 5.0과 같이 실수가 나옴 버림 나눗셈 몫 구하기: // >>> 7 // 3 2 >>> 6 // 3 2 나눗셈 나머지 구하기: % >>> 3 % 1 0 거듭제곱 구하기: ** >>> 3 ** 3 27 값을 정수로 만들기 : integer(정수)를 의미하는 int를 사용한다. int(숫자) int(계산식) int(문자열) >>> int('500') 500 객체의 자료형 알아내기 type(값) >>> type(500) 몫과 나머지 함께 구하기 : divmod를 사용해 피제수를 제수로 나누면 튜플 형태로 몫과 나머지가 반환된다. divmod(피제수, 제수) -> (몫, ..