In: Computer Science
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?
0) range() function is a built in python function which returns sequence of numbers from start(0 is the default value) till stop-1 and increments by 1 by default.It takes 3 arguments ,
range(start,stop,increment)
Example -
range(2,20,2) will give generate values from 2(start) till 19(stop-1) in increments of 2-
2 4 6 8 10 12 14 16 18
Range function is use to iterate list,string,tuples.dictionary with for and while loop.
While Python slicing is used to obtain substring from a given string .It also accepts 3 parameters like range function which is start,stop,step.if start index is not mentioned then it takes default value of 0 like range() function .Also step default value is 1 which is similar to increment in range function.
slice(start,stop,step)
Example -
string1 = 'CALIFORNIA'
slice(1,6,2) will generate substring 'AIO' since start index is 1 and stop is 6 so we will consider index only til stop-1 which is 5th index just like in range function and step is 2 which is the increment betweent the indexes
1)i=2
while i<12:
print(i)
i=i+3
Explanation - Since start value in range function is 2 so assign i with value 2.
Stop value in range function is 12 it means you have to print numbers till 11.Hence use while condition i<12 and print i until i is less than 12.
Increment vaue in range function is 3 so after printing i , increment i by 3.
2) For and while loop both are used for iterations.
For loop is used when the number of iterations is known
while loop is an indefinite iteration which repeats unkown number of times and end when some condition is met.
Use the for
loop when you know how many times the
loop should execute
Use the while loop when you do not know many times the loop should execute
Example -
If you want to iterate over a list ,string or any data structure we use for loop
for i in list1:
print(i)
If you want to prompt user to input a number repeatedly until the user enters 0 then we use while loop
while num!=0:
num=int(input("enter a number"))
4)You can create tuple in 2 ways -
first way is by not giving any element in paranthesis
t= ()
Second way is by using the tuple function
t=tuple()
5)The find() method returns the lowest index of the substring if it is found in given string.It returns -1 if the substring is not found
Example -
string1 = "all of you is all of me"
res =string1.find('of')
res1=string1.find("together")
print(res)
print(res1)
output :
4 since 'of' is found at index 4.
-1 since "together" is not found in string1
The index() method also returns the lowest index of the substring if it is found in given string.But it raises an exception if the substring is not found
Example -
string1 = "all of you is all of me"
res =string1.index('of')
res1=string1.index("together")
print(res)
print(res1)
output :
4 since 'of' is found at index 4.
ValueError: substring not found since "together" is not found in string1
.