ALGORITHMIC_THINKING_WITH_PYTHON_LAB_MANUAL[1] (1)
ALGORITHMIC_THINKING_WITH_PYTHON_LAB_MANUAL[1] (1)
AIM:
To write a Python program to develop simple desktop calculator(Only the five basic arithmetic
operators.)
ALGORITHM:
Step 1: Start
Step 3: Sum=a+b
Step 5: Diff=a-b
Step 7: Product=a*b
Step 9: Division=a/b
PROGRAM:
# Define numbers
a = 10
b=3
c=5
# Addition
Sum = a + b
print("Addition: a + b =", Sum)
# Subtraction
Diff = a-b
# Product
Product = a * b
# Division
Div = a / b
# Modulus
Modulus = a % b
OUTPUT:
RESULT:
STRING OPERATIONS
AIM:
To create, concatenate, and print a string and access a sub-string from a given string.
ALGORITHM:
Step 1: Start
Step 8: Stop
PROGRAM:
# Create strings
str1 = "Hello"
str2 = "World"
# Concatenate strings
print("Concatenated String:")
# Accessing substrings
# Example string
print("\nSubstrings:")
OUTPUT:
RESULT:
AIM:
ALGORITHM:
Step 1: Start
Step 3: Use the datetime.now() method to fetch the current local date and time and store it in
variable now.
Step 7: Use strftime("%A, %B %d, %Y %I:%M %p") to format as Weekday, Month Day, Year
Hour:Minute AM/PM and store this in formatted_now_3
Step 9: Stop
PROGRAM
OUTPUT
RESULT:
LIST OPERATIONS
AIM:
To write a program to create, append, and remove lists in Python using NumPy.
ALGORITHM:
Step 1: Start.
Step 11: Append a new Column into array2 and print array2.
PROGRAM:
import numpy as np
# NumPy doesn't have a direct remove method, but you can use np.delete()
array1 = np.delete(array1, 2)
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
EXPERIMENT 5
AIM:
ALGORITHM:
Step 1: Start.
Step 10: Call the function find_largest(num1, num2, num3) and store the result.
PROGRAM:
largest = a
largest = b
else:
largest = c
return largest
# Example usage
num1 = 10
num2 = 25
num3 = 15
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
EXPERIMENT 6
TEMPERATURE CONVERSION
AIM:
To write a program to convert temperature values back and forth between Celsius (c), and
Fahrenheit (f). [Formula: c/5 = f-32/9]
ALGORITHM:
Step 1: Start.
Step 13: Call the function celsius_to_fahrenheit(temp_c) and print the result.
Step 14: Call the function fahrenheit_to_celsius(temp_f) and print the result.
PROGRAM:
def celsius_to_fahrenheit(celsius):
return fahrenheit
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
return celsius
# Example usage
temp_c = 25 # Celsius
temp_f = 77 # Fahrenheit
converted_f = celsius_to_fahrenheit(temp_c)
converted_c = fahrenheit_to_celsius(temp_f)
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
EXPERIMENT 7
PATTERN CONSTRUCTION
AIM:
To write a Program to construct patterns of stars (*), using a nested for loop.
ALGORITHM:
Case 1:
Step 1: Start.
Step 2: Define a function star_pattern(rows) to construct a right-angled triangle star
pattern.
Step 3: Inside the function star_pattern(rows), do steps 4-6.
Step 4: For i in the range from 1 to rows + 1, do steps 5-6.
Step 5: For j in the range from i, print a star without a newline.
Step 6: After printing stars in a row, move to the next line.
Step 7: Initialize the variable rows with the value 5.
Step 8: Call the function star_pattern(rows).
Step 9: Stop.
Case 2:
Step 1: Start.
Step 2: Define a function inverted_star_pattern(rows) to construct an inverted triangle
star pattern.
Step 3: Inside the function inverted_star_pattern(rows), do steps 4-6.
Step 4: For i in the range from rows to 0, do steps 5-6.
Step 5: For j in the range i, print a star without a newline.
Step 6: After printing stars in a row, move to the next line.
Step 7: Initialize the variable rows with a value .
Step 8: Call the function inverted_star_pattern(rows).
Step 9: Stop.
Case 3:
Step 1: Start.
Step 2: Define a function pyramid_star_pattern(rows) to construct a pyramid star pattern.
Step 3: Inside the function pyramid_star_pattern(rows), do steps 4-6.
Step 4: For i in the range from 1 to rows + 1, do steps 5-6.
Step 5: Print spaces followed by stars.
Case 1:
def star_pattern(rows):
# Example usage
rows = 5
star_pattern(rows)
Case 2:
def inverted_star_pattern(rows):
for i in range(rows, 0, -1): # Outer loop for each row (descending order)
print("*", end="")
# Example usage
rows = 5
inverted_star_pattern(rows)
Case 3:
def pyramid_star_pattern(rows):
# Example usage
rows = 5
pyramid_star_pattern(rows)
OUTPUT:
Case 1:
Case 2:
Case 3:
RESULT:
The program was executed successfully and the output was verified.
EXPERIMENT 8
AIM:
ALGORITHM:
Step 1: Start.
Step 2: Define a function is_prime(num) to check if a number is prime.
Step 3: Inside the function is_prime(num), do steps 4-7.
Step 4: Check if num is less than 2.
Step 5: If num is less than 2, return False.
Step 6: For i in the range from 2 to square root of (num + 1), do steps 7-8.
Step 7: If num is divisible by i, return False.
Step 8: Return True.
Step 9: Define a function print_primes_less_than_20() where num=20.
Step 10: Inside the function print_primes_less_than_20(), do steps 11-13.
Step 11: For num in the range from 2 to 20, do steps 12-13.
Step 12: If is_prime(num) returns True, print num.
Step 13: Call the function print_primes_less_than_20().
Step 14: Print a message indicating the prime numbers less than 20.
Step 15: Stop.
PROGRAM:
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1): # Check divisibility up to the square root of the
number
if num % i == 0:
return False
return True
# Function to print prime numbers less than 20
def print_primes_less_than_20():
if is_prime(num):
# Example usage
print_primes_less_than_20()
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
EXPERIMENT 9
AIM:
ALGORITHM:
Step 1: Start.
Step 8: Call the function factorial(num) and store the result in result.
PROGRAM:
def factorial(n):
return 1
else:
# Example usage
num = 5
result = factorial(num)
print(f"The factorial of {num} is: {result}")
OUTPUT:
RESULT:
The program was executed successfully and the output was verified
EXPERIMENT 10
AIM:
ALGORITHM:
Step 1: Start.
Step 8: Call the function add_recursive(num1, num2) and store the result in result.
PROGRAM:
return a
else:
# Example usage
num1 = 5
num2 = 3
result = add_recursive(num1, num2)
OUTPUT:
RESULT:
The program was executed successfully and the output was verified
EXPERIMENT 11
AIM:
ALGORITHM:
Step 1: Start.
Step 8: Call the function factorial(num) and store the result in result.
PROGRAM:
return 0
else:
# Example usage
num1 = 5
num2 = 3
result = multiply_recursive(num1, num2)
RESULT:
The program was executed successfully and the output was verified
EXPERIMENT NO: 12
AIM:
To find the greatest common divisor of two positive numbers using recursion
ALGORITHM:
Step 1: Start.
Step 2: Define a function gcd_recursive(a, b) to find the GCD of two positive numbers.
Step 8: Call the function gcd_recursive(num1, num2) and store the result in result.
PROGRAM:
return a
else:
# Example usage
num1 = 48
num2 = 18
OUTPUT:
RESULT:
The program was executed successfully and the output was verified.
EXPERIMENT 13
AIM:
To write a Program that accepts the lengths of three sides of a triangle as inputs. The program
should output whether or not the triangle is a right triangle (Recall from the Pythagorean
Theorem that in a right triangle, the square of one side equals the sum of the squares of the other
two sides). Implement using functions.
ALGORITHM:
Step 1: Start.
Step 4: Sort the sides [a, b, c] and store the result in sides (with the largest side last).
Step 5: Use the Pythagorean Theorem to check if sides[0]^2 + sides[1]^2 is approximately equal
to sides[2]^2.
Step 7: Define a function main() to get user input and check if the sides form a right triangle.
Step 9: Use a try block to get user input for a, b, and c (the lengths of the three sides).
Step 10: Call the function is_right_triangle(a, b, c) to check if the sides form a right triangle.
Step 11: If the function returns True, print "The triangle is a right triangle."
Step 13: Use an except block to catch ValueError and print "Please enter valid numbers."
PROGRAM:
import math
def main():
try:
if is_right_triangle(a, b, c):
else:
except ValueError:
# Example usage
main()
OUTPUT
RESULT:
The program was executed successfully and the output was verified
EXPERIMENT NO: 14
FIBONACCI SERIES
AIM:
To define a module to find Fibonacci Numbers and import the module to another program.
ALGORITHM:
Step 2.3: Create an empty list fib_sequence to store the Fibonacci numbers.
Step 3.3: Update a and b to the next numbers in the sequence using a, b = b, a + b.
Step 1:Start
Step 4: Call the imported fibonacci(n) function and store the result (a list of Fibonacci numbers)
in fib_sequence.
Step 6: Stop
PROGRAM:
# fibonacci_module.py
def fibonacci(n):
"""
"""
fib_sequence = []
a, b = 0, 1
for _ in range(n):
fib_sequence.append(a)
a, b = b, a + b
return fib_sequence
# main_program.py
fib_sequence = fibonacci(n)
RESULT:
The program was executed successfully and the output was verified.
EXPERIMENT NO: 15
AIM:
To check whether the given number is a valid mobile number or not using functions.
ALGORITHM:
Step 1: Start
Step 2.4 Return True if all conditions are met, otherwise return False.
Step 6: Else
Step 7: Stop
PROGRAM
def is_valid_mobile_number(number):
return True
return False
# Input: Mobile number as a string
if is_valid_mobile_number(mobile_number):
else:
OUTPUT
RESULT
MERGING AND SORTING TWO LISTS WITH EVEN NUMBERS FIRST AND ODD
NUMBERS LAST
AIM:
To input two lists from the user. Merge these lists into a third list such that in the merged list, all
even numbers occur first followed by odd numbers.
ALGORITHM:
Step 1: Start
Step 2: Input Two Lists from the User , list1 and list2
Step 3: Use the concatenation operator (+) to merge list1 and list2 into a new list called
merged_list
Step 4.2: Provide a custom sorting key using lambda x: (x % 2, x), which sorts:
where even numbers have a lower value (0) and come first.
Step 4.2.2: For even and odd numbers separately, sort by the number itself.
Step 5: Print the sorted list with even numbers first, followed by odd numbers, both in
ascending order.
Step 6: Stop
PROGRAM
list1 = list(map(int, input("Enter the elements of the first list separated by spaces: ").split()))
list2 = list(map(int, input("Enter the elements of the second list separated by spaces: ").split()))
# - Then sort by the number itself for the even and odd parts
print("Merged and sorted list with even numbers first, followed by odd numbers:")
print(sorted_list)
OUTPUT
RESULT
AIM:
To implement a simple two-player stick game where players take turns picking 1, 2, or 3 sticks.
The player forced to take the last stick loses.
ALGORITHM:
Step 1: Start
Step 3: While total_sticks > 0, continue the loop, allowing both players to take turns.
Step 4.2: Ask Player 1 to input their choice (player1_choice) of how many sticks to
take (1, 2, or 3).
Step 5.1: Ensure Player 1's choice is valid by checking: The number is between 1 and 3
and does not exceed the remaining sticks.
Step 5.2: If the input is invalid, keep asking for a valid input.
Step 7.1: If total_sticks == 0 after Player 1’s turn, print "Player 1 has taken the last stick.
Player 1 loses!" and end the game.
Step 9: The game ends when either Player 1 or Player 2 is forced to take the last stick, and that
player loses.
PROGRAM:
total_sticks = 16
# Game loop
total_sticks -= player1_choice
if total_sticks == 0:
break
total_sticks -= player2_choice
if total_sticks == 0:
break
OUTPUT:
RESULT:
AIM:
To implement the famous Monty Hall problem, allowing players to understand the probabilities
involved in the game when choosing to stay with their initial choice or switch after a door is
opened.
ALGORITHM:
Step 1: Start
Step 4: Create two counters: stay_wins = 0 (for wins when the player stays) and switch_wins =
0 (for wins when the player switches).
Step 6.1: f the player's initial choice has the car, increment stay_wins.
Step 6.2: If the switching door has the car, increment switch_wins.
Step 8: Set the number of trials (e.g., num_trials = 10000) and Call the function and capture the
results.
PROGRAM:
import random
def monty_hall_simulation(num_trials):
stay_wins = 0
switch_wins = 0
for _ in range(num_trials):
doors = [0, 0, 0]
car_position = random.randint(0, 2)
doors[car_position] = 1
player_choice = random.randint(0, 2)
door_opened_by_host = random.choice(remaining_doors)
if doors[player_choice] == 1:
stay_wins += 1
if doors[switch_choice] == 1:
switch_wins += 1
num_trials = 10000
OUTPUT:
RESULT: