Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 주피터노트북
- 파이썬시각화
- 파이썬
- 파이썬데이터분석
- 파이썬데이터분석주피터노트북
- 파이썬알고리즘
- SQL
- 주피터노트북맷플롯립
- 파이썬크롤링
- 팀플기록
- 파이썬수업
- Python
- 주피터노트북그래프
- 판다스데이터분석
- sql연습하기
- 주피터노트북데이터분석
- 데이터분석시각화
- 판다스그래프
- sql따라하기
- 파이썬차트
- 주피터노트북판다스
- python데이터분석
- SQLSCOTT
- 수업기록
- sql연습
- python알고리즘
- python수업
- matplotlib
- SQL수업
- 맷플롯립
Archives
- Today
- Total
IT_developers
Python 개념 및 실습 - list(1) 본문
list 구조- 자료형(배열과 같은 개념)
- 대괄호 사용
- 다양한 형태의 자료들을 담을 수 있음.
- 자료형을 혼합해도 상관없다.
# 생성
list1 = []
list2 = ["a", "b", "c"]
list3 = ["a", "b", "c", 1, 2]
list4 = [1, 2, 3, 4, 5, 6.5]
list5 = [1, 2, ["Life", "is", "short"]] # 2차원 list
list6 = list()
print(list1)
print(list2)
print(list3)
print(list4)
print(list5)
print(list6)
# 인덱싱 : 하나의 기본 자리 개념이 있음
print("list2[0] : ", list2[0])
print("list3[-1] : ", list3[-1])
print("list4[3] : ", list4[3])
# 4 + 2 = 6자리 개념이 있음
print("list4[3] + list5[1] : ", (list4[3] + list5[1]))
print("list5[2][0] : ", list5[2][0]) # 2번에서 0번을 가지고 나오겠다.
# ["Life", "is", "short"] 전체가 -1, 안에서 0,1,2,번 이라 short가 출력됨.
print("list5[-1][2] : ", list5[-1][2])

# 슬라이싱 : 어디부터 어디, 자르기 개념
print("list2[0:3] : ", list2[0:3])
print("list3[-1:3] : ", list3[1:3])
print("list5[2:] : ", list5[2:])

# 실습 : is 출력
list6 = [1, 2, ["a", "b", ["Life", "is"]]]
print("list6 [2][2][1] : ", list6[2][2][1])
print("list6 [-1][-1][1] : ", list6[-1][-1][1])
print("list6 [2][-1][-1] : ", list6[2][-1][-1])

# 연산자
# + : list와 list의 연결, 숫자와 문자도 가능
# 인덱싱 + : 산술연산자
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = ["a", "b", "c"]
print("list1 + list2 = ", (list1 + list2))
print("list1 + list3 = ", (list1 + list3))
print("list1[0] + list2[1] = ", (list1[0] + list2[1]))
# 1 + b => TypeError: unsupported operand type(s) for +: 'int' and 'str'
# print("list1[0] + list3[1] = ", (list1[0] + list3[1]))

# * : 반복
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = ["a", "b", "c"]
print("list1 * 3 = ", (list1 * 3)) # [1, 2, 3]가 세번 반복됨.
print("list1[0] *3=", (list1[0] * 3))

# 리스트 요소 값 변경
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = ["a", "b", "c"]
print("list1 = ", list1)
list1[1] = 7 # 1번 자리에 7 넣기
print("list1 =", list1)
list1[2] = "Life"
print("list1 =", list1)
print("list2 =", list2)
list2[1:2] = [10, 11]
print("list2 =", list2)
list2[1] = [15, 16, 17] # list 안에 list로 삽입
print("list2 =", list2)

# 리스트 요소 삭제 : del, [ ]
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = ["a", "b", "c"]
print("list1 =", list1)
del list1[2]
print("list1 =", list1)
del list1[1:3]
print("list1 =", list1)
list1[1:3] = []
print("list1 =", list1)

list1 = [1, 2, 3, 4, 5, 6, 7, 8]
for num in list1:
print(num, end=" ")

실습
numbers = [273, 103, 5, 32, 65, 9, 72, 800, 99]
# 리스트 안의 숫자 중 100이상인 숫자 출력
for num in numbers:
if num >= 100:
print(num, end=" ")
# 리스트 안의 숫자가 홀수/짝수인지 판별하기
for num in numbers:
if num % 2 == 0:
print("{}는 짝수".format(num))
else:
print("{}는 홀수".format(num))
# 리스트 안의 숫자들의 자릿수 출력하기
# 273 3자리, 103은 3자리, 5는 1자리
for num in numbers:
print("{}은 {}자리".format(num, len(str(num))))

'Python' 카테고리의 다른 글
Python 개념 및 실습 - list(3) (0) | 2022.09.06 |
---|---|
Python 개념 및 실습 - list(2) (0) | 2022.09.05 |
Python 개념 및 실습 - for문 (0) | 2022.09.04 |
Python 개념 및 실습 - if (0) | 2022.09.04 |
Python 개념 및 실습 - while (0) | 2022.09.03 |
Comments