지누.log
article thumbnail
✏️ 본 포스팅은 코드잇 대학생 코딩캠프 1기 활동 기록 입니다. 

📌리스트 함수

<python />
numbers = [2, 3, 5, 7, 11, 13, 17, 19] del numbers[3] # 삭제하고 싶은 인덱스 print(numbers) # 결과 [2, 3, 5, 11, 13, 17, 19] numbers.insert(4, 3) print(numbers) # 결과 [2, 3, 5, 11, 3, 13, 17, 19]

 

 📌리스트 정렬

<python />
numbers = [19, 3, 2, 5, 3, 11, 7, 17] new_list = sorted(numbers, reverse=True) print(new_list) # 실행결과: [19, 17, 11, 7, 5, 3, 3, 2] print(numbers) # 실행결과: [19, 3, 2, 5, 3, 11, 7, 17] numbers.sort() print(numbers) # 실행결과: [2, 3, 3, 5, 7, 11, 17, 19] numbers.sort(reverse=True) print(numbers) # 실행결과: [19, 17, 11, 7, 5, 3, 3, 2]

👉 sorted: 기존 리스트 변형 X, 정렬된 새로운 리스트 리턴

👉 sort: 아무것도 리턴하지 않고, 기존 리스트 정렬

 

📌소수점 반올림

  • round(실수, n): 소수점을 n번째 까지만 표현하고 반올림 하고 싶을 때

 

📌리스트 내 값의 존재 유무 확인

<python />
# 값이 있는지 확인 primes = [2, 3, 5, 7, 11, 13, 17, 19, 23] print(7 in primes) print(12 in primes) # 결과 True # False # 값이 없는지 확인 print(7 not in primes) print(12 not in primes) # 결과 False # True

 

📌리스트 정렬함수

  • list.reverse()
  • list.index(x) : list에서 x 의 값을 갖고 있는 인덱스 반환
  • list.remove(x): list에서 첫번째로 x의 값을 가지고 있는 원소 삭제

📌파라미터 range 함수

  • 파라미터 2개 버전
    • for i in range(start, stop): start 부터, stop - 1 까지의 범위
  • 파라미터 1개 버전
    • for i in range(stop): 0 부터 stop - 1 까지의 범위
  • 파라미터 3개 버전
    • for i in range(start, stop, step): start 부터 stop - 1 까지의 범위를 step 간격으로
  • 장점: 간편하고 깔끔하며 메모리를 효율적으로 활용할 수 있음

📌딕셔너리

  • dict.keys()
  • dict.values()
  • dict.items()
  • dict 값 추가: dict[key] = value
  • dict 삭제: dict.pop(key)

📌Aliasing (가명)

<python />
x = [2, 3, 5, 7, 11[ y = x y[2] = 4 print(x) print(y) #결과 #[2, 3, 4, 7, 11] #[2, 3, 4, 7, 11] # y는 x의 가명, alias라고 함 # y의 값만 바꾸기 위해서는 y = list(x) # 이렇게 해야함

 

📌리스트와 문자열

  • 공통점
    • 인덱싱, 슬라이싱, for 반복문, 덧셈 연산 ➡︎ 연결, len()
  • 차이점
    • 리스트: Mutable 수정 가능
    • 문자열: Immutable 수정 불가능
profile

지누.log

@지누:

이 포스팅으로 한 분이라도 도움이 된다면 좋겠습니다.