3: Interface

Instructions

The code below assigns the value 5 to the variable b. Add a new line below that assigns 6 to b.


In [1]:
b = 5
b = 6

In [2]:
assert b == 6

4: Comments

Instructions

Make your mark in the code by adding a comment.


In [3]:
# Here's a comment!
b = 5
# Here's another comment! Neither of these comments are evaluated by Python!
# this is my comment

5: Print function

Instructions

Use the print() function to display the value stored in b. Then, use the print() function to display the number 500.


In [4]:
b = 6
print(b)
print(500)


6
500

6: Types

Instructions

Assign the integer value 100 to the variable named hundred_integer. Assign the string value "hundred" to the variable named hundred_string. Assign the float value 100.5 to the variable named hundred_float.


In [5]:
# Integer
i = 1
# String
s = "Hello World"
# Float
f = 55.55

hundred_integer = 100
hundred_string = "hundred"
hundred_float = 100.5

In [6]:
assert hundred_integer == 100

7: Type function

Instructions

Assign the type of 10 to c then use the print() function to display c.


In [1]:
a = type(5)
# The type is assigned to a. When you print the type, it is abbreviated to `str`
print(a)

c = type(10)
print(c)


<class 'int'>
<class 'int'>

8: Arithmetic operators

Instructions

The variable five contains the integer value 5.

Use the arithmetic operators and the variable five to perform calculations that result in 25. Assign to the variable twenty_five.

Use the arithmetic operators and the variable five to perform calculations that result in -5. Assign to the variable negative_five.


In [2]:
five = 5
twenty_five = five * 5
negative_five = -five
assert twenty_five == 25
assert negative_five == -5

9: Converting types

Instructions

The variable eight contains the integer 8 and the variable ten contains the string "10". Convert eight to a string using the str() function and assign to str_eight. Then convert ten to an integer using the int() function and assign to int_ten.


In [4]:
ten = "10"
eight = 8

int_ten = int(ten)
str_eight = str(eight)

In [5]:
assert int_ten == 10
assert str_eight == u"8"

11: Lists

Instructions

Add new code that appends March to the list l, then appends April to the list, then displays that list, l, using the print() function.


In [6]:
l = []

# Print the type of `l` to confirm it's a list.
print(type(l))
l.append("January")
l.append("February")
l.append("March")
l.append("April")
print(l)


<class 'list'>
['January', 'February', 'March', 'April']

12: Creating lists with values

Instructions

Write a single line of code that creates a list years and fills it with the integer values 2010, 2011, 2012, 2013, and 2014, in that order.


In [11]:
l = ["January", "February", "March", "April"]
m = [0,1,2,3]
years = [_ for _ in range(2010, 2015)]
print(years)


[2010, 2011, 2012, 2013, 2014]

13: Multiple types in list

Instructions

Create a list o that contains the following elements, in order:

  • "Jan" (string)
  • 5.0 (float)
  • 1.0 (float)
  • "uary" (string)
  • 10 (integer)

In [12]:
o = [u"Jan", 5., 1., u"uary", 10]

In [13]:
print(o)


['Jan', 5.0, 1.0, 'uary', 10]

14: List index

Instructions

The list int_months contains the values 1 to 12. Select the value at index 4 and assign to the variable index_four. Then select the last value in the list and assign to last_value.


In [14]:
int_months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
index_four = int_months[4]
last_value = int_months[-1]

15: List length

Instructions

Retrieve the second to last element from the list months and assign it to second_last. Then use the print() function to display second_last.


In [15]:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"]
second_last = months[-2]

16: List slicing

Instructions

Use slicing to return a list named five_nine that contains the values from index 5 to index 9 (including the value at index 9). Then use the print() function to display the first 5 elements in months.

Answer


In [18]:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"]
eight_eleven = months[8:12]
ending_index = len(months)
eight_eleven = months[8:ending_index]

five_nine = months[5:10]
print(months[:5])


['Jan', 'Feb', 'Mar', 'Apr', 'May']

In [ ]: