Our goal for today will be to use what we have learned thus far to build the function below.
This function will take a URL (which is stored as a string) and categorize the url based on a set of criteria.
def get_cats_from_url(url):
if len(url.split('/')) < 6:
return "homepage"
else:
return url.split('/')[5]
Build a function that:
Takes a variable with a single value (e.g., x = 50),
multiplies that variable it by 100, and
prints the result.
So if x = 50, the result will equal 5000
w = 25
x = 50
y = 100
def mult100(x):
solution = x * 100
print solution
mult100(w)
mult100(x)
mult100(y)
2500 12300 10000
Create a function that:
Only multiplies a varaible by 100 if the variable is greater than 30 (hint: use if-else statements in the function).
If the variable is less than 30, the function should print: "Initial value is less 30. Request denied."
w = 25
x = 50
y = 100
def mult100(x):
if x > 30:
solution = x * 100
print solution
else:
print "Initial value is less 30. Request denied."
mult100(w)
mult100(x)
mult100(y)
Initial value is less 30. Request denied. 12300 10000
A string is a sequence of characters.
Use the single or double quotations,"", to create a string.
fruit = "banana"
print fruit
banana
String are immutable. You cannot modify elements within the string (e.g., replacing the H in hello with L).
However, you can add more string characters to the beginning or end of a string.
greeting = 'Hello World'
print greeting
Hello World
greeting = greeting + "!!!"
print greeting
greeting = "!!!" + greeting
print greeting
Hello World!!! !!!Hello World!!!
Strings are examples of Python objects.
An object contains both data (the actual string itself) and "methods".
Methods are functions that are built into the object and are available to any instance of the object
# the `type` function shows the type of an object
type(greeting)
str
Python has a function called dir
which lists the methods available for an object.
The methods with leading and trailing underscores are 'magic methods'. We won't focus on these for this workshop.
We will focus on the methods without the underscore.
dir(greeting)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
You can also use the help command for each object.
Let's see what rstrip does.
help(str.rstrip)
Help on method_descriptor: rstrip(...) S.rstrip([chars]) -> string or unicode Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping
We can also use variables in the help function
help(greeting.rstrip)
Help on built-in function rstrip: rstrip(...) S.rstrip([chars]) -> string or unicode Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping
Let's try using the method rstrip.
We invoke methods using dot notation.
The object (i.e., variable) appears before the dot, and the name of the method appears after the dot.
greeting = "Hello World "
print greeting
Hello World
print greeting + "!"
Hello World !
print greeting.rstrip() + "!"
Hello World!
greeting = "Hello World%%%%&%%%%%&"
print greeting
Hello World%%%%&%%%%%&
print greeting.rstrip('&')
Hello World%%%%&%%%%%
print greeting.rstrip('&, %')
Hello World
let's try using .upper and .lower
print greeting.upper()
print greeting.lower()
HELLO WORLD%%%%&%%%%%& hello world%%%%&%%%%%&
You can even use mutiple methods in the same line of code
print greeting.lower().rstrip('&, %')
hello world
Remember that the outputs for the methods we used have not been saved anywhere.
print greeting
Hello World%%%%&%%%%%&
A list is an ordered sequence of elements.
Each element inside a list is called an item.
Lists are defined by having values between square brackets [ ] with elements seperated by commas [1, 2, 3].
numbers = [2, 4, 6, 8]
An index refers to a position within an ordered list.
Python uses 0-indexing, which means the first character within a list or string is assigned 0.
colors = ['red', 'blue', 'green']
from IPython.display import Image
Image(filename='images/s5_colorsindex.png')
Using indexing, we can print the item, 'blue'.
We use [] to access specific characters within a list.
print colors[1]
blue
A string can be thought of a list of characters, which we can also index.
Image(filename='images/s4_indexing.png')
print fruit[2]
n
Note the differences in how lists and strings are indexed
Length gives us the length of a list or a string.
For lists, it gives us the length of items.
For strings, it gives us the length fo characters.
Note the differences in how lists and strings produce length sizes.
sampleList = [2, 3, 5, 10, 100]
fruit = "banana"
url_file = "https://canvas.eee.uci.edu/courses/2230/files/742190?module_item_id=62039"
url_assignment = "https://canvas.eee.uci.edu/courses/2230/assignments/49367/submissions"
url_home = "https://canvas.eee.uci.edu/courses/2230"
urlSplit = url_file.split("/")
print urlSplit
print len(urlSplit)
print urlSplit[5]
['https:', '', 'canvas.eee.uci.edu', 'courses', '2230', 'files', '742190?module_item_id=62039'] 7 files
def get_cats_from_url(url):
if len(url.split('/')) < 6:
return "homepage"
else:
return url.split('/')[5]
print get_cats_from_url(url_file)
print get_cats_from_url(url_assignment)
print get_cats_from_url(url_home)
files assignments homepage
We'll use just three urls to test the function. But in the later weeks, we'll use this function to traverse and categorize all urls in our data.