Saturday, Feb. 19 to Friday, Feb. 26
You have spent five weeks on C. Malan is trying to introduce Python in one long lecture by just comparing the way things are done in C with the way they are done in Python. This only kind of works pedagogically. Give it a go, but don’t be surprised if you start feeling like you are at sea in both languages. First, it is not always simply translation. The way you do things in the languages are sometimes more different than line-by-line translations.
Second, even if it are simply doing line-by-line translation, keeping two syntaxes in your head is non-trivial. For example, I have had to switch back and forth between C, Java, Swift, Python, Perl, and PHP to do various projects. Each of them is a little different syntactically, and I have never been good with the syntax at more than one language at a time. After five weeks of C, I’m going to be accidentally typing semicolons and using ‘//’ for comments as we switch to Python. Go easy on yourself if neither language is at all natural yet.
Third and finally, this one-lecture tour of Python is unsystematic. It should be followed by a systematic introduction of Python’s language features. Malan is doing what he can in the amount of time he has.
hello.py
, Python’s f-strings, such as f"Hello, {answer}"
. Ways of incrementing a counter. Indentation is necessary in Python but you don’t have to do the curly braces. If/elif/else. while(True). Python lists (which are called lists, but they have the behavior of C arrays). The range function. C is strongly typed. Python is loosely typed. It infers the type you want most of the time. Python data types: bool
, float
, int
, str
. Also, range
, list
, tuple
, dict
, set
. CS50 library functions obtained by doing from cs50 import get_float, get_int, get_string
.blur.py
making heavy use of external libraries. dictionary.py
using for line_of_text in file:
and line_of_text.rstrip()
which strips the trailing newline, and other string features from Python.addition.py
. Turning strings into int (such as int(x)
where x
is a string taken from input). Variants of the import syntax, name-spacing. Single quotes and double quotes do the same thing in Python. Python can do this because a one-character string is the way it holds a char. agree.py
. You can use ==
to compare strings in Python.meow.py
. Introducing the reasons for using main() in Python. You can define all the functions, and only at the bottom of the file execute main(). The interesting thing is that in Python, defining main() does not cause main() to get executed. If you want it to execute, you have to call main() yourself after it has been defined. positive.c
. while(True)
.print("?", end="")
. I believe named arguments are the same as “keyword arguments” which is what I usually refer to them as. Illustration of integers with unlimited precision. sum()
and len()
acting on a list. Getting command-line arguments using from sys import argv
.It is also worth noting that some of the comparisons between C and Python Malan makes are unfair. C has practically endless helpful libraries to allow you to avoid writing things from scratch. We were of course not using those because the idea was specifically to learn how things are written from scratch. So showing how easy Python is compared to C when you are allowed to tap into libraries like PIL (the Python Imaging Library) is an apples-to-oranges comparison.
Of course, Python is on balance substantially easier. The main reason for teaching you C before Python is that Python hides so much behind higher-level features that people who know Python and not C usually have no picture of what the computer is actually doing. You have that picture! Celebrate! Understanding memory, pointers, and buffers is the insight you need to structure programs efficiently even when working in a language that hides them.
Five Python programs to write! Hello, Mario, Cash or Credit (your choice), Readability, and DNA.