여러가지/Python

Day 21 - 프로젝트 Snake Game (2)

15June 2024. 4. 29. 20:02

● GIT URL : https://github.com/yooyijun15/Python/tree/main/Day21

 

● 학습 내용

1) 클래스(Class) 상속

class Animal:
    def __init__(self):
        self.num_life = 3

    def breathe(self):
        print("Inhale - Exhale.")


# Animal 클래스 상속
class Fish(Animal):
    def __init__(self):
        # 상위 클래스 불러오기
        super().__init__()

    def swim(self):
        print("-- In water --")

    def breathe(self):
        # 상위 클래스 특정 함수 불러오기
        super().breathe()
        print("(In water)")

nemo = Fish()
nemo.swim()
nemo.breathe()
print(f"LIFE : {nemo.num_life}")

 

2) Slice(슬라이스)

piano_key = ['a','b','c','d','e','f','g']
print(piano_key[2:5])
> ['c','d','e']
print(piano_key[2:])
> ['c','d','e','f','g']
print(piano_key[:5])
> ['a','b','c','d','e']
print(piano_key[2:5:2])
> ['c','e']
print(piano_key[::2])
> ['a','c','e','g']
print(piano_key[::-1])
> ['g','f','e','d','c','b','a']
# 시작, 끝, 단위

※ 리스트, 터플 모두 가능합니다.