CS50 Python
Overview Key Terms
There are many programming languages used in the world of computer science. Many • Python
of these languages can accomplish equivalent tasks, but programmers choose a lan- • high-level
guage depending on the project they’re working on. Think of different programming • low-level
languages as different kinds of shoes. Shoes all share core functionality: protecting • list
your feet from the ground. But while athletic shoes are great for running, they are • tuple
much worse in the cold than a pair of warm boots. Similarly, while programming lan- • dict
guages all share the same basic functionality, each emphasizes particular features that
• dynamically
are optimal for specific uses.
typed
Python
Python is a high-level programming language developed to be easy to learn, easy to read, and broadly appli-
cable. By abstracting away low-level technical details like memory management, Python reads more similarly
to a natural language than a low-level language like C. Python is also different from C in that it is interpreted
at run time rather than compiled to machine code beforehand. This allows us to run a single command (for ex-
ample, python hello.py) to run a program. Because of its convenience, Python is widely used and compatible
with other technologies such as databases, graphical user interfaces (GUIs), and web programming.
Syntax Built-in data types
list - an ordered and changeable collection of items (can be updat-
from cs50 import get_string ed and length can be changed)
>>> mylist = [“foo”, “bar”] create a new list
def main(): >>> mylist.append(“baz”) append “baz” to mylist
print(“hello, world”) >>> mylist show mylist
[“foo”, “bar”, “baz”] value of mylist
if __name__ == “__main__”:
main() tuple - an ordered and unchangeable collection of items
>>> mytuple = (“foo”, “bar”, “baz”) create a new tuple
>>> mytuple[0] show the value at the 0 index of mytuple
while True: “foo” the value at the 0 index of mytuple
print(“hello, world”)
dict (dictionary) - an unordered, changeable list of key value pairs
in which the key is the index to access a value
for i in range(50): >>> fruitcolor = {“apple”: 3, “lime”: 10} create a new dict
print(“hello, world”) >>> fruitcolor[“apple”] show the value of “apple”
“red” value of “apple”
if x < y:
print(“x is less than y”)
Things to Remember
elif x > y: • Tabs and new lines are used to denote the end of commands and
print(“x is greater than y”) functions, no semicolons here!
else: • Python uses colons similar to the way we use open curly braces
print(“x is equal to y”)
({) in C, but these should be on the same line as the code, not a
line all by themselves.
import sys • Python is a dynamically typed language, meaning it infers data
types at the time of assignment. + acts as both arithmetic addition
for s in sys.argv: and string concatenation depending on the variable types.
print(s) • Python comes with more functions out of the box than C. Ad-
ditionally, there are lots of libraries that exist for Python so it is
typically a good idea to check if functions exist in Python’s many
libraries before you implement them yourself.
© 2018 This is CS50.