728x90
  • 숫자형

# 사칙연산
>>> a=3
>>> b=4
>>> a+b
7
>>> a*b
12
>>> a/b
0.75	# 소수점 제한 없음
# 제곱
>>> a**b
81		
# 나머지
>>> a%b
3		
# 정수로 출력
>>> a//b
0		
  • 문자열

# ' '로 출력
>>> "Hello world"
'Hello world'
>>> 'hello world'
'hello world'
>>> """Hello world"""
'Hello world'
>>> '''hello world'''
'hello world'
# " "로 출력
>>> string = '"Hello"'
>>> print(string)
"Hello"
>>> string = "\"Hello\""
>>> print(string)
"Hello"
# 줄바꿈 사용
>>> string = "Hello\nWorld"
>>> print(string)
Hello
World
# 문자열 더하기
>>> string1 = "Hello"
>>> string2 = "World"
>>> string1+string2
'HelloWorld'
# 문자열 곱하기
>>> string = "Hello"
>>> string*2
'HelloHello'
  • 문자열 인덱싱

# 문자열 인덱싱
>>> string = "Hello world"
>>> string[0]
'H'
>>> string[10]
'd'
>>> string[-1]
'd'
# 문자열 슬라이싱
>>> string[0:4]
'Hell'
>>> string[:4]
'Hell'
>>> string[4:]
'o world'
  • 문자열 포매팅

>>> "I eat %d apples." %3
'I eat 3 apples.'
>>> 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.'
  • 정렬과 공백

# 오른쪽 정렬
>>> "%10s" % "hi"
'        hi'
# 왼쪽 정렬
>>> '%-10s' % 'hi'
'hi        '
>>> '%-10sjane.' % 'hi'
'hi        jane.'
  • 소수점

# 정수부분 자릿 수와 소수부분 자릿수
>>> "%0.4f" % 3.4231323
'3.4231'
>>> "%10.4f" % 3.4231323
'    3.4231'
  • 문자열 개수 세기(count)

>>> string = "abbbaaaabb"
>>> string.count('b')
5
  • 문자 위치 알려주기(find)

# find 사용
>>> string = "Hello world"
>>> string.find('o')
4
# 찾으려는게 없으면 -1 반환
>>> string.find('k')
-1
# index 사용
>>> string.index('o')
4
# 찾으려는게 없으면 오류
>>> string.index('k')
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    string.index('k')
ValueError: substring not found
  • 문자열 처리

# 문자열 삽입
>>> char=','
>>> string='abcd'
>>> char.join(string)
'a,b,c,d'
# 소문자를 대문자로 바꾸기
>>> string = string.upper()
>>> string
'ABCD'
# 대문자를 소문자로 바꾸기
>>> string = string.lower()
>>> string
'abcd'
# 공백 제거
>>> char=' '
>>> char.join(string)
'a b c d'
>>> string.strip()
'abcd'
# 문자열 바꾸기
>>> string = 'hello world'
>>> string.replace('world','python')
'hello python'
# 문자열 나누기
>>> string.split()
['hello', 'world']
>>> string = 'a.b.c.d'
>>> string.split('.')
['a', 'b', 'c', 'd']
  • 리스트

>>> arr = [1,2,3]
>>> arr1 = [ ]
>>> arr2 = ['a','b','c']
>>> arr3 = ['a','b',1,2]
>>> arr4 = [1,2,['a','b']]
>>> arr4
[1, 2, ['a', 'b']]
>>> arr4[2]
['a', 'b']
>>> arr4[2][1]
'b'
>>> arr[0]
1
>>> arr[1] + arr[2]
5
>>> arr[-1]
3
>>> arr[0:2]
[1, 2]
# 리스트 더하기
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a+b
[1, 2, 3, 4, 5, 6]
# 리스트 반복하기
>>> a * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
  • 리스트 수정

# 수정하기
>>> a=[1,2,3]
>>> a[2] = 4
>>> a
[1, 2, 4]
# 끼워넣기
>>> a[1:2]
[2]
>>> a[1:2] = ['a','b','c']
>>> a
[1, 'a', 'b', 'c', 4]
# 삭제하기
>>> a[1:3] = []
>>> a
[1, 'c', 4]
>>> del a[1]
>>> a
[1, 4]
# 추가하기(맨 끝에)
>>> a.append(6)
>>> a
[1, 4, 6]
# 삽입하기(원하는 위치에)
>>> a = [1,2,3]
>>> a.insert(0,4)
>>> a
[4, 1, 2, 3]
# 제거하기(앞에서부터 원하는 요소)
a = [1,2,1,1,2,3,4]
>>> a.remove(1)
>>> a
[2, 1, 1, 2, 3, 4]
  • 리스트 정렬하기

# 정렬하기
>>> a = [1,3,4,2]
>>> a.sort()
>>> a
[1, 2, 3, 4]
# 뒤집기
>>> a.reverse()
>>> a
[4, 3, 2, 1]
# 위치 반환
>>> a.index(2)
2
>>> a.index(5)
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    a.index(5)
ValueError: 5 is not in list
  • 리스트 꺼내기

>>> a = [1,2,3]
>>> a.pop()
3
>>> a.pop()
2
>>> a
[1]
  • 리스트 요소 개수 세기

>>> a = [1,2,3,1]
>>> a.count(1)
2
  • 리스트 확장하기

>>> a = [1,2,3]
>>> a.extend([4,5])
>>> a
[1, 2, 3, 4, 5]
>>> a.extend([[4,5]])
>>> a
[1, 2, 3, 4, 5, [4, 5]]
728x90

+ Recent posts