python 자잘한 것 정리(1탄)
랜덤함수
from random import *
randint or random 사용
변수 내용 길이자르기
변수[0:5]
변수의 길이
len(변수[0:5])
변수 내 해당하는 글자의 갯수
변수.count("단어")
sep= 각""사이의 콤마(,)의 형식을 정해주는 것
end= 끝나고 띄어쓰기 되는 부분을 다른 형식으로 바꿔줌 ex("?")
file=sys.stdout = 표준 입출력
file=sys.stderr = 표준 입출력 에러
ljust(숫자) rjust(숫자) 왼쪽,오른쪽정렬 후 숫자만큼 빈칸
ex)
#시험성적
scores = {"수학":0, "영어":50, "코딩":100}
for subject, score in scores.items():
print(subject.ljust(8),str(score).rjust(4),sep=":")
출력값:
수학 : 0
영어 : 50
코딩 : 100
.zfill(숫자) = 숫자만큼 공백 숫자로 채움
ex)
for num in range(1,11):
print("대기번호 :" + str(num).zfill(3))
출력값:
대기번호 :001
대기번호 :002
대기번호 :003
대기번호 :004
대기번호 :005
대기번호 :006
대기번호 :007
대기번호 :008
대기번호 :009
대기번호 :010
txt파일 오픈
ex)
score_file=open("score.txt","w",encoding="utf8")
print("수학 : 0", file=score_file)
print("영어 : 50", file=score_file)
score_file.close()
만든 txt파일에 글 작성하기
score_file=open("score.txt","a",encoding="utf8")
score_file.write("과학 : 80")
score_file.write("\n코딩 : 100")
score_file.close()
txt에 적혀있는 파일 터미널에 불러오기
score_file=open("score.txt","r",encoding="utf8")
print(score_file.read())
score_file.close()