책 없이 파이썬 공부하기.
codecademy.com
에서 파이썬을 배운 내용을 나중에 편하게 찾아보기 위해 정리해봄.
1. Python Syntax 1) Python Syntax
2. Variables 변수
자료형 선언 필요 X. 'var'도 필요 X
my_var = 10
print myvar
3. Booleans
only True or False
? true or false => NameError
? TRUE or FALSE -> NameError
? 0 or 1 => 정수
5. Whitespace
whitespace(공백, 인덴트)로 구조 인식. 중괄호 사용 X
def spam():
eggs = 12 # IndentationError
def spam():
eggs = 12
인덴트는 탭, 띄어쓰기 상관 없음. 단 띄어쓰기 수를 맞출 것!
def spam():
eggs = 12 # 띄어쓰기 4번
return eggs # 띄어쓰기 5번
print spam()
=> IndentationError: unexpected indent
def spam():
eggs = 12 # 띄어쓰기 4번
return eggs # 띄어쓰기 4번
print spam() # 탭 1번
=> IndentationError: unexpected indent
def spam():
eggs = 12 # 띄어쓰기/탭
return eggs # 띄어쓰기/탭
print spam() # 띄어쓰기 1번
=> IndentationError: unindent does not match any outer indentation level
6. Whitespace Means Right Space
니가 인덴트로 띄어쓰기를 두번 하든 네번 하든 상관없지만,
통일 시켜라.
8,9. Single Line / Multi-Line Comments
주석
한줄은 # comment ( // comment )
여러줄은 """ comment """ ( /* comment */ )
11. Exponentiation
거듭제곱, 지수
eggs = 10 ** 2
print eggs # 100
12. Modulo
모드 연산. 나머지 연산. 똑같음. %
2) Tip Calculator
3. Escapint characters
문자열에서 따옴표. 이스케이프 문자(역슬래쉬, 원화기호)와 써야함.
'There's a snake' (X)
'There\'s a snake' (O)
4. Access by Index
파이썬 문자열 인덱스로 문자 초기화
fifth_letter = "MONTY"[4]
5. String methods
parrot = "Norwegian Blue"
len(parrot) # call the len() function with a argument
parrot.lower() # invoke the parrot's .lower() function
parrot.upper()
pi = 3.14
str(pi) # java의 toString()
13. Explicit String Conversion
print 3.14
print "String" + 3.14 # TypeError: cannot concatenate 'str' and 'float'
objectsprint "String" + str(3.14)
14. String Formatting with %, Part1
파이썬 출력 printf 변환명세 스트링 포맷
first = "A"
second = "B"
print "first %s and second %s" %(first, second)
15. part2
문자열 입력받기
name = raw_input("What is your name? ")
quest = raw_input("What is your quest? ")
color = raw_input("What is your favorite color? ")
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)
16. 정리
Three ways to create strings
'Alpha'
"Bravo"
str(3)
String methods
len("Charlie")
"Delta".upper()
"Echo".lower()
Printing a string
print "Foxtrot"
Advanced printing techniques
g = "Golf"
h = "Hotel"
print "%s, %s" % (g, h)
Date and Time
2. Getting the Current Date and Time
현재시각
frome datetime import datetime
now = datetime.now()
3. Extracting Information
now.year
now.month
now.day
now.hour
now.minute
now.second
from datetime import datetime
now = datetime.now()
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute,
now.second)
Conditionals & Control Flow
9. This and That(or This, But Not That!)
파이썬 비교 연산자 우선순위 비교 연산 순서
1. not is evaluated first;
2. and is evaluated next;
3. or is evaluated last.