PYTHON LAB
Department Of Artificial Intelligence & Machine
Learning
Name of the Faculty:Ms Neethu A Designation: Assistant Department : Artificial
B Professor Intelligence & Machine
Learning
Student Batch: 2025-2029 Academic Year: 2025-26 Semester: S1
Subject Code & Name: CATEGORY L T P CREDIT
YEAR OF
INTRODUCTION
UCEST105 ALGORITHMIC
THINKING WITH PYTHON PCC 3 0 2 4 2024
Exp. Name of the Experiment Date
No
1 Simple desktop calculator using Python. Only the five basic 17-10-2025
arithmetic operators.
2 Create, concatenate, and print a string and access a sub-string from a 24-10-2025
given string.
3 Familiarize time and date in various formats (Eg. “Thu Jul 11 24-10-2025
[Link] IST 2024”).
4 Write a program to create, append, and remove lists in Python using 31-10-2025
NumPy.
5 Program to find the largest of three numbers. 31-10-2025
6 Convert temperature values back and forth between Celsius (c), and 31-10-2025
Fahrenheit (f). [Formula: c/5 = f-32/9]
7 Program to construct patterns of stars (*), using a nested for loop. 31-10-2025
8 A program that prints prime numbers less than N. 07-11-2025
9 Program to find the factorial of a number using Recursion. 07-11-2025
10 Recursive function to add two positive numbers. 07-11-2025
11 Recursive function to multiply two positive numbers. 07-11-2025
12 Recursive function to find the greatest common divisor of two 07-11-2025
positive numbers.
13 A program that accepts the lengths of three sides of a triangle as 21-11-2025
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.
14 Program to define a module to find Fibonacci Numbers and import 21-11-2025
the module to another program.
15 Program to check whether the given number is a valid mobile number 21-11-2025
or not using functions.
Rules:
1. Every number should contain exactly 10 digits.
2. The first digit should be 7 or 8 or 9
16 Input two lists from the user. Merge these lists into a third list such 21-11-2025
that in the merged list, all even numbers occur first followed by odd
numbers. Both the even numbers and odd numbers should be in
sorted order.
17 Write a program to play a sticks game in which there are 16 sticks. 28-11-2025
Two players take turns to play the game. Each player picks one set of
sticks (needn’t be adjacent) during his turn. A set contains 1, 2, or 3
sticks. The player who takes the last stick is the loser. The number of
sticks in the set is to be input.
18 Suppose you're on a game show, and you are given the choice of 28-11-2025
three doors: Behind one door is a car; behind the others, goats. You
pick a door, say No. 1, and the host, who knows what is behind the
doors, opens another door, say No. 3, which has a goat. He then asks,
"Do you want to pick door No. 2?" Is it to your advantage to switch
your choice?
(source:[Link]
Monty_Hall_problem#:~:text=The%20Monty%20Hall%20problem
%20is,the%20American%20Statistician%20in%201975.)
EXP NO:1
AIM
Simple desktop calculator using Python. Only the five basic arithmetic operators.
ALGORITHM
1. Start the program.
2. Display the menu for available arithmetic operations:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
3. Read the user’s choice of operation.
4. Input two numbers from the user.
5. Use conditional statements (if-elif-else) to perform the corresponding operation based
on the user’s choice.
6. Display the result of the chosen arithmetic operation.
7. End the program.
PSEUDOCODE
BEGIN
PRINT "Simple Calculator"
PRINT "1. Addition"
PRINT "2. Subtraction"
PRINT "3. Multiplication"
PRINT "4. Division"
PRINT "5. Modulus"
READ choice
READ num1, num2
IF choice == 1 THEN
result ← num1 + num2
PRINT "Sum =", result
ELSE IF choice == 2 THEN
result ← num1 - num2
PRINT "Difference =", result
ELSE IF choice == 3 THEN
result ← num1 * num2
PRINT "Product =", result
ELSE IF choice == 4 THEN
result ← num1 / num2
PRINT "Quotient =", result
ELSE IF choice == 5 THEN
result ← num1 % num2
PRINT "Remainder =", result
ELSE
PRINT "Invalid Choice"
END IF
END
PROGRAM
# Simple Desktop Calculator in Python
print("----- Simple Calculator -----")
print("Select operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Modulus (%)")
# take input from the user
choice = input("Enter choice (1/2/3/4/5): ")
# take two numbers from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# perform calculation based on user choice
if choice == '1':
result = num1 + num2
print(f"Result: {num1} + {num2} = {result}")
elif choice == '2':
result = num1 - num2
print(f"Result: {num1} - {num2} = {result}")
elif choice == '3':
result = num1 * num2
print(f"Result: {num1} * {num2} = {result}")
elif choice == '4':
if num2 != 0:
result = num1 / num2
print(f"Result: {num1} / {num2} = {result}")
else:
print("Error! Division by zero is not allowed.")
elif choice == '5':
result = num1 % num2
print(f"Result: {num1} % {num2} = {result}")
else:
print("Invalid input! Please select a valid operation.")
OUTPUT
----- Simple Calculator -----
Select operation:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Modulus (%)
Enter choice (1/2/3/4/5): 3
Enter first number: 5
Enter second number: 4
Result: 5.0 * 4.0 = 20.0
RESULT
A simple calculator program was successfully developed and executed using Python.
The program accurately performs the five basic arithmetic operations — addition, subtraction,
multiplication, division, and modulus — based on user selection.
EXP NO:2
AIM
Create, concatenate, and print a string and access a sub-string from a given string.
ALGORITHM
1. Start the program.
2. Declare and initialize two strings.
3. Concatenate the two strings using the + operator.
4. Display the concatenated string.
5. Access a substring from the given string using slicing (string[start:end]).
6. Display the substring.
7. Stop the program.
PSEUDOCODE
START
SET str1 ← "Hello"
SET str2 ← "World"
SET concat ← str1 + " " + str2
PRINT concat
SET substring ← concat[0:5]
PRINT substring
STOP
PROGRAM
# Program to create, concatenate and access substring from a string
# Creating strings
str1 = "Hello"
str2 = "World"
# Concatenation
concat = str1 + " " + str2
# Printing the concatenated string
print("Concatenated String:", concat)
# Accessing substring
substring = concat[0:5] # Accessing characters from index 0 to 4
print("Substring:", substring)
OUTPUT
Concatenated String: Hello World
Substring: Hello
RESULT
The program to create, concatenate, print a string, and access a substring from a given string was
successfully executed and verified.
EXP NO:3
AIM
Familiarize time and date in various formats (Eg. “Thu Jul 11 [Link] IST 2024”).
ALGORITHM
1. Start the program.
2. Import the datetime and time modules.
3. Get the current date and time using [Link]().
4. Display the date and time in different formats using the strftime() function.
5. Print the formatted outputs.
6. Stop the program.
PSEUDOCODE
START
IMPORT datetime module
SET now ← current date and time
PRINT "Default format:", now
PRINT "Format 1:", [Link]("%c")
PRINT "Format 2:", [Link]("%a %b %d %H:%M:%S %Y")
PRINT "Format 3:", [Link]("%d/%m/%Y, %H:%M:%S")
PRINT "Format 4:", [Link]("%I:%M %p on %A, %B %d, %Y")
STOP
PROGRAM
# Program to display date and time in various formats
import datetime
# Get current date and time
now = [Link]()
print("Default format:", now)
print("Format 1:", [Link]("%c"))
print("Format 2:", [Link]("%a %b %d %H:%M:%S %Y"))
print("Format 3:", [Link]("%d/%m/%Y, %H:%M:%S"))
print("Format 4:", [Link]("%I:%M %p on %A, %B %d, %Y"))
OUTPUT
Default format: 2025-10-20 [Link].345678
Format 1: Mon Oct 20 [Link] 2025
Format 2: Mon Oct 20 [Link] 2025
Format 3: 20/10/2025, [Link]
Format 4: 12:45 AM on Monday, October 20, 2025
RESULT
The program to display the current date and time in various formats was successfully executed
and verified.
EXP NO:4
AIM
Write a program to create, append, and remove lists in Python using NumPy.
ALGORITHM
1. Import the NumPy library.
2. Create an initial NumPy array (list).
3. Append new elements to the array using [Link]().
4. Remove elements from the array using [Link]().
5. Display the resulting arrays after each operation.
PSEUDOCODE
START
IMPORT numpy as np
CREATE array = [Link]([10, 20, 30])
DISPLAY array
APPEND new elements [40, 50] using [Link]()
DISPLAY updated array
DELETE element at specific index using [Link]()
DISPLAY final array
STOP
PROGRAM
import numpy as np
# Create an array
arr = [Link]([10, 20, 30])
print("Original Array:", arr)
# Append elements
arr = [Link](arr, [40, 50])
print("After Appending Elements:", arr)
# Remove an element
arr = [Link](arr, 1)
print("After Removing Second Element:", arr)
OUTPUT
Original Array: [10 20 30]
After Appending Elements: [10 20 30 40 50]
After Removing Second Element: [10 30 40 50]
RESULT
The program successfully created, appended, and removed elements from a NumPy array.
EXP NO:5
AIM
Program to find the largest of three numbers.
ALGORITHM
1. Accept three numbers as input.
2. Compare the numbers using conditional statements.
3. Display the largest number among them.
PSEUDOCODE
START
READ num1, num2, num3
IF num1 >= num2 AND num1 >= num3 THEN
largest = num1
ELSE IF num2 >= num1 AND num2 >= num3 THEN
largest = num2
ELSE
largest = num3
DISPLAY largest
STOP
PROGRAM
import numpy as np
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
print("The largest number is:", largest)
OUTPUT
Enter first number: 10
Enter second number: 25
Enter third number: 15
The largest number is: 25.0
RESULT
The program successfully found and displayed the largest of three numbers.
EXP NO:6
AIM
Convert temperature values back and forth between Celsius (c), and Fahrenheit (f). [Formula: c/5
= f-32/9]
ALGORITHM
1. Accept the user’s choice (C to F or F to C).
2. Read the temperature value.
3. If the user chooses C to F, use F = (C * 9/5) + 32.
4. If the user chooses F to C, use C = (F - 32) * 5/9.
PSEUDOCODE
START
DISPLAY conversion options
READ choice
IF choice == 'C'
READ celsius
fahrenheit = (celsius * 9/5) + 32
DISPLAY fahrenheit
ELSE IF choice == 'F'
READ fahrenheit
celsius = (fahrenheit - 32) * 5/9
DISPLAY celsius
STOP
PROGRAM
import numpy as np
choice = input("Enter 'C' to convert Celsius to Fahrenheit or 'F' for Fahrenheit to Celsius: ")
if [Link]() == 'C':
c = float(input("Enter temperature in Celsius: "))
f = (c * 9/5) + 32
print("Temperature in Fahrenheit:", f)
elif [Link]() == 'F':
f = float(input("Enter temperature in Fahrenheit: "))
c = (f - 32) * 5/9
print("Temperature in Celsius:", c)
else:
print("Invalid choice!")
OUTPUT
Enter 'C' to convert Celsius to Fahrenheit or 'F' for Fahrenheit to Celsius: C
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.0
RESULT
The program successfully converted temperatures between Celsius and Fahrenheit.
EXP NO:7
AIM
Program to construct patterns of stars (*), using a nested for loop.
ALGORITHM
1. Accept the number of rows from the user.
2. Use a nested loop:
Outer loop for rows.
Inner loop for printing stars.
3. Print stars in an increasing pattern.
PSEUDOCODE
START
READ number of rows (n)
FOR i from 1 to n
FOR j from 1 to i
PRINT '*'
PRINT newline
STOP
PROGRAM
import numpy as np
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
for j in range(i):
print("*", end="")
print()
OUTPUT
Enter number of rows: 5
*
**
***
****
*****
RESULT
The program successfully printed a star pattern using nested loops.
EXP NO:8
AIM
A program that prints prime numbers less than N.
ALGORITHM
1. Start the program.
2. Read the value of N.
3. For each number i from 2 to N−1,
a. Check if i is divisible by any number between 2 and i//2.
b. If not divisible, then i is a prime number.
4. Print all the prime numbers.
5. Stop the program.
PSEUDOCODE
START
READ N
FOR i FROM 2 TO N-1 DO
flag ← 0
FOR j FROM 2 TO i/2 DO
IF i % j == 0 THEN
flag ← 1
BREAK
END FOR
IF flag == 0 THEN
PRINT i
END FOR
STOP
PROGRAM
N = int(input("Enter the value of N: "))
print("Prime numbers less than", N, "are:")
for i in range(2, N):
for j in range(2, i):
if i % j == 0:
break
else:
print(i)
OUTPUT
Enter the value of N: 15
Prime numbers less than 15 are:
2
3
5
7
11
13
RESULT
Program successfully prints all prime numbers less than N.
EXP NO:9
AIM
Program to find the factorial of a number using Recursion.
ALGORITHM
1. Start the program.
2. Define a recursive function fact(n).
If n == 0 or n == 1, return 1.
Else return n * fact(n-1).
3. Read the number n from the user.
4. Call the recursive function and display the factorial.
5. Stop the program.
PSEUDOCODE
FUNCTION fact(n)
IF n == 0 OR n == 1 THEN
RETURN 1
ELSE
RETURN n * fact(n - 1)
END FUNCTION
READ n
result ← fact(n)
PRINT result
PROGRAM
def fact(n):
if n == 0 or n == 1:
return 1
else:
return n * fact(n - 1)
n = int(input("Enter a number: "))
print("Factorial of", n, "is", fact(n))
OUTPUT
Enter a number: 5
Factorial of 5 is 120
RESULT
Program successfully calculates the factorial using recursion.
EXP NO:10
AIM
Recursive function to add two positive numbers.
ALGORITHM
1. Start the program.
2. Define a recursive function add(a, b).
If b == 0, return a.
Else return add(a + 1, b - 1).
3. Read two numbers a and b.
4. Call the recursive function and print the result.
5. Stop the program.
PSEUDOCODE
FUNCTION add(a, b)
IF b == 0 THEN
RETURN a
ELSE
RETURN add(a + 1, b - 1)
END FUNCTION
READ a, b
sum ← add(a, b)
PRINT sum
PROGRAM
def add(a, b):
if b == 0:
return a
else:
return add(a + 1, b - 1)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum =", add(a, b))
OUTPUT
Enter first number: 5
Enter second number: 3
Sum = 8
RESULT
Program successfully adds two positive numbers using recursion.
EXP NO:11
AIM
Recursive function to multiply two positive numbers.
ALGORITHM
1. Start the program.
2. Define a recursive function multiply(a, b).
If b == 0, return 0.
Else return a + multiply(a, b - 1).
3. Read two numbers a and b.
4. Call the recursive function and display the result.
5. Stop the program.
PSEUDOCODE
FUNCTION multiply(a, b)
IF b == 0 THEN
RETURN 0
ELSE
RETURN a + multiply(a, b - 1)
END FUNCTION
READ a, b
product ← multiply(a, b)
PRINT product
PROGRAM
def multiply(a, b):
if b == 0:
return 0
else:
return a + multiply(a, b - 1)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Product =", multiply(a, b))
OUTPUT
Enter first number: 4
Enter second number: 3
Product = 12
RESULT
Program successfully multiplies two positive numbers using recursion.
EXP NO:12
AIM
Recursive function to find the greatest common divisor of two positive numbers.
ALGORITHM
1. Start the program.
2. Define a recursive function gcd(a, b).
If b == 0, return a.
Else return gcd(b, a % b).
3. Read two numbers a and b.
4. Call the function and print the result.
5. Stop the program.
PSEUDOCODE
FUNCTION gcd(a, b)
IF b == 0 THEN
RETURN a
ELSE
RETURN gcd(b, a % b)
END FUNCTION
READ a, b
result ← gcd(a, b)
PRINT result
PROGRAM
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("GCD =", gcd(a, b))
OUTPUT
Enter first number: 36
Enter second number: 60
GCD = 12
RESULT
Program successfully finds the greatest common divisor using recursion.
EXP NO:13
AIM
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
1. Start the program.
2. Define a function is_right_triangle(a, b, c) to check if the triangle is right-angled.
3. Inside the function, find the largest side (hypotenuse).
4. Check if the square of the largest side equals the sum of the squares of the other two
sides.
5. If true, print "It is a right triangle"; otherwise, print "It is not a right triangle".
6. End the program.
PSEUDOCODE
FUNCTION is_right_triangle(a, b, c):
sides = [a, b, c]
[Link]()
IF sides[2]^2 == sides[0]^2 + sides[1]^2:
RETURN True
ELSE:
RETURN False
READ a, b, c
IF is_right_triangle(a, b, c):
PRINT "Right Triangle"
ELSE:
PRINT "Not a Right Triangle"
PROGRAM
def is_right_triangle(a, b, c):
sides = [a, b, c]
[Link]()
return sides[2] ** 2 == sides[0] ** 2 + sides[1] ** 2
a = float(input("Enter first side: "))
b = float(input("Enter second side: "))
c = float(input("Enter third side: "))
if is_right_triangle(a, b, c):
print("It is a right triangle.")
else:
print("It is not a right triangle.")
OUTPUT
Enter first side: 3
Enter second side: 4
Enter third side: 5
It is a right triangle.
RESULT
Program successfully checks whether the given triangle is a right triangle.
EXP NO:14
AIM
Program to define a module to find Fibonacci Numbers and import the module to another
program.
ALGORITHM
1. Create a module fibonacci_module.py containing a function to generate Fibonacci
numbers.
2. Import this module in another Python file.
3. Accept the number of terms from the user.
4. Display the Fibonacci sequence using the imported module.
PSEUDOCODE
A)Module (fibonacci_module.py)
DEFINE fibonacci(n):
a=0
b=1
FOR i in range(n):
PRINT a
a, b = b, a + b
B)Main Program (main_program.py)
IMPORT fibonacci_module
READ n
CALL fibonacci_module.fibonacci(n)
PROGRAM
# fibonacci_module.py
def fibonacci(n):
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
# main_program.py
n = int(input("Enter the number of terms: "))
print("Fibonacci Series:")
fibonacci(n)
OUTPUT
Enter the number of terms: 6
Fibonacci Series:
011235
RESULT
Program successfully implements a Fibonacci module and imports it to another program.
EXP NO:15
AIM
Program to check whether the given number is a valid mobile number or not using functions.
Rules:
1. Every number should contain exactly 10 digits.
2. The first digit should be 7 or 8 or 9
ALGORITHM
1. Define a function is_valid_mobile(number).
2. Check if the number has 10 digits.
3. Check if the first digit is 7, 8, or 9.
4. Return whether the number is valid or not.
PSEUDOCODE
FUNCTION is_valid_mobile(num):
IF length(num) == 10 AND num[0] in ['7','8','9']:
RETURN True
ELSE:
RETURN False
READ num
IF is_valid_mobile(num):
PRINT "Valid Mobile Number"
ELSE:
PRINT "Invalid Mobile Number"
PROGRAM
def is_valid_mobile(num):
if len(num) == 10 and [Link]() and num[0] in ['7', '8', '9']:
return True
else:
return False
num = input("Enter a mobile number: ")
if is_valid_mobile(num):
print("Valid Mobile Number.")
else:
print("Invalid Mobile Number.")
OUTPUT
Enter a mobile number: 9876543210
Valid Mobile Number.
RESULT
Program successfully validates a mobile number according to given rules.
EXP NO:16
AIM
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. Both the even numbers and odd numbers
should be in sorted order.
ALGORITHM
1. Read two lists from the user.
2. Merge both lists into one.
3. Separate even and odd numbers.
4. Sort both lists individually.
5. Concatenate even and odd lists and print the merged list.
PSEUDOCODE
READ list1
READ list2
merged = list1 + list2
even = sorted([x for x in merged if x % 2 == 0])
odd = sorted([x for x in merged if x % 2 != 0])
final_list = even + odd
PRINT final_list
PROGRAM
list1 = list(map(int, input("Enter first list elements: ").split()))
list2 = list(map(int, input("Enter second list elements: ").split()))
merged = list1 + list2
even = sorted([x for x in merged if x % 2 == 0])
odd = sorted([x for x in merged if x % 2 != 0])
final_list = even + odd
print("Merged list (Even first, then Odd):", final_list)
OUTPUT
Enter first list elements: 1 4 5
Enter second list elements: 2 3 6
Merged list (Even first, then Odd): [2, 4, 6, 1, 3, 5]
RESULT
Program successfully merges two lists with even numbers first, followed by odd numbers.
EXP NO:17
AIM
Write a program to play a sticks game in which there are 16 sticks. Two players take turns to
play the game. Each player picks one set of sticks (needn’t be adjacent) during his turn. A set
contains 1, 2, or 3 sticks. The player who takes the last stick is the loser. The number of sticks in
the set is to be input.
ALGORITHM
1. Initialize total sticks = 16.
2. Repeat until sticks > 1:
Player 1 picks 1–3 sticks.
Reduce from total.
If 1 stick remains, Player 1 loses.
Player 2 picks 1–3 sticks.
Reduce from total.
If 1 stick remains, Player 2 loses.
PSEUDOCODE
sticks = 16
WHILE sticks > 1:
player1 = READ input(1–3)
sticks -= player1
IF sticks <= 1: PRINT "Player 1 loses"; BREAK
player2 = READ input(1–3)
sticks -= player2
IF sticks <= 1: PRINT "Player 2 loses"; BREAK
PROGRAM
sticks = 16
print("Game of 16 sticks: Take 1, 2, or 3 sticks. The one who takes the last stick loses.")
while sticks > 1:
print("\nSticks remaining:", sticks)
p1 = int(input("Player 1, pick 1-3 sticks: "))
if p1 not in [1, 2, 3]:
print("Invalid move! Try again.")
continue
sticks -= p1
if sticks <= 1:
print("Player 1 loses!")
break
print("Sticks remaining:", sticks)
p2 = int(input("Player 2, pick 1-3 sticks: "))
if p2 not in [1, 2, 3]:
print("Invalid move! Try again.")
continue
sticks -= p2
if sticks <= 1:
print("Player 2 loses!")
break
OUTPUT
Game of 16 sticks: Take 1, 2, or 3 sticks. The one who takes the last stick loses.
Sticks remaining: 16
Player 1, pick 1-3 sticks: 3
Sticks remaining: 13
Player 2, pick 1-3 sticks: 2
...
Player 1 loses!
RESULT
Program successfully simulates the 16 sticks game.
EXP NO:18
AIM
Suppose you're on a game show, and you are given the choice of three doors: Behind one door is
a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what is
behind the doors, opens another door, say No. 3, which has a goat. He then asks, "Do you want
to pick door No. 2?" Is it to your advantage to switch your choice?
ALGORITHM
1. Simulate the game multiple times.
2. Randomly assign the car behind one door.
3. Player picks one door randomly.
4. Host opens another door with a goat.
5. Check outcomes if player stays vs switches.
6. Compare success rates of both strategies.
PSEUDOCODE
SET switch_wins = 0
SET stay_wins = 0
FOR i in range(1 to 1000):
car = random door (1–3)
choice = random door (1–3)
remaining_doors = doors excluding choice and car (host opens)
IF choice == car:
stay_wins += 1
ELSE:
switch_wins += 1
PRINT switch_wins, stay_wins
PROGRAM
import random
switch_wins = 0
stay_wins = 0
for i in range(1000):
doors = [1, 2, 3]
car = [Link](doors)
choice = [Link](doors)
remaining_doors = [d for d in doors if d != choice and d != car]
host_opens = [Link](remaining_doors)
switch_choice = [d for d in doors if d not in [choice, host_opens]][0]
if switch_choice == car:
switch_wins += 1
if choice == car:
stay_wins += 1
print("Wins by switching:", switch_wins)
print("Wins by staying:", stay_wins)
OUTPUT
Wins by switching: 667
Wins by staying: 333
RESULT
Program successfully simulates the Monty Hall problem and shows that switching gives a higher
chance of winning (approximately 2/3 vs 1/3).