[SSAFY] 사전 교육 파이썬 3일차

전은평·2023년 7월 4일
0

1. 모듈

1) calendar 모듈에서 윤년인지 판단하는 함수

calendar.isleap(year)	# year에 해당하는 연도가 윤년인지 아닌지 boolean값으로 알려줌

+) help()를 이용하면 해당 함수가 어떤 기능을 가지고 있는지 알려준다

1) help(calendar.isleap)


## Help on function isleap in module calendar:

## isleap(year)
##     Return True for leap years, False for non-leap years.

2) help(calendar.TextCalendar)

## Help on class TextCalendar in module calendar:

class TextCalendar(Calendar)
 |  TextCalendar(firstweekday=0)
 |  
 |  Subclass of Calendar that outputs a calendar as a simple plain text
 |  similar to the UNIX program cal.
 |  
 |  Method resolution order:
 |      TextCalendar
 |      Calendar
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  formatday(self, day, weekday, width)
 |      Returns a formatted day.
 |  
 |  formatmonth(self, theyear, themonth, w=0, l=0)
 |      Return a month's calendar string (multi-line).
 |  
 |  formatmonthname(self, theyear, themonth, width, withyear=True)
 |      Return a formatted month name.
 |  
 |  formatweek(self, theweek, width)
 |      Returns a single week in a string (no newline).
 |  
 |  formatweekday(self, day, width)
 |      Returns a formatted week day name.
 |  
 |  formatweekheader(self, width)
 |      Return a header for a week.
 |  
 |  formatyear(self, theyear, w=2, l=1, c=6, m=3)
 |      Returns a year's calendar as a multi-line string.
 |  
 |  prmonth(self, theyear, themonth, w=0, l=0)
 |      Print a month's calendar.
 |  
 |  prweek(self, theweek, width)
 |      Print a single week (no newline).
 |  
 |  pryear(self, theyear, w=0, l=0, c=6, m=3)
 |      Print a year's calendar.
 |  
 |  ----------------------------------------------------------------------
.
.
.
. 중략..

2) random 모듈

0-1 미만의 숫자 중에서 랜덤으로 하나 뽑아서 돌려준다

import random 
random.random()				# 0.352256998759493

원하는 숫자 구간을 설정하고 싶을 경우 randrange() 함수를 이용하면 된다

import random
random.randrange(1,7) 		# 1-6까지 숫자중에 랜덤으로 출력(1이상 7미만의 난수)

시퀀스를 뒤죽박죽으로 섞어놓고 싶을 경우 shuffle() 함수를 이용하면 된다

abc = ['a','b','c','d','e']

random.shuffle(abc)			# ['a','d','e','b','c']

아무 원소나 하나 뽑아주고 싶을 경우 choice() 함수를 이용하면 된다

random.choice(abc)		    # 'e'

# tuple에도 적용됨

menu = '쫄면','육계장','비빔밥'
random.choice(menu)			# '쫄면'

random.choice([True,False])	# True

3) string 모듈

import string

string.ascii_uppercase		# 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.ascii_lowercase		# 'abcdefghijklmnopqrstuvwxyz'
string.ascii_letters		# 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

string.digits				# '0123456789'

2. 텍스트 파일 읽고, 쓰기

1) 읽기
읽기 전에 우선 텍스트 파일 만들기!

f = open("C:/Users/전은평/AppData/Local/Programs/Python/Python310/wikidocs-chobo-python/ch06/python_text_file/Python_for_Fun.txt")

f.read()		# 'Programming is fun.\nVery fun!\n\nYou have to do it yourself...'

2) 새 텍스트 파일 만들면서 쓰기

# 'w'는 작성하겠다는 의미
letter = open("C:/Users/전은평/AppData/Local/Programs/Python/Python310/wikidocs-chobo-python/ch06/python_text_file/letter.txt",'w')

letter.write('Dear Father,')

# 꼭 닫기~
letter.close()

3) 기존 텍스트 파일에 이어 작성하기

# '+a' or 'a+' 적어주기
letter = open("C:/Users/전은평/AppData/Local/Programs/Python/Python310/wikidocs-chobo-python/ch06/python_text_file/letter.txt",'+a')

letter.write('\nHow are you?')
13

letter.close()
profile
`아는 만큼 보인다` 라는 명언을 좋아합니다. 많이 배워서 많은 걸 볼 수 있는 개발자가 되고 싶습니다.

0개의 댓글