study with Q - 파이썬
-
29- for 반복문과 리스트study with Q - 파이썬 2024. 3. 31. 10:05
#항등원 :임의의 원소에 특정 연산을 했을 때 재귀시키는 원소 e.g. 덧셈 연산의 항등원: 0 / 곱센 연산의 항등원 : 1 #총합과 총곱 !a = (a_1, a_2, a_3)* 일때, 총합: 모두 더한다 !\sum a* : !a_1 + a_2 + a_3* 총곱: 모두 곱한다 !\prod a* : !a_1* x! a_2 * x!a_3* #for 반복문 : 기본 형태 for 반복자 in 리스트: 코드 #반복자에는 리스트의 요소가 하나하나 들어간다 #반복자에 주로 쓰이는 문자는 i, j, k ,m ,n 그래서 list = [8, 20, 11, 13, 44, 32] for l in list: print(l) >>> 8 20 11 13 44 32 지난주 배운 sort 기능도 잠깐 넣어보지 뭐 더보기 list ..
-
28 - 리스트의 함수study with Q - 파이썬 2024. 3. 27. 22:33
#파괴적 연산과 비파괴적 연산 파괴적 연산 연산 후에 피연산자가 변형되는 것 e.g. 할당 연산자 = 원본이 파괴되기 때문에 메모리를 많이 차지하지 않음 리스트는 크기가 클 가능성이 높으므로 파괴적 연산을 사용함 비파괴적 연산 연산 후에 피연산자가 변형되지 않는 것 e.g. 연산자 +-*/ 비파괴적 연산은 결과를 저장하기 위해 할당 연산자를 함께 사용해야 함 장점) 원본과 결과가 모두 남기 때문에 안전함 단점) 원본과 결과를 둘 다 남기기 때문에 메모리를 많이 차지함 #요소 추가 : append(), insert(), extend() append(요소) : 리스트 가장 뒤에 요소 추가 l = [1,2,3,4,5] l.append(6) print(l) >>> [1, 2, 3, 4, 5, 6] insert(..
-
27 - 수열, 배열, 리스트study with Q - 파이썬 2024. 3. 22. 21:03
#수열 : 수의 나열 !a* = (1,2,3,4, ...) 항 = 수열의 요소 길이: 수열 요소의 수 유한수열 : 길이가 유한 무한수열 : 길이가 무한 #서수와 기수 서수 : 순서를 나타내는 숫자 기수: 개수를 나타내는 숫자 #배열 : 문자열, 숫자, 불자료형 등의 나열 : (파이썬에서는) 0부터 세는 0-index를 사용 : 길이가 고정되어 있음 #리스트 :배열에 요소 추가/제거 등의 기능을 추가한 것 : 여러 개의 자료형을 쓸 수 있으며 각각의 요소(element)를 대괄호[] 안에 쉼표로 연결해서 생성한다. 리스트도 하나의 자료이기때문에 변수에 넣어서 저장할 수 있음 ‣ 요렇게 list_f = [1, 24, "oreoreo", False] ※ ㄱ기억ㄱ 리스트 기호인 대괄호[] 안에 들어간 숫자를 ..
-
25 - 조건문study with Q - 파이썬 2024. 3. 20. 23:55
# 함수 int() : 정수로 변환 float() : 부동소수점으로 변환 str() : 문자열로 변환 bool() : 불자료형으로 변환 #Boolean (불 자료형) : True(참)과 False(거짓) 값만 가질 수 있다. : '어떤 명제'의 결과가 될 때 의미를 갖는다 ‣ False(거짓)로 변환되는 값: None, 0, 0.0, 빈 컨테이너(현재 수준에서는 빈 문자열) bool(0) >>> False bool(0.0) >>> False bool("") >>> False bool(100) >>> True bool(0.2) >>> True bool("False") >>> True #손코딩 사용자가 빈 문자열을 넣었을 때, "프로그램을 재실행하시오." 메시지 띄우기 코드1 더보기 ri = input("값..
-
-
21 - if 조건문 연습study with Q - 파이썬 2024. 3. 4. 21:11
Q. 어떤 정수에서 짝수와 홀수 구분하기 사람의 방식 : 일의 자리가 0,2,4,6,8이면 짝수 / 1,3,5,7,9이면 홀수 #입력 raw = input("정수를 입력해주세요: ") hu = raw[-1] #짝 or 홀 ##짝 if hu == "0" or hu == "2" or hu == "4" or hu == "6" or hu == "8": print("짝수") ##홀 if hu == "1" or hu == "3" or hu == "5" or hu == "7" or hu == "9": print("홀수") ※주의! or 뒤에는 피연산자가 붙어야 하므로 hu == "0" or "2"의 형태로는 쓸 수 없다. 다른 방법 1 더보기 raw2 = input("정수를 입력해주세요: ") an = raw[-1]..