0% found this document useful (0 votes)
24 views2 pages

Python Programming Lab Exercises

The document contains Python programs to solve various exercises involving basic concepts like loops, functions, strings, lists, tuples, dictionaries and more. It includes programs to check scores and leap years, find averages, factorials, prime numbers, calculate areas, series, palindromes and more.
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)
24 views2 pages

Python Programming Lab Exercises

The document contains Python programs to solve various exercises involving basic concepts like loops, functions, strings, lists, tuples, dictionaries and more. It includes programs to check scores and leap years, find averages, factorials, prime numbers, calculate areas, series, palindromes and more.
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

Exercise-1:

1. Implement a Python Program to Prompt for a Score between 0.0 and 1.0. If the Score Is Out of
Range, Print an Error. If the Score Is between 0.0
and 1.0, Print a Grade Using the Following Table.
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F

2. Implement a Python Program to Check If a Given Year Is a Leap Year.

Exercise-2:
3. Implement a Python Program to Display First 10 Numbers Using
while Loop Starting from 0 to 9.
4. Implement a Program to Find the Average of n Natural Numbers Where n Is the Input
from the User.

Exercise-3:
5. Implement a Python Program to Find the GCD of Two Positive Numbers.
6. Implement a Program to Find the Sum of All Odd and Even Numbers up to a Number Specified by the
User.

Exercise-4:
7. Implement a Python Program to Find the Factorial of a Number.

8. Implement a Python Program to Check Whether a Number Is Prime or Not.

Exercise-5:
9. Implement a Python Program to Demonstrate continue Statement.
10. Implement a Python Program to Demonstrate a Function with and without Arguments.
Exercise-6:
11. Implement a Python Program to Find the Area of Trapezium Using the Formula Area
= (1/2) * (a + b) * h Where a and b Are the 2 Bases of Trapezium and h Is the Height.
12. Implement a Python Program to Demonstrate the Return of Multiple Values from a
Function Definition

Exercise-7:
13. Implement a Python Program to calculate the value of sin(x) up to n Terms Using the Series
3 x5 x7
sin (x) x x ⋯ Where x Is in Degrees 1! 3!
5! 7!
14. Implement a Python Program to Check If a 3 Digit Number Is Armstrong Number or Not.

Exercise-8:
15. Implement a Python Program to calculate and Add the Surface Area of Two
Cubes. Use Nested Functions

16. Implement a Python Program to Demonstrate the Use of *args and **kwargs.

Exercise-9:
17. Implement a Python Code to Determine Whether the Given String Is a Palindrome or Not Using
Slicing.
18. Implement a Python Program to Demonstrate String Traversing Using the for Loop.
Exercise-10:
19. Implement a Python Program to Count the Total Number of Vowels, Consonants and Blanks in a
String.
20. Implement a Python Program to Calculate the Length of a String Without Using Built-In
len() Function.

Exercise-11:
21. Implement a Python Program That Accepts a Sentence and Calculate the Number of Words,
Digits, Uppercase Letters and Lowercase Letters.
22. Implement a Python Program to Convert Uppercase Letters to Lowercase and Vice
Versa.
23. Implement a Python Program to Convert Uppercase Letters to Lowercase and Vice
Versa.

Exercise-12:
24. Implement a Python Program to Find Mean, Variance and Standard Deviation of List Numbers.
25. Implement a Program to Find the Transpose of a Matrix.
26. Implement a Program to Find the Transpose of a Matrix.
27. Implement a Program to Dynamically Build User Input as a List.
Exercise-13:
28. Implement a Python Program to Count the Number of Times Each Word Appears in a Sentence.
29. Implement a Python Program to Generate a Dictionary That Contains (i: i*i) Such that i Is a
Number Ranging from 1 to n.

Exercise-14:

30. Implement a Python Program to Populate Tuple with User-Entered Items


[Link] a Python Program to Sort Words in a Sentence in Decreasing Order of Their Length.
Display the Sorted Words along with Their Length.

Common questions

Powered by AI

In Python, you can count the number of vowels, consonants, and blanks in a string by iterating over each character and checking it against lists of vowel characters (e.g., ['a', 'e', 'i', 'o', 'u']). Use conditions to increment respective counters. Consonants can be identified by checking if a character is alphabetic but not a vowel, while blanks can be found by checking if a character is a space .

To calculate and add the surface area of two cubes using nested functions in Python, you define an outer function to compute the surface area of a single cube using the formula 6 * side^2, where 'side' is the length of a side of the cube. Then, you define an inner function within this outer function to calculate the surface area of another cube. Use the inner function to process the second cube's side length, and sum the results from both functions for the final answer .

To determine if a string is a palindrome using slicing in Python, reverse the string using slicing and compare it to the original string. This can be achieved by setting up the comparison str == str[::-1], where str[::-1] reverses the string. If the condition is true, the string is a palindrome .

To calculate the value of sin(x) using a series expansion in Python, convert the angle x from degrees to radians since the series depends on radian measure. The series expansion for sin(x) is x - x^3/3! + x^5/5! - x^7/7! + ... . Implement this using a loop or generator, iterating n times, and summing the terms. Each term can be calculated as ((-1)^k) * (x^(2*k+1)) / ((2*k+1)!), where k is the term index .

In Python, convert uppercase letters to lowercase and vice versa by iterating over each character of a string and using the swapcase() method, which efficiently swaps the case of each letter automatically .

To check if a 3-digit number is an Armstrong number in Python, sum the cubes of its digits and compare the result with the original number. Decompose the number into its digits, then compute each digit's cube, and sum these values. If the sum equals the original number, it is an Armstrong number. For example, the number 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153 .

To dynamically build a list from user input in Python, initialize an empty list and use a loop to take user inputs. Append each input to the list using the append() method. Continue until a stopping condition, such as entering an explicit 'exit' command or reaching a predefined number of inputs .

In Python, *args is used to pass a variable number of positional arguments to a function, allowing you to handle more arguments than initially defined. **kwargs allows passing a variable number of keyword arguments. For example, def fun(*args): can be used to process any number of positional arguments such as fun(1, 2, 3). Similarly, def fun(**kwargs): can handle keyword arguments like fun(a=1, b=2). When used, these parameters provide great flexibility in function definitions .

To transpose a matrix in Python without built-in functions, manually swap rows with columns. Create a new matrix with dimensions switched. Iterate over the rows of the original matrix and set the elements of the new matrix such that new_matrix[j][i] = original_matrix[i][j] for all i, j index combinations .

To sort words in a sentence by decreasing order of their length in Python, first split the sentence into words. Sort the list of words using the sort() method with a key that specifies each word’s length, using key=len and setting reverse=True for descending order. After sorting, iterate over the sorted list and print each word along with its length using a formatted string .

You might also like