정보처리기사 시험 준비 Python (11일차)

모코코개발자·2024년 3월 10일
post-thumbnail

Set

s = {1,5,7}
s.add(1) // {1,5,7}
s.update([1,2,3,4]) // {1,2,3,4,5,7}
s.remove(1) // {2,3,4,5,7}

출력할 때 순서 무작위

List

출력할 때 순서 중요

append()

s1 = [1,2,3]
s1.append(10) s1.append(9) s1.append('hello')
print(s1) // [1,2,3,10,9,hello]

remove()

s1.remove('hello')
print(s1) // [1,2,3,10,9]

insert()

s1.insert(3, 'hi')
print(s1) // [1,2,3,'hi',10,9]
del list[3]
print(s1) // [1,2,3,10,9]

sort()

s1.sort()
print(s1) // [1,2,3,9,10]
s1.sort(reverse = True)
print(s1) // [10,9,3,2,1]

  • remove vs del, append vs insert
    remove와 append는 '값'을 인자로 받고 삭제하거나 삽입한다. // s1.remove(a) // s1.append(a)
    del과 insert는 '인덱스번호'를 인자로 받고 삭제하거나 삽입한다. // del s1[3] // s1.insert(3,'hello')

Tuple

s2 = (1,2,3)

Dictionary

s3 = {1:"a", 2:"b", '나이' : 20} // 키 : 값
s3['나이'] = 30 // {'1:"a", 2:"b", '나이' : 30} // 값이 갱신
s3['고향'] = "대전" // {'1:"a", 2:"b", '나이' : 30, '고향':'대전'} // 키와 값이 추가

for문

for i in range(1, 10):
print(i) // 1~9 출력
//print(i, end="\n") 디폴트로 개행이 되어있다.
for i in range(1, 100, 2): // step=2
print(i) // 홀수만 출력

  • step
    s="파이썬너무쉬워요"
    print(s[0:5:2]) // 두 칸 간격

  • sep
    print(a, sep=',')

for i in range(100, 0, -1): // step은 음수도 가능
print(i) // step을 사용해 역순으로 출력

함수

def print_num(x):
print(x)

format

name = "Alice"
age = 30
formatted_string = "이름: {}, 나이: {}".format(name, age) // 순서대로 괄호안으로 들어감
print(formatted_string)
// 이름: Alice, 나이: 30 출력

제곱연산자

a**2

  • 주의점
    True -> boolean
    False -> boolean
    true -> 일반 변수
    false -> 일반 변수

클래스

  • 클래스의 첫글자는 대문자로 작성해야 한다.
  • 숫자나 특수문자로는 시작할 수 없다.
  • 상속문제 다수 출제

class Car:
def init(self, maxV): // _._init__생성자 함수
self.maxV = maxV
def drive(self):
print("자동차가 최대속력 {}km로 달립니다.".format(self.maxV))
c1 = Car(220)
c1.drive()
c2 = Car(250)
c2.drive()

  • 클래스 내부에서 함수를 정의할 때 함수는 무조건 self 매개변수를 갖고 있어야한다.

class Car :
maxVelo = 0
def drive(self) :
print("자동차가 최대속력 {}km로 달립니다.".format(self.maxVelo))
c1 = Car()
c2 = Car()
c1.maxVelo = 220
c2.maxVelo = 250
c1.drive()
c2.drive()

init 생성자를 사용하지 않은 ver.

상속

class Bus(Car): // Car클래스를 상속받는 Bus클래스

오버로딩 vs 오버라이딩

  • 오버로딩
    동일한 이름의 함수 but 매개변수만 다름 but 파이썬은 지원 X

  • 오버라이딩
    하위클래스에서 상위클래스 함수를 재정의 하는 것

class A:
def funcA(self):
print("A")
class B(A):
def funcA(self):
print("B")
a = A()
b = B()
a.funcA() // A출력
b.funcA() // B출력

B에서 A클래스의 함수를 쓰고싶다면

class B(A):
def funcA(self): // B출력
print("B")
super().funcA() // super() 키워드를 활용 // A출력

shift 연산자 >>, <<

a = 100
result = a >> i

  1. 100을 2진수로 변환
  2. 변환된 2진수에서 오른쪽으로 i만큼 자리이동
  3. 이동한 2진수를 다시 10진수로 변환
profile
모코코개발자

0개의 댓글