Python is a programming language.
A programming language is a set words you can put together to tell a computer to do something.
We like using Python in Software Carpentry Workshops for lots of reasons
It is widely used in science
Has a huge supporting community so there are lots of ways to learn and get help
In my experience it is really nice to work with because it is easier to get started than other languages.
like this Jupyter Notebook. Not a lot of languages have this kind of thing. It's great.
Even if you aren't using Python in your work, you can use Python to learn the fundamentals of programming that will apply accross languages
VARIABLES
We store values inside variables a variable can hold any kind of data type or structure we can refer to variables in other parts of our programs
In [3]:
example_variable = "ljhkjhkjkgjkg"
# I can display what is inside example_variable by using
# the print command lets us do this
# try changing the value.
print (example_variable)
DATA TYPES characters and numbers characters: '0' '3' ';' '?' 'x' 'y' 'z' numbers (integers and decimals): 1 2 3 100 10000 10.0 56.9 -100 -3.765 booleans: True, False
DATA STRUCTURES strings, lists, dictionaries, and tuples
In [27]:
# STRINGS are one or more characters stung togehter: "Hello World!"
greeting = "Hello World!"
print ("The greeting is, %s" % greeting)
In [3]:
# LISTS are collections of things in a list:
list_of_characters = ['a', 'b', 'c']
print (list_of_characters)
list_of_numbers = [1, 2, 3, 4]
print (list_of_numbers )
# We can access any value in the list by it's position in the list. This is called the index
# Indexes start at 0
print ("The second value in the list is %d" % list_of_numbers[1])
In [23]:
# DICTIONARIES are collections of things that you can lookup like in a real dictionary:
dictionary_of_definitions = {"aardvark" : "The aardvark is a medium-sized, burrowing, nocturnal mammal native to Africa.",
"boat" : "A boat is a thing that floats on water"}
# we can find the definition of aardvark by giving the dictionary the "key" to the definition we want.
# In this case the key is the word we want to lookup
print ("The definition of aardvark is, %s" % dictionary_of_definitions["aardvark"])
dictionary_of_colors = {1 : "purple", 2 : "green", 3 : "blue"}
# This sets up an association between numbers and colors. You can do this for any pair or pairs of associations
# Sometimes dictionaries are called "associative arrays" for this reason.
# We call each association a "key-value pair".
# A dicitonary can be thought of as a list of associations known as key-value pairs
# we can find the color associated with a number by asking the dictionary the number we want to lookup
print ("The color at item 2 is %s " % dictionary_of_colors[2])
#
ASSESSMENT Which one of these is a valid entry in a dictionary?
In [3]:
# TUPLES are like sets of values
# an example is a set of x and y coordinates like this
tuple_of_x_y_coordinates = (3, 4)
print (tuple_of_x_y_coordinates)
# tuples can have any number of values
coordinates = (1, 7, 38, 9, 0)
print (coordinates)
icecream_flavors = ("strawberry", "vanilla", "chocolate")
print (icecream_flavors)
# you might be asking, what is the difference between a tuple and a list
# Once you have created a list you can add more items to it
# Once you have created a tuple, you cannot add more items to it
# Let's start with an empty list
add_things_list = []
add_things_list.append("one")
print (add_things_list)
add_things_list.append("two")
print (add_things_list)
In [ ]:
# Add things to the list above: add_things_list
OK great. Now what can we do with all of this?
We can plug everything together with a bit of logic and python language
and make a program that can do things like
process data
parse files
data analysis
What kind of logic are we talking about?
We are talking about something colled a "logical structure"
There are two logical structures we will use
In [13]:
# Conditionals are how we make a decision in the program
# we do this with something called an "if statement"
# Here is an example
it_is_daytime = False # this is the variable that holds the current condition of it_is_daytime which is True or False
if it_is_daytime:
print ("Have a nice day.")
else:
print ("Have a nice night.")
# before running this cell
# what will happen if we change it_is_daytime to True?
# what will happen if we change it_is_daytime to False?
In [18]:
# what if a condition has more than two choices? Does it have to use a boolean?
# python if-statments will let you do that with elif
# elif stands for "else if"
user_name = "Joe"
if user_name == "Marnee": #notice the double equals sign. This is used to differentiate between comparing two values and
# and assiging a value to a variable
print ("Marnee likes to program in Python and F#")
elif user_name == "Frank":
print ("Frank does lots of interesting image processing in Python.")
elif user_name == "Julian":
print ("Julian ia an awesome programmer at Cyverse")
else:
print ("We do not know who you are")
# for each possibility of user_name we have an if or else-if statment to check the value of the name
# and print a message accordingly.
In [19]:
# loops tell a program to do the same thing over and over again until a certain condition is met
# we can loop over cellections of things like lists or dictionaries
# or we can create a looping structure
In [23]:
# LOOPING over a collection
# LIST
# If I want to print a list of fruits, I could write out each print statment like this:
print("apple")
print("banana")
print("mango")
# or I could create a list of fruit
# loop over the list
# and print each item in the list
list_of_fruit = ["apple", "banana", "mango"]
# this is how we write the loop
# "fruit" here is a variable that will hold each item in the list, the fruit, as we loop
# over the items in the list
print (">>looping>>")
for fruit in list_of_fruit:
print (fruit)
In [36]:
# LOOPING over a collection
# DICTIONARY
# We can do the same thing with a dictionary and each association in the dictionary
fruit_price = {"apple" : 0.10, "banana" : 0.50, "mango" : 0.75}
for key, value in fruit_price.items():
print ("%s price is %f" % (key, value))
In [7]:
# LOOPING a set number of times
# We can do this with range
# range automatically creates a list of numbers in a range
# here we have a list of 10 numbers starting with 0 and increasing by one until we have 10 numbers
# What will be printed
for x in range(0,10):
print (x)
In [ ]:
# That's it. With just these data types, structures, and logic, you can build a program
# let's do that next with functions