>>> python_versions = [1.0, 1.5, 1.6, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6] >>> python_versions[0] 1.0 >>> python_versions[1] 1.5 >>> python_versions[7] 2.4 >>> python_versions[-1] 3.6
Error
>>> my_list = ['a','b','c','d','e'] >>> my_list[4] 'e' >>> my_list[5] Traceback (most recent call last): File "", line 1, in my_list[5] IndexError: list index out of range
def how_many_days(month_number): days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31] return days_in_month[month_number-1] print(how_many_days(8))
slice
>>> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] >>> q3 = months[6:9] >>> print(q3) ['July', 'August', 'September'] >>> print(months) ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
eclipse_dates = ['June 21, 2001', 'December 4, 2002', 'November 23, 2003', 'March 29, 2006', 'August 1, 2008', 'July 22, 2009', 'July 11, 2010', 'November 13, 2012', 'March 20, 2015', 'March 9, 2016'] last_eclipse= eclipse_dates[-3:] print(last_eclipse)
>>> sample_string = "And Now For Something Completely Different" >>> sample_list = ['Graham', 'John', 'Terry', 'Eric', 'Terry', 'Michael'] >>> sample_string[4] 'N' >>> sample_list[4] 'Terry' >>> sample_string[12:21] 'Something' >>> sample_list[2:4] ['Terry', 'Eric'] >>> len(sample_string) 42 >>> len(sample_list) 6 >>> 'thing' in sample_string True >>> 'Rowan' in sample_list False