Practical Writeups Python
Practical Writeups Python
—1:
Aim: To write a Python program that adds two numbers entered by the user.
Learning Objectives:
Theory:
Addition is a fundamental arithmetic operation that combines two numbers (operands) into a single result (sum).
Mathematical Representation:
2. Addition in Programming:
Addition in programming is a simple operation where the + operator is used to sum two values. These values can be:
3. Mixed Types – Python can handle operations between integers and floats automatically. Example: 4+2.5=6.5 .
Python interprets the input based on the data type, which needs careful handling for accurate results.
Python takes input using the input() function, which reads user input as a string by default.
To perform arithmetic operations, you must convert the string input into a numerical type (e.g., int or float) using int() or float()
functions.
Example:
Python supports both, and you can ensure precision by working with floats for decimal inputs.
To prevent program crashes due to invalid input (e.g., letters instead of numbers), exception handling with try-except blocks
can be used:
try:
except ValueError:
1. Commutative Property:
a+b=b+a
2. Associative Property:
(a + b) + c = a + (b + c)
3. Identity Property:
Code:
Output:
Learning Outcomes:
Course Outcomes:
Conclusion:
The program successfully adds two user-input numbers and displays the result, reinforcing the understanding of basic Python concepts.
Viva Questions:
Learning Objectives:
Theory:
The square root of a number is a value such that . For example, the square root of 16 is 4, as .
Mathematical Representation:
A square root is denoted by , and in Python, we compute it using either the ** operator or the math.sqrt() function.
For non-negative numbers, the square root exists and is a real number. For negative numbers, square roots exist in the complex
domain.
1. Using ** operator:
Python allows us to calculate the square root of a number by raising it to the power of .
Example: x ** 0.5.
Python's math module provides a function sqrt() for calculating the square root more efficiently and precisely.
Example: math.sqrt(x).
The square root of a negative number cannot be calculated in the real domain using the above methods. However, Python’s cmath
(complex math) module can handle square roots of negative numbers, representing them as complex numbers.
Code:
Output:
Learning Outcomes:
Course Outcomes:
Conclusion:
Experiment No.—3:
Learning Objectives:
Theory:
A triangle is a closed figure with three sides and three vertices. The area of a triangle represents the amount of space enclosed by its
three sides. Depending on the type of input data, there are multiple ways to calculate the area:
Geometric Concepts
1. Types of Triangles:
For triangles where the base (b) and height (h) are given:
Area= ½*b*h
h: The perpendicular height from the opposite vertex to the base.
2. Heron’s Formula:
Used when the lengths of all three sides (, , and ) are provided. It is based on the semi-perimeter ():
s = {a + b + c}/2
Area =(s(s-a)* s(s-b)* s(s-c))1/2
Key Points of Heron’s Formula:
For a right triangle with base (b) and perpendicular height (h):
Area= ½*b*h
4. Special Formula for an Equilateral Triangle:
1. Invalid Inputs: Ensure that the sides satisfy the triangle inequality theorem.
2. Negative or Zero Input: Base, height, or side lengths cannot be zero or negative. These cases must be validated.
3. Complex Scenarios: For curved triangles (spherical geometry), other mathematical approaches are required, beyond the scope of this
program.
Computational Concepts
1. Mathematical Libraries:
2. Time Complexity:
3. Input Validation:
Code:
Output:
Learning Outcomes:
Course Outcomes:
Conclusion:
1. Base and height formula for right or specific triangles where the base and height are given.
2. Heron’s formula, which is applicable for any triangle when all three sides are known.
It reinforces geometric problem-solving and demonstrates how mathematical formulas can be implemented programmatically.
Viva Questions:
1. What is the formula for calculating the area of a triangle using base and height?
2. How does Heron’s formula work for calculating the area?
3. How do you verify if three sides form a valid triangle?
4. Why is it necessary to use the math.sqrt() function in Heron’s formula?
5. Can you modify the program to handle invalid or zero input values for sides?
6. For Faculty use:
Experiment No.—4:
Aim: To write a Python program to determine whether a given number is odd or even.
IDE: Any Python-compatible text editor like Visual Studio Code, PyCharm, or Jupyter Notebook.
Libraries: None
Learning Objectives:
Theory:
An even number is any integer divisible by 2 without leaving a remainder. Examples: -6,-2,0,2,4,8 etc.
Mathematical representation:
n % 2 = 0 (for +ve numbers)
Zero (0):
Negative Integers:
The rules for odd and even numbers apply to negative integers as well. For example:
-4(even),
-5 (odd in absolute value).
3. Importance in Mathematics:
The modulo operator (%) calculates the remainder of the division of one number by another.
Conditional Logic: Identifying even or odd numbers is critical in algorithms like sorting, filtering, and distributing tasks.
Memory Allocation: In computer architecture, even and odd memory addresses may be treated differently.
Pattern Recognition: Creating number sequences, dividing tasks, or applying rules in games and simulations.
You can also determine whether a number is even or odd by dividing it by 2 and checking the integer part:
Code:
Output:
Learning Outcomes:
Course Outcomes:
Conclusion:
The program successfully checks if a number is odd or even by using the modulo operator %. It reinforces the understanding of basic
mathematical operations and conditional statements in Python.
Viva Questions: