[熱情]

python ... 본문

Programming/python

python ...

rootkaien 2016. 7. 12. 11:44

https://github.com/simonmonk/ 여기가면. 책 예제를 다 다운받을수 있음.


>>> print('hello python')
hello python
>>> 


>>> 2+2


file.py로 파일 만들어서


$ python file.py

로 실행하면된다..



*는 곱하기..

/는 나누기

나눗셈보다 곱셈을 먼저 계산.. 덧셈보다 나눗셈을 계산한다.


>>> 20*9/5+32
68.0



for 문


>>> for a in [1, 2, 3]:
... print(a)
  File "<stdin>", line 2
    print(a)
        ^
IndentationError: expected an indented block   // <- 요기서 오류 나는건 tab(들여쓰기)를 안해서 오류가 남 ..
>>> for x in range(1,10):
...     print(x)
... //<- 요기서 엔터 한번 입력
1
2
3
4
5
6
7
8
9
>>> 


난수. 램덤..

>>> import random
>>> random.randint(1,6)
2
>>> 


>>> import random
>>> for x in range(1, 11)
...     random_number = random.randint(1, 6)
...     print(random_number)
...
4
4
6
3
2
3
1
3
6
3
>>>


import random

while True:
        throw_1 = random.randint(1,6)
        throw_2 = random.randint(1,6)
        total = throw_1 + throw_2

        print(total)

        if throw_1 == 6 and throw_2 == 6:
                break

print('Double Six thrown!') 


문자열..

>>> book_name = 'Programming Raspberry Pi'
>>> book_name
'Programming Raspberry Pi'
>>> print(book_name)
Programming Raspberry Pi
>>> len(book_name)
24
>>> book_name[1]
'r'
>>> book_name[0]
'P'
>>> book_name[100]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> book_name[0:11]
'Programming'
>>> book_name[12:]
'Raspberry Pi'
>>> book_name+' by test'
'Programming Raspberry Pi by test'
>>> 


리스트..

>>> numb = [1,123,34,5,34] //리스트 선언
>>> len(numb)  // 리스트 크기
5
>>> numb[0] // 지정요소 출력
1
>>> numb[2]
34
>>> numb[2] = 888  // 지정요소 변경
>>> numb[2]
888
>>> numb
[1, 123, 888, 5, 34]
>>> more_numb= [1923,123,123,12,23]
>>> numb+more_numb // 리스트 더하기
[1, 123, 888, 5, 34, 1923, 123, 123, 12, 23]
>>> numb.sort() // 정렬
>>> numb
[1, 5, 34, 123, 888]
>>> numb.pop() // 삭제 마지막요소 삭제
888
>>> numb
[1, 5, 34, 123]
>>> numb.pop(1) // 지정해서 삭제
5
>>> numb
[1, 34, 123]
>>> numb.insert(1,88) // 지정요소에 삽입
>>> numb
[1, 88, 34, 123]
>>> 

리스트안에 다른 리스트를 담는 경우


>>> big_list = [123, 'hello', ['inner list', 2, True]]
>>> big_list
[123, 'hello', ['inner list', 2, True]]
>>> list = [1,'one',2,True]
>>> for item in list:
...     print(item)
...
1
one
2
True
>>>



함수..

>>> def make_polite(sentence):
...     polite_sentence = sentence+' please'
...     return polite_sentence
...
>>> print(make_polite('Pass the salt'))
Pass the salt please
>>> 


- 함수의 시작은 def 키워드다..

- 첫행은 반드시 콜론으로 끝나야된다.

- 함수의 마지막은 return명령이 차지한다.

- 함수를 사용하려면 함수명을 지정하고 적절한 인수를 지정한다.

- 리턴 값은 의무 사항이 아니다.

>>> def say_hello(n):
...     for x in range (0, n):
...             print('hello')
...
>>> say_hello(7)
hello
hello
hello
hello
hello
hello
hello 





딕셔너리 (dictionary)


튜플 (tuple)

- 리스트와 닮았다.

- 대괄호가 없다.

- 변경할수 없는 (immutable)특징을 가지고 있다.  다른값으로 바꿀수 없다는 뜻이다.

-


>>> tuple = 1,2,3
>>> tuple
(1, 2, 3)
>>> tuple[1]
2
>>> tuple[0]=5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>




다중 대입

>>> a, b, c = 1, 3, 5
>>> a
1
>>> b
3
>>> c
5
>>> 


다중 리턴


def stats(numb):
    numb.sort()
    return (numb[0], numb[-1])

list = [12, 34, 99, 5, 3]
min, max = stats(list)
print(min)
print(max)


3
99



예외(exception)










Comments