Hi Can I get some help with this Python question set out bel

Hi, Can I get some help with this Python question set out below. Thanks

Assuming a = \"blueberry\" answer the fillowing:

• what does a[2], a[1], a[0] evaluate to?

• Evaluate for a[-1] and a[-2]. Any surprises?

• What is an index? What is a[2-1]? What is a[len(a)-1]?

• What is a[len(a)]? What does a[-len(a)] evaluate to?

• Evaluate a[-len(a)-1] and a[-len(a)+1].

Solution

Index is the position of the item in the array.

In Python programming language array index starts from 0 (zero) and ends in length of the array minus 1 (one)
   and also that array index can starts from -(length of the array) and ends in (-1).
  
Given a = \"blueberry\" , len(a) = 9 //length of a, here len(a) function determines the length of the string a

Then we can interpret the whole string as follows -

   a[0] = \'b\' , a[1] = \'l\' , a[2] = \'u\' , a[3] = \'e\' , a[4] = \'b\' ,
  
   a[5] = \'e\' , a[6] = \'r\' , a[7] = \'r\' , a[8] = \'y\'
  
  
We can interpret the whole string also as follows -

   a[-9] = \'b\' , a[-8] = \'l\' , a[-7] = \'u\' , a[-6] = \'e\' , a[-5] = \'b\' ,
  
   a[-4] = \'e\' , a[-3] = \'r\' , a[-2] = \'r\' , a[-1] = \'y\'
  

Question - what does a[2], a[1], a[0] evaluate to?

   Answer - a[2] evaluates to \'u\' , a[1] evaluates to \'l\' , a[0] evaluates to \'b\'
  

Question - Evaluate for a[-1] and a[-2]. Any surprises?

   Answer - a[-1] evaluates to \'y\' , a[-2] evaluates to \'r\'
  

Question - What is an index? What is a[2-1]? What is a[len(a)-1]?

   Answer - Index is the position of the item in the array ,
           a[2-1] = a[1] evaluates to \'l\' and
           a[len(a)-1] = a[9-1] = a[8] evaluates to \'y\'
             
Question - What is a[len(a)]? What does a[-len(a)] evaluate to?

   Answer - a[len(a)] = a[9] , now for string a index starts from 0 and ends in 8 and also index can start from -9 to -1
           So, as the index ends in 8 for string a , a[9] will generate an error such that \"string index out of range\"
             
           a[-len(a)] = a[-9] evaluates to \'b\'
             

Question - Evaluate a[-len(a)-1] and a[-len(a)+1]

   Answer - a[-len(a)-1] = a[-9-1] = a[-10] , now for string a index starts from 0 and ends in 8 and also index can start from -9 to -1
           So, as the index ends in -9 for string a , a[-10] will generate an error such that \"string index out of range\"
             
           a[-len(a)+1] = a[-9+1] = a[-8] evaluates to \'l\'

Hi, Can I get some help with this Python question set out below. Thanks Assuming a = \
Hi, Can I get some help with this Python question set out below. Thanks Assuming a = \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site