본문 바로가기

Programming/Python [피선]

(32)
22.11.22 Functions - Variable and scope *args와 **kwargs를 이용해 함수의 모든 variables에 접근할 수 있었다. def performOperation(*args, **kwargs): print(args) print(kwargs) performOperation(1, 2, operation='sum') 추가로, asterisk * 없이도 함수의 모든 variables에 접근할 수 있다. Local variable : Things that are defined inside the function - locals() 함수를 이용하면 모든 local variables을 key : value 형태의 dictionary 타입으로 반환한다. def performOperation(num1, num2, operation='sum'): print..
22.11.21 Functions - The anatomy of a function Positional & Keyword Arguments Def를 이용해 함수를 구현하는 것 이외에 좀 더 기본적인 기능들 및 명칭들을 알아보자. 아래는 흔히 알고 있는 함수 사용법은 아래와 같다. 정의된 "arguments" - num1, num2, operation - 에 "variable"을 입력하여 함수를 수행한다. def performOperation(num1, num2, operation) : if operation == 'sum': return num1 + num2 if operation == 'multiply': return num1 * num2 performOperation(2, 3, 'sum') #return 5 추가로, 아래와 같이 argument에 대해 default parameter..
22.11.20 Challenge_Bounding Off Scribes 주말에 꼭 알맞게 Chanllege다! Challenge Goals - Scribe should bound off canvas edges - Vertical wall : Make the x direction negative - while y direction keeps going on - Horizontal wall : Make the y direction negative - while x direction keeps going on Hints! - You may want to modify the "hitsWall" function in the canvas to provide more information 우선 hitsWall 함수를 아래와 같이 변경하였다. 4개의 축에 부딪혔을 때 서로 다른 문구를 반..
22.11.18 Control Flow - For - For 구문 꽤나 직관적이고 지금까지 당연하게 사용해왔던 For 구문. >>> myList = [1,2,3,4,5] >>> for item in myList: ... print(item) ... 1 2 3 4 5 - Pass Statement 아래와 같이 pass 사용 시 아무 일도 나타나지 않는다! >>> animalLookup = { ... 'a':['aardvark','antelope'], ... 'b':['bear'], ... 'c':['cat'], ... 'd':['dog'], ... } >>> for letter,animals in animalLookup.items(): ... pass - Continue Statement 아래와 같이 continue를 이용하여 특정 조건을 만족할 시 특정..
22.11.17 Control Flow - While While은 자칫 잘못 썼다가는 무한루프로 빠지기 십상이기 때문에 적절한 control statements를 함께 써줄 필요가 있다. 오늘은 해당 control statements를 한 번 보자. - While 구문 아래 구문은 무한 루프는 아니지만 화면은 'still waiting'으로 가득찰 것이다. 위로 올라갈 수 없다. 아무리 올라가도 'still waiting' 뿐이다. Python은 상당히 빠르고 2초는 굉장히 길기 때문이다. from datetime import datetime wait_until = datetime.now().second + 2 #현재로부터 2초 뒤 while datetime.now().second != wait_until: print('still waiting') prin..
22.11.15 Control Flow - If and Else If + Else statements을 Elif statements로 간소화 가능, Single line if statements로 더 간소화 가능! - If + Else 구문 >>> for n in range(1,10): ... if n%3 == 0: ... print('FizzBuzz') ... else: ... if n%5 ==0: ... print('Fizz') ... else: ... print(n) ... 1 2 FizzBuzz 4 Fizz FizzBuzz 7 8 FizzBuzz - Elif 구문 >>> for n in range(1,10): ... if n%3 == 0: ... print('FizzBuzz') ... elif n%5 == 0: ... print('Fizz') ... else:..
22.11.13 Challenge_Structuring Scribes(3) 세 개의 data set이 있다. 타입은 dictionaries. 첫 번째, [15,15]에서 시작해서 30'의 각도로 100번 이동. {'degree' : 30, 'position' : [15,15], 'instructions' : [ {'function':'forward', 'duration':100} ]} 두 번째, [0,0]에서 시작해서 145'의 각도로 10번 이동 후, 오른쪽으로 20번 이동 후, 아래로 2번 이동. {'degree' : 135, 'position' : [0,0], 'instructions' : [ {'function':'forward', 'duration':10}, {'function':'down', 'duration':2}, {'function':'right', 'durat..
22.11.12 Challenge_Structuring Scribes(2) 오늘은 정답지를 봐야겠다. 함수를 보고, 한 번 코드 돌려보고, 출제자의 의도가 무엇인지 파악하고, object를 고쳐 보아야겠다! Solution의 결과창...... canvas = Canvas(30, 30) scribes = [ #3개의 Key가 존재하는 dictionaries를 3개 정의 {'degrees': 30, 'position': [15,15], 'instructions': [ {'function':'forward', 'duration': 100} ]}, {'degrees': 135, 'position': [0, 0], 'instructions': [ {'function':'forward', 'duration': 10}, {'function':'down', 'duration': 2}, {'f..