In [218]:
# WELCOME TO THE WORLD OF PROGRAMMING
# Fear not... it's not actually as scary as it sounds
# A lot of it starts to make sense once you know the basic rules of the road
In [219]:
# And Welcome to New Orleans. Laissez les bon temps roulez!
In [220]:
# Let's start rolling by doing a little...math.
In [221]:
# In programming, an INTEGER is a whole number.
# If a number has a decimal point, it's called a FLOAT instead.
# So let's play around with some integers.
# You can do all sorts of math on integers, just like you'd do with a calculator or in Excel.
In [222]:
2+2
Out[222]:
In [223]:
5*5
Out[223]:
In [224]:
# In the command line Python interpreter or here in Jupyter Notebooks, these results will always show up.
# But if you run an entire Python (.py) program, however short, they won't.
# To get results to actually appear, you need to "print" them to the screen.
In [225]:
print (2+2)
In [226]:
# So it's good practice to get into the habit of "printing" things when you want to see the results
In [227]:
print (100*5)
In [228]:
# Order of operations works the same as real-world math you learned back in grade school
In [229]:
2+2*10
Out[229]:
In [230]:
# ...is different than:
In [231]:
(2+2)*10
Out[231]:
In [232]:
# VARIABLES
# Instead of always using actual values, we can create a VARIABLE to hold it
# It's kind of like a nickname, but much more than that.
# It stores the value and let's us do things with it.
In [233]:
mynumber = 14
In [234]:
# Let's print to see what we get
In [235]:
print (mynumber)
In [236]:
# Once you have a variable, you can run math on it by using its name like you would the number
# Multiplication
In [237]:
print (mynumber * 5)
In [238]:
print (mynumber + 20)
In [239]:
print (mynumber - 5)
In [240]:
# You can get as complex as you want with these...
In [241]:
print ((mynumber*2+88)*100)
In [242]:
# You can work with powers by using two (*) together
In [243]:
print (3**2)
In [244]:
print (10**5)
In [245]:
# You can also create more than one variable, and then do math on them
In [246]:
num1 = 100
num2 = 5
print (num1*num2)
In [247]:
# You can idenifty what TYPE your variable is like this
In [248]:
type(mynumber)
Out[248]:
In [249]:
# Are there other types? The answer - indeed!
In [250]:
# One that we won't get into right now, but is useful to know about, is called a Boolean.
# It's basically a True/False choice.
# Note: Because the equal sign (=) is used for variables, to say something actually EQUALS the value of something else,
# you need to use a double equal sign (==). Like this:
In [251]:
10 == 11
Out[251]:
In [252]:
10 == 10
Out[252]:
In [253]:
# Now let's assign this to a variable that will store the result
In [254]:
myboolean = 10==10
print (myboolean)
In [255]:
type(myboolean)
Out[255]:
In [256]:
# Now, let's look at another type - one of the most popular out there -- TEXT
# In programming, TEXT is referred to as a STRING
In [257]:
# At its most basic, a string can be displayed like this... note the quotation marks (" ")
# Python actually doesn't care if you use single quotes or double quotes. But best to be consistent.
In [258]:
print ("Hello World")
In [259]:
print ("Welcome to New Orleans")
In [260]:
# Let's create a variable assigned to a string instead.
# In Python, you don't have to declare a variable before you assign a value to it.
# Just give it a name and assign it a value using the = sign.
In [261]:
mystring = "Jackson Square"
In [262]:
mystring
Out[262]:
In [263]:
print (mystring)
In [264]:
#Once the variable is assigned, you can do all kinds of things with your string
#You can add other text to combine with it:
In [265]:
print ("You should go explore " + mystring)
In [266]:
# You can also "multiply" your string.
# Let's see what that means...
In [267]:
print (mystring*3)
In [268]:
# .upper() is one of dozens of string methods, in many ways similar to the functions you might be familiar with in Excel.
# Can you guess what it does? Yep!
In [269]:
print (mystring.upper())
In [270]:
# You might notice that the .upper() method doesn't PERMANENTLY change the "variable" variable.
# We'll get to that in a minute, but watch what happens when we do this:
In [271]:
print (mystring)
In [274]:
# Let's lowercase the string, too
In [275]:
print (mystring.lower())
In [276]:
#These work without variables at all, too, of course
In [277]:
print ("MUFFULETTA".lower())
In [278]:
#SLICING AND DICING STRINGS
In [279]:
# You can use the brackets to 'slice' out characters from a string based on their position.
# Notice in Python, that we start counting at zero. So variable[0] returns the "n" in "nicar".
# The value after the colon is the position that we stop before. So variable[0:2] returns all the characters from
# position 0 until before position 2.
In [280]:
print ("MUFFULETTA"[0:3])
In [281]:
#Using a variable of course works the same way
In [282]:
print (mystring[0:2])
In [283]:
# Let's print the first four characters
In [284]:
print (mystring[:4])
In [285]:
# Let's print everything after the second character
In [286]:
print (mystring[2:])
In [287]:
# Remember when the .upper() method didn't change the variable?
# A string is IMMUTABLE.
# You can't change it in place, so if you want to change it, you have to create a NEW VARIABLE
# and assign the new value to it.
In [288]:
firsttwo = mystring[0:2]
print (firsttwo)
In [289]:
#Another exmample - you can use replace() to do swap out part of the string for something new instead
In [290]:
mystring.replace("Jackson", "Times")
Out[290]:
In [291]:
#But look what happens when we call mystring...
In [292]:
print (mystring)
In [293]:
# See what I mean? It doesn't "stick" unless you create a new variable
In [294]:
newmystring = mystring.replace("Jackson", "Times")
In [295]:
print (newmystring)
In [296]:
#You can also use a function called split() to SPLIT a string based on a certain character
#Think about how this could be used for divded up names separated by commas:
In [297]:
name1 = "Mayfield, Irvin"
In [298]:
# Let's try to split the string on the comma...
In [299]:
name1.split(',')
Out[299]:
In [300]:
print (name1.split(','))
In [301]:
# Let's create a variable to hold the results of this string
In [302]:
name1split = name1.split(',')
In [303]:
print (name1split)
In [304]:
# This type looks a little different, no?
# Let's get the type
In [305]:
print (type(name1split))
In [306]:
# A list? Hmm. That's a new one.
# Because it's a LIST, we can get a specfic item by its index
In [307]:
print (name1split[0])
In [309]:
#Wait, what IS a "list"???
#Ask, and ye shall have an answer in the next session...
In [310]:
# Continue to Part 2 ----->