Niraj Python
Niraj Python
Program-1
PROCEDURE:
• First declare and initialize two variables a and b and print their sum.
• Second show the indentation error by spacing 4 whitespaces and print sum of
a and b
• Now, correct the indentation error by removing 4 whitespaces before print.
CODE:
# Write a program to purposefully raise Indentation Error and correct it
a=3
b=4
print(a+b) #raise indentation
CODE (Corrected):
# Write a program to purposefully raise Indentation Error and correct it
a=3
b=4
print("indentation correct",(a+b))
INPUT/OUTPUT:
pg. 1
RA2332241030027 / / 2024
INPUT/OUTPUT(Corrected):
RESULT:
pg. 2
RA2332241030027 / / 2024
Program-2
AIM: Write a program to compute distance between two point staking input
from the user (Pythagorean Theorem).
PROCEDURE:
• First take two variable a and b from input user.
• Then calculate the distance between them using the Pythagoras Theorem: distance
=((a**2)+(b**2))**0.5
• Then store the distance between two points using Pythagoras Theorem in c
variable and print the c.
CODE:
INPUT/OUTPUT:
RESULT:
pg. 3
RA2332241030027 / / 2024
Program-3
AIM: Write a program add.py that takes 2 numbers as command line arguments
PROCEDURE:
• Import the sys module:
• Define the add_numbers function:
• Takes two numbers (num1, num2) as input.
• Returns the sum of those numbers using the + operator.
• Define the main function:
• Checks if the correct number of command-line arguments are provided (should be 3,
including the script name itself)
• If not, prints an error message and exits the program.
• Retrieves the first two command-line arguments as strings. • Converts those strings to
floating-point numbers.
• Calls the add_numbers function with the parsed numbers as input, storing the result in
result.
• Prints the sum using an f-string to format the output.
• Call the main function
CODE:
import sys
def main():
if len(sys.argv) != 3:
print("enter only two number")
sys.exit(1)
num1 = float(sys.argv[1])
num2 = float(sys.argv[2])
INPUT/OUTPUT:
pg. 4
RA2332241030027 / / 2024
RESULT:
pg. 5
RA2332241030027 / / 2024
Program-4
PROCEDURE:
CODE:
x = int(input("enter first no"))
y = int(input("enter second no"))
x, y = y, x
print("FIRST NO=", x)
print("SECOND NUMBER =", y)
INPUT/OUTPUT:
RESULT:
pg. 6
RA2332241030027 / / 2024
Program-5
PROCEDURE:
• Take the input from user in variable num
• Apply the if condition in which if num is divided by 2 and gives remainder zero then num
iseven otherwise it is odd.
• Then we check the prime number if number is not divisible by any other number then the
number itself it is prime number
CODE:
num= int(input("enter the no"))if
num%2 == 0:
print("number is even")
else:
print("number is odd")
if num==1:
print("number is not prime")
elif num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num) break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
INPUT/OUTPUT:
RESULT:
pg. 7
RA2332241030027 / / 2024
Program -6
AIM: Write a program to find out the positive and negative number in list.
PROCEDURE:
Define the function find_positive_negative_numbers():
• This function takes no arguments and performs the task of identifying positive and negative
numbers in a list.
• Get input:
• Prompts the user to enter the number of elements they want in the list.
• Creates empty lists numbers, positive_numbers, and negative_numbers to store the values.
• Take user input for numbers:
• Uses a for loop to iterate n times (based on the user's input). • Prompts the user to enter each
number and appends it to the numbers list.
• Separate positive and negative numbers:
• Iterates through each number in the numbers list. • If a number is greater than 0, it's appended
to the positive_numbers list.
• If a number is less than 0, it's appended to the negative_numbers list.
• Print the results:
• Prints the final lists of positive numbers and negative numbers separately using formatted
output.
• Call the function:
• The code calls the find_positive_negative_numbers() function to start the process.
CODE:
def find_positive_negative_numbers():
n = int(input("Enter the number of elements in the list: "))
numbers = []
positive_numbers = []
negative_numbers = []
for i in range(n):
num = int(input(f"Enter element {i+1}: "))
numbers.append(num)
for num in numbers:
if num > 0:
positive_numbers.append(num)
elif num < 0:
negative_numbers.append(num)
print(f"Positive numbers: {positive_numbers}")
print(f"Negative numbers: {negative_numbers}")
find_positive_negative_numbers()
INPUT /OUTPUT:
pg. 8
RA2332241030027 / / 2024
RESULT:
pg. 9
RA2332241030027 / / 2024
Program -7
PROCEDURE:
• Define the get_median function:
• Takes a list t as input.
• Sorts the list in ascending order using sorted(t).
• Handles two cases based on the list's length:
• Odd length:
• Finds the middle index using int((len(ls)+1)/2 - 1).
• Returns the value at the middle index.
• Even length:
• Finds the two middle indices using int(len(ls)/2 - 1) and int(len(ls)/2).
• Calculates the average of the values at those indices and returns it.
• Creates a sample tuple t with 5 numbers.
• Calls the get_median function to find the median of t and prints the result.
CODE:
def get_median(a):
ls = sorted(a)
if len(ls) % 2 != 0:
m = int((len(ls)+1)/2 - 1)
return ls[m]
else:
m1 = int(len(ls)/2 - 1)
m2 = int(len(ls)/2)
return (ls[m1]+ls[m2])/2
a= (1, 2, 3, 4, 5)
print("median of the tuple a =",get_median(a))
INPUT/OUTPUT:
RESULT:
pg. 10
RA2332241030027 / / 2024
Program- 8
AIM: Write a program for finding whether the string is palindrome or not.
PROCEDURE:
• A string is said to be a palindrome if the reverse of the string is the same as the string.
Forexample, “PAPA” is a palindrome, but “TEST” is not a palindrome.
• Take a string str as input from user
• Convert the string to lowercase
• Used reversed function to store the reverse of a string
• Compare the reversed of a string to the input string
• If reversed of a string is equal to the string then the string is palindrome.
CODE:
str = input("Enter the string")
str = str.casefold()
rev = reversed(str)
if list(str) == list(rev):
print( "THE STRING IS PALINDROME !")
else:
print("THE STRING IS NOT PALINDROME !")
INPUT/OUTPUT:
RESULT:
pg. 11
RA2332241030027 / / 2024
Program-9
AIM: Write a function for breaking the set into the list of the sets.
PROCEDURE:
CODE:
lst = []
n = int(input("Enter number of elements"))
for i in range(0, n):
el = int(input())
lst.append(el)
los = list(map(lambda x: {x}, lst))
print("List of set:", los)
INPUT/OUTPUT:
RESULT:
pg. 12
RA2332241030027 / / 2024
Program-10
AIM: Write a program for printing the cube of list elements using lambda.
PROCEDURE:
INPUT/OUTPUT:
RESULT:
pg. 13