1. Variables
- 변수는 어디에 저장되는가?
- 프로그래밍에서 변수는 값이 저장되는 저장소
- 변수는 메모리 주소를 가지고 있고 변수에 들어가는 값은 메모리 주소에 할당됨
a = 3 # a라는 변수에 3이라는 값이 저장된다
b = 4 # b라는 변수에 4라는 값이 저장된다.
a+b
7
# 메모리 위에선 어디에 저장될까?
# id는 메모리 주소값을 알아보는 함수이다.
print(f'a의 객체 고유값은 {id(a)}')
print(f'b의 객체 고유값은 {id(b)}')
a의 객체 고유값은 140726973236976
b의 객체 고유값은 140726973237008
2. 변수명 저장법
1) 알파벳, 숫자, 언더스코어(_)로 선언가능
2) 변수명은 의미있는 단어로 표기하는 것이 좋다.
3) 변수명은 대소문자가 구분된다.
4) 특별한 의미가 있는 예약어는 쓰지 않는다
professor_name = 'SungChul Choi'
student_name = "Kim"
Abc = 0 #대문자
abc = 10 #소문자
print(professor_name)
print(student_name)
print(Abc)
print(abc)
SungChul Choi
Kim
0
10
예약어를 변수명으로 지정해선 안된다
for = 0
File "<ipython-input-5-f096fd0688eb>", line 1
for = 0
^
SyntaxError: invalid syntax
3. 간단한 연산 (Basic Operation)
1) 기본자료형 (primitive data type)
2) 연산자와 피연산자
3) 데이터 형변환
기본자료형
data1 = 1 # integer
data2 = 9.0 # float
data3 = 'abc' # string
data4 = True # boolean
print(type(data1))
print(type(data2))
print(type(data3))
print(type(data4))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
first = 100
second = 100
print(first==second)
True
연산자와 피연산자
3+2 # 3과 2는 피연산자, +는 연산자
5
3*2
6
3**2
9
데이터형변환
# float => int : int()함수는 소수점 이하 내림 (버림, 반올림이 아니다)
a = 9.7
b = 9.3
print(int(a))
print(int(b))
9
9
컴퓨터의 반올림 오차
a = 0.1 * 3
print(a)
a
0.30000000000000004
0.30000000000000004
4. 리스트
인덱싱 슬라이싱
colors = ['red','blue','green']
print(colors[0])
print(colors[2])
print(len(colors)) # 리스트의 길이
red
green
3
cities = ['서울','부산','인천','대구','대전','광주','울산','수원']
print(cities[0:6])
print(cities[-9:])
print(cities[:])
print(cities[-50:50]) # 범위 넘어갈 경우 자동으로 최대 범위를 지정
print(cities[::2]) # 2칸 단위로 슬라이싱
print(cities[::-1]) # 순서 역으로 슬라이싱
['서울', '부산', '인천', '대구', '대전', '광주']
['서울', '부산', '인천', '대구', '대전', '광주', '울산', '수원']
['서울', '부산', '인천', '대구', '대전', '광주', '울산', '수원']
['서울', '부산', '인천', '대구', '대전', '광주', '울산', '수원']
['서울', '인천', '대전', '울산']
['수원', '울산', '광주', '대전', '대구', '인천', '부산', '서울']
리스트의 연산 -추가와 삭제
color = ['red','blue','green']
color2 = ['orange','black','white']
concatenation
color+color2
['red', 'blue', 'green', 'orange', 'black', 'white']
할당
color[0]='yellow'
*n : n번 반복, concatenation
color*2
['yellow', 'blue', 'green', 'yellow', 'blue', 'green']
in 연산자
'blue' in color
True
'blue' in color2
False
append : 인자로 리스트 원소가 될 부분을 받음
color.append('white')
color
['yellow', 'blue', 'green', 'white']
extend : 인자로 리스트를 받음
color.extend(['black','purple'])
color
['yellow', 'blue', 'green', 'white', 'black', 'purple']
del, remove
del color[0]
color
['blue', 'green', 'white', 'black', 'purple']
color.remove('blue')
color
['green', 'white', 'black', 'purple']
sort, sorted
a = [5,4,3,2,2,1]
sorted(a)
[1, 2, 2, 3, 4, 5]
b = [9,7,6,5,4,3]
b.sort()
b
[3, 4, 5, 6, 7, 9]
'[파이썬] 개념정리 > [파이썬] 자료구조' 카테고리의 다른 글
[Python] 자료구조 - 우선순위큐와 힙(Heap) (0) | 2020.07.09 |
---|---|
[Python] 자료구조 - 트리 (0) | 2020.07.08 |
[Python] 자료구조 - 덱(Deque) (0) | 2020.07.08 |
[Python] 자료구조 - 스택(Stack) (0) | 2020.07.08 |
[Python] 자료구조 - 큐(Queue) (0) | 2020.07.07 |