-
Python 요약#1computing 2012. 10. 25. 11:24
python 요약#1
Variable and Data Types
- 특징: High-level, Interpreted, Object-oriented
- i = 123
- f = 12.3
- b = True
- case-sensitive
Whitespace and Statements
- statement 구분은 whitespace로
Comment
- # single comment
- “”” multi
line
comment
“””
Math Operations
- +, -, *, /, %, **(exponentiation)
Strings
- “string”
- ‘string’
- “string with \’escape\’”
- “string”[0] == s
string methods
- len(“string”)
- str(23)
- “string”.upper()
- “STRING”.lower()
Print
- print “string”
- print “your name is %s. and age is %s” %(“Ted”, str(39))
- print "%d to the power of %d is %d." % (base, exponent, result)
Date and Time
- from datetime import datetime
now = datetime.now()
now.month, now.day, now.year, now.hour, now.minute, now.second
Control Flows
Comparators
- ==, !=, >=, <=, >, <
Boolean Operator
- and, or, not
if, Else and Elif
- if 8 > 9:
print ‘..’
elif 9 > 8:
print ‘xxxx’
else:
print ‘test’
string and char
- <string>[<start index>:<end index(not included)>
s = “foobar”
s[0:2] # fo
functions
- def tax(bill):
"""Adds 8% tax to a restaurant bill."""
bill *= 1.08
print bill
return bill - def favorite_fruits(*args):
print "좋아하는 과일은:" , args
favorite_fruits("사과", “배", “수박", “바나나")
importing module
- 모듈 import하기
import math
print math.sqrt(25) - 모듈내 특정함수만 import
from <모듈명> import <함수명> - ex) from math import sqrt
- 모들내 모든함수 import
from <모듈명> import *
반응형