Python Conditional Statements

 3: Conditional Statements

3.1 INTRODUCTION

3.2 IF, IF-ELSE, AND IF-ELIF-ELSE CONSTRUCTS

3.3 THE IF-ELIF-ELSE LADDER

3.4 LOGICAL OPERATORS

3.5 THE TERNARY OPERATOR

3.6 THE GET CONSTRUCT

3.7 EXAMPLES

3.8 POINTS TO REMEMBER

3.9 EXERCISES

3.10 PROGRAMMING EXERCISE


3.1 INTRODUCTION

The preceding chapters presented the basic data types and simple statements in Python. The concepts studied so far are good for the execution of a program which has no branches. However, a programmer would seldom find a problem solving approach devoid of branches.

Before proceeding any further let us spare some time contemplating life. Can you move forward in life without making decisions? The answer is NO. In the same way the problem solving approach would not yield results until the power of decision making is incorporated. This is the reason one must understand the implementation of decision making and looping. This chapter describes the first concept. This is needed to craft a program which has branches. Decision making empowers us to change the control-flow of the program. In C, C++, Java, C#, etc., there are two major ways to accomplish the above task. One is the ‘if’ construct and the other is ‘switch’. The ‘if’ block in a program is executed if the ‘test’ condition is true otherwise it is not executed. Switch is used to implement a scenario in which there are many ‘test ’ conditions, and the corresponding block executes in case a particular test condition is true.

The chapter introduces the concept of conditional statements, compound statements, the if-elif ladder and finally the get  statement. The chapter assumes importance as conditional statements are used in every aspect of programming, be it client side development, web development, or mobile application development.

The chapter has been organized as follows. The second section introduces the ‘if’ construct. Section 3.3 introduces ‘if-elif’ ladder. Section 3.4 discusses the use of logic operators. Section 3.5 introduces ternary operators. Section 3.6 presents the get  statement and the last section concludes the chapter. The reader is advised to go through the basic data types before proceeding further.

 

3.2 IFIF-ELSE, AND IF-ELIF-ELSE  CONSTRUCTS

Implementing decision making gives the power to incorporate branching in a program. As stated earlier, a program is a set of instructions given to a computer. The instructions are given to accomplish a task and any task requires making decisions. So, conditional statements form an integral part of programming. The syntax of the construct is as follows:

General Format
1.      if

2.   

3.  if <test condition>:

4.     <block if the test condition is true>

5.      if-else

6.   

7.  if <test condition>:

8.     <block if the test condition is true>

9.  else:

10.    <block if the test condition is not true>

11. ...

12. If else ladder (discussed in the next section)

13. 

14. if <test condition>:

15.    <block if the test condition is true>

16. elif <test 2>:

17.    <second block>

18. elif <test 3>:

19.    <third block>

20. else:

21.    <block if the test condition is true>

Note that indentation is important, as Python recognizes a block through indentation. So, make sure that the 'if (<condition>):'  is followed by a block, each statement of which is at the same alignment. In order to understand the concept, let us consider a simple example. A student generally clears a university exam in India if he scores more than 40%. In order to implement the logic, the user is asked to enter the value of the percentage. If the percentage entered is more than 40 then “exam cleared” is printed, otherwise “failed” is printed. The situation has been depicted in the following figure (Figure 3.1).

Figure 3.1: Flow chart for example 1



Illustration 3.1

Ask the user to enter the marks of a student in a subject. If the marks entered are greater than 40 then print “pass,” if they are lower print “fail.”

Program

>>>a = input("Enter marks : ")

if int(a)> 40:

    print('Pass')

else:

    print('Fail')

...

Output 1

Enter Marks : 50

    Pass

Output 2

Enter Marks : 30

    Fail


Let us have a look at another example. In the problem, the user is asked to enter a three digit number to find the number obtained by reversing the order of the digits of the number; then find the sum of the number and that obtained by reversing the order of the digits and finally, find whether this sum contains any digit in the original number. In order to accomplish the task, the following steps (presented in Illustration 3.2) must be carried out.

Illustration 3.2

Ask the user to enter a three digit number. Call it 'num' . Find the number obtained by reversing the order of the digits. Find the sum of the given number and that obtained by reversing the order of the digits. Finally, find if any digit in the sum obtained is the same as that in the original number.

Solution:

The problem can be solved as follows:

·         When the user enters a number, check whether it is between 100 and 999, both inclusive.

·         Find the digits at unit’s, ten’s and hundred’s place. Call them 'u', 't' and 'h'  respectively.

·         Find the number obtained by reversing the order of the digits (say, ‘rev’) using the following formula.

·         Number obtained by reversing the order of the digits, rev = h + t × 10 + u × 100

·         Find the sum of the two numbers.
Sum = rev + num

·         The sum may be a three digit or a four digit number. In any case, find the digits of this sum. Call them 'u1', 't1', 'h1' and 'th1'  (if required).

·         Set 'flag=0' .

·         Check the following condition. If any one is true, set the value of flag to 1. If “sum” is a three digit number
u = = u1
u = = t1
u = = h1
t = = u1
t = = t1
t = = h1
h = = u1
h = = t1
h = = h1

·         If “sum” is a four digit number the above conditions need to be checked along with the following conditions:
u = = th1
h = = th1
t = = th1

·         The above conditions would henceforth be referred to as “set 1.” If the value of “flag” is 1, then print 'true' else print 'false' .

·         The above process has been depicted in the following figure (Figure 3.2).

Figure 3.2: Flow chart for Illustration 2

Program

 

num=int(input('Enter a three digit number\t:'))

if ((num<100) | (num>999)):

    print('You have not entered a number between 100 and 999')

else:

    flag=0

    o=num%10

    t=int(num/10)%10

    h=int(num/100)%10

    print('o\t:',str(o),'t\t:',str(t),'h\t:',str(h))

    rev=h+t*10+o*100

    print('Number obtained by reversing the order of the

                                     digits\t:',str(rev))

    sum1=num+rev

    print('Sum of the number and that obtained by

             reversing the order of digits\t:',str(sum1))

    if sum1<1000:

    o1=sum1%10

     t1=int(sum1/10)%10

     h1=int(sum1/100)%10

    print('o1\t:',str(o1),'t1\t:',str(t1),'h1\t:',str(h1))

    if ((o==o1)|(o==t1)|(o==h1)|(t==o1)|(t==t1)|

                        (t==h1)|(h==o1)|(h==t1)|(h==h1)):

     print('Condition true')

     flag==1

    else:

     o1=sum1%10

     t1=int(sum1/10)%10

     h1=int(sum1/100)%10

     th1=int(sum1/1000)%10

     print('o1\t:',str(o1),'t1\t:',str(t1),'h1\

                             t:',str(h1),'t1\t:',str(t1))

    if ((o==o1)|(o==t1)|(o==h1)|(o==th1)|(t==o1)|(t==t1)|

         (t==h1)|(t==th1)|(h==o1)|(h==t1)|(h==h1)|(h==th1)):

     print('Condition true')

     flag==1

Output: First run

>>>  

========= RUN C:/Python/Conditional/Problem 2.py =========

Enter a three digit number :4

You have not entered a number between 100 and 999

>>>  

Output: Second run

>>>  

========= RUN C:/Python/Conditional/Problem 2.py =========

Enter a three digit number :343

o : 3 t : 4 h : 3

Number obtained by reversing the order of the digits : 343

No digit of the sum is same as the original number

>>>  

Output: Third run

>>>  

========= RUN C:/Python/Conditional/Problem 2.py =========

Enter a three digit number : 435

o : 5 t : 3 h : 4

Number obtained by reversing the order of the digits : 534

No digit of the sum is same as the original number

>>> 

Output: Fourth run

>>>  

========= RUN C:/Python/Conditional/Problem 2.py =========

Enter a three digit number :121

o : 1 t : 2 h : 1

Number obtained by reversing the order of the digits : 121

Sum of the number and that obtained by reversing the order of digits : 242

o1 : 2 t1 : 4 h1 : 2

Condition true

>>>  

.

Tip

One must be careful regarding the indentation, failing which the program would not compile. The indentation decides the beginning and ending of a particular block in Python. It is advisable not to use a combination of spaces and tabs in indentation. Many versions of Python may treat this as a syntax error.

The if-elif  ladder can also be implemented using the get statement, explained later in the chapter. The important points regarding the conditional statements in Python are as follows:

·         The if <test> is followed by a colon.

·         There is no need of parentheses for this test condition. Though enclosing test in parentheses will not result in an error.

·         The nested blocks in Python are determined by indentation. Therefore, proper indentation in Python is essential. As a matter of fact, an inconsistent indentation or no indentation will result in errors.

·         An if can have any number of if's  nested within.

·         The test condition in if must result in a True or a False .

 

Illustration 3.3

Write a program to find the greatest of the three numbers entered by the user.

Solution: First of all, three variables (say num1num2, and num3 ) are needed. These variables will get their values from the user. This input will be followed by the condition checking as depicted in the following program. Finally, the greatest number will be displayed. The listing is given as follows:

Program

>>>num1 = input('Enter the first number\t:')

...num2 = input('Enter the second number\t:')

...num3 = input('Enter the third number\t:')

...if int(num1)> int(num2):

... if int(num1) > int(num3):

... big= int(num1)

... else:

... big = int(num2)

...

...else:

... if int(num2)> int(num3)

... big= num2

... else:

... big = num3

...

...

...print(big)


3.3 THE IF-ELIF-ELSE  LADDER

If there are multiple conditions and the outcomes decide the action, then an if- elif- else  ladder can be used. This section discusses the construct and presents the concept using relevant examples. The syntax of this construct is as follows:

Syntax

if <test condition 1>:

    # The task to be performed if the condition 1 is true

elif <test condition 2>:

    # The task to be performed if the condition 2 is true

elif <test condition 3>:

    # The task to be performed if the condition 1 is true

else:

    # The task to be performed if none of the above condition is true

The flow of the program can be managed using the above construct. Figure 3.3 shows the diagram depicting the flow of the program which uses the above constructs.

Figure 3.3: The flow graph of if and elif ladder



In the figure, the left edge depicts the scenario where the condition C is true and the right edge depicts the scenario where the condition is false. In the second graph, conditions C1, C2, C3, and C4 lead to different paths [Programming in C#, Harsh Bhasin, 2014].

The following section has programs that depict the use of the elif ladder. It may be noted that if there are multiple else statements, then the second else is taken along with the nearest if .


3.4 LOGICAL OPERATORS

In many cases the execution of a block depends on the truth value of more than one statement. In such cases the operators “and” (“&”) and “or” (“|”) come to our rescue. The first ('and') is used when the output is 'true', when both the conditions are 'true'. The second ('or') is used if the output is 'true', if any of the conditions are 'true' .

The truth table of 'and' and 'or'  is given as follows. In the tables that follow “T” stands for “true” and “F” stands for “false.”

Table 3.1: Truth table of a&b

a

b

a&b

<p>t</p>

T

T

<p>t</p>

F

F

<p>F</p>

T

F

<p>F</p>

F

F

    print('The value of a greatest')

Table 3.2: Truth table of a|b

a

b

a|b

<p>t</p>

T

T

<p>t</p>

F

T

<p>F</p>

T

T

<p>F</p>

F

F

The above statement helps the programmer to easily handle compound statements. As an example, consider a program to find the greatest of the three numbers entered by the user. The numbers entered by the user are (say) 'a''b', and 'c', then 'a' is greatest if (a > b) and (a > c) . This can be written as follows:

 

if((a>b)&(a>c))

In the same way, the condition of ‘b’ being greatest can be crafted. Another example can be that of a triangle. If all the three sides of a triangle are equal, then it is an equilateral triangle.

 

if((a==b) or (b==c) or (c==a))

    //The triangle is isosceles;


3.5 THE TERNARY OPERATOR

The conditional statements explained in the above section are immensely important to write any program that contains conditions. However, the code can still be reduced further by using the ternary statements provided by Python. The ternary operator performs the same task as the if-else construct. However, it has the same disadvantage as in the case of C or C++. The problem is that each part caters to a single statement. The syntax of the statement is given as follows.

Syntax

<Output variable> = <The result when the condition is

                                                                 true>

if <condition> else <The result when the condition is not

                                                             true>

For example, the conditional operator can be used to check which of the two numbers entered by the user is greater.

great = a if (a>b) else b

Finding the greatest of the three given numbers is a bit intricate. The following statement puts the greatest of the three numbers in “great.”

great = a if (a if (a > b) else c)) else(b if (b>c) else c))

The program that finds the greatest of the three numbers entered by the user using a ternary operator is as follows.

Illustration 3.4

Find the greatest of three numbers entered by the user, using a ternary operator.

Program

a = int(input('Enter the first number\t:'))

b = int(input('Enter the second number\t:'))

c = int(input('Enter the third number\t:'))

big = (a if (a>c) else c) if (a>b) else (b if (b>c) else c)

print('The greatest of the three numbers is '+str(big))

>>> 

Output

========== RUN C:/Python/Conditional/big3.py ==========

Enter the first number 2

Enter the second number 3

Enter the third number 4

The greatest of the three numbers is 4

>>> 

 

3.6 THE GET CONSTRUCT

In C or C++ (even in C# and Java) a switch is used in the case where different conditions lead to different actions. This can also be done using the 'if-elif' ladder, as explained in the previous sections. However, the get  construct greatly eases this task in the case of dictionaries.

In the example that follows there are three conditions. However, in many situations there are many more conditions. The contact can be used in such cases. The syntax of the construct is as follows:

Syntax

<dictionary name>.get('<value to be searched>',

                           'default value>')

Here, the expression results in some value. If the value is value 1, then block 1 is executed. If it is value 2, block 2 is executed, and so on. If the value of the expression does not match any of the cases, then the statements in the default  block are executed. Illustration 5 demonstrates the use of the get construct.

Illustration 3.5

This illustration has a directory containing the names of books and the corresponding year they were published. The statements that follow find the year of publication for a given name. If the name is not found the string (given as the second argument, in get) is displayed.

Program

hbbooks = {'programming in C#': 2014, 'Algorithms': 2015, 'Python': 2016}

print(hbbooks.get('Programming in C#', 'Bad Choice'))

print(hbbooks.get('Algorithms', 'Bad Choice'))

print(hbbooks.get('Python', 'Bad Choice'))

print(hbbooks.get('Theory Theory, all the way', 'Bad Choice'))

Output

>>>  

========== RUN C:/Python/Conditional/switch.py ==========

Bad Choice

2015

2016

Bad Choice

>>>  

Note that in the first case the “P” of “Programming” is capital, hence “Bad Choice” is displayed. In the second and the third cases, the get function is able to find the requisite value. In the last case the value is not found and so the second argument of the get function appears. Note that it is similar to the default of the “C” type switch  statement. The flow diagram given in Figure 3.4 shows a program that has many branches.

Figure 3.4: A program having multiple conditional statements



Observation

In Python, dictionaries and lists form an integral part of the language basics. The use of the get  construct was not explained in Chapter 2 of this book, as it implements the concept of conditional selection. It may be noted that this construct greatly reduces the problems of dealing with the situations where mapping is required and is therefore important.

 

3.7 EXAMPLES

The 'if'  condition is also used for input validation. The process will be explained in the following sections of this book. However, the idea has been used in the example that follows. The program asks the user to enter a character and checks whether its ASCII value is greater a certain value.

Illustration 3.6

Ask the user to enter a number and check whether its ASCII value is greater than 80.

Program

 

inp = input('Enter a character :')

if ord(inp) > 80:

    print('ASCII value is greater than 80')

else:

    print('ASCII value is less than 80')

Output 1:

>>>Enter a character: A

ASCII value is less than 80

...

Output 2

>>>Enter a character: Z

ASCII value is greater than 80

>>> 

The construct can also be used to find the value of a multi-valued function. For example, consider the following function:


The following example asks the user to enter the value of x and calculates the value of the function as per the given value of x.

Illustration 3.7

Implement the above function and find the values of the function at x = 2 and x = 4 .

Program

fx = """

f(x) = x^2 + 5x + 3 , if x > 2

= x + 3 , if x <= 2

"""

x = int (input('Enter the value of x\t:'))

if x > 2:

f = ((pow(x,2)) + (5*x) + 3)

else:

f = x + 3

print('Value of function f(x) = %d' % f )

Output

========== RUN C:\Python\Conditional\func.py ==========

Enter the value of x :4

Value of function f(x) = 39

>>>  

========== RUN C:\Python\Conditional\func.py ==========

Enter the value of x :1

Value of function f(x) = 4

>>> 

The 'if-else'  construct, as stated earlier, can be used to find the outcome based on certain conditions. For example, two lines are parallel if the ratio of the coefficients of x’s is the same as that of those of y’s.

For a1x + b1y + c1 = 0 and a2x + b2y + c2 = 0. Then the condition of lines being parallel is:




The following program checks whether two lines are parallel or not.

Illustration 3.8

Ask the user to enter the coefficients of a1x + b1y + c1 = 0 and a2x + b2y + c2 = 0 and find out whether the two lines depicted by the above equations are parallel or not.

Program

print('Enter Coefficients of the first equation [a1x + b1+  c1 = 0]\n')

r1 = input('Enter the value of a1: ')

a1 = int (r1)

r1 = input('Enter the value of b1: ')

b1 = int (r1)

r1 = input('Enter the value of c1: ')

c1 = int (r1)

print('Enter Coefficients of second equation [a2x + b2y + c2 = 0]\n')

r1 = input('Enter the value of a2: ')

a2 = int (r1)

r1 = input('Enter the value of b2: ')

b2 = int (r1)

r1 = input('Enter the value of c2: ')

c2 = int (r1)

if (a1/a2) == (b1/b2):

           print('Lines are parallel')

else:

            print('Lines are not parallel')

Output

>>>  

========== RUN C:\Python\Conditional\parallel.py ==========

Enter Coefficients of the first equation [a1x + b1y + c1 = 0]

Enter the value of a1: 2

Enter the value of b1: 3

Enter the value of c1: 4

Enter Coefficients of second equation [a2x + b2y + c2 = 0]

Enter the value of a2: 4

Enter the value of b2: 6

Enter the value of c2: 7

Lines are parallel

>>> 

The above program can be extended to find whether the lines are intersecting or overlapping: two lines intersect if the following condition is true.

a1x  + b1y + c1 = 0 and a2x + b2y + c2 = 0. Then the lines intersect if:




And the two lines overlap if:




The following flow-chart shows the flow of control of the program (Figure 3.5).

Figure 3.5: Checking whether lines are parallel, overlapping, or if they intersect


The following program implements the above logic.

 

Illustration 3.9

Ask the user to enter the values of a1a2b1b2c1, and c2 and find whether the lines are parallel, or if they overlap or intersect.

Program

print('Enter Coefficients of the first equation [a1x + b1y + c1 = 0]\n')

r1 = input('Enter the value of a1: ')

a1 = int (r1)

r1 = input('Enter the value of b1: ')

b1 = int (r1)

r1 = input('Enter the value of c1: ')

c1 = int (r1)

print('Enter Coefficients of second equation [a2x + b2y + c2 = 0 ]\n')

r1 = input('Enter the value of a2: ')

a2 = int (r1)

r1 = input('Enter the value of b2: ')

b2 = int (r1)

r1 = input('Enter the value of c2: ')

c2 = int (r1)

if ((a1/a2) == (b1/b2))&((a1/a2)==(c1/c2)):

    print('Lines overlap')

elif (a1/a2)==(b1/b2):

    print('Lines are parallel')

else:

    print('Lines intersect')

Output

>>>  

========== RUN C:/Python/Conditional/Lines.py ==========

Enter Coefficients of the first equation [a1x + b1y + c1 = 0]

Enter the value of a1: 2

Enter the value of b1: 3

Enter the value of c1: 4

Enter Coefficients of second equation [a2x + b2y + c2 = 0]

Enter the value of a2: 1

Enter the value of b2: 2

Enter the value of c2: 3

Lines intersect


3.8 POINTS TO REMEMBER


  • The 'if' statement implements conditional branching.
  • The test condition is a Boolean expression which results in a true or a false.
  • The block of 'if' executes if the test condition it true.
  • The else part executes if the test condition is false.
  • Multiple branches can be implemented using the if-elif ladder.
  • Any number of if-elif can be nested.
  • A ternary if can be implemented in Python.
  • Logical operators can be used in implementing conditional statements.

3.9 EXERCISES



MULTIPLE CHOICE QUESTIONS

1.      What will be the output of the following?
if 28:
 print('Hi')
else:
print('Bye')

a.      Hi

b.      Bye

c.       None of the above

d.      The above snippet will not compile

Answer:

(a)

2.      a = 5
b = 7
c = 9
if a>b:
if b>c:
   
print(b)
else:
   
print(c)
else:
if b>c:
   
print(c)
else:
   
print(b)

a.      7

b.      9

c.       34

d.      None of the following

Answer:

(a)

3.      a = 34
b = 7
c = 9
if a>b:
if b>c:
   
print(b)
else:
   
print(c)
else:
if b>c:
   
print(c)
else:
   
print(b)

a.      7

b.      9

c.       None of the above

d.      The code will not compile

Answer:

(b)

4.      a = int(input('First number\t:'))
b = int(input('Second number\t'))
c = int(input('Third number\t:'))
if ((a>b) & (a>c)):
  print(a)
elif ((b>a) &(b>c)):
  print(b)
else:
  print(c)

a.      The greatest of the three numbers entered by the user

b.      The smallest of the three numbers entered by the user

c.       None

d.      The code will not compile

Answer:

(a)

5.      n = int(input('Enter a three digit number\t:'))
if (n%10)==(n//100):
  print('Hi')
else:
  print('Bye')
  # The three digit number entered by the user is 453

a.      Hi

b.      Bye

c.       None of the above

d.      The code will not compile

Answer:

(b)

6.      In the above question, if the number entered is 545, what would the answer be?

a.      Hi

b.      Bye

c.       None of the above

d.      The code will not compile

Answer:

(a)

7.      hb1 = ['Programming in C#','Oxford University Press', 2014]
hb2 = ['Algorithms', 'Oxford University Press', 2015]
if hb1[1]==hb2[1]:
  print('Same')
else:
  print('Different')

a.      same

b.      Different

c.       No output

d.      The code would not compile

Answer:

(a)

8.      hb1 = ['Programming in C#','Oxford University Press', 2014]
hb2 = ['Algorithms', 'Oxford University Press', 2015]
if (hb1[0][3]==hb2[0][3]):
  print('Same')
else:
  print('Different')

a.      Same

b.      Different

c.       No output

d.      The code will not compile

Answer:

(b)

9.      In the snippet given in question 8, the following changes are made. What will the output be?
hb1 = ['Programming in C#','Oxford University Press', 2014]
hb2 = ['Algorithms', 'Oxford University Press', 2015]
if (str(hb1[0][3])==str(hb2[0][3])):
  print('Same')
else:
  print('Different')

a.      Same

b.      Different

c.       No output

d.      The code will not compile

Answer:

(b)

10. Finally, the code in question 8 is changed to the following. What will the output be?
hb1 = ['Programming in C#','Oxford University Press', 2014]
hb2 = ['Algorithms', 'Oxford University Press', 2015]
if (char(hb1[0][3])==char(hb2[0][3])):
  print('Same')
else:
  print('Different')

a.      Same

b.      Different

c.       No output

d.      The code will not compile

Answer:

(b)

 

3.10 PROGRAMMING EXERCISE


1.      Ask the user to enter a number and find the number obtained by reversing the order of the digits.

2.      Ask the user to enter a four digit number and check whether the sum of the first and the last digits is same as the sum of the second and the third digits.

3.      In the above question if the answer is true then obtain a number in which the second and the third digit are one more than that in the given number.
Example: Number 5342, sum of the first and the last digit = 7 that of the second and the third digit = 7. New number: 5452

4.      Ask the user to enter the concentration of hydrogen ions in a given solution (C) and find the PH of the solution using the following formula.

PH = log10 C

5.      If the PH is <7 then the solution is deemed acidic, else it is deemed as basic. Find if the given solution is acidic.

6.      In the above question find whether the solution is neutral. (A solution is neutral if the PH is 7)

7.      The centripetal force acting on a body (mass m), moving with a velocity v, in a circle of radius r, is given by the formula mv2/r. The gravitational force on the body is given by the formula (GmM)/R2, where m and M are the masses of the body and earth and R is the radius of the earth. Ask the user to enter the requisite data and find whether the two forces are equal or not.

8.      Ask the user to enter his salary and calculate the TADA, which is 10% of the salary; the HRA, which is 20% of the salary and the gross income, which is the sum total of the salary, TADA and the HRA.

9.      In the above question find whether the net salary is greater than $300,000.

10. Use the Tax Tables of the current year to find the tax on the above income (question number 8), assuming that the savings are $100,000.

11. Find whether a number entered by the user is divisible by 3 and 13.

12. Find whether the number entered by the user is a perfect square.

13. Ask the user to enter a string and find the alphanumeric characters from the string.

14. In the above question find the digits in the strings.

15. In question 13, find all the components of the string which are not digits or alphabets.