Loops: For and Ranges

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Associate the average of the numbers from 1 to n (where n is a positive integer value) with the variable avg.

avg = (1 + n) / 2

Write a for loop that prints the integers 50 through 1, each on a separate line. Use no variables other than count.

for count in range(50, 0, -1): print (count)

Write a for loop that prints in ascending order all the positive integers less than 200 that are divisible by both 2 and 3, separated by spaces.

for i in range(6, 200, 6): print (i, end=" ")

Write a for loop that prints the integers 0 through 39, each value on a separate line.

for x in range (0, 40): print(x)

Write a for loop that prints the odd integers 11 through 121 inclusive, each value on a separate line.

for x in range (11, 122, 2): print(x)

Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by spaces.

for x in range (80,19,-2): print(x, end=" ")

Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, each value on a separate line.

for x in range(5, 175, 5): print (x)

Associate True with the variable is_ascending if the list numbers is in ascending order (that is, if each element of the list is greater than or equal to the previous element in the list). Otherwise, associate False with is_ascending

is_ascending=True for i in range(1,len(numbers)) : if numbers[i]<numbers[i-1] : is_ascending=False

Given a positive integer n, assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)

is_prime = True for m in range (2, n): if n%m == 0: is_prime = False

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

An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers is the same. 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 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.

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

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 = 0 for k in range(1,51): total = total + k*k


Set pelajaran terkait

International Human Rights Law - Final Exam

View Set

Subject Pronouns: Replace the subject with a pronoun

View Set

Domain 5- Protection of Information Assets

View Set

Mosby Review - Section 2 Spinal Anatomy

View Set