x = 12
print x
12
Value - letters of numbers
Types - 1) integers, 2) floats, and 3) strings
You can check value types by using 'type()'
x = 12
print type(x)
y = 'Coffee'
print type(y)
z = 12.1
print type(z)
<type 'int'> <type 'str'> <type 'float'>
# Python automatically assigns variables as integers or floats
n = 15
pi = 3.14
# But not strings
message = Python is so cool
File "<ipython-input-8-eaa18bcaba9c>", line 2 message = Python is so cool ^ SyntaxError: invalid syntax
message = "Python is so cool"
print message
print type(message)
Python is so cool <type 'str'>
# Hey, why is there a hash mark to the left of this text?
Variable names can contain letters and numbers but cannot start with a number
Begin variable names with a lowercase letter
You can also use the underscore character (_)
sat_math
For two-word variable names, popular convention is to make first letter of second work uppercase
satMath
import keyword
print(keyword.kwlist)
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
# let's try using a keyword as a variable
not = 15
File "<ipython-input-16-08cb990de263>", line 2 not = 15 ^ SyntaxError: invalid syntax
Operators are special symbols that represent computations like addition and multiplication
from IPython.display import Image
Image(filename='images/operators.png')
x = 15
y = 4
# Output: x + y = 19
print(x+y)
# Output: x - y = 11
print(x-y)
# Output: x * y = 60
print(x*y)
# Output: x / y = 3.75
print(x/y)
# Output: x // y = 3
print(x//y)
# Output: x ** y = 50625
print(x**y)
11
These operators are use to compare values, and returns either TRUE or FALSE
Image(filename='images/comp_operators.png')
x = 10
y = 12
# Output: x > y is False
print(x>y)
# Output: x < y is True
print('x < y is',x<y)
# Output: x == y is False
print(x==y)
# Output: x != y is True
print('x != y is',x!=y)
# Output: x >= y is False
print('x >= y is',x>=y)
# Output: x <= y is True
print('x <= y is',x<=y)
False False
Lists are a sequence of values.
They can be any time of character
Values in a list are called elements
or items
To create a list, enclose elements in square brackets
cheeses = ['Cheddar', 'American', "Swiss"]
numbers = [17, 123]
optional = []
print cheeses, numbers, optional
['Cheddar', 'Edam', 'Gouda'] [17, 123] []
Image(filename='images/clickstreamdata.png')
url = "http://www.amazon.com/coffee/filters/sdodfeiXsw"
url2 = "http://www.amazon.com/coffee/mugs/sklfwejklwefofew"
print url
http://www.amazon.com/coffee/filters/sdodfeiXsw
# Split function
urlSplit = url.split("/")
print urlList
In the coming weeks, we will learn how to build customized functions that traverse through a list of URLS and categorizes them.
urlFile = "https://canvas.eee.uci.edu/courses/2230/files/742190?module_item_id=62039" # -> categorize as "files"
urlAssignment = "https://canvas.eee.uci.edu/courses/2230/assignments/49367/submissions" # -> categorize as "assignments"
urlHome = "https://canvas.eee.uci.edu/courses/2230"
print urlFile.split("/")
['https:', '', 'canvas.eee.uci.edu', 'courses', '2230', 'files', '742190?module_item_id=62039']
def get_cats_from_url(url):
if len(url.split('/')) < 6:
return "homepage"
else:
return url.split('/')[5]
print get_cats_from_url(urlFile)
print get_cats_from_url(urlAssignment)
print get_cats_from_url(urlHome)
files assignments homepage