반응형
Jen'_'
Jen's Space
Email
GitHub
전체 방문자
오늘
어제
  • 분류 전체보기 (98)
    • Ansible (4)
      • 우아한앤서블 (4)
    • Cloud (19)
      • AWS (17)
      • GCP (2)
    • DevOps (40)
      • Docker (6)
      • Kubernetes (26)
      • CICD (6)
      • Terraform (2)
    • Language (13)
      • Python (12)
      • Node.js (1)
    • Linux (10)
    • etc. (7)
    • 개인기록 (1)
    • AEWS (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • 피드백 환영합니다 :)

인기 글

태그

  • vagrant
  • Docker
  • EKS
  • 쿠버네티스
  • 자격증
  • INGRESS
  • jenkins
  • ansible
  • autoscaling
  • k8s
  • CICD
  • Kubernetes
  • ubuntu
  • AWS
  • gitops
  • karpenter
  • argocd
  • terraform
  • 우아하게앤서블
  • ALB

최근 댓글

Jen'_'

Jen's Space

Python 강의 정리 3. 리스트
Language/Python

Python 강의 정리 3. 리스트

2021. 5. 2. 21:43
반응형

 

리스트

- 리스트엔 숫자, 문자열, 불자료형 모두 올수 있음

list_e=[234, 4, 102, "문자열", True, False]

print(list_e)

------------------

(result)

[234, 4, 102, '문자열', True, False]

 

이중 리스트

print("list_e[3][0]의 결과: ", list_e[3][0]) #'문' 출력

list_e=[[1,2,3],[4,5,6],[7,8,9]]

print(list_e[1][1]) # 5 출력

print()

 

리스트 기본 연산자

list_a=[1,2,3]

list_b=[4,5,6]

print("리스트 목록")

print("list_a: ", list_a)

print("list_b: ", list_b)

print()



print("# 리스트 기본 연산자")

print("list_a + list_b = ", list_a + list_b)

print("list_a * 3 = ", list_a * 3)

print()

 


 

길이구하기

len()

list_a = [1,2,3]

print("len(list_a)=", len(list_a))

print()

 

원소 추가

append(원소)

- 리스트 맨 뒤에 원소 추가

list_a = [1,2,3]
list_a.append(4) 
list_a.append(5)
print(list_a)
print()
----------------
(result)
[1, 2, 3, 4, 5]

 

insert(인덱스, 원소)

- 인덱스 위치에 원소 추가

list_a = [1,2,3]
list_a.insert(0,10) 
print(list_a)
print()
----------------
(result)
[10, 1, 2, 3]

 

extend(리스트)

- 원소 여러개 추가할때 사용

list_a = [1,2,3]
list_a.extend([100,200,300])
print(list_a)
print()
----------------
(result)
[1, 2, 3,  100, 200, 300]

 


 

원소 제거

del 리스트명[인덱스]

list_c = [1,2,3,4,5]
print("list_c =", list_c)
del list_c[1]
print("del list_c[1] = ", list_c)
-----------
(result)
list_c = [1, 2, 3, 4, 5]
del list_c[1] =  [1, 3, 4, 5]

 

# 범위제거

list_c=[1,2,3,4,5]
print("list_c =", list_c)
del list_c[1:]
print("del list_c[1:] = ", list_c)
print()

list_c=[1,2,3,4,5]
print("list_c =", list_c)
del list_c[:1]
print("del list_c[:1] = ", list_c)
------------------
(result)
list_c = [1, 2, 3, 4, 5]
del list_c[1:] =  [1]

list_c = [1, 2, 3, 4, 5]
del list_c[:1] =  [2, 3, 4, 5]

 

 

음수인덱스도 있다!

 리스트 범위 

[start인덱스 : end-1인덱스]

start인덱스를 공백으로 두면 기본값 0

end인덱스를 공백으로 두면 기본값 -1

ex) list_c = [1,2,3,4,5]

     list_c[0:5]와 list_c[:]는 같다

 

 

pop(인덱스)

list_c = [1,2,3,4,5]

list_c.pop(1)

print("list_c.pop(1) = ", list_c)

print("list_c[1] = ", list_c[1])



# pop()은 인덱스값을 안넣고도 사용할 수 있는데, 이때 기본값은 -1이다.

list_c = [1,2,3,4,5]

list_c.pop() # 원소 5가 지워짐

print("list_c.pop() = ", list_c)

print()

 

 

remove(원소)

- 특정 원소를 지정하여 제거, 똑같은 원소가 여러개 있을땐 첫번째 원소만 제거됨

list_c=[1,2,3,4,5]

print("list_c=[1,2,3,4,5]", list_c)

list_c.remove(3)

print("list_c.remove(3) = ", list_c)

 

clear()

- 리스트 원소 모두 제거

list_c.clear()

print("list_c.clear() = ", list_c)

print()

 

 

 

반응형
저작자표시 (새창열림)

'Language > Python' 카테고리의 다른 글

Python 강의 정리 5. 리스트 뒤집기, enumerate() 함수  (0) 2021.05.02
Python 강의 정리 4. 반복문(for문,while문), 딕셔너리  (0) 2021.05.02
Python 강의 정리 2. continue, pass, break 차이점  (0) 2021.05.02
Python 강의 정리 1. 입출력, 조건문(if문)  (0) 2021.05.02
2020년도 2회차 정처기 실기 파이썬문제 - set(집합)  (0) 2021.05.02
    'Language/Python' 카테고리의 다른 글
    • Python 강의 정리 5. 리스트 뒤집기, enumerate() 함수
    • Python 강의 정리 4. 반복문(for문,while문), 딕셔너리
    • Python 강의 정리 2. continue, pass, break 차이점
    • Python 강의 정리 1. 입출력, 조건문(if문)
    Jen'_'
    Jen'_'
    DevOps 엔지니어 Jen의 기술 블로그 / 이메일: jenakim47@gmail.com

    티스토리툴바