Getting Familiar with Python#

Author: Mike Wood

Learning objectives: By the end of this notebook, you should be able to:

  1. Identify differences between Python and other computing languages

  2. Describe common syntax and stylistic conventions of the Python language

  3. Assess how data is stored in coding cells in Jupyter Notebooks

Differences between Python and other common languages#

This guide is aimed at people who have some prior knowledge of another programming language. For example, computer science students likely have some experience with Java or C while data or marine science students may have some experience with MATLAB or R. To get us on the same page, we’ll begin by examining four differences between Python and other common language paradigms.

Difference 1: Implicit Variable Assignment#

Once facet of the Python programming language is that variable types are assigned implicitly – that is, they are assigned based on the on the type of data stored in them upon declaration.

Let’s look at a couple examples:

# define a variable and store an integer in it
x = 5

# print the type of the variable
print(type(x))
<class 'int'>
# define another variable and store a string in it
y = 'five'

# print the type of the second variable
print(type(y))
<class 'str'>

As we can see, the variable x has an int type because it was assigned 5 while y has type str because it was assigned 'five'. Notice that neither of these types were declared prior to assignment. For this reason, it is important to keep in mind the type of each variable and to check types when looking for possible bugs in your code.

Difference 2: Variable Type Re-Assignment#

In many languages, a variable type cannot be implicitly changed after it is declared. For example, in the example above, the variable x could not be reassigned to 'five' because it is already in integer. These restriction are relaxed in Python, so that variable types can be changed by reassignment.

Let’s take a look at an example:

# reassign an integer to the second variable
y = 5

# print the second variable and its type
print(y, type(y))
5 <class 'int'>

As we can see above, the variable y is now an integer, rather than a string. This feature of Python can be very convenient but beware - it is quite easy to reuse the same variable names in long pieces of code without realizing it.

Difference 3: Integer Divisions#

In many program languages, conducting mathematical operations with numbers will retain the initial variable types. For example, consider computing the sum of two integers:

# define three integers x, y, and z
x = 6
y = 5
z = 2
# compute the sum of pairs of variables and store into new variables sum_1 and sum_2
sum_1 = x+z
sum_2 = y+z

Then, let’s check their types:

# print sum_1 and sum_2 along with their types
print(sum_1, type(sum_1))
print(sum_2, type(sum_2))
8 <class 'int'>
7 <class 'int'>

As we can see, the sume of two integers gives you an interger - so far so good. What about division? As of Python 3, integer division will generate a float, if necessary!

# compute the division of two sets of variables and store them in
# variables divisor_1 and divisor_2
divisor_1 = x/z
divisor_2 = y/z

# print divisor_1 and divisor_2
print(divisor_1, type(divisor_1))
print(divisor_2, type(divisor_2))
3.0 <class 'float'>
2.5 <class 'float'>

As we can see, division will always result in a float - even if the result is a whole number. This is a big change from Python 2 and many other programming languages.

Difference 4: Blocks (a.k.a. spaces matter)#

In other programming languages, coding “blocks” (e.g. conditional execution statements, loops, etc) are delineated by curly bracers or other annotations. In Python, code blocks are delineated by spaces. Typically, 4 spaces are used to indicated an indented code block. For example, in an if statement, all code inside the statement should be indented by 4 spaces:

# write a simple if block to showcase Python's indentation
a=1
if a==1:
    print('Yep, a = 1')
Yep, a = 1

Similarly, for a for loop, all statements in the loop should be indented by 4 spaces.

# write a simple for block to showcase Python's indentation
for i in range(5):
    print(i)
0
1
2
3
4

Python syntax and stylistic conventions#

As with all programming languages, Python users have developed a unique set of syntax rules and stylistic conventions for writing and organizing code. Here are 6 notes on syntax and style:

Note 1: Comments#

As in all of programming, extensive comments in the code are essential for code comprehension and collaboration. In Python, comments are written with “hashtags” (or “pound signs”, depending on your generation). In general, the more descriptive the comment, the better! For example:

# here I define the variable x
x = 17

# now I will print x
print(x)
17

Note 2: Variable Names#

In Python, the following conventions are used for variables, functions, classes, variable types, and constants. (Respectively, these styles are refered to as “snake case”, “Camel Case”, and “Screaming Snake Case”.)

Type

Example

variable or function name

a_variable_name

class or type name

aClassName

constant name

name of a constant value

Note #3: Splitting strings across lines#

In Python, strings must be kept on the same line. However, we may want long strings in our code that extend too far. In this case, you will want to split a string across one or more lines. To split strings across a line, you have two options.

First, you can wrap your string components in parentheses:

# make a string called sentence and split it across
# two lines with parentheses - then print it
sentence = ("The quick brown fox jumped over "
"the lazy brown dog.")

# print the string
print(sentence)
The quick brown fox jumped over the lazy brown dog.

Alternatively, you can use the concatenation symbol (+) and the continuation character (\):

# make a string called sentence and split it across
# two lines with a concatentation symbol - then print it
sentence = 'The quick brown fox jumped over ' \
 + 'the lazy brown dog.'

# print the string
print(sentence)
The quick brown fox jumped over the lazy brown dog.

Note #4: String Quotes#

Speaking of string, you may have noticed in the previous coding blocks that one example used double quotes while another used single quotes. In Python, it turns out that it doesn’t matter whether you use single or double quotes! However, it is good practice to remain consistent throughout your code.

While sticking with single and double quotes is good practice, it is also OK to use both together since they are equivalent:

# showcase two strings defined with both single and double quotes
string_1 = "Mike "
string_2 = 'Wood'

# print the strings
print(string_1 + string_2)
Mike Wood

In fact, it can be advantageous to use one or the other if you also need quotes within your string:

# show two examples of one type of quote used inside a string defined with the other
string_1 = "Mike's last name is "
string_2 = '"Wood"'

# print the strings
print(string_1 + string_2)
Mike's last name is "Wood"

Note #5: Reserved Names#

Similar to Python’s flexibility in allowing a user to redefine a variable type by reassignment, Python also allows you to overwrite built-in methods. Be careful with this functionality as it could cause some issues.

For example, we have been using the built-in print method to print out strings and variables, e.g.:

# print one of the strings above
print(string_1)
Mike's last name is 

In Python, there are no rules against using print as a variable name:

# assign an integer to a variable named print
print = 10

However, print is no longer our usual print command. What happens if you run the following cell?

# uncomment and run this cell
# print(string_1)

Arg! We get an error and we can no longer use the print method. If you happen to overwrite a method, then use the del command to delete the reference.

del print

Now, we can use our print method yet again:

# print one of the strings above
print(string_1)
Mike's last name is 

To see all of the names defined in Python, you can use the following command:

# uncomment to see all names
# dir(__builtins__)

Note #6: Python Style and Readablility!#

In Python, the goal is to make your code as readable as possible. This is helpful for collaboration and for returning to code you have written.

To begin you exploration in Python, here are 4 recommendations for good Python style:

4 Preliminary Style Recommendations#

  1. Add comments to your code (or markdown cells in Jupyter)

  2. Use descriptive variables names

  3. Avoid extremely long lines (cap at 72 characters)

  4. Add spacing between lines and between operators

There are full list of guidelines available at https://peps.python.org/pep-0008/

A Final Note on Jupyter Variables#

As with nearly all resources in this guide, this page is written in a Jupyter notebook. In Jupyter notebooks, variables are stored into memory and can be accessed by subsequent cells. However, cells above the current cell can also use variables define here.

This can get you into trouble and is worthy of a warning:

Warning

Variables defined in subsequent cells in a notebook can overwrite those defined previously. Use caution when running cells out of the top-to-bottom order in a Jupyter notebook.

Let’s take a look at an example. Consider the following variable definition:

# assign 'six' to the variable z
z = 'six'

Now, return to the cells above on sums and divisors and run one of the calculations. What happens?