0% found this document useful (0 votes)
159 views3 pages

Python Programming Lab Exercises List

This document outlines exercises for a Python programming lab course. It includes exercises on basic concepts like control structures, loops, operators, and I/O. More advanced exercises cover lists, strings, functions, recursion, tuples, files, searching/sorting, and exception handling. The goal is for students to practice and demonstrate proficiency in these core Python topics through programming assignments.
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)
159 views3 pages

Python Programming Lab Exercises List

This document outlines exercises for a Python programming lab course. It includes exercises on basic concepts like control structures, loops, operators, and I/O. More advanced exercises cover lists, strings, functions, recursion, tuples, files, searching/sorting, and exception handling. The goal is for students to practice and demonstrate proficiency in these core Python topics through programming assignments.
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

17CI62 PYTHON PROGRAMMING LAB

Lab Exercises

I. Exercise programs on basic control structures & loops.

a) Write a program for checking the given number is even or odd.

b) Using a for loop, write a program that prints the decimal equivalents of 1/2, 1/3,
1/4 ,....... 1/10

c) Write a program for displaying reversal of a number.

d) Write a program for finding biggest number among 3 numbers.

e) Write a program using a while loop that asks the user for a number, and prints a
countdown from that number to zero.

II. Exercise programs on operators & I/O operations.

a) Write a program that takes 2 numbers as command line arguments and prints its
sum.

b) Implement python script to show the usage of various operators available in python
language.

c) Implement python script to read person’s age from keyboard and display whether
he is eligible for voting or not.

d) Implement python script to check the given year is leap year or not.

III. Exercise programs on Python Script.

a) Implement Python Script to generate first N natural numbers.

b) Implement Python Script to check given number is palindrome or not.

c) Implement Python script to print factorial of a number.

d) Implement Python Script to print sum of N natural numbers.

e) Implement Python Script to check given number is Armstrong or not.

f) Implement Python Script to generate prime numbers series up to n


IV. Exercise programs on Lists.

a) Finding the sum and average of given numbers using lists.

b) To display elements of list in reverse order.

c) Finding the minimum and maximum elements in the lists.

V. Exercise programs on Strings.

a) Implement Python Script to perform various operations on string using string


libraries.

b) Implement Python Script to check given string is palindrome or not.

c) Implement python script to accept line of text and find the number of characters,
number of vowels and number of blank spaces in it.

VI. Exercise programs on functions.

a) Define a function max_of_three() that takes three numbers as arguments and


returns the largest of them.

b) Write a program which makes use of function to display all such numbers which
are divisible by 7 but are not a multiple of 5, between 1000 and 2000.

VII. Exercise programs on recursion & parameter passing techniques.

a) Define a function which generates Fibonacci series up to n numbers.

b) Define a function that checks whether the given number is Armstrong

c) Implement a python script for Call-by-value and Call-by-reference

d) Implement a python script for factorial of number by using recursion.

VIII. Exercise programs on Tuples.

a) Write a program which accepts a sequence of comma-separated numbers from


console and generate a list and a tuple which contains every number. Suppose the
following input is supplied to the program: 34, 67, 55, 33, 12, 98. Then, the output
should be: ['34', '67', '55', '33', '12', '98'] ('34',67', '55', '33', '12', '98').

b) With a given tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), write a program to print the first half
values in one line and the last half values in one line.
IX. Exercise programs on files.

a) Write Python script to display file contents.

b) Write Python script to copy file contents from one file to another.

X. Exercise programs on searching & sorting Techniques.

a) Implement a python script to check the element is in the list or not by using Linear
search & Binary search.

b) Implement a python script to arrange the elements in sorted order using Bubble,
Selection, Insertion and Merge sorting techniques.

XI. Exercise programs on Exception handling concepts.

a) Write a python program by using exception handling mechanism.


b) Write a python program to perform various database operations (create, insert,
delete, update).

Common questions

Powered by AI

Tuples are preferred over lists in scenarios where immutability is required, such as when ensuring data integrity or when defining constant sets of values that should not change throughout the program. Immutability allows tuples to be used as keys in dictionaries or elements in sets, which is not possible with lists due to their mutable nature . Using tuples prevents accidental changes to data, offering protection in a multi-threaded environment where concurrent modifications could lead to unpredictable states. Furthermore, the immutability of tuples can lead to performance optimizations since they consume less memory and their contents can be accessed more quickly .

Using recursion to generate a Fibonacci series can be both intuitive and elegant, as it directly maps to the mathematical definition of Fibonacci numbers. However, it has significant computational drawbacks. Each call generates two more calls until the base case is hit, leading to an exponential time complexity of O(2^n) due to the large number of repeated calculations of the same Fibonacci numbers . This inefficiency can be mitigated by using techniques like memoization to store previously computed values and avoid redundant calculations, thus improving performance to linear time complexity, O(n). Despite its inefficiency without optimization, recursion offers clear and concise code .

Exception handling in Python improves reliability and robustness by allowing programs to gracefully manage errors and unexpected conditions during runtime, rather than crashing. In file operations, this is especially critical because issues such as file not found, permission errors, or read/write failures are common . By using try-except blocks, programs can catch specific exceptions, respond appropriately, and maintain a smooth user experience. For instance, a program may attempt to open a file in a try block and perform operations on it, and if an exception arises (like IOError or FileNotFoundError), execution is transferred to the except block where the error can be logged, user notified, or alternative actions taken. This ensures that the application continues to work or fails in a controlled manner .

Generating a prime number series up to a given number in Python involves checking each number's divisibility, which poses a challenge due to the computational cost of checking large numbers. The basic method, testing every number from 2 to n, can be inefficient for large n due to repeated calculations . Efficiency can be enhanced by implementing the Sieve of Eratosthenes algorithm, which has a time complexity of O(n log log n). This uses a boolean array to mark non-prime numbers in a range, skipping even numbers and any previously marked as composite. This method significantly reduces the number of tests needed, improving implementation efficiency while maintaining accuracy in identifying primes .

Bubble sort repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. Its time complexity is O(n^2), making it inefficient for large lists . Selection sort also has a time complexity of O(n^2), sorting by repeatedly finding the minimum element and moving it to the sorted portion of the array . Insertion sort builds a sorted array one element at a time, inserting each new element into its correct position within the sorted elements, also at O(n^2) time complexity, but it is more efficient than bubble and selection for partially sorted arrays . Merge sort, on the other hand, follows a divide and conquer approach, dividing the array into halves until each subarray has one element, then merging those subarrays back together in sorted order. It has a better time complexity of O(n log n), making it suitable for larger datasets .

Python's list and tuple functionalities provide diverse capabilities for data manipulation and storage, aiding in accomplishing various computational goals like iteration, storage of collections, and implementing algorithms. Lists, being mutable, are ideal for scenarios requiring frequent data modifications, such as appending, inserting, or deleting elements. This versatility comes at the cost of higher memory usage and slower processing for large datasets . Tuples offer immutability, making them suitable for fixed collections of data that require fast lookups and reduced memory consumption. The choice between lists and tuples often depends on the data nature (mutable vs immutable) and required operations (modification vs stability). For instance, a fixed set of configuration parameters might be better suited for a tuple, while a list is apt for dynamic datasets like session logs .

Python's input and output operations enable greater interactivity by allowing programs to receive user data and provide feedback or results in response. This interactivity forms the basis for versatile applications where user input determines program behavior. For example, combining input with operators enables dynamic calculations. A program can take two numbers as input using the `input()` function, process them with arithmetic operators, and present the result using the `print()` function. This demonstrates how operators work in conjunction with I/O functions to create programs that can adapt based on user-provided data, enhancing both functionality and user engagement .

To design a Python function that identifies whether a string is a palindrome, consider ignoring spaces, capitalization, and punctuation to focus purely on the letters and their order . The function should first preprocess the string by normalizing it. This involves converting all characters to the same case and possibly removing all non-alphabetic characters. Once preprocessed, the string should be compared to its reverse. This can be done by using slicing, where `s[::-1]` gives the reverse of the string `s`. The function returns `True` if the original and reversed strings are identical, indicating a palindrome, and `False` otherwise. This approach ensures that the palindrome checking is robust and accounts for typical variations in input format .

When using a while loop to implement a countdown program, the primary consideration is determining and updating the termination condition correctly; the loop continues to execute as long as this condition remains true. This differs from a for loop where the number of iterations is predetermined and explicitly defined through range. The while loop provides more flexibility in cases where the number of iterations depends on conditions that change unpredictably during execution . However, it also increases the risk of infinite loops if the decrement condition is not correctly implemented or updated. The flexibility of while loops allows for more complex and conditionally driven iterations compared to the systematic approach of for loops .

Parameter passing techniques like call-by-value and call-by-reference play crucial roles in determining how data is transmitted to functions in Python. In call-by-value, a copy of the argument is passed to the function, meaning modifications to this parameter within the function do not affect the original variable. For example, passing an integer to a function in Python behaves as call-by-value since integers are immutable . Call-by-reference involves passing the reference to the variable, allowing changes within the function to affect the original data. This is typically observed with mutable objects like lists. Passing a list to a function allows element modification within the function to reflect in the list outside. Understanding these distinctions guides function design relative to intended modifications and variable scope .

You might also like