This workshop is licensed under a Creative Commons Attribution 4.0 International License.
General programming language...good at most tasks
CTRL + Enter
will run a cell; Shift + Enter
will run a cell and highlight the next cell.
In [ ]:
=
symbol assigns the value on the right to the name on the left.age
and a name in quotes to a variable first_name
.
In [ ]:
_
(typically used to separate words in long variable names)__alistairs_real_age
have a special meaning
so we won't do that until we understand the convention.print
to display values.print
that prints things as text.
In [ ]:
print
automatically puts a single space between items to separate them.
In [ ]:
print(last_name)
In [ ]:
In [ ]:
# Exercise 1
# line 1 x = ? y = ? swap = ?
# line 2 x = ? y = ? swap = ?
# line 3 x = ? y = ? swap = ?
# line 4 x = ? y = ? swap = ?
# line 5 x = ? y = ? swap = ?
int
): represents positive or negative whole numbers like 3 or -512.float
): represents real numbers like 3.14159 or -2.5.str
): text.type
to find the type of a value.type
to find out what type a value has.
In [ ]:
print()
In [ ]:
height = 'average'
print()
In [ ]:
print()
In [ ]:
print()
In [ ]:
In [ ]:
separator = '=' * 10
print(separator)
In [ ]:
print()
In [ ]:
print()
In [ ]:
print()
1 + '2'
be 3
or '12'
?
In [ ]:
print()
print()
In [ ]:
print()
print()
In [ ]:
print()
first
when doing the multiplication,
creates a new value, and assigns it to second
.second
does not remember where it came from.1) Number of days since the start of the year.
2) Time elapsed since the start of the year.
3) Call number of a book.
4) Standard book loan period.
5) Number of reference queries in a year.
6) Average library classes taught per semester.
In [ ]:
# Exercise 2
In [ ]:
# This sentence isn't executed by Python.
adjustment = 0.5 # Neither is this - anything after '#' is ignored.
# print(adjustment)
len
takes exactly one.int
, str
, and float
create a new value from an existing one.print
takes zero or more.print
with no arguments prints a blank line.
In [ ]:
print()
print()
print()
In [ ]:
print()
print()
In [ ]:
print()
In [ ]:
print()
In [ ]:
print()
In [ ]:
print()
In [ ]:
text = 'Hello there, nice to meet you!'
print(text)
print()
print()
print()
import
to load a library module into a program's memory.module_name.thing_name
..
to mean "part of".datetime
, one of the modules in the standard library:
In [ ]:
In [ ]:
help(datetime)
In [ ]:
In [ ]:
matplotlib
plotting library is often aliased as plt
.name_001
, name_002
, etc.,
would be at least as slow as doing them by hand.[...]
.,
.len
to find out how many values are in a list.
In [ ]:
#index 0 1 2 3 4
names = ['Cathy','Doug','Monica','Jake','Peter']
print(type(names))
print()
print()
In [ ]:
print()
print()
In [ ]:
In [ ]:
append
is a method of lists.object_name.method_name
to call methods.help(list)
for a preview.extend
is similar to append
, but it allows you to combine two lists. For example:
In [ ]:
teen_primes = [11, 13, 17, 19]
middle_aged_primes = [37, 41, 43, 47]
print(primes)
Note that while extend
maintains the "flat" structure of the list, appending a list to a list makes the result two-dimensional.
del
to remove items from a list entirely.del list_name[index]
removes an item from a list and shortens the list.
In [ ]:
print(primes)
del primes[4]
print(primes)
In [ ]:
goals = []
print(goals)
In [ ]:
values = [1,3,4,7,9,13]
print()
print()
print()
IndexError
if we attempt to access a value that doesn't exist.
In [ ]:
print()
values = ____
values.____(1)
values.____(3)
values.____(5)
print('first time:', values)
values = values[____]
print('second time:', values)
Desired Output:
first time: [1, 3, 5]
second time: [3, 5]
In [ ]:
# Exercise 3
{...}
."key":"value"
.,
.
In [ ]:
students = {"firstName":"John","lastName":"Smith"}
print()
print()
In [ ]:
In [ ]:
print()
print()
print()
In [ ]:
courses = {"courses":[
{
"Title":"Intro to Economics",
"Instructor": {
"firstName": "Robert",
"lastName":"Schiller",
},
"Number": "ECON 101",
"Size": 65,
"isFull": True,
},
{
"Title":"Intro to French",
"Instructor": {
"firstName": "Marie",
"lastName": "Gribouille",
},
"Number":"FREN 101",
"Size": 15,
"isFull": False,
}
]}
print(courses)
In [ ]:
print()
print()
'courses'
is the top level or root elementcourses['courses']
is a list; it's items are accessed using the index.keys()
& .values()
lists the dictionary keys and values respectively at the current dictionary level
In [ ]:
print()
print()
In [ ]:
# Exercise 4
print()