1-1) 숫자형
1-2) 숫자형 연산자
>>> a=3
>>> b=4
>>> a+b
7
>>> a*b
12
>>> a/b
0.75
>>> a=3
>>> b=4
>>> a**b
81
•나눗셈 후 나머지를 반환하는 %
>>> 7%3
1
>>> 3%7
3
•나눗셈 후 소수점 제거 //
>>> 7//4
1
2-1) 문자열
Life is too short
You need python
“””
2-2) 문자열 연산
•문자열 더하기
>>> head = "Python"
>>> tail = " is fun!"
>>> head + tail
'Python is fun!'
•문자열 곱하기
>>> a = "python"
>>> a*2
'pythonpython'
2-3) 문자열 인덱싱과 슬라이싱
•인덱싱
>>> a = "Life is too short, You need Python"
>>> a[0]
'L'
>>> a[12]
's'
>>> a[-1]
'n'
•슬라이싱
>>> a = "Life is too short, You need Python"
>>> a[0:4] <-- 0<=a<4
'Life'
>>> a = "20010331Rainy"
>>> year = a[:4]
>>> day = a[4:8]
>>> weather = a[8:]
>>> year
'2001'
>>> day
'0331'
>>> weather
'Rainy'
2-4) 문자열 포매팅
>>> number = 10
>>> day = "three"
>>> "I ate %d apples. so I was sick for %s days." % (number, day)
'I ate 10 apples. so I was sick for three days.'
2-5) 문자열 함수
•문자 개수 세기(count)
>>> a = "hobby"
>>> a.count('b)
2
•문자열 삽입(join)
>>> a = ","
>>> a.join('abcd')
'a,b,c,d'
>>> a = "Life is too short"
>>> a.index('t')
8
>>> a.index('k')
오류
•소문자 -> 대문자(upper)
>>> a = "hi"
>>> a.upper()
'HI'
•대문자 -> 소문자(lower)
>>> a = "HI"
>>> a.lower()
'hi'
>>> a = "Life is too short"
>>> a.split()
['Life', 'is', 'too', 'short']
>>> a = "a:b:c:d"
>>> a.split(':')
['a', 'b', 'c', 'd']
'IT > Python' 카테고리의 다른 글
Pycharm 다운로드 및 설치 (3) | 2018.05.26 |
---|---|
Python 다운로드 및 설치 (0) | 2018.05.26 |
1. 파이썬이란 (0) | 2016.09.30 |
R Python 비교 (0) | 2016.09.21 |