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:
- https://www.codecademy.com
- https://www.datacamp.com
- https://docs.python.org/3.8/tutorial/index.html
- https://w3schools.com
- Coursera courses such as "Programming for Everybody (Getting Started with Python)" from University of Michigan
- Linkedin Learning courses (free with UIC login) such as "Learning 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!
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)
my_number = 10
my_word = "snakes"
my_number
#If you assign a new number as my_number, it writes over what was previously stored
my_number = 20
my_number
# 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)
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.
(5 - 4) * 2**2
12 % 5
my_number + 1
#Why do you think this causes an error?
my_word + 1
#Note the plus sign has a special function with strings
#It can concatenate strings together
my_word + "WORD"
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 | != |
10 < 20
15 /3 == 3
my_number != 15
my_word == "snakes"
# 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"
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).
help(round)
my_number = 100.2151
round(my_number)
round(my_number, ndigits=1)
round(my_number, 1)
round(11410981, -6)
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:
#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
#Now we can call the new function
divisible_by(number = 49, divisor = 7)
#As long as you put the arguments in order,
#there's no need to use thier names
divisible_by(14, 3)
#If the divisor argument isn't included,
#it defaults to 1
divisible_by(14)
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.
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!
my_grocery_list[0]
my_grocery_list[1]
#negative numbers start at the end of the list
my_grocery_list[-1]
#The colon is used to make subsets of lists
#Here from index 0 UP TO index 2
my_grocery_list[0:2]
Using functions on lists¶
sorted(my_grocery_list)
len(my_lottery_numbers)
max(my_lottery_numbers)
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.
#Remove the hashtag at the start of the next line to run it. It is a long output.
#dir(my_grocery_list)
my_grocery_list.append('potato')
my_grocery_list
my_lottery_numbers.remove(66)
my_lottery_numbers
my_grocery_list.index("broccoli")
Using 'in' to check if an element is in a list¶
'apple' in my_grocery_list
Using 'for' to iterate over elements in a list and print them¶
for element in my_grocery_list:
print(element)
Defining a function containing a 'for loop' with lists¶
#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
dessertify(my_grocery_list)
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
my_movie_ratings = {"Avengers" : 3, "Shrek" : 4, "Jaws" : 1}
#Print the value for a key
my_movie_ratings["Jaws"]
#Update the value for a key
my_movie_ratings.update({"Avengers" : 4})
my_movie_ratings
#Add a new key value pair
my_movie_ratings["IT"] = 0
my_movie_ratings
#print a statement using each key, value pair
for key, value in my_movie_ratings.items():
print("I rate " + key + " " + str(value) + " 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.