Python Basics: Functions & Calculations
Python Basics: Functions & Calculations
A Python program can calculate an electricity bill by applying rates for different usage slabs through a series of if-elif statements. For units up to 50, multiply by 1.5; for units up to 150, add the previous total to (units - 50) * 2.5; for up to 250, add the total to (units - 150) * 4; beyond 250, add the total to (units - 250) * 6. Include a fixed charge at the end for maintenance. This approach allocates costs appropriately based on usage tiers, ensuring accurate billing .
To implement a loop in Python for printing natural numbers up to 'n', use a 'for' loop ranging from 1 to n+1, as it is exclusive of the end number. Print each number during each iteration. This approach uses precise loop control with a known number of iterations equal to 'n' .
To determine the largest number among three given numbers using if-else in Python, compare the numbers by checking conditions sequentially. Start by checking if the first number is greater than or equal to the second and the third numbers. If true, the first number is the largest. If not, check if the second number is greater than or equal to the first and third to decide if it is the largest. Otherwise, by default, the third number is the largest .
To calculate the sum and average of even and odd numbers from a dataset, iterate over the dataset with a loop. For each number, check its parity using the modulus operator (%). If a number is even (% yields 0), add it to the even sum and increment the even counter. Otherwise, add it to the odd sum and increment the odd counter. After processing all numbers, calculate the averages by dividing the sum by counts respectively, handling division by zero by checking counts before division .
To code a program that prints a multiplication table for a given number in Python, use a 'for' loop to iterate over the range 1 to 10. During each iteration, multiply the given number by the iterating variable and print the result in a formatted string. This method efficiently generates the multiplication table by leveraging loop structures for repetitive multiplication operations .
To calculate the factorial of a number in Python, recursive functions can be employed. Define a function that returns 1 for the base case where the input number 'n' is 0 or 1, as the factorial of both is 1. For other cases, return n multiplied by the factorial of (n-1), which recursively calculates the factorial until the base case is reached .
A Python program evaluates if a number is even or odd by using the modulus operator (%). If the number divided by 2 has a remainder of 0, it is even. Otherwise, it is odd. This operation relies on the properties of integer division and conditional statements to classify the number .
To sum natural numbers up to 'n' in Python, initialize a sum variable to 0. Use a 'for' loop to iterate from 1 to 'n', adding each integer to the sum variable. This iterative approach allows tracking cumulative addition to calculate the required sum effectively .
To calculate the area of a triangle with known side lengths, use Heron's formula. First, calculate the semi-perimeter 's' by averaging the sum of all sides: s = (a + b + c) / 2. Then, determine the area using the formula area = sqrt(s * (s - a) * (s - b) * (s - c)). This method leverages the triangle inequality theorem and ensures the area is calculated correctly for any triangle configuration .
To find the area and perimeter of a circle given its radius in Python, use the mathematical constants π (approximately 3.14). Compute the area with the formula πR² and the perimeter with 2πR. This exercise demonstrates understanding of basic geometry concepts and the ability to apply them programmatically using simple arithmetic operations .