Python Programming Practice Exercises
Python Programming Practice Exercises
The program can prompt the user to enter the circle's radius using 'input()' and convert it to a floating-point number. It then uses the formula 'area = π * radius^2' (where π can be obtained using the 'math.pi' constant) to compute the area, which is subsequently printed.
A value can be assigned to a variable and then manipulated using arithmetic operations. For instance, with the initial value 5, multiplying by 2 gives 10, and subtracting 1 results in 9. This illustrates the efficiency of using limited operations to achieve different results. Additionally, these expressions can be printed in a single line using the format method or string concatenation.
By leveraging Python's tuple assignment feature, we can assign the multiples in one line, e.g., 'a, b, c, d, e = n, 2*n, 3*n, 4*n, 5*n', and then use a loop or print statement to display them. This technique reduces code length while maintaining readability.
The program can take three inputs 'a', 'b', 'c'. It updates the first two variables using 'a = a + b' and 'b = b + c'. This logic reflects scenarios where sequential values need adjustment without using temporary variables or extra space, common in optimization challenges.
The program collects user details via 'input()' and uses formatted strings to display them sequentially and in new lines, separated by commands that introduce blank lines. This enhances readability and demonstrates the importance of format control in reporting user-specific data.
Python's exponentiation operator '**' can be used to calculate powers of a number. By reading a number 'n', the program can compute 'n^2', 'n^3', and 'n^4' directly using expressions like 'n**2, n**3, n**4', and print them in a structured format for efficient computation and display.
The program needs to account for the current month's total number of days minus today's date. This requires conditional logic to determine how many days remaining by addressing the length of each possible month in the Gregorian calendar, including considerations for leap year adjustments if applicable.
The program accepts height in centimeters, then uses conversion constants: 1 inch = 2.54 cm and 1 foot = 12 inches. The conversion involves calculating inches from centimeters, then dividing by 12 to get feet along with any additional leftover inches. These results are formatted and printed to display the height in feet and inches.
The program can use the 'input()' function to prompt the user to press the enter key before displaying the punchline. This will provide a pause that creates an engaging interaction with the user.
For simple interest, the program uses 'SI = P * R * T / 100'. For compound interest, it uses 'CI = P * (1 + R/100)^T - P'. It prompts for principal (P), rate (R), and time (T), computes both interests and prints them, making them distinguishable by using clear variable names and comments.