함수를 이용하여 특정한 동작을 하는 코드를 반복 할 수 있다.

예를 들어, 피타고라스의 정리를 수행하는 함수는 다음과 같이 정의하여 호출한다.

import math

def pythagoras(a,b):
    value = math.sqrt(a *a + b*b)
    print(value)

pythagoras(3,3)


함수의 계산 결과를 변수에 저장하여 호출하는 것도 가능하다.

import math

def pythagoras(a,b):
    value = math.sqrt(a*a + b*b)
    return value


result = pythagoras(3,3)
print(result)


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : Tuples [8]  (0) 2016.04.15
Python Tutorial : Lists [7]  (0) 2016.04.15
Python Tutorial : While loop [5]  (0) 2016.04.15
Python Tutorial : For loops [4]  (0) 2016.04.15
Python Tutorial : Variables [3]  (0) 2016.04.15

파이썬은 while 키워드로 반복 코드 작성이 가능하다.

다음은 5 라는 숫자를 예측하는 게임

x = 0
while x != 5:
   x = int(input("Guess a number:"))
   
   if x != 5:
      print("Incorrect choice")

print("Correct")


게임 방식을 확장하여 입력한 숫자에 대한 힌트를 추가

x = 0
while x != 5:
   x = int(input("Guess a number:"))
   
   if x != 5:
      print("Incorrect choice")
   if x > 5:
      print("Please enter a smaller number")
   if x < 5:
      print("Please enter a larger number")

print("Correct")


주의 할 점은 while 루프의 조건이 충족되지 않을 경우 무한 루프에 빠지는 문제를 발생시킬 수 있다.

'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : Lists [7]  (0) 2016.04.15
Python Tutorial : Functions [6]  (0) 2016.04.15
Python Tutorial : For loops [4]  (0) 2016.04.15
Python Tutorial : Variables [3]  (0) 2016.04.15
Python Tutorial : If statements [2]  (0) 2016.04.15

for 키워드를 사용하여 코드를 여러 번 반복 할 수 있다.

마지막 숫자 11은 포함되지 않는다.

결과 값은 1 ~ 10 출력

for i in range(1,11):
    print i


파이썬은 다른 프로그래밍 언어들과 마찬가지로 0부터 계산을 시작 할 수 있다.

결과 값은 0 ~ 9 출력

for i in range(0,10):
    print i


Nested loops

루프는 중첩될 수 있다.

for i in range(0,10):
    for j in range(0,10):
        print i,' ',j


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : Functions [6]  (0) 2016.04.15
Python Tutorial : While loop [5]  (0) 2016.04.15
Python Tutorial : Variables [3]  (0) 2016.04.15
Python Tutorial : If statements [2]  (0) 2016.04.15
Python Tutorial : Text input and output [1]  (0) 2016.04.15

파이썬에서는 변수에 숫자와 텍스트를 넣을 수 있다.

x = 2
price = 2.5
word = 'Hello'


변수에는 공백이나 특수문자를 쓸 수 없다. 변수를 정의하는 3가지 예

word = 'Hello'
word = "Hello"
word = '''Hello'''


변수는 재 정의가 가능하다.

x = 2

# increase x by one
x = x + 1

# replace x
x = 5



파이썬의 조건연산자

>   greater than
<   smaller than
==  equals
!=  is not


조건은 항상 변수와 결합 if 구문

x = int(input("Tell X"))

if x == 4:
    print 'You guessed correctly!'

print 'End of program.'


if else 구문

x = int(input("Tell X"))

if x == 4:
    print 'You guessed correctly!'
else:
    print 'Wrong guess'

print 'End of program.'


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : While loop [5]  (0) 2016.04.15
Python Tutorial : For loops [4]  (0) 2016.04.15
Python Tutorial : Variables [3]  (0) 2016.04.15
Python Tutorial : Text input and output [1]  (0) 2016.04.15
파이썬 기초 - Python이란?  (0) 2016.04.14

Output

화면에 텍스트 출력

print("Hello Python")


여러 줄의 텍스트 출력

print("Hello World\nThis is a message")


변수 출력

x = 3
print(x)


다수의 변수 출력

x = 2
y = 3
print(x, ' ', y)


Input

텍스트 입력

name = raw_input("Enter a name: ")


실수 입력

x = int(input("What is x? "))
x = float(input("Write a number"))


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : While loop [5]  (0) 2016.04.15
Python Tutorial : For loops [4]  (0) 2016.04.15
Python Tutorial : Variables [3]  (0) 2016.04.15
Python Tutorial : If statements [2]  (0) 2016.04.15
파이썬 기초 - Python이란?  (0) 2016.04.14

파이썬은 귀도 반 로썸(Guido van Rossum) 이라는 분이 심심해서(!) 만들었다고 전해지는 프로그래밍 언어의 일종.


현재 최신 버전은 Python3.x 이지만 내가 필요한건 Python2.7 이므로 2.7 기준으로 정리할 예정.

어차피 2to3라는 모듈이 있기 때문에 필요하면 Python3.x로 코드를 변환하는 것은 어렵지 않음.


C언어를 할 줄 알면 배우기 쉽지만, 코드 들여쓰기 등에는 상당히 엄격한 문법 체계를 가지고 있다.

(그런데 Python3.x에서는 한글도 변수명으로 사용할 수 있다는 건 함정. 유니코드를 활용하게 되면서 가능해진 듯.)


iOS MDM 서버를 구축하면서.. 파이썬을 전혀 몰라서 배워야 할 필요가 있다고 느껴 파이썬을 시작하게 되었다.


import this 를 하면 이스터에그로 파이썬의 정신(?) 하여튼.. 추구하는 바라던가 하는것이 나온다.


The ZEN of Python
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases are not special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
   Namespaces are one honking great idea -- let's do more of those!
대충 해석하면 아름답고 간결하며 실용적인 코드를 작성하라는 말씀.


앞으로 포스팅할 파이썬 튜토리얼은 Python.org의 비기너 가이드를 한국어로 번역 하면서 같이 공부 할 예정.


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : While loop [5]  (0) 2016.04.15
Python Tutorial : For loops [4]  (0) 2016.04.15
Python Tutorial : Variables [3]  (0) 2016.04.15
Python Tutorial : If statements [2]  (0) 2016.04.15
Python Tutorial : Text input and output [1]  (0) 2016.04.15

+ Recent posts