Question

In: Computer Science

0) Explain (with a non-trivial example that uses start, stop, and increment) the similarity between the...

0) Explain (with a non-trivial example that uses start, stop, and increment) the similarity between the range() function and slicing.

1) Convert:

for i in range(2, 12, 3):
   print(i)

into a while loop.

2) Explain the circumstances where a for loop makes more sense versus when a while loop makes more sense.

3) What is your favorite movie? (There should be one easy question, right?)

4) How do you create an empty tuple?

5) What is the difference between the string methods find and index?

Solutions

Expert Solution

Answer:

0) Explain (with a non-trivial example that uses start, stop, and increment/decrement) the similarity between the range() function and slicing.

range() function

range fuction return a sequence of numbers which have start point, end point and steps to be increase/decrese everytime.

->start point is optional and it will start from 0 by default

->step part is also optional it will increment/decrement by 1 by default.

syntax:

range(start point, stop point , step increment /decrement)

x = range(2, 10,2)  // from 2, till 10 , increment by 2

for n in x:
  print(n)

slice() function

slice fuction return a slice of sequence of a sequence which have start point, end point and steps to be increase/decrese everytime.

->start point is optional and it will start from 0 by default.

->step part is also optional it will increment/decrement by 1 by default.

i=2                   //start point
while(i<12):          //ending condition
        print(i)
        i+=3               //increment by 3
p = ("a", "b", "c", "d", "e", "f", "g", "h")    // create a tuple p having these values

x = slice(3, 8,2)   // start slicing from 3 , till the 8 , and increment by 2

print(p[x])

So, you can see the similarity that is arguments and it behaviour is same but slicing will apply on existing list while range is used to generate new list.

1) Convert:

for i in range(2, 12, 3):
   print(i)

into a while loop.

 i=2                       // start point
while(i<12):               // end 
        print(i)
        i+=3                    // increment by 3

2) Explain the circumstances where a for loop makes more sense versus when a while loop makes more sense.

->For loop makes more sence to be use when we know how much times we need to repeat the code .

example:


a=(1,22,3,5)
for i in range(0,2):    // here we know when loop will terminate
  print(a[i])
  i += 1

->While loop makes more sense to be use when we dont know how much time we need to repeat the code. example : suppose we in array we want to stop printing when element 3 occured.

i = 0
a=(1,22,3,5)
while a[i]!=3:          // here we don't know how much step it will repeat 
  print(a[i])
  i += 1

4) How do you create an empty tuple?

we can create empty tuple object by giving no elements inside the paranthesis during initialization. Empty tuple can also be created by tuple() built-in function without passing any arguments

Tp= () or Tp= tupple()

5) What is the difference between the string methods find and index?

Both method find() and index() of string is used to find the index of occurence of the substring but there is a difference in behaviour when substring is not found .

the find() function will return -1 while index() will throw an error when there is no match found.

example:

Let me know if you have any doubt.


Related Solutions

0) Explain (with a non-trivial example that uses start, stop, and increment) the similarity between the...
0) Explain (with a non-trivial example that uses start, stop, and increment) the similarity between the range() function and slicing. 1) Convert: for i in range(2, 12, 3): print(i) into a while loop. 2) Explain the circumstances where a for loop makes more sense versus when a while loop makes more sense. 3) What is your favorite movie? (There should be one easy question, right?) 4) How do you create an empty tuple? 5) What is the difference between the...
In Python: 0) Explain (with a non-trivial example that uses start, stop, and increment) the similarity...
In Python: 0) Explain (with a non-trivial example that uses start, stop, and increment) the similarity between the range() function and slicing. 1) Convert: for i in range(2, 12, 3): print(i) into a while loop. 2) Explain the circumstances where a for loop makes more sense versus when a while loop makes more sense. 4) How do you create an empty tuple? 5) What is the difference between the string methods find and index?
Explain the similarity between a hydrogen bond and a covalent bond. Explain a similarity between a...
Explain the similarity between a hydrogen bond and a covalent bond. Explain a similarity between a hydrogen bond and an electrostatic interaction. Explain a difference between an electrostatic interaction and a hydrogen bond.
Which is larger, an increment of 1°C, or of 1°F? Explain, using an example if needed.
Which is larger, an increment of 1°C, or of 1°F? Explain, using an example if needed.
Please give an example of the similarity between hedging currency risk and the cash and carry...
Please give an example of the similarity between hedging currency risk and the cash and carry model.
Explain the similarity and difference between process costing and job-order costing.
Explain the similarity and difference between process costing and job-order costing.
Explain the difference between volatile and non-volatile storage media. Provide an example of each.
Explain the difference between volatile and non-volatile storage media. Provide an example of each.
what is the minimum number of wires required between two start/stop push button controls?
what is the minimum number of wires required between two start/stop push button controls?
Define the following pairs of terms and explain the similarity, difference or relationship between the terms:...
Define the following pairs of terms and explain the similarity, difference or relationship between the terms: a) Depreciation and devaluation; b) Currency crisis and international financial crisis; c) Internal balance and external balance; d) Debt rescheduling and debt forgiveness; e) Hard peg and dollarization; f) Gold standard and fixed exchange rate; g) Policy instruments and policy targets; h) Country risk and currency risk
Define the following pairs of terms and explain the similarity, difference or relationship between the terms:...
Define the following pairs of terms and explain the similarity, difference or relationship between the terms: Expenditure changing and switching policies Gold standard and gold exchange standard
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT