chr
and ord
# Define a list
lst = [1, 'a', True]
# Print out the contents of a list
print(lst)
# Check the type of a list
type(lst)
# Define a list
my_lst = ['Julian', 'Amal', 'Richard', 'Juan', 'Xuan']
# Indexing: Count forward, starting at 0, with positive numbers
print(my_lst[1])
# Indexing: Count backward, starting at -1, with negative numbers
print(my_lst[-1])
# Indexing: Grab a group of adjacent items using `start:stop`, called a slice
print(my_lst[2:4])
# indexing to end of list
print(my_lst[2:])
# Indexing from beginning of list
print(my_lst[:4])
# slicing by skipping a value [start:stop:step]
print(my_lst[0:4:2])
start:stop
start
is included then every element until stop
, not including stop
itselfstart:stop:step
What would be the appropriate line of code to return ['butter', '&', 'jelly']
?
q3_lst = ['peanut', 'butter', '&','jelly']
q3_lst[2:4]
q3_lst[1:3]
q3_lst[:-2]
q3_lst[-3:]
q3_lst[1:4:2]
Note: The following has been added to the notes due to student questions in previous iterations. This and the following two cells are not someting you'll be tested on. Including as an FYI for those curious.
You can return ['jelly', '&', 'butter'] but it combines two different concepts.
start:stop
now refers to indices in the reverse.-1
is used as the step to reverse the output.More details about step
:
step
: the amount by which the index increases, defaults to 1. If it's negative, you're slicing over the iterable in reverse.
# slice in reverse
q3_lst[-1:-4:-1]
# you can use forward indexing
# makes this a little clearer
q3_lst[3:0:-1]
# reminder what's in my_lst
my_lst
# Redefine a particular element of the list
my_lst[2] = 'Rich'
# Check the contents of the list
print(my_lst)
What would the following code accommplish?
lst_update = [1, 2, 3, 0, 5]
lst_update[3] = 4
lst_update
lst_update
lst_update
# Define a tuple
tup = (2, 'b', False)
# Print out the contents of a tuple
print(tup)
# Check the type of a tuple
type(tup)
# Index into a tuple
tup[0]
# Get the length of a tuple
len(tup)
# Tuples are immutable - meaning after they defined, you can't change them
# This code will produce an error.
tup[2] = 1
Which of the following specifies a tuple of 2 items?
item_A = ['100-11-2233', '200-22-3344']
item_B = ('100-11-2233', '200-22-3344')
item_C = ['100-11-2233', '200-22-3344', 1234, 0]
item_D = ('100-11-2233', '200-22-3344', 1234, 0)
item_E = (12)
# Create a dictionary
dictionary = {'key_1' : 'value_1', 'key_2' : 'value_2'}
# Check the contents of the dictionary
print(dictionary)
# Check the type of the dictionary
type(dictionary)
# Dictionaries also have a length
# length refers to how many pairs there are
len(dictionary)
# Dictionaries are indexed using their keys
dictionary['key_1']
This means that dictionaries, once created, values can be updated.
completed_assignment = {
'A1234' : True,
'A5678' : False,
'A9123' : True
}
completed_assignment
# change value of specified key
completed_assignment['A5678'] = True
completed_assignment
Because dictionaries are mutable, key-value pairs can also be removed from the dictionary using del
.
print(completed_assignment)
len(completed_assignment)
## remove key-value pair using del
del completed_assignment['A5678']
print(completed_assignment)
len(completed_assignment)
# Last duplicate key assigned wins
{'Student' : 97, 'Student': 88, 'Student' : 91}
# lists are not allowed as key types
# this code will produce an error
{['Student'] : 97}
{'Student' : 97, 'student': 88, 'STUDENT' : 91}
Fill in the '---' in the code below to return the value stored in the second key.
height_dict = {'height_1' : 60, 'height_2': 68, 'height_3' : 65, 'height_4' : 72}
height_dict[---]
Write the code that would create a dictionary car
that stores values about your dream car's make
, model
, and year
.
# YOUR CODE HERE
in
operator¶in
operator asks whether an element is present inside a collection, and returns a boolean answer.
# Define a new list and dictionary to work with
lst_again = [True, 13, None, 'apples']
dict_again = {'Shannon': 33, 'Josh': 41}
# Check if a particular element is present in the list
True in lst_again
# The `in` operator can also be combined with the `not` operator
'19' not in lst_again
# In a dictionary, checks if value is a key
'Shannon' in dict_again
# does not check for values in dictionary
33 in dict_again
After executing the following code, what will be the value of output
?
ex2_lst = [0, False, 'ten', None]
bool_1 = False in ex2_lst
bool_2 = 10 not in ex2_lst
output = bool_1 and bool_2
print(output)
Every character has a unicode code point
- an integer that can be used to represent that character.
If a computer is using unicode, it displays a requested character by following the unicode encodings of which code point
refers to which character.
ord
returns the unicode code point for a one-character string.
chr
returns the character encoding of a code point.
print(ord('a'))
print(chr(97))
ord
and chr
are inverses of one another.
inp = 'b'
out = chr(ord(inp))
assert inp == out
print('Input: \t', inp, '\nOutput: ', out)
Write a function convert_with_offset
that will take a single input parametercharacter
(a single character in as input).
Inside the function, the code will convert the input character
to its unicode code point and then add an offset
value of 50, returning that value as the output from the function.
A) I did it! B) I think I did it. C) I tried but I am stuck. D) Super duper lost.
## YOUR CODE HERE
# TEST YOUR FUNCTION HERE
Note: This was introduced in the Variables lecture.
# Make a variable, and an alias
a = 1
b = a
print(b)
Here, the value 1 is assigned to the variable a
.
We then make an alias of a
and store that in the variable b
.
Now, the same value (1) is stored in both a
(the original) and b
(the alias).
What if we change the value of the original variable (a
) - what happens to b
?
After executing the following code, what will the values stored in a
and b
be?
# Make a variable & an alias
# change value of original variable
a = 1
b = a
a = 2
print(a)
print(b)
a
and b
both store 1a
and b
both store 2a
stores 2 b
stores 1a
stores 1 b
stores 2Reminder: integers are immutable.
What happens if we make an alias of a mutable variable, like a list?
first_list = [1, 2, 3, 4]
alias_list = first_list
alias_list
#change second value of first_list
first_list[1] = 29
first_list
# check alias_list
alias_list
For mutable type variables, when you change one, both change.
After executing the following code, what will the second value stored in second_tuple
?
# Make a variable & an alias
# change value of original variable
my_tuple = (1, 2, 3, 4)
second_tuple = my_tuple
my_tuple[1] = 29
Aliasing can get confusing and be difficult to track, so why does Python allow it?
Well, it's more efficient to point to an alias than to make an entirely new copy of a a very large variable storing a lot of data.
Python allows for the confusion, in favor of being more efficient.