The __________ method returns true if the string contains only numeric digits.

In this example, we iterate over the elements (chars) of the String, and we pass each char to isDigit() method from the Character class. The isDigit() method returns true if provided char is a digit, otherwise returns false.

Show

Example

class Test {

  public static void main(String[] args) {
    String str = "129843875";
    boolean containsOnlyDigits = true;

    for (int i = 0; i < str.length(); i++) {
      if (!Character.isDigit(str.charAt(i))) { // in case that a char is NOT a digit, enter to the if code block
        containsOnlyDigits = false;
        break; // break the loop, since we found that this char is not a digit
      }
    }

    if (containsOnlyDigits) {
      System.out.println("String contains only digits!");
    } else {
      System.out.println("String does not contain only digits!");
    }
  }
}

Output: String contains only digits!

Check for digits in a String using the Integer.parseInt() method

There is an easy way to determine if a String contains only digits without iterating over its chars.  We can do that by passing the String as an argument to the parseInt() method from the Integer class. This method throws a NumberFormatException if the String does not contain a parsable integer.

Example

class Test {

  public static void main(String[] args) {
    String str1 = "129843875";
    String str2 = "1234B";

    try {
      Integer.parseInt(str1);
      System.out.println("str1 contains only digits.");
    } catch (NumberFormatException nfe) {
      System.out.println("str1 does not contains only digits!");
    }

    try {
      Integer.parseInt(str2);
      System.out.println("str2 contains only digits.");
    } catch (NumberFormatException nfe) {
      System.out.println("str2 does not contains only digits!");
    }
  }
}

Output: str1 contains only digits. str2 does not contains only digits!

 

Here, if we get an exception in the try block, the catch block will be executed which means that the provided string does not contain only numbers.

Last chapter we introduced Python’s built-in types

>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
0,
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
1, and
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
2, and we stumbled upon
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
3.

Integers and floats are numeric types, which means they hold numbers. We can use the numeric operators we saw last chapter with them to form numeric expressions. The Python interpreter can then evaluate these expressions to produce numeric values, making Python a very powerful calculator.

Strings, lists, and tuples are all sequence types, so called because they behave like a sequence - an ordered collection of objects.

Squence types are qualitatively different from numeric types because they are compound data types - meaning they are made up of smaller pieces. In the case of strings, they’re made up of smaller strings, each containing one character. There is also the empty string, containing no characters at all.

In the case of lists or tuples, they are made up of elements, which are values of any Python datatype, including other lists and tuples.

Lists are enclosed in square brackets (

>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
4 and
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
5) and tuples in parentheses (
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
6 and
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
7).

A list containing no elements is called an empty list, and a tuple with no elements is an empty tuple.

[10, 20, 30, 40, 50]
["spam", "bungee", "swallow"]
(2, 4, 6, 8)
("two", "four", "six", "eight")
[("cheese", "queso"), ("red", "rojo"), ("school", "escuela")]

The first example is a list of five integers, and the next is a list of three strings. The third is a tuple containing four integers, followed by a tuple containing four strings. The last is a list containing three tuples, each of which contains a pair of strings.

Depending on what we are doing, we may want to treat a compound data type as a single thing, or we may want to access its parts. This ambiguity is useful.

Note

It is possible to drop the parentheses when specifiying a tuple, and only use a comma seperated list of values:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)

Also, it is required to include a comma when specifying a tuple with only one element:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>

Except for the case of the empty tuple, it is really the commas, not the parentheses, that tell Python it is a tuple.

3.2. Working with the parts of a sequence

The sequence types share a common set of operations.

3.2.1. Indexing

The indexing operator (

>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
4
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
5) selects a single element from a sequence. The expression inside brackets is called the index, and must be an integer value. The index indicates which element to select, hence its name.

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')

The expression

>>> last = seq[len(seq) - 1]
0 selects the character with index
>>> last = seq[len(seq) - 1]
1 from
>>> last = seq[len(seq) - 1]
2, and creates a new string containing just this one character, which you may be surprised to see is
>>> last = seq[len(seq) - 1]
3.

You probably expected to see

>>> last = seq[len(seq) - 1]
4, but computer scientists typically start counting from zero, not one. Think of the index as the numbers on a ruler measuring how many elements you have moved into the sequence from the beginning. Both rulers and indices start at
>>> last = seq[len(seq) - 1]
5.

3.2.2. Length

Last chapter you saw the

>>> last = seq[len(seq) - 1]
6 function used to get the number of characters in a string:

>>> len('banana')
6

With lists and tuples,

>>> last = seq[len(seq) - 1]
6 returns the number of elements in the sequence:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3

3.2.3. Accessing elements at the end of a sequence

It is common in computer programming to need to access elements at the end of a sequence. Now that you have seen the

>>> last = seq[len(seq) - 1]
6 function, you might be tempted to try something like this:

>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!

That won’t work. It causes the runtime error

>>> last = seq[len(seq) - 1]
9. The reason is that
>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'
0 returns the number of elements in the list, 16, but there is no element at index position 16 in
>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'
1.

Since we started counting at zero, the sixteen indices are numbered 0 to 15. To get the last element, we have to subtract 1 from the length:

>>> last = seq[len(seq) - 1]

This is such a common in pattern that Python provides a short hand notation for it, negative indexing, which counts backward from the end of the sequence.

The expression

>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'
2 yields the last element,
>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'
3 yields the second to last, and so on.

>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'

3.2.4. Traversal and the >>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] >>> prime_nums[-2] 29 >>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter") >>> classmates[-5] 'Ed' >>> word = "Alphabet" >>> word[-3] 'b' 4 loop

A lot of computations involve processing a sequence one element at a time. The most common pattern is to start at the beginning, select each element in turn, do something to it, and continue until the end. This pattern of processing is called a traversal.

Python’s

>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'
4 loop makes traversal easy to express:

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)

Note

We will discuss looping in greater detail in the next chapter. For now just note that the colon (:) at the end of the first line and the indentation on the second line are both required for this statement to be syntactically correct.

3.3. >>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] >>> prime_nums[-2] 29 >>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter") >>> classmates[-5] 'Ed' >>> word = "Alphabet" >>> word[-3] 'b' 6

As the standard

>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'
4 loop traverses a sequence, it assigns each value in the sequence to the loop variable in the order it occurs in the sequence. Sometimes it is helpful to have both the value and the index of each element. The
>>> prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_nums[-2]
29
>>> classmates = ("Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter")
>>> classmates[-5]
'Ed'
>>> word = "Alphabet"
>>> word[-3]
'b'
6 function gives us this:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
0

3.3.1. Slices

A subsequence of a sequence is called a slice and the operation that extracts a subsequence is called slicing. Like with indexing, we use square brackets (

>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
4
>>> seq = [1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0]
>>> last = seq[len(seq)]       # ERROR!
5) as the slice operator, but instead of one integer value inside we have two, seperated by a colon (
prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
1):

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
1

The operator

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
2 returns the part of the sequence from the n’th element to the m’th element, including the first but excluding the last. This behavior is counter-intuitive; it makes more sense if you imagine the indices pointing between the characters, as in the following diagram:

The __________ method returns true if the string contains only numeric digits.

If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string. Thus:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
2

What do you think

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
3 means? What about
prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
4?

Negative indexes are also allowed, so

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
3

Tip

Developing a firm understanding of how slicing works is important. Keep creating your own “experiments” with sequences and slices until you can consistently predict the result of a slicing operation before you run it.

When you slice a sequence, the resulting subsequence always has the same type as the sequence from which it was derived. This is not generally true with indexing, except in the case of strings.

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
4

While the elements of a list (or tuple) can be of any type, no matter how you slice it, a slice of a list is a list.

3.3.2. The prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] for num in prime_nums: print(num ** 2) 5 operator

The

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
5 operator returns whether a given element is contained in a list or tuple:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
5

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
5 works somewhat differently with strings. It evaluates to
prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
8 if one string is a substring of another:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
6

Note that a string is a substring of itself, and the empty string is a substring of any other string. (Also note that computer programmers like to think about these edge cases quite carefully!)

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
7

3.4. Objects and methods

Strings, lists, and tuples are objects, which means that they not only hold values, but have built-in behaviors called methods, that act on the values in the object.

Let’s look at some string methods in action to see how this works.

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
8

Now let’s learn to describe what we just saw. Each string in the above examples is followed by a dot operator, a method name, and a parameter list, which may be empty.

In the first example, the string

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
9 is followed by the dot operator and then the
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
00 method, which has an empty parameter list. We say that the “
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
00 method is invoked on the string,
prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
9. Invoking the method causes an action to take place using the value on which the method is invoked. The action produces a result, in this case the string value
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
03. We say that the
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
00 method returns the string
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
03 when it is invoked on (or called on) the string
prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
9.

In the fourth example, the method

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
07 (again with an empty parameter list) is invoked on the string
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
08. Since each of the characters in the string represents a digit, the
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
07 method returns the boolean value
prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
8. Invoking
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
07 on
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
12 produces
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
13.

The

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
14 removes leading and trailing whitespace.

3.5. The >>> thing = 2, 4, 6, 8 >>> type(thing) <class 'tuple'> >>> thing (2, 4, 6, 8) 15 function and docstrings

The previous section introduced several of the methods of string objects. To find all the methods that strings have, we can use Python’s built-in

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
16 function:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
9

We will postpone talking about the ones that begin with double underscores (

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
17) until later. You can find out more about each of these methods by printing out their docstrings. To find out what the
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
18 method does, for example, we do this:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
0

Using this information, we can try using the replace method to varify that we know how it works.

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
1

The first example replaces all occurances of

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
19 with
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
20. The second replaces the single character
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
21 with the two characters
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
22. The third example replaces the first two occurances of
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
23 with the empty string.

3.6. >>> thing = 2, 4, 6, 8 >>> type(thing) <class 'tuple'> >>> thing (2, 4, 6, 8) 24 and >>> thing = 2, 4, 6, 8 >>> type(thing) <class 'tuple'> >>> thing (2, 4, 6, 8) 25 methods

There are two methods that are common to all three sequence types:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
24 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
25. Let’s look at their docstrings to see what they do.

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
2

We will explore these functions in the exercises.

3.7. Lists are mutable

Unlike strings and tuples, which are immutable objects, lists are mutable, which means we can change their elements. Using the bracket operator on the left side of an assignment, we can update one of the elements:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
3

The bracket operator applied to a list can appear anywhere in an expression. When it appears on the left side of an assignment, it changes one of the elements in the list, so the first element of

>>> last = seq[len(seq) - 1]
2 has been changed from
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
29 to
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
30, and the last from
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
31 to
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
32. An assignment to an element of a list is called item assignment. Item assignment does not work for strings:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
4

but it does for lists:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
5

With the slice operator we can update several elements at once:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
6

We can also remove elements from a list by assigning the empty list to them:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
7

And we can add elements to a list by squeezing them into an empty slice at the desired location:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
8

3.8. List deletion

Using slices to delete list elements can be awkward, and therefore error-prone. Python provides an alternative that is more readable.

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
33 removes an element from a list:

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
9

As you might expect,

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
33 handles negative indices and causes a runtime error if the index is out of range.

You can use a slice as an index for

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
33:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
0

As usual, slices select all the elements up to, but not including, the second index.

3.9. List methods

In addition to

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
24 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
25, lists have several useful methods. Since lists are mutable, these methods modify the list on which they are invoked, rather than returning a new list.

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
1

The

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
38 method is particularly useful, since it makes it easy to use Python to sort data that you have put in a list.

3.10. Names and mutable values

If we execute these assignment statements,

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
2

we know that the names

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
39 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
40 will refer to a list with the numbers
>>> last = seq[len(seq) - 1]
1,
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
42, and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
43. But we don’t know yet whether they point to the same list.

There are two possible states:

The __________ method returns true if the string contains only numeric digits.

or

The __________ method returns true if the string contains only numeric digits.

In one case,

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
39 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
40 refer to two different things that have the same value. In the second case, they refer to the same object.

We can test whether two names have the same value using

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
46:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
3

We can test whether two names refer to the same object using the is operator:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
4

This tells us that both

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
39 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
40 do not refer to the same object, and that it is the first of the two state diagrams that describes the relationship.

3.11. Aliasing

Since variables refer to objects, if we assign one variable to another, both variables refer to the same object:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
5

In this case, it is the second of the two state diagrams that describes the relationship between the variables.

Because the same list has two different names,

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
39 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
40, we say that it is aliased. Since lists are mutable, changes made with one alias affect the other:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
6

Although this behavior can be useful, it is sometimes unexpected or undesirable. In general, it is safer to avoid aliasing when you are working with mutable objects. Of course, for immutable objects, there’s no problem, since they can’t be changed after they are created.

3.12. Cloning lists

If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not just the reference. This process is sometimes called cloning, to avoid the ambiguity of the word copy.

The easiest way to clone a list is to use the slice operator:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
7

Taking any slice of

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
39 creates a new list. In this case the slice happens to consist of the whole list.

Now we are free to make changes to

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
40 without worrying about
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
39:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
8

3.13. Nested lists

A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list:

>>> fruit = "banana"
>>> fruit[1]
'a'
>>> fruits = ['apples', 'cherries', 'pears']
>>> fruits[0]
'apples'
>>> prices = (3.99, 6.00, 10.00, 5.25)
>>> prices[3]
5.25
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> pairs[2]
('school', 'escuela')
9

If we print

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
54, we get
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
55. To extract an element from the nested list, we can proceed in two steps:

>>> len('banana')
6
0

Or we can combine them:

>>> len('banana')
6
1

Bracket operators evaluate from left to right, so this expression gets the three-eth element of

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
56 and extracts the one-eth element from it.

3.14. Strings and lists

Python has several tools which combine lists of strings into strings and separate strings into lists of strings.

The

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
57 command takes a sequence type as an argument and creates a list out of its elements. When applied to a string, you get a list of characters.

>>> len('banana')
6
2

The

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
58 method invoked on a string and separates the string into a list of strings, breaking it apart whenever a substring called the delimiter occurs. The default delimiter is whitespace, which includes spaces, tabs, and newlines.

>>> len('banana')
6
3

Here we have

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
59 as the delimiter.

>>> len('banana')
6
4

Notice that the delimiter doesn’t appear in the list.

The

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
60 method does approximately the oposite of the
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
58 method. It takes a list of strings as an argument and returns a string of all the list elements joined together.

>>> len('banana')
6
5

The string value on which the

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
60 method is invoked acts as a separator that gets placed between each element in the list in the returned string.

>>> len('banana')
6
6

The separator can also be the empty string.

>>> len('banana')
6
7

3.15. Tuple assignment

Once in a while, it is useful to swap the values of two variables. With conventional assignment statements, we have to use a temporary variable. For example, to swap

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
39 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
40:

>>> len('banana')
6
8

If we have to do this often, this approach becomes cumbersome. Python provides a form of tuple assignment that solves this problem neatly:

>>> len('banana')
6
9

The left side is a tuple of variables; the right side is a tuple of values. Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments. This feature makes tuple assignment quite versatile.

Naturally, the number of variables on the left and the number of values on the right have to be the same:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
0

3.16. Boolean values

We will now look at a new type of value - boolean values - named after the British mathematician, George Boole. He created the mathematics we call Boolean algebra, which is the basis of all modern computer arithmetic.

Note

It is a computer’s ability to alter its flow of execution depending on whether a boolean value is true or false that makes a general purpose computer more than just a calculator.

There are only two boolean values,

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
8 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
13.

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
1

Capitalization is important, since

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
67 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
68 are not boolean values in Python.:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
2

3.17. Boolean expressions

A boolean expression is an expression that evaluates to a boolean value.

The operator

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
46 compares two values and produces a boolean value:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
3

In the first statement, the two operands are equal, so the expression evaluates to

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
8; in the second statement, 5 is not equal to 6, so we get
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
13.

The

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
46 operator is one of six common comparison operators; the others are:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
4

Although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols. A common error is to use a single equal sign (

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
73) instead of a double equal sign (
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
46). Remember that
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
73 is an assignment operator and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
46 is a comparison operator. Also, there is no such thing as
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
77 or
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
78.

3.18. Logical operators

There are three logical operators:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
79,
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
80, and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
81. The semantics (meaning) of these operators is similar to their meaning in English. For example,
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
82 is true only if
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
83 is greater than 0 and at the same time, x is less than 10.

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
84 is true if either of the conditions is true, that is, if the number is divisible by 2 or divisible by 3.

Finally, the

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
81 operator negates a boolean expression, so
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
86 is true if
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
87 is false, that is, if
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
83 is less than or equal to
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
89.

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
5

3.19. Short-circuit evaluation

Boolean expressions in Python use short-circuit evaluation, which means only the first argument of an

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
79 or
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
80 expression is evaluated when its value is suffient to determine the value of the entire expression.

This can be quite useful in preventing runtime errors. Imagine you want check if the fifth number in a tuple of integers named

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
92 is even.

The following expression will work:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
6

unless of course there are not 5 elements in

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
92, in which case you will get:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
7

Short-circuit evaluation makes it possible to avoid this problem.

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
8

Since the left hand side of this

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
79 expression is false, Python does not need to evaluate the right hand side to determine that the whole expression is false. Since it uses short-circuit evaluation, it does not, and the runtime error is avoided.

3.20. “Truthiness”

All Python values have a “truthiness” or “falsiness” which means they can be used in places requiring a boolean. For the numeric and sequence types we have seen thus far, truthiness is defined as follows:

numberic types

Values equal to 0 are false, all others are true.

sequence types

Empty sequences are false, non-empty sequences are true.

Combining this notion of truthiness with an understanding of short-circuit evaluation makes it possible to understand what Python is doing in the following expressions:

>>> len(['a', 'b', 'c', 'd'])
4
>>> len((2, 4, 6, 8, 10, 12))
6
>>> pairs = [('cheese', 'queso'), ('red', 'rojo'), ('school', 'escuela')]
>>> len(pairs)
3
9

3.21. Glossary

aliases

Multiple variables that contain references to the same object.

boolean value

There are exactly two boolean values:

prime_nums = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

for num in prime_nums:
    print(num ** 2)
8 and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
13. Boolean values result when a boolean expression is evaluated by the Python interepreter. They have type
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
97.

boolean expression

An expression that is either true or false.

clone

To create a new object that has the same value as an existing object. Copying a reference to an object creates an alias but doesn’t clone the object.

comparison operator

One of the operators that compares two values:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
46,
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
99,
>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
00,
>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
01,
>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
02, and
>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
03.

compound data type

A data type in which the values are made up of components, or elements, that are themselves values.

element

One of the parts that make up a sequence type (string, list, or tuple). Elements have a value and an index. The value is accessed by using the index operator (

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
04) on the sequence.

immutable data type

A data type which cannot be modified. Assignments to elements or slices of immutable types cause a runtime error.

index

A variable or value used to select a member of an ordered collection, such as a character from a string, or an element from a list or tuple.

logical operator

One of the operators that combines boolean expressions:

>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
79,
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
80, and
>>> thing = 2, 4, 6, 8
>>> type(thing)
<class 'tuple'>
>>> thing
(2, 4, 6, 8)
81.

mutable data type

A data type which can be modified. All mutable types are compound types. Lists and dictionaries are mutable data types; strings and tuples are not.

nested list

A list that is an element of another list.

slice

A part of a string (substring) specified by a range of indices. More generally, a subsequence of any sequence type in Python can be created using the slice operator (

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
08).

step size

The interval between successive elements of a linear sequence. The third (and optional argument) to the

>>> singleton = (2,)
>>> type(singleton)
<class 'tuple'>
>>> not_tuple = (2)
>>> type(not_tuple)
<class 'int'>
>>> empty_tuple = ()
>>> type(empty_tuple)
<class 'tuple'>
09 function is called the step size. If not specified, it defaults to 1.

traverse

To iterate through the elements of a collection, performing a similar operation on each.

tuple

A data type that contains a sequence of elements of any type, like a list, but is immutable. Tuples can be used wherever an immutable type is required, such as a key in a dictionary (see next chapter).

tuple assignment

An assignment to all of the elements in a tuple using a single assignment statement. Tuple assignment occurs in parallel rather than in sequence, making it useful for swapping values.

What method returns true if the string contains only numeric digits?

Check if String Contains Only Numbers using isnumeric() Python String isnumeric() method returns “True” if all characters in the string are numeric characters, otherwise returns “False”.

How to check if a string contains only digits in C?

You can use the isdigit() macro to check if a character is a number. Using this, you can easily write a function that checks a string for containing numbers only. Subminiature stylistic sidenote: returns true for the empty string.

How to check if a string contains a numeric value in Java?

Perhaps the easiest and the most reliable way to check whether a String is numeric or not is by parsing it using Java's built-in methods:.
Integer. parseInt(String).
Float. parseFloat(String).
Double. parseDouble(String).
Long. parseLong(String).
new BigInteger(String).