본문 바로가기

Doc/컴퓨터

컴퓨터과학 CS50 2020 Lecture 6 : Python

 

 

 

 

<강의 필기>

 


value

 

Scratch

set counter to 0
counter + 1

 

C

int counter = 0;
counter = counter + 1;


Python

counter = 0
counter += 1;

 

 

 

 


condition

if (x < y)
{
    print("x is less than y")
}

if x < y:
    print("x is less than y")

 

Python은 curly braces를 작성하지 않는다. C는 작성한다.

 

 




loop

for I in range(3):
  print(“hello, world”)

For Loop range(3)는 list [0, 1, 2]의 범위를 가진다.

 

 

range

for I in range(start, stop, increment at a time):

 

 

infinite loop

while True:
  n = get_int(“Positive Integer : ”)
  if > 0:
    break
return n

 

 

 

 


data type
C는 strongly typed language이다.
bool char double float int long string ... 

Python은 loosely typed language이다.
bool float int str ...
data type을 정렬할 때 range list tuple dict set ... 을 사용한다.

string
“1” + “2” = “12”

int
1 + 2 = 3

int
1 / 2 = 0

float
1 // 2 = 0.5

 

 

 

 

 

library

 

C

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    string answer = get_string("What's your name? ");
    printf("hello, %s", answer);
}

 

 

Python

from cs50 import get_string

answer = get_string("What's your name? ")
print(f"hello, + {answer}“)

placeholder를 놓을 필요가 없어 formatted string이 편리하다.

 

 

from cs50 import get_string
...
print(c.upper(), end="")
...

Python에서는 . dot은 구조의 내부를 가리킨다.

C의 toupper와 tolower는 Python의 .upper와 .lower이다.

 

 

words = set()

def load(dictionary):
...
def check(word):
...

filter function은 before 변수가 함수를 거친 출력 결과를 반환한다.
c에서는 main이라고 불리는 default function을 입력해야하지만
python에서는 코드만 입력한다.

 

 

 

 

 

Interpreter

source code -> interpreter -> machine code

 

Python : 0.90초
C 언어 : 0.52초 
C에서 똑같은 코드를 실행하면 Python보다 더 빠르다.
shakespears.txt를 띄어쓰기 분리하는 코드는 실행 속도가 2배 빠르다. 

 

C에서는 컴퓨터가 알아들을 수 있는 말을 자세히 해주기 때문에
컴퓨터가 빠르게 알아들어서 실행 시간이 더 짧다.

 

반면 python에서는 컴퓨터친화적이기보다는 사용자친화적이라서
mahine code로 compile하는데 더 오랜 시간이 걸린다.

Python 같은 high level language에서는 
malloc. free. realloc하는 별도의 memory management를 하지 않는다.
 그리고 영어 문법과 비슷하게 코드를 작성하는 스타일을 갖는다.

 

C와 Python의 실행 시간 차이를 비교하는 것과 같이 분석하면서
계속 tradeoff하는 space와  time에서
complexity를 파악하는 것은

프로그래밍의 성능을 높이는 데 도움을 준다.

 

 

 



optional argument

for i in range(4):
    print("?", end="")
print()

Python에서는 default value에 end와 같이 여러 option을 설정할 수 있다.
C에서는 optional argument가 없다. 
그 자리에 속하냐 속하지 않냐를 설정할 수 있을 뿐이다.

 

 

 

 


Command-line arguments

from sys import argv

if len(argv) == 2:
    print(f"hello, {argv[1]}")
else:
    print("hello, world")

 

파이썬으로 작성된 파일을 실행할 때 인수(argument, 인자값)를 받아서 처리를 해야 되는 경우가 있다.



 


CS50 IDE
수강기간 동안에는 python을 직접 설치하기보다는 
CS50 IDE를 사용한다.
학생마다 다른 운영체제. 버전. 호환성을 갖고 있기 때문에
수업에 필요한 프로그래밍 언어와 라이브러리 설치에 어려움이 있을 수 있다.
그러나 CS50 IDE는 수업에 필요한 모든 프로그램이 호환되고 사용 가능하다.


 




강의 자료

 

Week 6 - CS50

Introduction to the intellectual enterprises of computer science and the art of programming. This course teaches students how to think algorithmically and solve problems efficiently. Topics include abstraction, algorithms, data structures, encapsulation, r

cs50.harvard.edu

 

 

 


CS50 Sandbox 

 

CS50 Sandbox

Temporary programming environments for students and teachers.

sandbox.cs50.io

 

 

 

파이썬 명령 인자값 받는 방법 (sys.argv) (Blog 네오가 필요해)

 

[Python] 파이썬 명령 인자값 받는 방법 (sys.argv)

파이썬으로 작성된 파일을 실행할 때 인수(argument, 인자값)를 받아서 처리를 해야 되는 경우가 있다. 예를 들어, 로컬과 개발 등의 환경이 서로 달라서 인자값을 줘야 한다던지 같은 파일을 다른

needneo.tistory.com










>