0% found this document useful (0 votes)
14 views1 page

Python Programming Lab Exercises

The document is a problem sheet for the M. Sc Data Science II Semester Python Programming Lab at PSG College of Technology. It includes seven programming tasks that cover basic input/output statements, variable usage, and operations, such as calculations, temperature conversion, simple interest computation, salary calculation, and population projection. Each task is designed to enhance programming skills in Python.

Uploaded by

wimel85164
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Python Programming Lab Exercises

The document is a problem sheet for the M. Sc Data Science II Semester Python Programming Lab at PSG College of Technology. It includes seven programming tasks that cover basic input/output statements, variable usage, and operations, such as calculations, temperature conversion, simple interest computation, salary calculation, and population projection. Each task is designed to enhance programming skills in Python.

Uploaded by

wimel85164
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PSG College of Technology COIMBATORE – 641004

M. Sc Data Science – II Semester


23XD28 - Python Programming Lab
Problem Sheet – 1 (Input, Output Statements, Variables, Operators)

1. Write a program that displays the result of


( 9 ×5 )−(14+(30 /3))
(26+(78−72))
2. Write a program that displays the sum of first hundred natural numbers.
3. Write a program to convert a given temperature in Celsius to Fahrenheit and vice versa.
4. Write a program to compute Simple Interest (SI) for a given principal, rate of interest and
duration in years.
5. Write a program to compute the salary of an employee. The user is prompted to enter the
Basic Salary. Dearness Allowance (DA) is 120% of Basic and City Compensatory
Allowance (CCA) is 20% of Basic. Total salary is Basic + DA + CCA.
6. The US Census Bureau projects population based on the following assumptions:
One birth every 7 seconds
One death every 13 seconds
One new immigrant every 45 seconds
Write a program to display the population for each of the next five years. Assume the
current population is 312032486 and one year has 365 days. Hint: in Python, you can use
integer division operator // to perform division. The result is an integer. For example, 5 //
4 is 1 (not 1.25) and 10 // 4 is 2 (not 2.5).
7. Write a program to calculate the perimeter and area of a rectangle.

Common questions

Powered by AI

Projecting future populations using daily birth, death, and immigration rates can present challenges such as accumulative rounding errors, changing rates over time, and real-world unpredictability (e.g., policy changes or natural disasters). Over extended periods, even minor initial inaccuracies can significantly skew results due to the compounding effect, leading to large divergences between projected and actual populations. Ensuring computational accuracy may require periodic revision of the input assumptions and the incorporation of stochastic models to simulate variable conditions .

To determine annual population changes, the program should account for the number of births, deaths, and immigrants each adding to or subtracting from the base population. With given rates, births occur every 7 seconds, deaths every 13 seconds, and immigration every 45 seconds. In one year (365 days), the total population change is calculated as the sum of new births, reduced by deaths, and increased by immigrants. Integer division is crucial here as it allows calculating these occurrences within a non-fractional context (e.g., total births = total seconds per year // 7), ensuring only whole numbers are considered, which aligns with real-world demographics where fractions of a person do not exist .

Understanding arithmetic and operator precedence is crucial for writing accurate programs as it ensures that expressions are evaluated in the intended order. Operator precedence dictates that multiplication and division are performed before addition and subtraction, unless overridden by parentheses. Misunderstanding precedence can lead to errors in calculations, as seen in complex expressions involving multiple operations. For example, in the expression (9×5)-(14+(30/3))/(26+(78−72)), knowing the correct precedence ensures the operations inside the parentheses are completed before others, yielding the correct result .

The program for summing the first hundred natural numbers can be optimized using the formula n(n+1)/2 instead of iterative summation. This method, derived from the arithmetic series sum formula, reduces time complexity from O(n) to O(1), as it involves a simple arithmetic calculation regardless of the number of terms. This optimization improves performance, particularly for larger summations, by minimizing operations to a fixed set, independent of input size .

The total salary of an employee is computed by summing the Basic Salary, a Dearness Allowance (DA) which is 120% of Basic, and a City Compensatory Allowance (CCA) which is 20% of Basic. This formula emphasizes that the bulk of variation in total salary is directly dependent on the value of the Basic Salary. For financial planning, this means that any increments to the Basic Salary can significantly affect the total income because DA and CCA are proportional to it .

Input validation is crucial in Python programs to ensure accurate and reliable computations, particularly in financial calculations like interest or temperature conversions. Invalid inputs (e.g., strings where numbers are expected) can lead to errors and incorrect outputs. Input validation safeguards against such issues by verifying data types, ranges, and formats before processing, thus maintaining integrity and functionality. This is critical for preventing crashes or faulty logic flows that could impact data-driven decision making .

Choosing appropriate data types in programs involving user input is critical, as it ensures data integrity, optimizes performance, and prevents errors. For instance, calculating salary components involves numerical operations on values like Basic Salary, DA, and CCA, which should use integer or floating-point types depending on precision needs. Incorrect data type selection, such as using strings for arithmetic operations, can lead to execution errors. Using the correct data type also facilitates faster data processing and storage optimization .

Data abstraction in programming refers to the separation of the high-level functionality from the low-level implementation details. When calculating the area and perimeter of a rectangle, one abstracts the concepts of length and width, allowing the creation of functions like calculate_area(length, width) and calculate_perimeter(length, width). This abstraction conceals the procedural complexity, enables code reuse, and enhances maintenance by localizing changes to specific function definitions, thus boosting code accuracy through encapsulation .

In Python, simple interest can be calculated using the straightforward formula SI = (P * R * T) / 100, where P is the principal amount, R is the rate of interest, and T is the time in years. A Python program would typically prompt the user for these input values, compute the interest, and then display it. This formula's simplicity allows for easy integration into financial analytics systems, providing fundamental insights into investment growth or loan cost over time .

The temperature conversion from Celsius to Fahrenheit involves using the formula F = (C * 9/5) + 32, where F represents the temperature in Fahrenheit and C represents the temperature in Celsius. This operation involves a basic arithmetic sequence of multiplication and addition which transforms the Celsius scaling to the Fahrenheit scale, capturing the difference in offset and scale between these two measures of temperature .

You might also like