DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET 1.1
Student Name: Aman thakur UID:22BCS16401
Branch: CSE Section/Group:901-A
Semester: 4 Date of Performance:15-01-24
Subject Name: Python Subject Code:22CSH-259
1. Aim: Implementing Secant Algorithms in Python
Variable, Print Statement, Commands, Data Types: Numeric Types, string , List
and Tuple , Control Structures: If, For and While, Functions : Function Definition,
Function Call, Return Statement ,Default Parameters
2. Source Code:
# Bisection Method
def bisection_method(f, a, b, tol=1e-6, max_iter=100):
print('\n\n*** BISECTION METHOD IMPLEMENTATION ***')
step = 1
condition = True
while condition:
c = (a + b) / 2.0
if f(a) * f(c) < 0:
b=c
else:
a=c
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
print('Iteration-%d, a = %0.6f, b = %0.6f, c = %0.6f and f(c) = %0.6f' %
(step, a, b, c, f(c)))
step = step + 1
condition = abs(f(c)) > tol and step <= max_iter
print('\n Required root is: %0.8f' % c)
# Newton-Raphson Method
def newton_raphson_method(f, f_prime, x0, tol=1e-6, max_iter=100):
print('\n\n*** NEWTON-RAPHSON METHOD IMPLEMENTATION
***')
step = 1
condition = True
while condition:
x1 = x0 - f(x0) / f_prime(x0)
print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1)))
if abs(x1 - x0) < tol:
break
x0 = x1
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
step = step + 1
condition = step <= max_iter
print('\n Required root is: %0.8f' % x1)
# Example usage for Bisection method
def f(x):
return x**3 - 5*x - 9
a=2
b=4
tol = 1e-6
max_iter = 100
bisection_method(f, a, b, tol, max_iter)
# Example usage for Newton-Raphson method
def f_prime(x):
return 3*x**2 - 5
x0 = 3
newton_raphson_method(f, f_prime, x0, tol, max_iter)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
else:
print("x is negative")
# For Loop for Iterating Over a List (my_list)
my_list = [1, 2, 3, 4] # Replace this with your actual list
print("Elements in my_list:")
for num in my_list:
print(num)
# While Loop to Print Numbers Up to 5
count = 0
print("Numbers up to 5:")
while count < 5:
print(count)
count += 1
2.4- CODE FOR CALCULATOR:
# Checking the Sign of a Variable (x)
x = 10 # Replace 10 with the actual value you want to check
# Check if x is positive, zero, or negative
if x > 0:
print("x is positive")
elif x == 0:
print("x is zero")
else:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
print("x is negative")
# For Loop for Iterating Over a List (my_list)
my_list = [1, 2, 3, 4] # Replace this with your actual list
# Iterate over the elements in my_list and print each element
print("Elements in my_list:")
for num in my_list:
print(num)
# While Loop to Print Numbers Up to 5
count = 0
# Print numbers up to 5 using a while loop
print("Numbers up to 5:")
while count < 5:
print(count)
count += 1
# CODE FOR CALCULATOR
# Define functions for basic arithmetic operations
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
# Check if y is not zero before performing division
if y != 0:
return x / y
else:
return "Cannot divide by zero"
# Define a calculator function
def calculator():
print("Simple Calculator")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. ALL")
# Get user input for operation choice
choice = input("Enter choice (1/2/3/4/5): ")
# Check the user's choice and perform the corresponding operation
if choice in ('1', '2', '3', '4', '5'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
elif choice == '5':
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
# Display results for all operations
print(f"{num1} / {num2} = {divide(num1, num2)}\n{num1} + {num2}
= {add(num1, num2)}\n{num1} - {num2} = {subtract(num1, num2)}\n{num1}
* {num2} = {multiply(num1, num2)}")
else:
print("Invalid input. Please enter a valid choice.")
# Run the calculator function if the script is executed directly
if __name__ == "__main__":
calculator()
2.5 CODE FOR STRING REVERSAL:
# Define a function to reverse a given string
def reverse_string(input_string):
return input_string[::-1]
# Example usage
original_string = "Hello, World!"
# Call the reverse_string function with the original string
reversed_string = reverse_string(original_string)
# Print the original and reversed strings
print(f"Original String: {original_string}")
print(f"Reversed String: {reversed_string}")
3. Screenshot of Outputs:
3.1- OUTPUT FOR Variables, Print Statement, Commands:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3.2 OUTPUT FOR DATA TYPES:
3.3 OUTPUT FOR CONTROL STRUCTURE:
3.4 OUTPUT FOR FUNCTIONS:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3.5 OUTPUT FOR CALCULATOR:
3.6 OUTPUT FOR STRING REVERSAL:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING