0% found this document useful (0 votes)
76 views10 pages

CHANDAN KUMAR Assignment - 01

This document contains solutions to 10 programming problems in Python. It explains concepts like type conversion, calculating area of geometric shapes, solving quadratic equations, and checking character types. For each problem, it provides the question, outlines the logic, and includes the full Python code for the solution.

Uploaded by

kumarichhoti4492
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)
76 views10 pages

CHANDAN KUMAR Assignment - 01

This document contains solutions to 10 programming problems in Python. It explains concepts like type conversion, calculating area of geometric shapes, solving quadratic equations, and checking character types. For each problem, it provides the question, outlines the logic, and includes the full Python code for the solution.

Uploaded by

kumarichhoti4492
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
You are on page 1/ 10

Assignment:- 01

1. What is type conversion? Explain two types of conversion with


examples.
Ans :- In Python, type conversion refers to the process of changing
the data type of a variable from one type to another. Python provides
built-in functions for performing these conversions. Two common
types of type conversion are:
1. Implicit Type Conversion (or Coercion):
 Implicit type conversion is performed automatically by
Python during certain operations when the data types of
the operands are compatible.
 For example, when you perform arithmetic operations
between different numeric types, Python will
automatically convert the operands to a common type.
2. # Example python code of Implicit conversion from int to float
3. x = 5
4. y = 2.0
5. result = x + y
6. print(result)
output :- 7.0

In this example, the integer x is implicitly converted to a float


before performing the addition operation with the float y.
2. Explicit Type Conversion (or Type Casting):
 Explicit type conversion is performed by the programmer
using built-in functions to convert a variable from one
data type to another.
Common functions for explicit type conversion include int(), float(),
str(), etc.
# Example python code of Explicit conversion from float to int
a = 10.5
b = int(a)
print(b)
Output :- 10

In this example, the float variable a is explicitly converted to an int


using the int() function.
It's important to note that explicit type conversion may result in loss
of data or precision, especially when converting to a narrower data
type. Care should be taken to handle potential issues that may arise
during type conversion.

2. Write a Python program to find the area of triangle when we


know the lengths of all three of its sides.

Ans :- We use here Heron’s formula to find the area of triangle when
the lengths of all three sides are known. Heron’s formula states that
the area (A) of triangle with sides of lengths ‘a’, ‘b’ and ‘c’ is given by:

A = √ S∗( s−a )∗( s−b )∗(s−c)

Where ‘S’ is semi-perimeter of the triangle calculated as:


a+b+ c
S= 2
Here’s python program that takes the triangle of the three sides as
input and calculates the area of the triangle:
import math

def calculate_triangle_area(a, b, c):


# Calculate semi-perimeter
s = (a + b + c) / 2

# Calculate area using Heron's formula


area = math.sqrt(s * (s - a) * (s - b) * (s - c))
return area

# Input: lengths of the three sides


side_a = float(input("Enter the length of side a: "))
side_b = float(input("Enter the length of side b: "))
side_c = float(input("Enter the length of side c: "))

# Calculate and display the area


triangle_area = calculate_triangle_area(side_a, side_b, side_c)
print(f"The area of the triangle is: {triangle_area}")

Output :-

3. Write a Python program to find the area and perimeter of a


rectangle.
Ans :- This program takes user input for the length and width of the
rectangle, calculates the area and perimeter using the provided
functions, and then prints the results to the console.
Here's a simple Python program to calculate the area and perimeter
of a rectangle:
import math

def calculate_rectangle_area(length, width):


area = length * width
return area

def calculate_rectangle_perimeter(length, width):


perimeter = 2 * (length + width)
return perimeter

# Input: length and width of the rectangle


length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# Calculate area and perimeter


rectangle_area = calculate_rectangle_area(length, width)
rectangle_perimeter = calculate_rectangle_perimeter(length, width)

# Display the results


print(f"The area of the rectangle is: {rectangle_area}")
print(f"The perimeter of the rectangle is: {rectangle_perimeter}")
Output :-

4. Convert the following mathematical expression into Python


Equivalent
i) area= s(s-a)(s-b)(s-c)
Ans :- The mathematical expression you've provided is Heron's
formula for calculating the area of a triangle, where s is the semi-
perimeter, and a, b, and c are the lengths of the sides. Here's the
equivalent Python code for this expression:
import math

def calculate_triangle_area(a, b, c):


# Calculate semi-perimeter
s = (a + b + c) / 2

# Calculate area using Heron's formula


area = math.sqrt(s * (s - a) * (s - b) * (s - c))
return area

# Example: lengths of the three sides


side_a = float(input("Enter the length of side a: "))
side_b = float(input("Enter the length of side b: "))
side_c = float(input("Enter the length of side c: "))

# Calculate and display the area


triangle_area = calculate_triangle_area(side_a, side_b, side_c)
print(f"The area of the triangle is: {triangle_area}")
Output :-

ii) x = -b + b2 -4ac.

Ans :- Below are the equivalent python code for this expression :
import cmath # Import the complex math module for handling complex roots

# Coefficients of the quadratic equation ax^2 + bx + c = 0


a = float(input("Enter the coefficient a: "))
b = float(input("Enter the coefficient b: "))
c = float(input("Enter the coefficient c: "))

# Calculate the discriminant


discriminant = b**2 - 4 * a * c

# Check if the discriminant is non-negative for real roots


if discriminant >= 0:
# Calculate the two real roots
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
print(f"The real roots are: {root1} and {root2}")
else:
# Calculate the complex roots
complex_root1 = (-b + cmath.sqrt(discriminant)) / (2 * a)
complex_root2 = (-b - cmath.sqrt(discriminant)) / (2 * a)
print(f"The complex roots are: {complex_root1} and {complex_root2}")

Output : -

This code calculates the discriminant and then checks whether it is


non-negative to determine if the roots are real or complex. It then
calculates and prints the roots accordingly.
5. Write a Python program to find the reverse number of any 3 digit
number.
Ans :- Here's a simple Python program to find the reverse of a three-
digit number:
# Input: Get a 3-digit number from the user
number = int(input("Enter a 3-digit number: "))

# Check if the input is a 3-digit number


if 100 <= number <= 999:
# Calculate the reverse of the number
reverse_number = int(str(number)[::-1])

# Display the reverse number


print(f"The reverse of {number} is: {reverse_number}")
else:
print("Please enter a valid 3-digit number.")
Output :-

6. Write a Python program to Enter any Number and print how many
note of given number in 2000,1000,500,200,100,50,20,10,5,2,1.
Ans :- Here's a Python program that takes a number as input and
calculates the number of each denomination of currency notes in it:
def count_notes(amount):
denominations = [2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
notes_count = {}

for denomination in denominations:


count = amount // denomination
if count > 0:
notes_count[denomination] = count
amount %= denomination

return notes_count

# Input: Get a number from the user


amount = int(input("Enter the amount: "))
# Check if the input amount is positive
if amount > 0:
# Calculate and display the count of each denomination
notes_count = count_notes(amount)
print("Number of notes for each denomination:")
for denomination, count in notes_count.items():
print(f"{denomination} : {count}")
else:
print("Please enter a positive amount.")

Output :-

7. Take three integer values from user and print lowest among them.
Ans :- Here's a simple Python program that takes three integer values
from the user and prints the lowest among them:
# Input: Get three integers from the user
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
num3 = int(input("Enter the third integer: "))

# Find the lowest among the three numbers


lowest = min(num1, num2, num3)

# Display the result


print(f"The lowest among {num1}, {num2}, and {num3} is: {lowest}")
Output :-
8. Take two int values a & b from user and print interchange value of
a & b without using any other variable.
Ans :- we can swap the values of two variables a and b without using any
other variable by using arithmetic operations. Here's a Python program to
achieve this:
# Input: Get two integers from the user
a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))

# Swap values without using a temporary variable


a = a + b
b = a - b
a = a - b

# Display the interchange values


print(f"After swapping, the value of a: {a}")
print(f"After swapping, the value of b: {b}")
Output :-

9. Write a program to check whether a entered character is


lowercase ( a to z ) or uppercase ( A to Z ).
Ans :- Here's a Python program that checks whether an entered character
is lowercase (a to z) or uppercase (A to Z):
# Input: Get a character from the user
char = input("Enter a character: ")

# Check if the input is a single character


if len(char) == 1:
# Check if the character is lowercase
if 'a' <= char <= 'z':
print(f"{char} is a lowercase character.")
# Check if the character is uppercase
elif 'A' <= char <= 'Z':
print(f"{char} is an uppercase character.")
else:
print(f"{char} is not a letter.")
else:
print("Please enter a single character.")
Output :-

10. Calculate the Result (Passed/Failed) of any student after


input the marks of 5 subject.
Ans :- Here's a Python program that takes the marks of 5 subjects as input
and calculates whether a student has passed or failed based on a
predefined passing criteria:
# Input: Get marks for 5 subjects from the user
subject1 = float(input("Enter the marks for subject 1: "))
subject2 = float(input("Enter the marks for subject 2: "))
subject3 = float(input("Enter the marks for subject 3: "))
subject4 = float(input("Enter the marks for subject 4: "))
subject5 = float(input("Enter the marks for subject 5: "))

# Define passing criteria (e.g., total marks out of 500)


passing_marks = 200

# Calculate the total marks


total_marks = subject1 + subject2 + subject3 + subject4 + subject5

# Check if the student has passed or failed


if total_marks >= passing_marks:
result = "Passed"
else:
result = "Failed"

# Display the result


print(f"Total Marks: {total_marks}")
print(f"Result: {result}")
Output :-
Note :- Here I am using VS code Ide for writing the code and taking
output.

NAME :- CHANDAN KUMAR


SUBJECT :- MACHINE LEARNING USING PYTHON ASSIGNMENT 01
EMAIL ID :- [email protected]
Mob No :- 8789145208
BRANCH :- ECE
COLLEGE :- SCE SAHARSA
REG NO :- 20104132019

You might also like