Python Programming Lab Exercises
Python Programming Lab Exercises
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 .