Review: CS50 A Taste of Python
This weekend, I attended one of the new CS50’s New Year’s Seminars, “A Taste of Python”. I had never worked in Python prior to this, so I was excited to get my first “taste” of a new language (new, for me).
We began, of course, with ‘hello, world!’.
So, friendly! So far, so good. The course was taught by Brian Yu, and there were an unbelievable 1,000 participants on the zoom call, with another estimated 1,000 more following on YouTube, and even more following on FaceBook.
Given the large number of participants, it was delightfully surprising to see how well the group was able to communicate, that there were very productive comments in the chat, and that the instructor was able to interact with the participants so easily. Many questions that were posted, were read, and answered quickly by Brian Yu, which was quite amazing given the large number of posts scrolling the chat so quickly.
So, in addition to building a small “hello, world”, program, we went through input, converting strings to integers, simple arithmetic, conditional statements (if, elif, else), variables, loops, sum of array, random, and then were able to write a few simple programs, including, appropriately, a Countdown to Happy New Year.
The course lasted about three hours, and went at a pace that was comfortable for someone with at least a basic background in some programming. But, perhaps too quickly for anyone who had no prior coding experience. And left us with two further ‘homework’ assignments to complete on our own. Here are my solutions to the homework problems.
First we were to write a program that asks the user to type in a number n, and then prints a pyramid of hash marks of height n, like the pyramid in Mario. This was my solution
n = int(input("Number: "))for i in range(n):
for x in range(n-i-1):
print(" ", end="")
for y in range(i+1):
which produces this output, when run:
The second assignment is to write a program, called Cash, that asks the user to type in an amount of money and prints the minimum number of US coins (quarters, dimes, nickels and pennies)required to make change.
def main():cash = float(input("How much cash? $ "))
coins = 0
if cash > 0:
cents = round(cash * 100)
quarters = int(cents / 25)
dimes = int((cents % 25) / 10)
nickels = int(((cents % 25) % 10) / 5)
pennies = int(((cents % 25) % 10) % 5)coins = coins + quarters
if dimes > 0:
coins = coins + dimes
if nickels > 0:
coins = coins + nickels
if pennies > 0:
coins = coins + pennies