Python Programming Exercises List
Python Programming Exercises List
To print a multiplication table for a given number n, iterate over a range, typically from 1 to 10 or another specified maximum. For each iteration i, compute and print the product of n and i. This can be done using a simple loop structure, effectively displaying 'n * i = result' for each line.
To determine if a given number is a perfect square, you can take the square root of the number and check if the result is an integer. In a program, this can be implemented by calculating the integer square root and squaring it again to see if you return to the original number. For example, for a given number n, compute sqrtN = integer(sqrt(n)). If sqrtN * sqrtN == n, then n is a perfect square.
To calculate the area of a triangle given its three sides a, b, and c, use Heron's formula: First compute the semi-perimeter s = (a + b + c) / 2. Then the area is sqrt(s * (s-a) * (s-b) * (s-c)). This method is efficient for any triangle as long as the sides satisfy the triangle inequality theorem.
To check if a number n is prime, first eliminate numbers less than 2. Then, check divisibility from 2 up to the square root of n. If any number in this range divides n evenly, it's not prime. The algorithm can be optimized by checking divisibility by 2 and 3 first, then using a loop with i and i+2 increments (for numbers 6k ± 1). If none divide, n is prime.
To swap two numbers without a third variable, you can use arithmetic operations. For example, if you have two numbers a and b, you can swap them by doing: 1) a = a + b, 2) b = a - b, 3) a = a - b. Alternatively, you can use bitwise XOR operations: 1) a = a ^ b, 2) b = a ^ b, 3) a = a ^ b. Both methods will result in the values of a and b being swapped without using a third variable.
To perform arithmetic operations using a switch case, first accept two operands and an operator as inputs. Use the switch construct, passing the operator as the switch expression. For each case within the switch, perform the respective operation: addition, subtraction, multiplication, or division, and print the result. Include a default case to handle invalid operators by displaying an error message.
To reverse a number programmatically, you can repeatedly extract the last digit and construct the reversed number. Initialize a result variable to 0. While the original number n is not zero, extract the last digit (n % 10), and update the result by multiplying it by 10 and adding the extracted digit. Divide n by 10 to remove the last digit. Continue until n becomes zero.
To convert a string from any case to its opposite case, iterate through each character in the string. For each character, check if it is uppercase using a function like isupper(), and if true, convert it to lowercase using tolower(). Conversely, if the character is lowercase, convert it to uppercase using toupper(). This can be implemented in most programming languages that support character operations.
To calculate the average marks from three subjects, you first need to accept the marks as inputs. Sum these three values and then divide the total by 3. In a program, you can create a function that accepts three arguments (marks in the three subjects), sums them up, and divides the result by 3 to get the average.
To find the largest number in a list, iterate through each element, maintaining a variable that tracks the largest value encountered. Initialize this variable to the first element of the list, then iterate, update this tracker if a larger value is found. After completing the loop, the tracker holds the largest number.