In: Computer Science
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?
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.