Computer Science
Python Sequence
Python sequence refers to a collection of ordered and indexed elements that can be of different data types such as integers, strings, and lists. It includes various built-in functions and methods that allow manipulation and iteration over the sequence. Python provides three types of sequences: lists, tuples, and range objects.
Written by Perlego with AI-assistance
Related key terms
1 of 5
5 Key excerpts on "Python Sequence"
- No longer available |Learn more
Python 3
Pocket Primer
- James R. Parker(Author)
- 2017(Publication Date)
- Mercury Learning and Information(Publisher)
C H A P T E R SEQUENCES: STRINGS, TUPLES, AND LISTS 3 I t was mentioned in Chapter 2 that for loops in Python are different from those found in many other languages in that they use a tuple to define the values that will be assigned to the control variable. Tuples are useful in many situations, and are only one example of a wider range of data types that includes strings, tuples, and lists as objects that consist of multiple parts. They are called sequence types. An integer or a float is a single number, whereas a sequence type consists of a collection of items, each of which is a number or a character. Each member of a sequence is given a number based on its position: the first element in the sequence is given 0, the second is 1, and so on. This is a fundamental data structure in Python and has influenced the syntax of the language. Strings A string is a sequence of characters. The word sequence implies that the order of the characters within the string matters, and that is certainly true. Strings most often represent the way that communication between a computer and a human takes place. The order of the characters within a word matters a great deal to a human because some sequences are words and others are not. The string “last” is a word, but “astl” is not. Also, the strings “salt” and “slat” are words and use exactly the same characters as “last” but in a different order. Because order matters, the representation of a string on a computer will impose an order on the characters within, and so there will be a first character, a second, and so on, and it should be possible to access each 36 • PYTHON 3 POCKET PRIMER character individually. A string will also have a length, which is the number of characters within it. A computer language will provide specific things that can be done to something that is a string: these are called operations, and a type is defined at least partly by what operations can be done to something of that type. - Jose M. Garrido(Author)
- 2015(Publication Date)
- Chapman and Hall/CRC(Publisher)
III Data Structures, Object Orientation, and Recursion 99 C H A P T E R 7 Python Lists, Strings, and Other Data Sequences 7.1 INTRODUCTION Most programming languages support arrays , which are one the most funda-mental data structures. With an array, multiple values can be stored and each is referenced with the name of the array and specifying an index value. The individual values of an array are known as elements . Python uses lists , which are more general data structures that can be used to represent arrays. This chapter discusses lists and other data sequences used in Python. In a subsequent chapter, the extensive array handling operations and objects in the NumPy library will be discussed. 7.2 LISTS A list in Python is simply an ordered collection of items each of which can be of any type. A list is a dynamic mutable data structure and this means that items can be added to and deleted from it. The list data structure is the most common data sequence in Python. A sequence is a set of values identified by integer indices. To define a list in Python, the items are separated by commas and in square brackets. A simple list with name vv and n items is defined as follows: vv = [ p 1 , p 2 , p 3 , . . . , p n ] . For example, the command that follows defines a list with name vals and six data items: vals = [1,2,3,4,5,6] 101 102 squaresolid Introduction to Computational Models with Python 7.2.1 Indexing Lists An individual item in the list can be referenced by using an index, which is an integer number that indicates the relative position of the item in the list. The values of index numbers always start at zero. In the list vals defined previously, the index values are: 0 , 1 , 2 , 3 , 4, and 5. In Python, the reference to an individual item of a list is written with the name of the list and the index value or an index variable within brackets.- No longer available |Learn more
Python 3
Pocket Primer
- James R. Parker(Author)
- 2017(Publication Date)
- Mercury Learning and Information(Publisher)
CHAPTER 3SEQUENCES : STRINGS , TUPLES , AND LISTSI t was mentioned in Chapter 2 that for loops in Python are different from those found in many other languages in that they use a tuple to define the values that will be assigned to the control variable. Tuples are useful in many situations, and are only one example of a wider range of data types that includes strings, tuples, and lists as objects that consist of multiple parts. They are called sequence types. An integer or a float is a single number, whereas a sequence type consists of a collection of items, each of which is a number or a character. Each member of a sequence is given a number based on its position: the first element in the sequence is given 0, the second is 1, and so on. This is a fundamental data structure in Python and has influenced the syntax of the language.StringsA string is a sequence of characters. The word sequence implies that the order of the characters within the string matters, and that is certainly true. Strings most often represent the way that communication between a computer and a human takes place. The order of the characters within a word matters a great deal to a human because some sequences are words and others are not. The string “last” is a word, but “astl” is not. Also, the strings “salt” and “slat” are words and use exactly the same characters as “last” but in a different order.Because order matters, the representation of a string on a computer will impose an order on the characters within, and so there will be a first character, a second, and so on, and it should be possible to access each character individually. A string will also have a length - eBook - PDF
- Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser(Authors)
- 2013(Publication Date)
- Wiley(Publisher)
Instead, Python represents a list or tuple instance using an internal storage mechanism of an array of object references. At the lowest level, what is stored is a consecutive sequence of memory addresses at which the elements of the se- quence reside. A high-level diagram of such a list is shown in Figure 5.4. 0 3 1 2 5 4 Rene Virginia Joseph Helen Jonas Janet Figure 5.4: An array storing references to strings. Although the relative size of the individual elements may vary, the number of bits used to store the memory address of each element is fixed (e.g., 64-bits per address). In this way, Python can support constant-time access to a list or tuple element based on its index. In Figure 5.4, we characterize a list of strings that are the names of the patients in a hospital. It is more likely that a medical information system would manage more comprehensive information on each patient, perhaps represented as an in- stance of a Patient class. From the perspective of the list implementation, the same principle applies: The list will simply keep a sequence of references to those ob- jects. Note as well that a reference to the None object can be used as an element of the list to represent an empty bed in the hospital. 188 Chapter 5. Array-Based Sequences The fact that lists and tuples are referential structures is significant to the se- mantics of these classes. A single list instance may include multiple references to the same object as elements of the list, and it is possible for a single object to be an element of two or more lists, as those lists simply store references back to that object. As an example, when you compute a slice of a list, the result is a new list instance, but that new list has references to the same elements that are in the original list, as portrayed in Figure 5.5. 3 4 5 6 7 0 1 2 0 1 2 primes: temp: 3 11 5 2 19 17 13 7 Figure 5.5: The result of the command temp = primes[3:6]. - No longer available |Learn more
Python
Pocket Primer
- Oswald Campesato(Author)
- 2014(Publication Date)
- Mercury Learning and Information(Publisher)
For example, you can modify the code in the preceding section to display the alphabetically sorted words and their associated word count. 78 • Python Pocket Primer Python Multi-Dictionaries You can define entries in a Python dictionary so that they reference lists or other types of Python structures. Listing 3.9 displays the contents of Multi- Dictionary1.py that illustrates how to define more complex dictionaries. LISTING 3.9: MultiDictionary1.py from collections import defaultdict d = {'a' : [1, 2, 3], 'b' : [4, 5]} print 'firsts:',d d = defaultdict(list) d['a'].append(1) d['a'].append(2) d['b'].append(4) print 'second:',d d = defaultdict(set) d['a'].add(1) d['a'].add(2) d['b'].add(4) print 'third:',d Listing 3.9 starts by defining the dictionary d and printing its contents. The next portion of Listing 3.9 specifies a list-oriented dictionary and then modifies the values for the keys a and b. The final portion of Listing 3.9 specifies a set- oriented dictionary and then modifies the values for the keys a and b as well. The output of Listing 3.9 is here: first: {'a': [1, 2, 3], 'b': [4, 5]} second: defaultdict(, {'a': [1, 2], 'b': [4]}) third: defaultdict(>> for i, v in enumerate(['x', 'y', 'z']): ... print i, v ... 0 x 1 y 2 z Python Collections • 79 Bytearray objects are created with the built-in function bytear- ray(). Although buffer objects are not directly supported by Python syntax, you can create them via the built-in buffer() function. Objects of type xrange are created with the xrange() function.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.




