04-03 for and ranges
Assume there is a variable, h which has been assigned 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). Assign the sum you compute to 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.
Solution 1 q = 0 for i in range(1, h+1) : [Tab Key]q += i * i Solution 2 q = 0 for i in range(1, h+1) : [Tab Key]q += i**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) : [Tab Key]print(count)
Assign True to 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, assign False with is_ascending.
for i in range(1,len(numbers)) : [Tab Key]if numbers[i] < numbers[i-1] : [Tab Key][Tab Key]is_ascending = False [Tab Key][Tab Key]break [Tab Key]else : [Tab Key][Tab Key]is_ascending = True
Write a for loop that prints the odd integers 11 through 121 inclusive, each value on a separate line.
for i in range(11, 122, 2) : print(i)
Write a for loop that prints the integers 0 through 39, each value on a separate line.
for i in range(40) : print(i)
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 i in range(5, 175, 5) : print(i)
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, 201, 6) : print(i, end=" ")
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by spaces.
for i in range(80, 18, -2) : print(i, end=" ")
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 i in range(2, n) : [Tab Key]if n % i == 0 : [Tab Key][Tab Key]is_prime = False [Tab Key][Tab Key]break
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, assign the sum of the elements of the arithmetic progression from 1 to n with distance distance to the variable sum. For example, if distance is 2 and n is 10, then sum would contain 25 because 1+3+5+7+9 = 25.
sum = 0 for i in range(1, n+1, distance) : [Tab Key]sum += i
Compute the average of the numbers from 1 to n (where n is a positive integer value) and assign it to the variable avg.
total = 0 for i in range(1, n+1) : [Tab Key]total += i avg = float(total) / n
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(51): [Tab Key]total += k*k
Given a variable n that contains a positive integer 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(n+1): [Tab Key]total += k*k*k