Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Lists In this chapter, we look at a means of structuring and accessing a collection of data. In particular, we look at a way of organizing.

Similar presentations


Presentation on theme: "Chapter 4 Lists In this chapter, we look at a means of structuring and accessing a collection of data. In particular, we look at a way of organizing."— Presentation transcript:

1 Chapter 4 Lists In this chapter, we look at a means of structuring and accessing a collection of data. In particular, we look at a way of organizing data in a linear sequence, generally referred to as a list. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons

2 Motivation The way that data is organized has a significant impact on how effectively it can be used. One of the most obvious and useful ways to organize data is as a list. We use lists in our everyday lives—we make shopping lists, to-do lists, and mental checklists. Various forms of lists are provided by programming languages, differing in the elements they can store (mixed type?), their size (variable size?), whether they can be altered (mutable?), and the operations that can be performed on them. Lists also occur in nature. Our DNA is essentially a long list of molecules in the form of a double helix, found in the nucleus of all human cells and all living organisms. Its purpose is also to store information—specifically, the instructions that are used to construct all other cells in the body—that we call genes . Given the 2.85 billion nucleotides that make up the human genome, determining their sequencing (and thus understanding our genetic makeup) is fundamentally a computational problem. In this chapter, we look at the use of lists and other sequences in Python. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons

3 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons

4 List Structures The concept of a list is similar to our everyday notion of a list. We read off (access) items on our to-do list, add items, cross off (delete) items, and so forth. We look at the use of lists next. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

5 What is a List? A list is a linear data structure, meaning that its elements have a linear ordering. An example of a list is sequence of daily temperatures for a given week: Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

6 The location at index 0 stores the temperature for Sunday, the location at index 1 stored the temperature for Monday, and so on. It is customary in programming languages to begin numbering sequences of items with an index value of 0 rather than 1. This is referred to as zero-based indexing. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

7 Common List Operations
Operations commonly performed on lists include: access (retrieve) update append insert delete (remove) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

8 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

9 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

10 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

11 List Traversal A list traversal is a means of accessing, one-by-one, each element of a list. List traversal may be used, for example, to: search for a particular item in a list add up all the elements of a list Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

12 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

13 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

14 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

15 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

16 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.1 List Structures

17 Lists (Sequences) in Python
A list in Python is a mutable, linear data structure of variable length, allowing mixed-type elements. By mutable is meant that the contents of the list may be altered. Lists in Python use zero-based indexing. Thus, all lists have index values 0..n-1, where n is the number of elements in the list. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

18 [1, 2, 3] ['one', 'two', 'three'] ['apples', 50, True]
Lists are denoted by a comma-separated list of elements within square brackets, [1, 2, 3] ['one', 'two', 'three'] ['apples', 50, True] An empty list is denoted by an empty pair of square brackets, []. Elements of a list are accessed by use of an index value within square brackets, For lst = [1, 2, 3], lst[0]  1 access of first element lst[1]  2 access of second element lst[2]  3 access of third element     Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

19 For example, the following prints the first element of list lst,
print (lst[0]) The elements of lst can be summed as follows, sum = lst[0] + lst[2] + lst[2] To update, lst[2] = 4 replacement of 3 with 4 at index 2 To delete, del lst[2] removal of 4 at index 2 To insert, lst.insert(1,3) insertion of 3 at index 1 To append, lst.append(4) appends 4 to end of list     Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

20 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

21 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

22 Tuples A tuple is an immutable linear data structure. Thus, in contrast to lists, once a tuple is defined it cannot be altered. Otherwise, tuples and lists are essentially the same. To distinguish tuples from lists, tuples are denoted by parentheses instead of square brackets as given below, nums = (10, 20, 30) student = ('John Smith', 48, 'Computer Science', 3.42) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

23 >>> (1,) >>> (1) (1) 1
Another difference of tuples and lists is that tuples of one element must include a comma following the element. Otherwise, the parenthesized element will not be made into a tuple, as shown below, CORRECT WRONG >>> (1,) >>> (1) (1) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

24 An empty tuple is represented by a set of empty parentheses, ().
The elements of tuples are accessed the same as lists, with square brackets, >>> nums[0] >>> student[0] 'John Smith' Any attempt to alter a tuple is invalid. Thus, delete, update, insert and append operations are not defined on tuples. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

25 Sequences A sequence in Python is a linearly-ordered set of elements accessed by index number. Lists, tuples and strings are all sequences. Strings, like tuples, are immutable, therefore they cannot be altered. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

26 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

27 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

28 Nested Lists Lists and tuples can contains elements of any type, including other sequences. Thus, lists and tuples can be nested to create arbitrarily complex data structures Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

29 This list stores three exam grades for each student.
class_grades[0] equals [85, 91, 89] class_grades[1] equals [78, 81, 86] To access the first exam grade of the first student in the list, student1_grades = class_grades[0]  [85, 91, 89] student1_exam1 = student1_grades[0]  85 OR class_grades[0][0]  85 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

30 To calculate the average on the first exam, a while loop can be constructed that iterates over the first grade of each student’s list of grades, sum = 0 k = 0 while k < len(class_grades): sum = sum + student_grades[k][0] k = k + 1 average_exam1 = sum / float(len(grades)) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

31 To produce a new list names exam_avgs containing the exam average for each student in the class,
exam_avgs = [] k = 0 while k < len(class_grades): avg = (student_grades[k][0] + \ student_grades[k][1] + \ student_grades[k][2]) / 3.0 exam_avgs.append(avg) k = k + 1 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

32 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

33 A Chinese Zodiac Program ➤ tuples ➤ datetime module
Let’s Apply It A Chinese Zodiac Program The following program determines the animal and associated characteristics from the Chinese Zodiac for a given year of birth. This program utilizes the following programming features: ➤ tuples ➤ datetime module Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

34 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

35 Line 3 imports the datetime module. It provides the current year
Line 3 imports the datetime module. It provides the current year. Lines 9-24 perform the initialization for the program. The set of characteristics of each animal is represented as a tuple for the twelve-year cycle of the zodiac . Variable terminate, is a Boolean flag used to quit the program. Lines display the program greeting. Lines read the year entered by the user. The while loop at line 38 ensures that the entered year is valid. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

36 The results are output on lines 45-47.
On line 43, the cycle_num for the individual is a value between 0-11, based on their year of birth. Since the year 1900 was the year of the rat in the Chinese zodiac, the determination of the cycle_num is (birth_year ) % 12. Lines 45 and 47 then use the cycle_num as an index into list zodiac_animals (to get the animal for that birth year) and list characteristics (to get the associated personal characteristics). Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

37 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

38 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

39 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

40 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

41 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

42 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

43 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.2 Lists (Sequences) in Python

44 Iterating over Lists (Sequences) in Python
Python’s for statement provides a convenient means of iterating over lists (and other sequences). Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

45 for Statements A for statement is an iterative control statement that iterates once for each element in a specified sequence of elements. For loops are used to construct definite loops. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

46 Variable k is referred to as a loop variable
Variable k is referred to as a loop variable. Since there are six elements in the provided list, the for loop iterates exactly six times. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

47 To contrast the use of for loops and while loops for list iteration, the same iteration is provided as a while loop below, In the while loop version, loop variable k must be initialized to 0 and incremented (by 1) each time through the loop. In the for loop version, however, loop variable k automatically iterates over the provided sequence of values. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

48 The for statement can be applied to all sequence types, including strings.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

49 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

50 The Built-In range Function
Python provides a built-in range function that can be used for generating a sequence of integers that a for loop can iterate over, sum = 0   for k in range(1, 11): sum = sum + k Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

51 sum = 0   for k in range(1, 11): sum = sum + k The values in the generated sequence include the starting value, up to but not including the ending value. For example, range(1, 11) generates the sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. Thus, this for loop adds up the integer values 1-10. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

52 The range function is convenient when long sequences of integers are needed.
(Actually, range does not create a sequence of integers. It creates a generator function able to produce each next item of the sequence. This saves memory, especially for long lists. Therefore, typing range(0, 9) in the Python shell does not produce a list as expected – it simply “echoes out” the call to range.) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

53 (containing a step value of 2)
By default, the range function generates a sequence of consecutive integers. A “step” value can be provided, range(0, 11, 2)  [0, 2, 4, 6, 8, 10], (containing a step value of 2) range(10, 0, -1)  [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]. (containing a step value of -1). Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

54 Iterating over List Elements vs. List Index Values
When the elements of a list need to be accessed, but not altered, a loop variable that iterates over each list element is an appropriate approach. However, there are times when the loop variable must iterate over the index values of a list instead. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

55 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

56 Suppose the average of a list of class grades named grades needs to be computed. In this case, a for loop can be constructed to iterate over the grades, for k in grades: sum = sum + k print('Class average is', sum/float(len(grades))) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

57 for k in range(len(grades)):
However, suppose that the instructor made a mistake in grading, and a point needed to be added to each student’s grade? In order to accomplish this, the index value (the location) of each element must be used to update each grade value. for k in range(len(grades)): grades[k] = grades[k] + 1 In this case, loop variable k is also functioning as an index variable. An index variable is a variable whose changing value is used to access elements of a sequence. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

58 The range function may be given only one argument
The range function may be given only one argument. In that case, the starting value of the range defaults to 0, thus, range(len(grades)) is equivalent to range(0,len(grades)) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

59 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

60 While Loops and Lists (Sequences)
There are situations in which a sequence is to be traversed until a given condition is true. In such cases, a while loop is the appropriate control structure. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

61 The loop continues until either the item is found, or the complete list has been traversed. The final if statement determines which of the two possibilities of ending the loop occurred, displaying either 'item found' or 'item not found'. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

62 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

63 Password Encryption/Decryption Program
Let’s Apply It Password Encryption/Decryption Program The following program allows a user to encrypt and decrypt passwords containing upper/lower case characters, digits and special characters. This program utilizes the following programming features: ➤ for loop ➤ nested sequences (tuples) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

64 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

65 Lines 4-8 perform initialization
Lines 4-8 perform initialization. Variable password_out holds the encrypted/decrypted password, initialized to the empty string, with each next translated character appended to it. Variable encryption_key holds the tuple (of tuples) used to encrypt/decrypt passwords: encryption_key = (('a', 'm'), ('b', 'h'), etc. In lines 15-19, variable encrypting is set to either True or False, based on user response. Lines performs the encryption/decryption. Variable case_changer (line 32) is set to the difference between the encoding of the lower and upper case letters . The substitution occurs in the nested for loops in lines Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

66 The substitution occurs in the nested for loops in lines 34-48.
The outer for loop iterates variable ch over each character in the entered password (to be encrypted or decrypted). The first step of the outer for loop is to initialize letter_found to False. This variable is used to determine if each given character is a (upper or lower case) letter. If a letter, it is replaced by its corresponding encoding character. If not, it must be a digit or special character, and thus appended as is (line 48). The code on lines and lines is similar. The only difference is that since the letters in the encryption key are all lower case, any upper case letters in the password need to be converted to lower case before being compared to the letters in the key. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

67 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

68 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

69 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

70 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

71 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.3 Iterating over Lists

72 The Assignment and Copying of Lists List Comprehensions
More on Python Lists The Assignment and Copying of Lists List Comprehensions Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

73 Assignment of Lists in Python
Because of the way that lists are represented in Python, when a variable is assigned to another variable holding a list, e.g., list2 = list1, each variable ends up referring to the same instance of the list in memory. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

74 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

75 This has important implications
This has important implications. For example, if an element of list1 is changed, then the corresponding element of list2 will change as well, >>> list1 = [10, 20, 30, 40] >>> list2 = list1 >>> list1[0] = 5 >>> list1 [5, 20, 30, 40] change made in list1 >>> list2 [5, 20, 30, 40] change in list1 caused by change in list2 This issue does not apply to strings and tuples, since they are immutable, and therefore cannot be modified. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

76 Copying of Lists in Python
When needed, a copy of a list can be made as given below, list3 = list(list1) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

77 In this case, we get the following results,
  >>> list1 = [10, 20, 30, 40] >>> list2 = list(list1) >>> list1[0] = 5 >>> list1 [5, 20, 30, 40] change made in list1 >>> list2 [10, 20, 30, 40] change in list1 does NOT cause change in list2 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

78 When copying lists that have sublists, another means of copying called deep copy may be needed.
We discuss this further in Chapter 6 when discussing objects in Python. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

79 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

80 List Comprehensions The range function allows for the generation of sequences of integers in fixed increments. List comprehensions in Python can be used to generate more varied sequences. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

81 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

82 Let’s Try It From the Python Shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.4 More on Python Lists

83 Calendar Year Program We look at the problem of displaying a given calendar year. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

84 Calendar Year The Problem
The problem is to display a calendar year for any year between 1800 and 2099, inclusive. The format of the month should be as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

85 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

86 Calendar Year Problem Analysis
The computational issues for this problem are similar to the calendar month program of Chapter 3. We need an algorithm for computing the first day of a given month for years However, since the complete year is being displayed, only the day of the week for January 1st of the given year needs be computed – the rest of the days follow from knowing the number of days in each month (including February for leap years). The algorithm developed to properly display a calendar month, however, is not relevant for this program. Instead, the information will first be stored in a data structure allowing for the months to be displayed three across. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

87 Calendar Year Program Design
Meeting the Program Requirements Data Desciption Algorithmic Approach Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

88 Meeting the Program Requirements
We will develop and implement an algorithm that displays the calendar year as given. There is no requirement of how the month and year are to be entered. We shall therefore request the user to enter the month name (e.g., ‘January’) and the four-digit year, with appropriate input error checking. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

89 Data Description The program needs to represent: the year entered
whether the year is a leap year the day of the week for January 1st of the year to display, the number of days in each month (accounting for leap years) The names of each of the twelve months will also be stored for error checking of user input, and for display in the calendar year. Given this information, the calendar year can be appropriately constructed and displayed. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

90 The list structure for the calendar year is given below,
We make use of nested lists for representing the calendar year. The calendar year will start out as an empty list and be built incrementally as each new calendar month is computed. The list structure for the calendar year is given below, calendar_year = [ [calendar_month], [calendar_month], etc.] ] calendar_month = [ week_1, week_2, …, week_k ] Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

91 Each italicized month is represented as a list of four to six strings, with each string storing a week of the month to be displayed, Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

92 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

93 The strings are formatted to contain all the spaces needed for proper alignment when displayed. For example, since the first week of May 2015 begins on a Friday, the string value for this week would be, Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

94 The complete data structure (with the details of January and May only shown) .
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

95 Algorithmic Approach Since the only date for which the day of the week needs to be determined is January 1 of a given year, the original day of the week algorithm can be simplified by removing variable day and replacing its occurrence with 1. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

96 The Overall Steps of the Program
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

97 Calendar Year Program Implementation
Stage 1— Determining the Day of the Week (for January 1st) Prompts user for month and year. Validates that value entered for month correct (1-12), and that year entered is in the range , inclusive. Determines whether the year is a leap year. Also determines the number of days in the month. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

98 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

99 Below is a sample test run of this stage of the program.
Stage 1 Testing Below is a sample test run of this stage of the program. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

100 A Set of Test Cases Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

101 Program Implementation
Stage 2 – Constructing the Calendar Year Data Structure Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

102 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

103 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

104 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

105 The program terminates with an error on line 54,
Stage 2 Testing The program terminates with an error on line 54, Enter year (yyyy) (-1 to quit): 2015 Traceback (most recent call last): File "C:\My Python Programs\CalendarYearStage2.py", line 54, in <module> month_name = month_names[month_num] IndexError: tuple index out of range Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

106 This line is within the for loop at line 53,
for month_num in range(12): month_name = month_names[month_num] For some reason, index variable month_num is out of range for tuple month_names. We look at the final value of month_num by typing the variable name into the Python shell, >>> month_num 11 Since months_names has index values 0-11 (of length 12), an index value of 11 should not be out of range. How, then, can this index out of range error happen? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

107 >> len(month_names) 11
Just to make sure that month_names has the right values, we display its length, >> len(month_names) 11 This is not right! Tuple month_names should contain all twelve months of the year. That is the way it was initialized on line 7, and tuples, unlike lists, cannot be altered, they are immutable. This does not seem to make sense. To continue our investigation, we display the value of tuple month_names. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

108 We need to look at the line of code that creates this tuple.
>> month_names ('January', 'February', 'March', 'April', 'May', 'JuneJuly', 'August', 'September', 'October', 'November', 'December') Now we see something that doesn’t look right. Months June and July are concatenated into one string value 'JuneJuly' making the length of the tuple 11, and not 12 (as we discovered). That would explain why the index out of range error occurred. What, then, is the problem? Why were the strings 'June' and 'July‘ concatenated? We need to look at the line of code that creates this tuple. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

109 month_names = ('January', 'February', 'March', 'April', 'May', 'June'
'July', 'August', 'September', 'October', 'November', 'December') It looks OK. Strings 'June' and 'July' were written as separate strings. We then decide to count the number of items in the tuple. Since items in tuples and lists are separated by commas, we count the number of items between the commas. We count the items up to 'May', which is five items as it should be, then 'June', which is six items … ah, there is no comma after the string 'June'! That must be why strings 'June' and 'July' were concatenated, and thus the source of the index out of range error. We try and reproduce this in the shell, >>> 'June' 'July' 'JuneJuly' Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

110 Enter year (yyyy) (-1 to quit): 2015
[[' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ']] Since the calendar_year structure looks correct, we now develop the final stage of the program that displays the complete calendar year. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

111 Program Implementation
Stage 3 – Displaying the Calendar Year Data Structure Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

112 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

113 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

114 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

115 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

116 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program

117 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 4.5 Calendar Year Program


Download ppt "Chapter 4 Lists In this chapter, we look at a means of structuring and accessing a collection of data. In particular, we look at a way of organizing."

Similar presentations


Ads by Google