파이썬(Python)
- 루비랑 비슷하긴 하지만 일부가 몹시다르다. CS를 공부해야하니 기초만 다뤄보자.
- 버전 : 2.7.0
문자열(String)
minwoo = "My name is minwoo"
print(miwnoo) #My name is minwoo
print(minwoo.find("My")) #0
print(minwoo.find(" ")) #2
print(miwnoo.replace("minwoo", "python")) #My name is python
print(miwnoo.upper) #MY NAME IS PYTHON
print(miwnoo.lower) #my name is python
- find(arg) : string 내에서 arguments의 위치(index)를 표시해준다.
- replace(arg1, arg2) : arg1부분을 arg2로 대체한다.
- uppper, lower : string을 대문자, 소문자로 바꾼다.
숫자(Number)
a = 10
b = 3
print(a + b) #13
print(a - b) #7
print(b - a) #-7
print(a * b) #30
print(a / b) #3
print(a % b) #1
print(a ** b) #1000
print(pow(a,b)) #1000
print(max(4,5,6,a,b)) #10
print(min(4,5,6,a,b)) #3
- pow(arg1,arg2) : arg1**arg2
- max(arg1,arg2..argN) : arg값들중 최대값
- min(arg1,arg2..argN) : arg값들중 최소값
import math
print(math.ceil(10.5)) #11
print(math.floor(10.5)) #10.0
print(math.sqrt(4)) #2.0
print(math.sin(1)) #0.841470984808
- math.ceil(arg) : arg소수점 올림.
- math.floor(arg) : arg소수점 내림
- math.sqrt(arg) : arg의 제곱근
- math.sin(arg) : arg의 sin값..수알못
배열(List)
food = ["Pancake", "Ramen", "Pho"]
print(food[0]) #Pancake
print(food[3]) #Error
print(food[-1]) #Pho
food[1] = "Noodle"
print(food) #['Pancake', 'Noodle', 'Pho']
아래와 같이 정의할수도 있다.
meat = list(["Pancake", "Ramen", "Pho"]) #['Pancake', 'Ramen', 'Pho']
print("Pancake" in meat) #true
print("Noodle" not in meat) #true
print(len(meat)) #3
print(max(meat)) #Ramen
print(min(meat)) #Pancake
meat.sort() #['Pancake', 'Pho', 'Ramen']
meat.reverse() #['Ramen', 'Pho', 'Pancake']
meat.append("Noodle") #['Ramen', 'Pho', 'Pancake', 'Noodle']
new_food = ["Egg", "Milk"]
meat.extend(new_food) #['Ramen', 'Pho', 'Pancake', 'Noodle', 'Egg', 'Milk']
meat.remove(new_food[0]) #['Ramen', 'Pho', 'Pancake', 'Noodle', 'Milk']
딕셔너리(Dictionary)
contact = {
'email': '[email protected]',
'phone': '010-0000-0000',
'address': 'seoul'
}
print(contact['email']) #[email protected]
contact['edu'] = 'konkuk university' #edu넣음
del contact['email'] #email빠짐
print('email' in contact.keys()) #false
print('edu' in contact.keys()) #true
2중구조
contacts = {
'minwoo1': {
'email': '[email protected]',
'phone': '010-0000-1111',
'address': 'seoul1'
},
'minwoo2': {
'email': '[email protected]',
'phone': '010-0000-2',
'address': 'seoul2'
}
}
print(contacts['minwoo1']['email'])
제어흐름(Control flow)
if/else
i = 10
if i % 2 == 0:
if i > 10:
print("Number is even and greater than 10")
elif i < 10:
print("Number is even and less than 10")
else:
print("Number is evern and being 10")
else:
print("Number is odd")
for/in
names = ["Minwoo", "Jane", "Gatsby"]
print(names[0])
print(names[1])
print(names[2])
for n in names:
print(n)
index = 0
while index < len(names):
print(names[index])
index += 1
함수(Function)
def say_hi(name,age):
print("My name is " + name + ".")
print("I'm " + str(age) + " years old.")
say_hi("minwoo", 32)
스코프(이거 안되는 이유점)
number = 1
inside_number = 1
def summary():
global number
number = 10
inside_number = 10
print("number in summary " + str(number))
print("inside_Number in summary " + str(inside_number))
summary()
print("number in Outside" + str(number))
print("inside_Number in Outside" + str(inside_number))
클래스(Class)
class Restaurant:
def __init__(self, name, address, revenue):
self.name = name
self.address = address
self.revenue = revenue
def show_address(self):
print(self.address)
def show_revenue(self):
print(self.revenue)
def order(self, money):
self.revenue += money
r1 = Restaurant("Pho", "itawon", 10)
r1.show_address() #itawon
r1.show_revenue() #10
r1.order(190)
r1.show_revenue() #200
모듈(Module)
import math #이것이 모듈
n = math.sqrt(16)
print(n) #4
직접 모듈을 만들어 호출할수 있다.
#my_module.py
number_of_styles=2
def style_upper(text):
return text.upper()
def style_lower(text):
return text.loweR()
#app.py
import my_module
print(my_module.number_of_styles)
print(my_module.style_upper("minwoo"))