3. 화면 입출력과 리스트
2023. 2. 1. 00:16
- 표준 입력 함수 : input() - 괄호 안에 문자열 삽입 가능, 실행 시 입력 대기로 인수 typing 진행 필요
print("본 프로그램은 섭씨온도를 화씨온도로 변환하는 프로그램입니다.")
print("변환하고 싶은 섭씨온도를 입력하세요.")
temp = float(input())
f = temp*1.8 + 32
print("섭씨온도 : ", temp)
print("화씨온도 : ", f)
- 리스트 : 파이썬에서 배열(array)을 의미, 하나의 변수에 여러개의 값을 저장함
- indexing : 리스트 내의 상대적인 주소, 0부터 시작
- slicing : 리스트 전체 혹은 일부를 잘라내는 것
study = ['calculus', 'statistics', 'python', 'linear algebra' ,'english']
# indexing
print(study[1])
# slicing
print(study[1:3]) #마지막 index 의 -1 까지 출력
# slicing의 step(증가분)
print(study[::2]) # 2개 단위
print(study[::-1]) # 역순
>>statistics
>>['statistics', 'python']
>>['calculus', 'python', 'english']
>>['english', 'linear algebra', 'python', 'statistics', 'calculus']
- 리스트의 연산
- list 간의 덧셈 가능 (뒤로 붙이기)
- list 에 상수 곱셈 가능 (동일 list 반복)
- appned() : list 의 마지막 index 뒤에 값 추가
- extend() : list 이 마지막 index 뒤에 list 추가 (덧셈과 동일)
- insert() : list 내 지정한 index 위치에 값 추가
- remove() : list 내 해당 값 삭제
- packing, unpacking : list 에 할당하는 개념을 packing, list 내의 값을 변수에 할당하는 것을 unpacking
study1 = ['calculus', 'statistics', 'python']
study2 = ['linear algebra' ,'english']
# 기본 list 연산
study = study1 + study2
study.append('semiconductor')
study.extend(['science', 'chemistry'])
study.insert(0, 'physics')
study.remove('english')
print(study)
# unpacking
a,b,c,d,e,f,g,h = study
print(a,b,c,d,e,f,g,h)
>>['physics', 'calculus', 'statistics', 'python', 'linear algebra', 'semiconductor', 'science', 'chemistry']
>>physics calculus statistics python linear algebra semiconductor science chemistry
'Python' 카테고리의 다른 글
for문 에서 2개의 인수를 받기 위한 enumerate 기능 (0) | 2023.02.26 |
---|---|
날짜와 시간 (0) | 2023.02.08 |
format method (0) | 2023.02.08 |
4. 조건문과 반복문 (0) | 2023.02.05 |
2. 변수와 자료형 (0) | 2023.01.27 |