Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. ·...

24
Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept. of Automobile & IT Convergence Kookmin University

Transcript of Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. ·...

Page 1: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Programming Language(Memory, variables, and data types)

Prof. Jong-Chan KimDept. of Automobile & IT Convergence

Kookmin University

Page 2: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Schedule (Tentative)Mon Wed

Week 1 Course introduction Setting up python environment

Week 2 Memory, variables, and data types Basic operators

Week 3 Decision making Loops

Week 4 Numbers Strings

Week 5 Lists Tuples

Week 6 Dictionary Date and time

Week 7 Functions Modules

Week 8 Mid-term exam

Week 9 File I/O Exceptions

Week 10 Classes and objects (1/2) Classes and objects (2/2)

Week 11 GUI programming Network programming

Week 12 Project 1 (1/2) Project 1 (2/2)

Week 13 Buddha’s Birthday Project 2

Week 14 Project 3 (1/2) Project 3 (2/2)

Week 15 Final exam

Page 3: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

오늘의 강의 목표• 변수(Variable)와메모리(Memory)에대한 이해• 자료형(Data Type)에 대한 이해• 형변환(Type Conversion)에대한 이해

Page 4: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Variables>>> pi = 3.141592>>> r = 10>>> area = pi * r ** 2>>> area314.1592

3.141592pi

10r

area pi * r ** 2

Page 5: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Creating Variables

>>> my_age = 20 # create my_age variable>>> print(my_age)20>>> your_age = my_age # create your_age variable>>> print(your_age)20

assignment

• = (assignment) 연산자를 이용하여 변수 생성• 생성된 변수는 변수명을 통해 접근

>>> my_age = your_age = 20 # create two variables

Page 6: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Destroying Variables

>>> year = 2015 # create year variable>>> print(year)2015>>> del year # destroy year variable>>> print(year)Traceback (most recent call last):

File "<pyshell#14>", line 1, in <module>print(year)

NameError: name ‘year' is not defined

• del 명령어를 이용하여 변수 삭제• 삭제된 변수는 접근 불가

Page 7: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Working with Variables>>> days = 3 # create days>>> hours = days * 24 # create hours with days>>> hours72>>> days = days + 1 # update days>>> days4>>> days += 1 # a shortcut>>> days5

Page 8: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Creating String Variables>>> my_name = "John" # string variable with ">>> print(my_name)John>>> your_name = 'Taylor' # string variable with '>>> print(your_name)Taylor>>> message = ''' # string variable with '''Dear Professor,My name is Jong-Chan.Nice to see you.'''>>> print(message)

Dear Professor,My name is Jong-Chan.Nice to see you.

For multi-line string

Page 9: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Deleting String Variables>>> my_name = "John" >>> print(my_name)John>>> del my_name>>> print(my_name)Traceback (most recent call last):

File "<pyshell#111>", line 1, in <module>print(my_name)

NameError: name 'my_name' is not defined

Use del command

Page 10: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Variables and Memory• 변수(Variable): 자료를 저장하기 위한 저장 장소• 각 변수는 Memory의 일부 장소를 사용

0 1 0 1 0 0 1 1

J

o

h

n

1 Byte = 8 bits

10 Bytes

>>> my_name = "John"

>>> del my_name

my_name 변수에할당된 메모리 영역

변수 생성

변수 삭제

0번지

1번지

2번지

3번지

4번지

5번지

6번지

7번지

8번지

9번지

Page 11: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Variables and Memory• 각 변수가 차지하는 사이즈

– sys.getsizeof()

>>> import sys>>> a = 10>>> sys.getsizeof(a)28 # 28 Bytes for variable a>>> b = "John">>> sys.getsizeof(b)53 # 53 Bytes for variable b>>> b = "John is working in the backyard">>> sys.getsizeof(b)80 # Variable b grows to 80 Bytes

Page 12: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Python Data Types• Numbers

– 숫자 ex) 10, -78, 080, 0x80, 3.14, 1 + 3j• String

– 문자열 ex) "John", 'Hello World'• List

– 목록 ex) [1, 2, 3, 4], ['a', 'b', 'c'], [1, 'a', 'b', 2]• Tuple

– 조 ex) (1, 2), ('a', 'b', 'c')• Dictionary

– 사전 ex) {1:"John", 2:"Taylor"}

Page 13: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Python Number Types• int

– 정수

• float– 실수

• complex– 복소수

Page 14: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Python Number Types>>> a = 10>>> b = 10.0>>> c = 10 + 10j>>> type(a)<class 'int'>>>> type(b)<class 'float'>>>> type(c)<class 'complex'>

type() 명령 이용하여 자료형 확인

Page 15: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Python Strings>>> s = 'Hello World'>>> type(s)<class 'str'>>>> len(s) # string length11>>> print(s.upper()) # upper caseHELLO WORLD>>> print(s.lower()) # lower casehello world

이 외에도 많은 string 함수 제공 (아래 링크 참조)

https://docs.python.org/3.4/library/stdtypes.html#string-methods

Page 16: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Working with Strings>>> my_name = "John">>> full_name = my_name + " Kim" # + for concatenation >>> print(full_name)John Kim>>> print(my_name * 3) # * for repetitionJohnJohnJohn>>> my_age = 20>>> print(my_name + "is " + my_age)Traceback (most recent call last):

File "<pyshell#42>", line 1, in <module>print(my_name + "is " + my_age)

TypeError: Can't convert 'int' object to str implicitly>>> print(my_name + "is " + str(my_age))John is 20

Why this error?

Convert int to string

Page 17: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

More on Python Strings

>>> s = 'Hello World'>>> s[0]'H'>>> s[0:4]'Hell'>>> s[0:5]'Hello'>>> s[6:11]'World'>>> s[6:]'World'

H e l l o W o r l d

s[0]

s:

s[1]

s[0:5]

s[6]…

s[6:11]

s[7] s[10]…

Page 18: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

List (1/2)

>>> l1 = [1, 2, 3, 4]>>> l1[0]1>>> l2 = [5, 6, 7, 8]>>> l = l1 + l2>>> l[1, 2, 3, 4, 5, 6, 7, 8]>>> l = l * 2>>> l[1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]

• 순서대로 나열된 자료들의 집합• 한 List가 서로 다른 Type의 자료들 포함 가능

Page 19: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

List (2/2)>>> l = [1, "Hello", 2, "World", 3]>>> l[1, 'Hello', 2, 'World', 3]>>> l.append("Goodbye")>>> l[1, 'Hello', 2, 'World', 3, 'Goodbye']>>> l[3] = 'Korea'>>> l[1, 'Hello', 2, 'Korea', 3, 'Goodbye']

Page 20: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Tuple>>> t = (1, "Hello", 2, "World", 3)>>> t(1, 'Hello', 2, 'World', 3)>>> t.append("Goodbye")Traceback (most recent call last):

File "<pyshell#191>", line 1, in <module>t.append("Goodbye")

AttributeError: 'tuple' object has no attribute 'append‘>>> t[3] = 'Korea'Traceback (most recent call last):

File "<pyshell#197>", line 1, in <module>t[3] = 'Korea'

TypeError: 'tuple' object does not support item assignment

Cannot append to a tuple

Cannot modify an item inside

Page 21: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Dictionary

>>> d = {'John': 177, 'Taylor':173, 'Brenndon':183}>>> d['John']177>>> d['Taylor']173>>> d['Brenndon']183>>> d = {1:'Apple', 2:'Orange', 3:'Pineapple'}>>> d[1]'Apple'>>> d[2]'Orange'>>> d[3]'Pineapple'

• Dictionary: Set of (Key, Values) Pair

Page 22: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Data Type Conversion (1/2)

>>> a = 10>>> type(a)<class 'int'>>>> b = str(a) # int to str 형변환>>> b'10'>>> type(b)<class 'str'>'

• 값을 유지한 채 Data Type을 변환• 다양한 형 변환 함수 제공

int(), float(), str() 등

Page 23: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Data Type Conversion (2/2)>>> a = 10>>> b = '20'>>> a + bTraceback (most recent call last):

File "<pyshell#22>", line 1, in <module>a + b

TypeError: unsupported operand type(s) for +: 'int' and 'str'>>> a + int(b) # str to int 형변환30

>>> a = 10.0>>> b = "15.5">>> a + float(b) # str to float 형변환25.5>>>

Page 24: Programming Language - KOCWelearning.kocw.net/KOCW/document/2015/kookmin/... · 2016. 9. 9. · Programming Language (Memory, variables, and data types) Prof. Jong-Chan Kim Dept.

Questions