Python brings many features we are already familiar with in other programming languages, but gives the user a bit more power in their documentation.
# This is a comment
"""This is a single line docstring"""
"""This is a multiline
comment"""
"""This is a multiline docstring
Notice the separation? Let's get into that and explain it.
"""
Docstrings are used to provide a convenient way to associate modules, functions, classes and methods with Python documentation.
"""This function takes a list of strings and returns a deduplicated copy."""
# convert the list to a set then back to a list to dedup.
Python 2 and 3 are similar but NOT COMPATIBLE! Python 3 does break compatibility. We will be focusing on Python 3.7 but will cover 2.7 along side the entire course. Here are just a few of the major differences:
Python 3 replaced Python 2's print statement with a print function(). While Python 2 will be happy as is or with parentheses... Python 3 will throw an error if the parentheses are not included.
print 'Hello World!' # outputs: Hello World!
mystr = 'Goodbye World!' # Notice we didn't declare a type?
print mystr # outputs: Goodbye World!
print("This works too") # This works too!
print("Hello World!") # Hello World!
mystr = 'Goodbye World!' # Again, no type declared. It's automatically determined.
print(mystr) # Goodbye World!
print 3/2 # 1
print 3.0/2 # 1.5
print (3/2) # 1.5
The Python Interpreter uses a concept called REPL:
The Python Interpreter can be launched from the command prompt or terminal using the following commands:
# >>> represents the interpreter asking for a command.
>>> x = 100
>>> x
100
>>> x + 5
105
>>> x
100
Conditionals can be checked and loops can be executed. Be sure to provide indentation. This can be done via pressing the space bar 2 or 4 times per line or pressing tab.
The other way to run Python code is using source files with the extension of .py. Python does not require compilation on the user's end. Executing .py source code is similar to starting the Interpreter.
Be sure to:
$ ls
file.txt fileIO.py hello_world.py hello_world_3.py
$ python hello_world.py
Hello World!
$ python3 hello_world_3.py
Hello World for Python 3!