어제 거 퀴즈 및 오늘 배울 내용들
1.랜덤(1-100)을 이욯해서 리스트 길이가 10 인거 구현하고 출력후, 여기서 홀수 따로 짝수 따로 나누고 출력하기.
#2.랜덤(1-30)뽑고 리스트 길이가 10 출력,1-10은 토끼, 11-20 원숭이 21-30 개 로 바꾸로 출력하기.
#3.함수를 구현하는데 이 함수는 n,m 이라는 매개변수 주면, n~m까지의 정수인 리스트 5개를 만들어주는 함수 구현하기.
#1번답
#import random
#numList = []
#for i in range(10):
#numList.append(random.randint(1, 101))
#print(numList)
#oddList = []
#evenList = []
#for i in numList:
#if i %2 ==0:
#evenList.append(i)
#else:
#oddList.append(i)
#print(oddList)
#print(evenList)
#2번답
numList = [random.randint(1,31) for i in range(101)]
zooList = []
for i in numList:
if 1<= i and i <= 10:
zooList.append('개')
elif 11<= i and i<= 20:
zooList.append('토끼')
else:
zooList.append('당근')
print(numList)
print(zooList)
#3번답
def makeList(m,n):
return[random.randint(m,n+1) for i in range(5)]
lambda m,n:[random.randint(m,n+1) for i in range(5)]
#map(): qusrud
#x = [1,2,3,4,5]
#newList = list(filter(lambda x: x>3,x))
#print(newList)
#1-10숫자 중에 369애들만 빼줘
#x = [1,2,3,4,5,6,7,8,9,10]
#newList = list(filter(lambda x: x % 3 !=0, x))
#print(newList)
#각요소에서 제곱근을 한 것들중에 25이상만 추출해라.
#x = [1,2,3,4,5,6,7,8,9,10]
#newList = list(filter(lambda x: x*x >25, x))
#print(newList)
#각요소에서 제곱근을 한 것들중에 25이상만 추출해라.
#x = [1,2,3,4,5,6,7,8,9,10]
#newList = list(filter(lambda x: x*x >25, x))
#print(newList)
#글자에서 6글자보다 큰 애들만 추출해라.
#fruits = ['shinmusket','mandarin','apple','peach','strawberry','banana']
#newList = list(filter(lambda x: len(x) >6, fruits ))
#print(newList)
#s만 포함된 단어만 골라줘라.
fruits = ['shinmusket','mandarin','apple','peach','strawberry','banana']
newList = list(filter(lambda x: 's' in x, fruits ))
print(newList)
#단어 길이가 짝수이고 a가 두개 이상 포함.
fruits = ['shinmusket','mandarin','apple','peach','strawberry','banana']
newList = list(filter(lambda x: len(x) %2 ==0 and x.count('a')>=2, fruits ))
print(newList)
filter(function, iterable) 는 주어진 함수의 결과가 참인 요소만 반환.
는 무조건 앞에 from functools import reduce를 가져와야 함.
acc는 현재까지 누적된 수
from functools import reduce
x = [1,2,3,4,5]
result = reduce(lambda acc,cur: acc+cur, x) 이거는 더하기
factorial = reduce(lambda acc,cur: acc*cur, x) 이거는 곱하기
print(factorial)
#int, float, bool, str, dict, list, tuple, set
#나만의 데이터 타입 만들기.
#기본형 -> 구조체(c언어)[struct](기본형 모음)라고 한다.-> 구조체에 함수를 넣은게 클래스[class]=구조체(변수)+ 함수
#height[float] -> student[str, int, int]
#class Dog:
#def init(self):_
#self.breed = '말티즈'
#self.snackList = ['개껌','개사료']
#def bark(self):
#print("멍멍!")
#a = Dog()
#a.bark()
#b = Dog()
#b.bark()
class burger:
def init(self):
self.bread = '참깨방'
self.ingredients = ['토마토','양파']
self.source = '마요네즈'
def addIngredient(self,x):
self.ingredients.append(x)
c = burger()
print(c.bread)
print(c.ingredients)
#int, float, bool, str, dict, list, tuple, set
#나만의 데이터 타입 만들기.
#기본형 -> 구조체(c언어)[struct](기본형 모음)라고 한다.-> 구조체에 함수를 넣은게 클래스[class]=구조체(변수)+ 함수
#height[float] -> student[str, int, int]
#class Dog:
#def init(self):_
#self.breed = '말티즈'
#self.snackList = ['개껌','개사료']
#def bark(self):
#print("멍멍!")
#a = Dog()
#a.bark()
#b = Dog()
#b.bark()
#class burger:
#def init(self):
#self.bread = '참깨방'
#self.ingredients = ['토마토','양파']
#self.source = '마요네즈'
#def addIngredient(self,x):
#self.ingredients.append(x)
#c = burger()
#print(c.bread)
#print(c.ingredients)
#c를 인스턴스라고 한다.
#객체 지향 프로그래밍[oop]
#object oriented programming
#클래스 변수
class Dog:
def init(self,x):
self.name = x
self.hp = 100
self.emotionstate = 'happy'
self.hungry = 0
self.friends = []
def eating(self):
self.hp = 10
a = Dog('jenny')
a.eating()
a.eating()
b = Dog('donald')
print(f"{a.name} {a.hp}")
print(f"{b.name} {b.hp}")
#이거는 도그가 최대치가 200이 넘으면 난리나게 만든거임.
class Dog:
def init(self,x):
self.name = x
self.hp = 100#체력이 200까지임
self.emotionstate = 'happy'
self.hungry = 0
self.friends = []
def eating(self):
if self.hp <200:
self.hp +=10
else:
print("체력이 꽉 찼습니다.")
a = Dog('jenny')
for i in range(100):
print(f"{i}")
a.eating
#객체에 대한 내용은 폰에 있음.
클래스를 쓸때는 대문자를 쓰는게 국룰임. Dog Burger
#학생관리 시스템의 학생 클래스 만들기
#이름과 나이 학년 전공 듣고 있는 수업
class Student:
def init(self,a,b,c):
self.name = a
self.grade = b
self.major = c
self.courses = []
def enroll_courses(self, courses):
if len(self.courses) < 5:
self.courses.append(courses)
else:
print('수업듣는 과목이 너무 많습니다.')
def cancel_course(self):
if len(self.courses) == 0:
print('과목이 없습니다. 밸 수가 없어요')
else:
for index, item in enumerate(self.courses):
print(f"{index}.{item}")
removeTarger = int(input("빼고 싶은 과목의 번호를 선액:"))
del self.courses[removeTarger]
def imformate(self):
print(f"{self.name} {self.grade} {self.major}")
for index,elem in enumerate(self.courses):
print(f"{index}. {elem}")
kim = Student('김주영','3','철학과')
kim.enroll_courses('철학의 이해')
kim.enroll_courses('철학의 쓸모')
kim.imformate()
##circle변수반지름
#함수: 원 넓이, 원 둘레
class Circle:
def init(self,x):
self.r = a
def width(self):
print(f"원의 넓이는 {self.r *2 3.14}")
def round(self):
print(f"원의 둘레는 {self.r 2 3.14}")
a = Circle(5)
a.width()
a.round()
b = Circle(7)
b.width()
b.round()