Introduction to Python
Python is the programming language that we will be using. There are many different programming languages. Each language has its own syntax but all programming languages have the same goal–writing a program that the computer can translate into a series of operations to produce some output.
Syntax refers to how specifically we will write our statements (the words and punctuation that we will use).
Python programs are composed of a series of statements that are executed in order, from top to bottom.
In this section, we’ll go over the print
function, comments, and we’ll start looking at order of execution.
Note: We’re using Python 3, so if you have experience with Python 2.7, you will see a few things that are different.
The print
Function
In the previous chapter about getting started with jupyter notebooks, you saw the print
function in some of the examples. Let’s break an example down:
print("hello")
In this example you can see that there are 3 different parts to the function (demonstrated by different colors and bold text in these notes).
print
this is the part of the function that says what we want to do.( ... )
the parentheses are indicating that this function takes an argument (see part 3)."hello"
this is what we call the argument. This is what we want to apply the functionprint
to.- Note: to print text, it’s important that we put quotes around it here. We’ll learn more about this in the Data Types section of the notes.
# TODO: write a couple print statements here.
# TODO: what order are your print statements executed
# in when you run the code?
# Answer:
Notice that when you execute a series of print
statements, the text from each subsequent statement is output on the next line. This is because the print
function automatically adds a “newline” character to the end of your text. Newline characters are what are produced when you are writing a text document and press the “return” or “enter” key to write on the next line.
Comments
In the examples that we’ve shown you so far, there are certain lines in our blocks of code that begin with a #
character. These lines are called comments. Comments are parts of programs that are not executed by the kernel but serve the extremely important purpose of giving clarification to the people who are reading your code.
There are a few ways to create comments in python. The first way is with a #
symbol. This makes it so that anything on the line after the #
is considered a comment.
# this is a comment that is a whole line
print("this is a print statement") # comment on a line with code
In the above example, the print
statement would still be executed like normal. Luckily for us, our programming environment is going to help us out by color-coding our code to help us identify which parts of our code are actually going to be executed (non-comments) and which parts won’t be (comments).
The second way to create a comment is for multi-line comments. Multi-line comments begin and end with three apostrophes, '''
. Multi-line comments are typically used for larger descriptions.
''' This is a multi-line comment
that can span as many lines
as we want it to! '''