Concept explainers
Explanation of Solution
a)
Given code segment:
#Loop from 1 to 11
for i in range(1, 11):
#Print square of the number
print(i * i)
Explanation:
Explanation of Solution
b)
Given code segment:
#Iterate the list
for i in [1, 3, 5, 7, 9]:
#Print the cube of each value
print(i, ":", i ** 3)
#print the value of i
print(i)
Explanation:
Explanation of Solution
c)
Given code segment:
#Declare required variables
x = 2
y = 10
#Loop
for j in range(0, y, x):
#Print the value of j
print(j, end = "")
#Print the value of x+y
print(x + y)
#print done
print("done")
Explanation:
Explanation of Solution
d)
Given code segment:
#Declare required variables
ans = 0
#Loop
for i in range(1, 11):
#Calculate the value of ans
ans = ans + i * i
#Print the value of i
print(i)
#print the value of ans
print(ans)
Explanation:
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters to 0, and then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment the counter variable that’s keeping count of the 1s that occur; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.arrow_forwardWrite a program that calculates the equivalent resistance of a circuit. n electrical resistors with resistance values R1, R2,.., Rn are said to be connected in parallel if the same voltage is applied across each. Their equivalent resistance Reg is related to the values R1, R2, ..., Rn by: 1 1 1 1 + +...+ R R R, R. "eq Write a program that prompts the user to enter the resistance of n resistors. The user should be prompted continuously by using a while loop until a zero or negative value is entered (the program should count the number of resistors n entered by the user and store the values in an array). Use another separate loop to print the resistance values entered by the user and the equivalent resistance Reg. Test your program with R1= 1 kn, R2= 2 kn, R3= 4 kQ and R4= 8 kQ.arrow_forwardWrite a C#program that displays the pattern show in the* sample executable.** Type of loop-> your favorite (you must use a pair of* nested loops)* Hints:* 1) Use variables that increments* 2) When running the sample, use values between 61 and* 76 for the number of columns, values between 16 and* 21 the number of rows* 3) Start with a rectangular array of "X" then figure out* how to create one of the upward lines and then the* backwards lines and then both lines*arrow_forward
- 3. Show the output that would be generated by each of the following program fragments: a) for ch in "aardvark": print (ch) b) for w in "Now is the winter of our discontent ... ".split (): print (w) c) for w in "Mississippi ".split (" i"): print (w, end=" ") d) msg for s in " secret ".split ("e"): = msg = msg + s print (msg)arrow_forward3. Write a python program to read a number and print a right triangle using E.g. : Input : 5 --Output---- ** *** **** ***** 4. Write a python program that print out the complete list of couples of prime numbers that are less than 50, but their sum is bigger than 40. For instance: (29,13) or (37,17), etc. Your program should print all couples. amazon PC w acer A S. P F6 F7 F8 F9 F10 F11 F12 Scr Lk Arrêtdéf PrtSc/Im A SysRc & 9. 3arrow_forwardcode in java pls Summary: Given integer values for red, green, and blue, subtract the gray from each value. Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255. Thus (255, 0, 0) is bright red, (130, 0, 130) is a medium purple, (0, 0, 0) is black, (255, 255, 255) is white, and (40, 40, 40) is a dark gray. (130, 50, 130) is a faded purple, due to the (50, 50, 50) gray part. (In other words, equal amounts of red, green, blue yield gray). Given values for red, green, and blue, remove the gray part. Ex: If the input is: 130 50 130 the output is: 80 0 80 Find the smallest value, and then subtract it from all three values, thus removing the gray.arrow_forward
- Task 10 Write Python code of a program that reads an integer, and prints the integer if it is NOT a multiple of 2 OR NOT a multiple of 5. For example, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22 hint(1): use the modulus (%) operator for checking the divisibility hint(2): You can consider the number to be an integer Example01: Input: 3 Output: 3arrow_forwardGiven the following program : sum = 0; for (i=0;i2;j--) sum=i+j; The value of sum after it is executed isarrow_forwardShow the output from the following fragments:a) for i in range (5) :print (i * i) b) for d in [3,1,4,1,5]:print (d, end=" ")c) for i in range ( 4) :print ("Hello")d) for i in range (5) :print (i, 2**i)arrow_forward
- Question 1: Please develop a program. The program shows a O(n^2) time complexity. You may design nested loops. When you input a number, the output should be how many times of the loops. For example, if your input is 10, the number of loops should be 100 times, 200 times, 300 times, etc. The number of loops do not have to be an accurate number. Question 2: Please develop a program. The program shows a O(nlgn) time complexity. You may design nested loops. When you input a number, the output should be how many times of the loops. For example, if your input is 10, the number of loops should be 30 times, 60 times, etc. The number of loops do not have to be an accurate number.arrow_forwardInput Your program 1. "1+2*(3/4)" 2. "1+2*[3*3+{4-5(6(7/8/9)+10)−11+(12*8)]+14" 3. "1+2*[3*3+{4-5(6(7/8/9)+10)}−11+(12*8)/{13+13}]+14" Program will take an arithmetic expression as a String input. For Example: Your program will determine whether the open brackets (the square brackets, curly braces and the parentheses) are closed in the correct order. Outputs: Output 1 1+2*(3/4) This expression is correct. Output 2 1+2* [3*3+{4-5(6(7/8/9)+10)−11+(12*8)]+14 This expression is NOT correct. Error at character # 10. *{*- not closed. Output 3 1+2* [3*3+{4-5(6(7/8/9)+10)}−11+(12*8)/{13+13}]+14 This expression is correct. Output 4 1+2]*[3*3+{4-5(6(7/8/9)+10)−11+(12*S)]+14 This expression is NOT correct. Error at character # 4. *]*- not opened. Task 1 Solve the above problem using an array based stack. Task 2 Solve the above problem using a linked list based stack.arrow_forwardusing c++, create a program with this expected output: Enter the number of rows and columns: 3 4 Enter 4 number(s) for row number 0: 1 2 3 4 Enter 4 number(s) for row number 1: 5 6 7 8 Enter 4 number(s) for row number 2: 9 10 11 12 Board: 1 2 3 4 5 6 7 8 9 10 11 12 note: use these methods: void fill(int **p, int rowSize, int columnSize); void print(int **p, int rowSize, int columnSize);arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr