November 7, 2019

Python

We can use try and except statements to check for possible errors that a user might encounter. The general syntax of a try and except statement is

try:
# some statement
except ErrorName:
# some statement

The zip command takes two or more lists as inputs and returns an object that controls a list of pairs. Each pair contains one element from each of the inputs.

To see nested lists, you can convert the zip object to a list.
print(list(name of zip))

We can add a single element to a list using append().
empty_list.append(1)

When we use .append() on a list that already has elements, out new element is added to the end of the list.

If we want to add a single element to a list using +, we have to put the element in brackets [].

The function range() takes a single input and generates numbers starting at 0 and ending at the number before the input.
my_range = range(10)

Just like zip(), range() returns an object that we can convert into a list.

By default, range() creates a list starting at 0. However, if we call range() with 2 arguments, we can create a list that starts at a different number.
range(2,9)

If we use a third argument, we can create a list that skips numbers. The third argument is the number to skip by.
range(0,11,2)

for <temp variable> in <list variable> :
<action>

—— dog_breeds = [‘french_bulldog’, ‘dalmation’, ‘shihtzu’, ‘poodle’, ‘collie’]
—— for breed in dog_breeds:
—— print(breed)

Previous
Previous

November 11, 2019

Next
Next

November 3, 2019