List

컴퓨터에서 ‘여러개’라는건 0 또는 하나 이상 list에는 아무것도 안들수도, 하나가 들어있을수도, 하나이상이 들어 있을 수 있음 list is a ‘collection’ of values

l = [1,2,3] -> list에 값을 하나씩 집어넣음

l = list([1,2,3]) -> list를 집어놓고 list를 만들라고 한것 -> list

l = [x in range(10)] ->set builder

list can hold another list

1
2
3
x = [1,2,3]
y = [1,2,x]
y

[1,2,[1,2,3]]


list can be modified

1
2
x.append(4)
x

[1,2,3,4]

append : x리스트에 값을 추가하라 . operator => method call 이라 부름

그럼 이것도 가능한가?

1
2
3
4
5
6
7
8
l = 3
l.append(4)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-dcf28f8b6fcf> in <module>()
----> 1 l.append(4)

AttributeError: 'int' object has no attribute 'append'

integer 값을 두개를 append할 수는 없음 append는 list값에만 수행될 수 있는것

1
2
3
4
5
6
7
8
9
x = (3,4)
x.append(5)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-399ca43270b6> in <module>()
      1 x = (3,4)
----> 2 x.append(5)

AttributeError: 'tuple' object has no attribute 'append'

tupple은 list처럼 값의 모임인데 append 왜 앙대지? => tupple은 변경될 수 없기 때무니딩

A Python solution

A python program to solve a problem?

  • Enter input
  • Print (display) output

How to create a Python solution?

  • Find a set of example values to compute
  • Store them in variables
  • Do computation on variables
  • Benefit? -> Generalization

Problem solving example

1)

If you run a 10 kilometer race in 43 minutes 30 seconds, what is your average time per mile? What is your average speed in miles per hour? (Hint: there are 1.61 kilometers in a mile).

1
2
3
4
5
6
7
sec = 43 * 60 + 30
km = 10
mile = km / 1.61
milepersec = mile / sec
mileperhour = milepersec*3600
mileperhour
8.567144998929106

Define a function to compute the speed in miles per hour given the distance in km and the time in seconds.

1
2
def convert_speed(km, sec) :
   ...
1
2
3
4
5
6
7
8
9
sec = 43 * 60 + 30
km = 10
def convert_speed(km, sec) :
  mile = km / 1.61
  milepersec = mile / sec
  mileperhour = milepersec*3600
  return mileperhour
convert_speed(km,sec)
8.567144998929106
1
2
3
4
5
6
7
8
9
10
11
12
13
14
km = 10
m = 43
s = 30
def minsec(m, s) :
  totalsec = m * 60 + s
  return totalsec

def convert_speed(km, sec) :
  mile = km / 1.61
  milepersec = mile / sec
  mileperhour = milepersec*3600
  return mileperhour
convert_speed(km,minsec(43,30))
8.567144998929106


2)

The volume of a sphere with radius r is 4/3πr³. What is the volume of a sphere with radius 5?

1
2
3
import math
r = 5
vol = 4/3 * math.pi * r ** 3


3)

The area of a circle with radius r is πr². What is the value of a sphere with radius 3.4?

1
2
3
import math
r = 3.4
area = math.pi*r**2
1
2
3
def circle_area(r):
  return math.pi*r ** 2
print(circle_area(3.4))


What if we increase the radius by 0.7?

1
2
3
import math
print(circle_area(3.4))
print(circle_area(3.4 + 0.7))

What about using a variable r?

1
2
3
4
5
import math
r = 3.4
print(circle_area(r))
r += 0.7
print(circle_area(r))

=> is this concept generalizable?

use class!

class

Circle 이라는 class를 만들어서 반지름을 넣으면 원이 만들어 지는 함수를 정의함

1
2
3
4
5
class Circle:
    def __init__(self,r):
        self.r = r
x = Circle(1)
x.r

1

self 대신 아무렇게나 써서 해도 됨

1
2
3
4
5
class Circle:
    def __init__(elmo,r):
        elmo.r = r
x = Circle(1)
x.r

1

하지만 파이썬에서는 self라는 걸 많이 사용함 (편의상)

1
2
3
4
5
6
class Circle:
  def __init__(self,r):
    self.r = r
  def area(self):
    return math.pi*self.r**2
x = Circle(1)
1
x.r

1

1
x.area()

3.14


1
2
3
4
5
6
7
8
9
10
11
12
class Circle:
  def __init__(self,r):
    self.r = r
  def area(self):
    return math.pi**2 * self.r
  def inc_r(self, d):
    self.r += d

c = Circle(3.4)
print(c.area())
c.inc_r(0.7)
print(c.area())

36.316811075498 52.81017250684442