PDA Intro to Python Webinar 2020

Last updated: August 27th, 20202020-08-27Project preview

Intro to Python

Python is a popular object-oriented programming language that can be used in many scientific fields for analyzing, visualizing and modeling data. The current version is version 3. A fun fact is that this language was named after the British comedy group Monty Python!

There are a lot of free resouces online to learn python:

To use this Jupyter Notebook you do not have to download anything, but a way I recommend to download and use Python is by downloading the Anaconda platform. From Anaconda you can launch and run Jupyter Notebooks and other code interpreters that you can use to write and run Python code.

https://www.anaconda.com/distribution/

In this short tutorial, I'll go over some basics of the Python programming language, including variables, data types, and functions. There is much more to learn, so I encourage checking out the above links after this introduction!

Jupyter Notebooks

Jupyter Notebooks are interactive computing notebooks that support many programming languages. The notebook consists of boxes of code that can be edited and run with the results displayed immediately below.

Click on the box below so it is highlighted in green, then press shift + enter to run the Python code in the cell and print "Hello World!".

Try changing the text that it prints and running it again!

In [ ]:
print("Hello World!")

Variables

A variable is a named object. To create a variable the format is:

variable name = object

Objects can be:

  • strings (text) in quotes like "Hello"
  • integers like 1
  • floats (decimal numbers) like 1.5
  • more complex things like lists and dictionaries

Variable names should:

  • only contain letters, numbers and/or underscores
  • not start with a number
  • have underscores instead of spaces (snake_case)
  • not overwrite a word with another meaning in Python (if you type the word and it is any color other than black it is a special word! Try typing the word class)
In [1]:
my_number = 10

my_word = "snakes"
In [2]:
my_number
Out[2]:
10
In [129]:
#If you assign a new number as my_number, it writes over what was previously stored

my_number = 20
In [130]:
my_number
Out[130]:
20
In [131]:
# you can assign multiple variables at the same time using commas
# sometimes comes in handy when you are swapping variables for example

my_number_1, my_number_2 = 100, 200

print(my_number_1)
print(my_number_2)
100
200

Mathematical operations

The symbols we are all familiar with, and some others, are used for math in Python.

Operation __
addition +
subtraction -
multiplication *
division /
exponent **
modulus (remainder) %
floor division (rounds down) //

You can use parenthesis as you would normally in algera to seperate calculations.

In [132]:
(5 - 4) * 2**2
Out[132]:
4
In [133]:
12 % 5
Out[133]:
2
In [134]:
my_number + 1
Out[134]:
21
In [135]:
#Why do you think this causes an error?

my_word + 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-135-a9adee886f55> in <module>
      1 #Why do you think this causes an error?
      2 
----> 3 my_word + 1

TypeError: can only concatenate str (not "int") to str
In [137]:
#Note the plus sign has a special function with strings
#It can concatenate strings together

my_word + "WORD"
Out[137]:
'snakesWORD'

Boolean Values

A boolean value just means a logical variable that can be true or false.

True and False (note specific capitalization) are special words in python that represent boolean values.

Boolean values can be calculated using comparison operators:

Operator __
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Equals ==
Does not equal !=
In [138]:
10 < 20
Out[138]:
True
In [139]:
15 /3 == 3
Out[139]:
False
In [140]:
my_number != 15
Out[140]:
True
In [141]:
my_word == "snakes"
Out[141]:
True
In [142]:
# with strings the greater than and less than operators work too
# they tell you how strings appear in alphabetical order, 'z' > 'a'

my_word > "kittens"
Out[142]:
True

Built-in functions

A function takes some input/inputs, performs some operations on them, returning some new output.

In Python many functions are available to use. https://docs.python.org/3/library/functions.html

Many, many other functions can be accessed through importing Python packages, which won't be covered here.

The format of a function is:

function name( arguments seperated by commas )

  • The function name is pre-set by Python and is performs a specific operation.

  • The arguments can be one or many, and include the variable or object the function will act on, and possibly arguments that adjust the functions behavior.

  • If you are curious about a function, you can use the help function to learn about it! Just type help(function's name).

In [143]:
help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

In [144]:
my_number = 100.2151

round(my_number)
Out[144]:
100
In [145]:
round(my_number, ndigits=1)
Out[145]:
100.2
In [146]:
round(my_number, 1)
Out[146]:
100.2
In [147]:
round(11410981, -6)
Out[147]:
11000000

Defining your own function

If you can't find a function to do what you want, you can write your own!

The format of defining a function is:

def function name( argument1 = optional default value, argument2 = optional default value ):

   code that does something using the arguments

   return desired output

  • The word 'def' is a special word that tells python you are defining a function. You can choose your own function name, and names for the arguments.

  • The arguments are the inputs, and can have default values, whih are used if the argument isn't included when the function is called.

  • Inside the function is the code that explains what to do with the arguments. Note the tab in front, it's important!

  • Finally the special word 'return' is followed by what you want the output of the function to be.

A simple example below should help clarify this:

In [148]:
#This function will tell us if a number is divisble
#by a specific divisor. The default divisor is 1.

def divisible_by(number, divisor = 1):
    
    #The modulo operator calculates the remainder
    remainder = number % divisor
    
    #The function returns a boolean value (True/False)
    #telling us if the reamainder is 0 or not
    return remainder == 0
In [149]:
#Now we can call the new function

divisible_by(number = 49, divisor = 7)
Out[149]:
True
In [150]:
#As long as you put the arguments in order,
#there's no need to use thier names

divisible_by(14, 3)
Out[150]:
False
In [151]:
#If the divisor argument isn't included, 
#it defaults to 1 

divisible_by(14)
Out[151]:
True

A new data type: Lists

Lists are ordered groups of data. They are also a "class" of objects, meaning they have special functions and features only defined for Lists.

The way to create a list is:

variable name = [ element1, element2, element3, ..., elementn ]

  • You can store multiple data types in a list, but usually it's just one type (strings or numeric).

  • You can also iterate over lists, which means pull out each element one at a time to do something with them.

In [175]:
my_grocery_list = ["lemon", "apple", "broccoli"]

my_lottery_numbers = [10, 18, 21, 51, 66]

Accessing List by index

The format is:

list name[index]

  • index is the number of the element in the list you want.

  • Important: In Python indexes starts at 0, not 1!

In [176]:
my_grocery_list[0]
Out[176]:
'lemon'
In [177]:
my_grocery_list[1]
Out[177]:
'apple'
In [178]:
#negative numbers start at the end of the list

my_grocery_list[-1]
Out[178]:
'broccoli'
In [179]:
#The colon is used to make subsets of lists
#Here from index 0 UP TO index 2

my_grocery_list[0:2]
Out[179]:
['lemon', 'apple']

Using functions on lists

In [180]:
sorted(my_grocery_list)
Out[180]:
['apple', 'broccoli', 'lemon']
In [181]:
len(my_lottery_numbers)
Out[181]:
5
In [182]:
max(my_lottery_numbers)
Out[182]:
66

Methods

Some other functions are only defined for the Lists class, and in this case they are called "methods".

To use a method the format is:

list variable name.method name(arguments)

To see what methods are available for an object, based on its class, you can type dir(object name). Ignore the ones that include underscores on either side for now.

In [183]:
#Remove the hashtag at the start of the next line to run it. It is a long output.

#dir(my_grocery_list)
In [184]:
my_grocery_list.append('potato')

my_grocery_list
Out[184]:
['lemon', 'apple', 'broccoli', 'potato']
In [185]:
my_lottery_numbers.remove(66)

my_lottery_numbers
Out[185]:
[10, 18, 21, 51]
In [186]:
my_grocery_list.index("broccoli")
Out[186]:
2

Using 'in' to check if an element is in a list

In [187]:
'apple' in my_grocery_list
Out[187]:
True

Using 'for' to iterate over elements in a list and print them

In [188]:
for element in my_grocery_list:
    print(element)
lemon
apple
broccoli
potato

Defining a function containing a 'for loop' with lists

In [189]:
#This function will add the word "pie" to the end of all the items in a list of strings

def dessertify(a_list):
    #create a new empty list by typing empty brackets
    dessert_list = []
    
    #make a for loop that iterates over a_list. The word 'item' could have been anything
    #this loop runs once for each item in a_list
    for item in a_list:
        
        #create a variable pie that is the original item + the word pie
        #this variable is defined in the for loop, so it is written over every time the loop runs
        pie = item + " pie"
        
        #append the current pie variable to the dessert_list
        dessert_list.append(pie)
    
    #output the new list containing only pies
    return dessert_list
In [190]:
dessertify(my_grocery_list)
Out[190]:
['lemon pie', 'apple pie', 'broccoli pie', 'potato pie']

A new data type: Dictionaries

Finally, we will introduce another common way to store data in Python. In a dictionary, the data are not ordered like in a list.

Dictionaries contain data that are linked in a key - value relationship.

To create a dictionary with two keys the format is:

dictionary variable name = { "key1" : "value1", "key2" : "value2"}

  • Curly brackets instead of square brackets
  • The key comes first followed by a colon, then the value
  • Key/value pairs are seperated by commas, there can be as many as you'd like
  • Values can be a list, can be different data types
  • Keys must be unique
In [191]:
my_movie_ratings = {"Avengers" : 3, "Shrek" : 4, "Jaws" : 1}
In [192]:
#Print the value for a key

my_movie_ratings["Jaws"]
Out[192]:
1
In [193]:
#Update the value for a key

my_movie_ratings.update({"Avengers" : 4})

my_movie_ratings
Out[193]:
{'Avengers': 4, 'Shrek': 4, 'Jaws': 1}
In [194]:
#Add a new key value pair

my_movie_ratings["IT"] = 0

my_movie_ratings
Out[194]:
{'Avengers': 4, 'Shrek': 4, 'Jaws': 1, 'IT': 0}
In [195]:
#print a statement using each key, value pair

for key, value in my_movie_ratings.items():
    print("I rate " + key + " " + str(value) + " stars.")
I rate Avengers 4 stars.
I rate Shrek 4 stars.
I rate Jaws 1 stars.
I rate IT 0 stars.

Congratulations!

Congratulations on learning about Python variables, basic operators, boolean values, functions, methods, lists and dictionaries! You know a lot of the basics now, and if you enjoyed this tutorial at all, you should spend some more time exploring the links at the top of the page.

Right now, you may be wondering how these basic Python commands could be helpful at all in your research projects. The capabilities of Python are really expanded upon when you import Python packages, which contain many new functions and classes of objects. However, the basics are essential and can be very powerful tools when you are working with customizing or troubleshooting anything in the future.

In the next webinar I'll talk about two Python packages, Pandas and Seaborn, that have very obvious practical applications with any data set.

Notebooks AI
Notebooks AI Profile20060