string 관련
str.lower() -> string 전체 소문자로 변경
str.upper() -> string 전체 대문자로 변경
str[0].isupper() -> str[0] 대문자 여부
str.replace("python","java") -> python을 java로 변경
index = str.index("n") -> string에서 문자 n의 인덱스
index = str.index("n",index+1) -> 첫번째 n의 인덱스 이후의 다음 n의 인덱스
str.find("java") -> 있으면 1 없으면 -1 리턴
str.count("n") -> string에서 "n"의 갯수
list 관련
list.index(value) -> list에서 value가 있는 index
list.append(value) -> list에 value를 추가
list.insert(value, n) -> list n번째 인덱스에 value값을 넣는다.
list.pop() -> list 맨 뒤의 값을 빼낸다.
list.count(value) -> list에 value가 몇개있는지 세본다
list.sort() -> list 정렬
list.reverse() -> list 순서 뒤집기
list.clear() -> list 값 모두 지우기
list.extend(other_list) -> list에 다른 리스트의 값들을 추가한다. (append)
shuffle(list) -> 리스트 안의 값들 랜덤으로 섞는다
sample(list,1) -> 리스트 안의 값들 중 하나를 무작위로 뽑는다.
dictionary 관련
dic.get(n) -> n이라는 key의 value값을 가져온다.
n in dic -> dictionary에 n이라는 key값이 있으면 true 없으면 false
del dic(n) -> dictionary에 n이라는 키값을 가진 항목 삭제
dic.keys() -> key들만 출력
dic.values() -> value들만 출력
dic.items() -> key와 value들 모두 출력
dic.clear() -> dictionary 내 모든 값을 지운다.
Set 관련 ( 중복 안됨, 순서 없음 )
ex)
Fruit1 = {"apple","banana","carrot"}
Fruit2 = {"apple","melon"}
Fruit1 & Fruit2
Fruit1. intersection(Fruit2) => 교집합 ( "apple" 출력 )
Fruit1 & Fruit2
Fruit1.union(Fruit2) => 합집합 ("apple","banana","carrot","melon" 출력)
Fruit1 - Fruit2
Fruit1.difference(Fruit2) => 차집합 ("banana","carrot" 출력 )
Fruit1.add("kiwi") => fruit1 Set에 kiwi 추가
파일 입출력
file = open("file.txt","w",enoding="utf8") => 파일 쓰기 열기
file = open("file.txt","a",enoding="utf8") => 파일에 추가로 쓰기
print(" abcde ", file = file ) => 문자 abcde 파일에 쓰기
file.close() => 파일 닫기
file = open("file.txt","r",encoding="utf8")
print(file.read()) => 파일 전체 읽기
print(socre_file.readline()) => 줄별로 읽기, 한 줄 읽고 커서는 다음 줄로 이동
lines = file.readlines() => 줄 별로 list 형태로 저장
file.close()
pickle
pickle_file = open("abcd.pickle", "wb") -> pickle파일 쓰기모드
abcd = {"이름" : "김민수", "나이" : 27, "취미" :["낚시", "캠핑"]}
pickle.dump(abcd,pickle_file ) -> abcd에 있는 정보를 file에 저장
abcd.close()
pickle_file = open("abcd.pickle", "rb") -> pickle 파일 읽기 모드
info = pickle.load(pickle_file) -> pickle_file에 있는 정보를 info에 불러오기
pickle_file.close()
파일 열기
with open("abcd.pickle","rb") as pickle_file: -> abcd.pickle파일을 열어 pickle_file이라는 변수에 저장
pickle.load(pickle_file) -> pickle_file 로드
with open("study.txt", "w", encoding="utf8") as study_file: -> study.txt에 쓰기, 변수명은 study_file
study_file.write( " 어쩌구 저쩌구 ") -> study.txt 파일에 "어쩌구 저쩌구" 쓰여짐
with open("study.txt","r", encoding="utf8") as study_file:
print(study_file.read()) -> study파일 출력
명령어를 모를때
list = [1,2,3]
print(dir(list)) => 하면 리스트에 관한 명령어들을 나열해준다. ( 자료구조, import한 라이브러리 등등 전부 사용 가능 )