CS50 Introduction To Python Programming
The CS50's Introduction to Programming with Python is a course offered by Harvard University, focusing on teaching Python programming.
This course, led by Harvard Professor David J. Malan, is designed for students with or without prior programming experience who want to learn Python specifically.
It covers topics such as functions, variables, conditionals, loops, exceptions, libraries, unit tests, file I/O, regular expressions, and object-oriented programming.
The course provides hands-on practice opportunities with exercises inspired by real-world programming problems. It can be taken independently or as a precursor to the more general CS50x course,
which covers computer science and programming with C, Python, SQL, and JavaScript.
The CS50's Introduction to Programming with Python course is available online through platforms like Harvard Online, edX, and YouTube, offering a comprehensive introduction to Python programming
Problem Set Solutions
Problem Set 0
- Indoor Voice
- Playback Speed
- Playback Speed
- Einstein
- Tip Calculator
Indoor Voice
In a file called indoor.py, implement a program in Python that prompts the user for input and then outputs that same input in lowercase.
Punctuation and whitespace should be outputted unchanged. You are welcome, but not required, to prompt the user explicitly, as by
passing a str of your own as an argument to input.
Source Code
def main():
text = input("")
print(text.lower())
main()
Playback Speed
Some people have a habit of lecturing speaking rather quickly, and it'd be nice to slow them down,
a la YouTub's 0.75 playback speed, or even by having them pause between words.
In a file called playback.py, implement a program in Python that prompts the user for input and then outputs that same input, replacing each space with ... (i.e., three periods).
Source Code
def slow_down_speech(): """Prompts the user for input and then outputs the same input with spaces replaced by '...''.""" user_input = input("") slowed_down_input = user_input.replace(" ", "...") print("" + slowed_down_input) slow_down_speech()
Making Faces
Before there were emoji, there were emoticons, whereby text like :) was a happy face and text like :( was a sad face.
Nowadays,programs tend to convert emoticons to emoji automatically!
In a file called faces.py, implement a function called convert that accepts a str as input and returns that same input with any :) converted to 🙂 (otherwise known as a slightly smiling face) and any :( converted to 🙁 (otherwise known as a slightly frowning face).
All other text should be returned unchanged.
Then, in that same file, implement a function called main that prompts the user for input, calls convert on that input, and prints the result.
You're welcome, but not required, to prompt the user explicitly, as by passing a str of your own as an argument to input. Be sure to call main at the bottom of your file.
Source Code
def main(): text = input("Say: ") print(convert(text)) def convert(s): return s.replace(":)", "🙂").replace(":(", "🙁") main()
Einstein
Even if you haven't studied physics (recently or ever!), you might have heard that E=mc2
, wherein E
represents energy (measured in Joules), m
represents mass (measured in kilograms),
and
represents the speed of light (measured approximately as 300000000 meters per second), per Albert Einstein et al.
Essentially, the formula means that mass and energy are equivalent.
In a file called einstein.py, implement a program in Python that prompts the user for mass as an integer (in kilograms) and then outputs the equivalent number of Joules as an integer. Assume that the user will input an integer.
Source Code
def calculate_energy(): """Prompts the user for mass and then outputs the equivalent energy in Joules.""" mass = int(input("")) energy = mass * (300000000 * 300000000) print("" + str(energy)) calculate_energy()
Tip Calculator
In the United States, it's customary to leave a tip for your server after dining in a restaurant, typically an amount equal to 15% or more of your meal's cost. Not to worry, though, we've written a tip calculator for you, below!
def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"Leave ${tip:.2f}") def dollars_to_float(d): # TODO def percent_to_float(p): # TODO main()Well, we've written most of a tip calculator for you. Unfortunately, we didn't have time to implement two functions:
dollars_to_float, which should accept a str as input (formatted as $##.##, wherein each # is a decimal digit), remove the leading $, and return the amount as a float. For instance, given $50.00 as input, it should return 50.0.
percent_to_float, which should accept a str as input (formatted as ##%, wherein each # is a decimal digit), remove the trailing %, and return the percentage as a float. For instance, given 15% as input, it should return 0.15.
Source Code
def main(): dollars = dollars_to_float(input("How much was the meal? ")) percent = percent_to_float(input("What percentage would you like to tip? ")) tip = dollars * percent print(f"${tip:.2f}") def dollars_to_float(d): # Remove the leading $ and convert the string to a float return float(d[1:]) def percent_to_float(p): # Remove the trailing % and convert the string to a float return float(p[:-1]) / 100 # Test the program with the specified input and output main()