Python
-
리스트와 튜플 응용Python 2023. 10. 4. 16:05
리스트 요소 추가 insert( 0, 요소 ): 리스트 맨 처음에 요소 추가 insert( len(리스트), 요소 ): 리스트 끝에 요소 추가 리스트 요소 삭제 첫 번째 줄: collections 모듈에서 deque를 가져옴 a.append(n): 덱의 오른쪽에 n 추가 a.popleft(): 덱의 왼쪽 요소 하나 삭제 index로 가장 처음 찾은 20의 인덱스를 구함 리스트 요소 정렬 sort() 또는 sort(reverse=False): 리스트 값을 작은 순서대로(오름차순) sort(reverse=True): 리스트 값을 큰 순서대로 정렬(내림차순) 리스트의 모든 요소 삭제 리스트를 슬라이스로 조작하기 값이 한 개 들어 있는 리스트를 할당하면 a.append(), a.extend()와 같음 리스트의 ..
-
FizzBuzz와 터틀 그래픽스Python 2023. 9. 27. 17:27
규칙 1. 1에서 100까지 출력 2. 3의 배수는 Fizz 출력 3. 5의 배수는 Buzz 출력 4. 3과 5의 공배수는 FizzBuzz 출력 코드 골프 방식으로 작성된 예제 코드 골프: 문자 수를 최대한 줄여 작성한 코드 → 코드의 문자 수를 얼마나 줄일 수 있는지 겨루는 놀이 혹은 여러 가지 방식 중 하나일 뿐 → 실무에서 이런 방식으로 작성하면 본인 포함 여러 사람 고생하게 됨 복권 숫자 뽑는 코드 터틀 그래픽스 점점 크기도 커지고 원에 가까워짐 색은 구글에서 'color picker' 검색 시 편하게 코드 딸 수 있음
-
딕셔너리, if 조건문Python 2023. 9. 25. 17:25
딕셔너리 키: 문자열, 정수, 실수, 불 등 사용 가능, 자료형 섞어서 사용 가능 값: 리스트, 딕셔너리 포함한 모든 자료형 사용 가능 → 키에는 리스트, 딕셔너리 사용 불가능 if 조건문 if 조건식: 코드(들여쓰기 4칸) if 조건문은 if에 조건식을 지정하고 :(콜론)을 붙이며 다음 줄에 실행할 코드가 오고, 이때 실행할 코드는 반드시 들여쓰기 파이썬에서 :(콜론)이 나오면 그 다음 줄부터는 무조건 들여쓰기 들여쓰기 하지 않으면 에러 조건문 안에 있는 들여쓰기 깊이 맞춰야 함 들여쓰기가 맞지 않아 조건문과 관계 없는 '끝입니다'만 출력 pass: 아무 일도 하지 않는 코드, 넣어 두고 나중에 할 일은 주석으로 남겨 놓는 방식 TODO, FIXME, BUG, NOTE 등 일관된 주석 사용 else 사..
-
시퀀스 자료형 확인하기Python 2023. 9. 25. 15:51
in 연산자를 사용해서 있으면 True, 없으면 False 출력 not in 연산자 특정 값이 없으면 True, 있으면 False 출력 튜플, range, 문자열도 같은 방법으로 활용 가능 시퀀스 객체 반복하기 (0 또는 음수를 곱하면 빈 객체가 나오며 실수는 곱할 수 없음) · 시퀀스 객체 * 정수 · 정수 * 시퀀스 객체 · range는 + 연산자로 연결할 수 없듯 * 연산자 이용해 반복 불가능 → 리스트 or 튜플로 만들어 반복 가능 · 문자열은 * 연산자 사용해 반복 가능 문자열의 길이는 공백까지 포함, 한글 문자열도 len으로 구할 수 있음 UTF-8에서 한글 글자 하나는 3 바이트로 표현하므로 실제 바이트 수 15 인덱스 사용 요소에 값 할당하기 시퀀스 객체는 []로 요소에 접근한 뒤 =로 값..
-
리스트와 튜플Python 2023. 9. 20. 17:11
리스트와 튜플 증가폭은 음수도 가능, 해당 값만큼 숫자 줄어듦 문자열이나 실수는 range에 넣을 수 없음 튜플은 읽기 전용 리스트 (리스트는 저장된 요소 변경 및 추가, 삭제 가능) 값 한 개를 괄호로 묶은 경우 그냥 값 ,(콤마)를 붙여야 튜플이 됨 위 사진에서 주석으로 달아 뒀던 문제인데, 리스트 입력할 때처럼 대괄호 대신 괄호 하나 더 넣어 주면 결과 값 정상 출력 앞서 사용했던 input().split()은 리스트를 반환해 리스트 언패킹 형식으로 입력 값을 변수 여러 개에 저장할 수 있음
-
불과 비교, 논리 연산자Python 2023. 9. 20. 17:10
불과 비교, 논리 연산자 id는 객체의 고유한 값(메모리 주소) 이미 변수 a가 있는 상태에서 다른 값을 할당하면 주소가 달라져 다른 객체가 되므로 값이 같아도 is로 비교하면 False 출력 값(숫자) 비교 시 is가 아닌 비교 연산자 사용 대소문자 구분으로 true 아닌 True로 써야 True 출력, 작은 따옴표로 감싼 False는 문자열 처리로 True 출력 파이썬의 변수 · 데이터, 함수, 클래스, 모듈, 패키지 등 모두 객체(object)로 취급 · 객체는 자료형(type)을 가지며 메모리(저장 공간) 차지 · 이런 특징 때문에 파이썬 변수는 값을 가지지 않음 → 값이 아닌 객체에 연결된 이름에 불과함 · 모든 객체는 메모리를 차지하고, 자료형뿐만 아니라 식별 번호(identity)를 가짐 ·..