728x90
문자열 연산
str_o1 = "python"
str_o2 = "Apple"
str_o3 = "How are you doing"
str_o4 = "Seoul Deajeon Busan Jinju"
print(str_o1 * 3)
print(str_o1 + str_o2)
print('y' in str_o1)
print('n' in str_o1)
print('P' not in str_o2)
pythonpythonpython --> 문자라서 문자 3번 반복
pythonApple --> 바로 붙여짐
True --> str_01에 y가 있냐?
True
True --> str_02에 p가 없냐?
문자열 형 변환
print(str(66),type(str(66)))
print(str(10.1))
print(str(True), type(str(True)))
66 <class 'str'>
10.1
True <class 'str'>
문자열 함수(upper, isalnum, startswith, count, endswith, isalpha)
print("Capitalize: ", str_o1.capitalize())
print("endswith? :", str_o2.endswith("e"))
print("replace", str_o1.replace("thon","Good"))
print("sorted: ", sorted(str_o1))
print("split: ", str_o4.split(' '))
Capitalize: Python
endswith? : True
replace pyGood
sorted: ['h', 'n', 'o', 'p', 't', 'y']
split: ['Seoul', 'Deajeon', 'Busan', 'Jinju']
Capitalize --> 첫 글자만 대문자로
endswith(끝나는문자, 문자열의시작, 문자열의끝)
replace(찾는 값,바꾸는 값)
sorted --> 분류
split --> 공백(스페이스, 탭, 엔터 등)을 기준으로 문자열을 나눔
반복(시퀀스)
im_str = "Good Boy!"
print(dir(im_str)) # __iter__
# 출력
for i in im_str:
print(i)
G
o
o
d
B
o
y
!
슬라이싱
str_s1 = "Nice Python"
print(len(str_s1))
# 슬라이싱 연습
print(str_s1[0:3]) # 0 1 2
print(str_s1[5:]) # [5:11]
print(str_s1[:len(str_s1)]) # str_s1[:11]
print(str_s1[:len(str_s1)-1]) # str_s1[:10]
print(str_s1[1:4:2]) # 시작,마지막, 단위
print(str_s1[-5:])
print(str_s1[1:-2])
print(str_s1[::2])
print(str_s1[::-1])
11
Nic
Python
Nice Python
Nice Pytho
ie
ython
ice Pyth
Nc yhn
nohtyP eciN
아스키 코드
a = 'z'
print(ord(a)) # 아스키 코드로
print(chr(122)) # 문자로
122
z
파이썬 리스트
자료구조에서 중요
리스트 자료형(순서o, 중복o, 수정o, 삭제o)
선언
a = []
b = list()
c = [10,20,30,40,50]
d = [22,33,"ace"]
e = [22,33,["ace",'test','check']]
f = [21.42,'foobar']
print('d - ', type(d),d)
print('d - ', d[1])
print('d - ', d[0] + d[1] + d[1])
print('d - ', d[-1])
print('d - ', e[-1][1])
print('d - ', list(e[-1][2]))
d - <class 'list'> [22, 33, 'ace']
d - 33
d - 88
d - ace
e - test
e - ['c', 'h', 'e', 'c', 'k']
슬라이싱
print('d - ', d[0:3])
print('d - ', d[2:])
print('d - ', e[-1][1:2])
d - [22, 33, 'ace']
d - ['ace']
d - ['test']
리스트 연산 결과도 리스트이다
print('c + d', c + d)
print('c * 3', c * 3)
print("'Test' + c[0]", 'Test' + str(c[0]))
c + d [10, 20, 30, 40, 50, 22, 33, 'ace']
c * 3 [10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
'Test' + c[0] Test10
값 비교
print (c == c[:3] + c[3:])
print(c)
print(c[:3]+c[3:])
True
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50]
Identity(id)
temp = c
print(temp, c)
print(id(c))
print(id(temp))
[10, 20, 30, 40, 50] [10, 20, 30, 40, 50]
2421757831936
2421757831936
리스트 수정, 삭제
c[0] = 4
print(c)
print('c - ', c)
c[1:2] = ['a','b','c'] # [ ]['a','b','c']] == c[1] = ['a','b','c']
print('c - ', c)
[4, 20, 30, 40, 50]
c - [4, 20, 30, 40, 50]
c - [4, 'a', 'b', 'c', 30, 40, 50]
c - [4, 'a', 'c', 30, 40, 50]
del c[2] # 삭제
print('c - ', c)
c - [4, 'a', 'c', 30, 40, 50]
리스트 함수
a = [5,4,3,2,1]
a.append(10) --> 뒤로 값을 넣음
print('a - ', a)
a.sort()
print('a - ', a)
print('a - ', a,a.index(3),a[3]) index(3) --> 3값의 위치
a.reverse() 뒤집어
print('a - ', a)
a.insert(2,7)
print('a - ', a)
a.reverse()
print('a - ', a)
# del a[123513232312]
a.remove(10) --> 값을 제거하는 방식
print('a - ', a)
print('a - ',a.pop())
print('a - ', a)
print('a - ',a.pop()) --> 뒤에 있는거 꺼냄
print('a - ', a)
print('a - ',a.count(1)) --> 1이 몇개 있냐?
ex = [ 8, 9]
a.extend(ex)
print('a - ',a)
a - append : [5, 4, 3, 2, 1, 10]
a - sort : [1, 2, 3, 4, 5, 10]
a - index : [1, 2, 3, 4, 5, 10] 2 4
a - reverse : [10, 5, 4, 3, 2, 1]
a - insert : [10, 5, 7, 4, 3, 2, 1]
a - reverse: [1, 2, 3, 4, 7, 5, 10]
a - remove: [1, 2, 3, 4, 7, 5]
a - pop : 5
a - [1, 2, 3, 4, 7]
a - pop : 7
a - [1, 2, 3, 4]
a - count : 1
[1, 2, 3, 4, 8, 9]
# 삭제 : remove, pop, del
반복문 활용
while a:
data = a.pop()
print(data)
9
8
4
3
2
1
'python' 카테고리의 다른 글
python 공부 3일차 - 3 (0) | 2020.12.23 |
---|---|
python 공부 3일차 (0) | 2020.12.22 |
python 공부 2일차 (0) | 2020.12.21 |
python 공부 1일차 (0) | 2020.12.21 |
python 설치하기 (0) | 2020.12.21 |