0% found this document useful (0 votes)
5 views13 pages

Pythonlab 2

The document contains a series of programming tasks and exercises in Python, including raising and correcting errors, calculating distances, summing numbers, checking evenness, and implementing various algorithms like sorting and searching. It also covers file operations, string manipulations, and mathematical functions such as mean and gcd. Each task is designed to enhance coding skills and understanding of Python programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Pythonlab 2

The document contains a series of programming tasks and exercises in Python, including raising and correcting errors, calculating distances, summing numbers, checking evenness, and implementing various algorithms like sorting and searching. It also covers file operations, string manipulations, and mathematical functions such as mean and gcd. Each task is designed to enhance coding skills and understanding of Python programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1.

A) Running instructions in Interactive interpreter and a Python Script

Q1.B) Write a program to purposefully raise Indentation Error and Correct it


Q2. A) Write a program to compute distance between two points taking input from the user
(Pythagorean Theorem)

Q2. B) Write a program [Link] that takes 2 numbers as command line arguments and prints
its sum.

Q3. A) Write a Program for checking whether the given number is a even number or not.
Q3.B) Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3,
1/4, . . . ,1/10.

Q3.C) Write a program using a for loop that loops over a sequence.

Q3.D) Write a program using a while loop that asks the user for a number, and prints a
countdown from that number to zero.
Q4. A) Find the sum of all the primes below two million. Adding the previous two terms,
each new term in the Fibonacci sequence is generated. By starting with 1 and 2, the first 10
terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

limit = 2000000

sieve = [True]*limit

sieve[0]=sieve[1]=False

for i in range (2, int(limit**0.5) + 1):

if sieve[i]:

for j in range (i*I, limit, i):

sieve[j] = False

print_sum = sum (i for i in range (limit ) if sieve[i])

print (prime_sum)

output:-

142913828922

Q4. B) By considering the terms in the Fibonacci sequence whose values do not exceed four
million,find the sum of the even valued terms.
Q4. C) Linear search and Binary search

Q4.D) Selection sort, Insertion sort


Q5. A) Write a program to count the numbers of characters in the string and store them in a
dictionary data structure

Q5. B) Write a program to use split and join methods in the string and trace a birthday with a
dictionary data structure.
Q6. A) Write a program combine_lists that combines these lists into a dictionary.

Q6. B) Write a program to count frequency of characters in a given file. Can you use
character frequency to tell whether the given file is a Python program file, C program file or
a text file?
Q7. A) Write a program to print each line of a file in reverse order.
Q.7B) Write a program to compute the number of characters, words and lines in a file.

Q8. A) Write a function ball_collide that takes two balls as parameters and computes if they
are colliding. Your function should return a Boolean representing whether or not the balls
are colliding. Hint: Represent a ball on a plane as a tuple of (x, y, r), r being the radiusIf
(distance between two balls centers) <= (sum of their radii), then (they are colliding)

Q8. B)
Find the mean, median, and mode for the given set of numbers in a list.
Q9. A) Write a function nearly_equal to test whether two strings are nearly equal. Two
strings a and bare nearly equal when a single mutation on b can generate a.
Q9. B) Write a function dups to find all duplicates in the list.

Q9. C) Write a function unique to find all the unique elements of a list.
Q10.A) Write a function cumulative_product to compute the cumulative product of a list of
numbers.

Q10.B) Write a function reverse to reverse a list. Without using the reverse function.

Q10.C) Write a function to compute gcd, lcm of two numbers. Each function shouldn’t
exceed one line.

You might also like