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

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"...
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 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 =...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
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...
Python CODE for this question: There are 2 red marbles and 98 blue marbles in a...
Python CODE for this question: There are 2 red marbles and 98 blue marbles in a jar. Draws are made at random with replacement. Find the smallest number of draws needed such that the probability is greater than 1/2 that a red marble is drawn at least once.
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT