Question

In: Computer Science

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?

Solutions

Expert Solution

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

.


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...
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...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
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.
Qantas flies a non-stop route from London, UK (51.5 N,0 E) to Perth, Australia (32 S,115.9...
Qantas flies a non-stop route from London, UK (51.5 N,0 E) to Perth, Australia (32 S,115.9 E). Assume the Earth is perfect sphere with radius of 6371 km. Calculate the total length of the flight first fly due east until reach longitude 115.9 degree east and then fly due south. Calculate the total length by first fly due south then due east. Calculate the arc angle of the great circle and therefore the shortest flight path.
Explain non-sampling risk. Give an example and explain how it relates to auditing.
Explain non-sampling risk. Give an example and explain how it relates to auditing.
Explain non-sampling risk. Give an example and explain how it relates to auditing.
Explain non-sampling risk. Give an example and explain how it relates to auditing.
Provide an example of a non-contractual promise that is "unenforceable," and explain "why" it is unenforceable...
Provide an example of a non-contractual promise that is "unenforceable," and explain "why" it is unenforceable (not from the textbook make a fictional case).
Please explain the sparse identification of nonlinear dynamics (SINDy) with an example in Python or R...
Please explain the sparse identification of nonlinear dynamics (SINDy) with an example in Python or R (Please put the code)
. Explain the different techniques of probability sampling and non-probability sampling and provide an example for...
. Explain the different techniques of probability sampling and non-probability sampling and provide an example for each type. The student has to provide an example for each type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT