파이썬 22

내가 헷갈리는 파이썬 정리

지수 표현 10e9 = 10의9제곱 소수점 반올림하기 round(123.45,2) 소수점셋째자리에서 반올림 리스트컴프리헨션 array = [i for i in range(10)] array = [10,10,10,10,...] 2차원 배열 만들기 array = [[0]*m for m in range(n)] n x m형태의 리스트 생성 가능 찾기 count() -> 갯수세기 index() -> 위치 찾기 find() -> 위치찾기 지우기 remove() -> 이거는 지우고 싶은 값을 넣음 됨 pop() -> 마지막 요소 사전 dic = dict() dic['hi'] = "1" 사전 키값만 뽑고싶다면 key_list = dic.keys() -> 근데 이거는 객체로 받아와줘서 key_list = list(dic..

파이썬 2022.07.02

맥북 파이썬 python conda interpreter 가상화면 설치하기

https://www.anaconda.com/products/distribution#macos Anaconda | Anaconda Distribution Anaconda's open-source Distribution is the easiest way to perform Python/R data science and machine learning on a single machine. www.anaconda.com 1. 먼저 아나콘다를 설치해준다. 2. 터미널을 연 후 conda --version 확인 3. 터미널에 입력한다. conda create --name 가상환경명 python==파이썬버전 나같은 경우는 conda create --name vin python==3.9 4. 질문이 나오면 y를 눌러준..

파이썬 고급 문법

고급 문법 열거 가능 객체 for 반복문의 순회 대상 객체 해당 객체의 __iter__() 메서드로 열거 가능 객체 획득 매 루프마다__next()__함수를 통해 다음요소를 추출 더 이상 요소가 없는데 __next()__를 호출하는 경우 nums = [11,22,33] it = iter(nums) while True: try: num = next(it) except StopIteration: break print(num) class Seq: def __init__(self, data): self.data = data self.index = -2 def __iter__(self): return self def __next__(self): self.index += 2 if self.index >= len(s..

파이썬 2022.01.17

파이썬 모듈

모듈 비슷한 성격의 변수, 함수들을 파일별로 나눠 정의 파일명이 모듈명이 됨 모듈을 사용하는 이유? 코드 재사용 굳 관리 재사용 굳 INCH = 2.54 def calcsum(n): sum = 0 for num in range(n+1): sum += num return sum print('util.py',__name__) import util print("1inch -" , util.INCH) print("~10 = ", util.calcsum(10)) print("ex01.py",__name__) import ex01 import util print('ex02.py',__name__) 모듈 테스트 __name__ 변수에 모듈명이 저장됨 단독으로 실행된 경우(실행주체) __main__으로 저장됨 모듈로 ..

파이썬/모듈 2022.01.14

파이썬 클래스 2

class Car: count = 0 #클래스 멤버변수 def __init__(self,name) -> None: self.name = name Car.count += 1 @classmethod def outcount(cls): print(cls.count) pride = Car("프라이드") korando = Car("코란도") Car.outcount() class Car: @staticmethod def hello(): print("오늘도 안전 운행 합시다.") count = 0 def __init__(self,name) -> None: self.name = name Car.count += 1 @classmethod def outcount(cls): print(cls.count) Car.hello()..

파이썬/클래스 2022.01.14

파이썬 클래스 2

모든 클래스는 object 클래스의 자식이다. class Human: def __init__(self,name,age) -> None: self.name = name self.age = age def intro(self): print(str(self.age) + "살" + self.name + "입니다.") def eat(self): print("식사를 합시다.") def __str__(self) -> str: return f'Human(name = {self.name}, age = {self.age})' class Student (Human): def __init__(self, name, age,stunum) -> None: super().__init__(name, age) self.stunum = s..

파이썬/클래스 2022.01.13

파이썬 클래스

클래스 정의 class 키워드로 정의 대문자로 시작하는 경우가 많음 파스칼 케이스 class Account: def __init__(self,balance): self.balance = balance def deposit(self,money): self.balance += money def inquire(self): print(f'잔액은 {self.balance}원 입니다.') def main(): account = Account(8000) account.deposit(1000) account.inquire() shihan = Account(8000) shihan.deposit(1000) shihan.inquire() nonghyup = Account(12000000) nonghyup.inquire() ..

파이썬/클래스 2022.01.12

mp3 프로그램

player.py import os import sys from pygame import mixer import eyed3 from file_util import get_file_list DIR_PATH = "c:/temp/music" # 음악 파일 디렉토리 song_list = [] # 재생 목록 song = None # 현재 재생중인 파일 current_index = -1 # 현재 재생 파일의 인덱스 # 메뉴 # 목록 재생 정지 이전 다음 종료 def init(): global song_list mixer.init() song_list = get_file_list(DIR_PATH, '.mp3') # 목록 보여주기 def print_list(): # 0] song1.mp3 for ix, song in ..