python chapter 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is a repetition structure?

A repetition structure causes a statement or set of statements to execute repeatedly.

Assume there is a variable , h already associated with a positive integer value. Write the code necessary to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Assign the sum you compute to a variable q For example, if h is 19, you would assign 4 to q because there are perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16.

i=1 q=0 while ((i*i)<h): q=(i*i)+q i+=1

In this exercise, use the following variables : i,lo, hi, and result. Assume that lo and hi each are associated with an int and that result refers to 0. Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result. Your code should not change the values associated with lo and hi. Also, just use these variables : i,lo, hi, and result.

i=lo result=0 while i<=hi: result=result+i i+=1

Given a variable n refers to a positive int value, use two additional variables , k and total to write a for loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus your code should put 1*1*1 + 2*2*2 + 3*3*3 +... + n*n*n into total. Use no variables other than n, k, and total.

k=0 total=0 for k in range (n+1): total=total +k*k*k

Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 with total. Use no variables other than k and total.

k=0 total=0 while k<50: k+=1 total+=k*k

Assume there is a variable , h already associated with a positive integer value. Write the code necessary to compute the sum of the first h perfect squares, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 4, you would assign 30 to q because the first 4 perfect squares (starting with 1) are: 1, 4, 9, 16 and 30==1+4+9+16.

q=0 for i in range(1,h+1): q+=i**2

a count-controlled loop

repeats a specific number of times.

An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6. Given the positive integer distance and the positive integer n, associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance. For example, if distance is 2 and n is 10, then sum would be associated with 25 because 1+3+5+7+9 = 25.

sum=0 for i in range (1,n+1,distance): sum+=i

given thatn refers to a positive int use a while loop to compute the sum of the cubes of the first n counting numbers, and associate this value with total. Use no variables other than n, k, and total.

total=0 k=1 while k<=n: total+=k**3 k+=1

Use two variables k and total to write a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus, your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

total=sum(k*k for k in range (1,51))

General format of the while loop in Python:

while condition: statement statement etc. the first line can be called the while clause. The while clause begins with the word while, followed by a Boolean condition that will be evaluated as either true or false.


संबंधित स्टडी सेट्स

Vocab: GRE Question(s) of the Day

View Set

Ch. 11 Costs and Profit Maximization Under Competition

View Set