Question

In: Computer Science

Question 5 (10 marks) Python Language What is the output of the following code ? (2...

Question 5 Python Language

  1. What is the output of the following code ? (2 points)

a, b = 0, 1

while b < 10: print b

a, b = b, a+b

B. Explain List Comprehension (2 points)

Given v = [1 3 5]

w = [ [2*x, x**2] for x in v] What is the content of w?

c. What is tuple ?   What is the difference between tuple and list ? (2 points)

D. What is a module ?   What is the role of     all    ? (2 points)

Solutions

Expert Solution

A.The screenshot of the program and the output generated is attached for reference:

The output:

Here the initial value of a is 0 and b is 1.

In the while loop value of b is passed to a and the sum of a+b (here a value equals to intial value before changing to value of b) is stored in b.

The while loop executes and prints value in b until the condition is violated.

B.List comprehension can be defined as a deadly useful method in python to create a list.

There are three ways to insert values to list:

1)for loop         2)map() function           3)list comprehension

What list comprehension offers is a declarative elegent syntax .Using list comprehension we just needs to focus on adding more values to the list , while the construction of list will be handled by python.

The contents in list w is [[2, 1], [6, 9], [10, 25]]

this is because for every value in list v value x changes and adds a new nested list in w with values 2*x and x**2

The screenshot of the program and output generated is attached for reference:

The output generated by this program:

C.Tuple is a datatype which is immutable in nature.

The difference between list and tuple are:

  1. The tuple datatype is immutable in nature while list is mutable datatype.
  2. List is declared using square brackets "[ ]" while tuples are declared using paranthesis "( )"
  3. Example for a list : l1 = [1,2,3,4] and example for a tuple: t1 =(1,2,3,4)

D.Modules are collection of functions which can be loaded into our program when needed.

Modules reduces the complexity and length of the overall program

In python modules are loaded into a program using from and import statement.

Example: from module_name import * , loads all the functions in that module

The role of all denoted by (*) is used to denote all the functions included in that module and so using it allows us to make use of all that module functions in our program.

Hope it helps!!!


Related Solutions

Output and Debugging Questions (10 marks each) [20 Marks] Note: Provide a copy of the code...
Output and Debugging Questions (10 marks each) [20 Marks] Note: Provide a copy of the code and screen shot for the output in the solutions’ 1. Trace the following program and write the exact output for the following inputs. Explain each output. [10 Marks] a. Input of an array { 20, 80 , 63, 89 } b. Input of an array { 1, 2 ,3, 4} c. Input of an array { 100, 200 ,300, 400} d. Input of an...
Suppose x is 1 and y is 10. What is the output of the following code?...
Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) && ( y <11)                                     Display statement                         else                                     Display nothing 2. Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) || ( y <11)                                     Display statement                         Else                                     Display nothing 3. Write a java program that prints 1 to infinite numbers. 4. The...
Question No 2: (5+5 =10 Marks) You are a senior accountant in a company. Your immediate...
Question No 2: (5+5 =10 Marks) You are a senior accountant in a company. Your immediate manager is a very forceful, dominant individual and you have been accepting his views over the last two years on ‘level of work in progress’ in the company. He has informed you that work in progress has increased by 200% during the current reporting period and instructed you to report this level in the monthly management accounts. The year-end draft of financial statements shows...
Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
Using python as the coding language please write the code for the following problem. Write a...
Using python as the coding language please write the code for the following problem. Write a function called provenance that takes two string arguments and returns another string depending on the values of the arguments according to the table below. This function is based on the geologic practice of determining the distance of a sedimentary rock from the source of its component grains by grain size and smoothness. First Argument Value Second Argument Value Return Value "coarse" "rounded" "intermediate" "coarse"...
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2,...
Write PYTHON CODE to answer the following question: Consider the following data: x = [0, 2, 4, 6, 9, 11, 12, 15, 17, 19] y = [5, 6, 7, 6, 9, 8, 8, 10, 12, 12] Using Python, use least-squares regression to fit a straight line to the given data. Along with the slope and intercept, compute the standard error of the estimate and the correlation coefficient. Best fit equation y = ___ + ___ x Standard error, Sy/x =...
Add the result envidented by the python compiler. use python language. 1(a) A Give the output...
Add the result envidented by the python compiler. use python language. 1(a) A Give the output for the array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) a. a[:6:2] = -1000 (ii) a[ : :-1] ) b. Display the values of 1D array using for loop?
 c. Write a code to print a 3D array?
 d. How to print the transpose of a 2D array?
 e Write a function to sort the array row and coloumn wise ?...
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
Question 5 (5 + 3 + 2 Marks) a) What is unified classification of soil? Explain...
Question 5 (5 + 3 + 2 Marks) a) What is unified classification of soil? Explain the classification and nomenclature of Gravels, Sands, Silts and Clays. b) Explain well-graded, poorly graded and gap-graded soil showing the graph between percentage passing and grain size. c) The grain size analysis was conducted and following results are obtained from graph. The d 10 = 0.15, d 30 =0.425 &amp; d 60 =1.5. Determine the grading of the soil.
Question 11 [10 marks / 10 minutes] For the mutations in the 5’ UTR of the...
Question 11 [10 marks / 10 minutes] For the mutations in the 5’ UTR of the trp operon given below, explain the effect on expression of the trp operon relative to wildtype (increased, decreased, or unchanged). Assume medium levels of tryptophan, such that TrpR (repressor) is inactive and attenuation is active. a) A mutation in region 2 such that it cannot bind with region 3. [4 marks] b) A mutation in region 4 such that it cannot bind with region...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT