You are on page 1of 3

9/21/23, 9:42 AM 210090107120_Assignment1

Practical 1 : Basics of Python


Name : Priyansh Jain
Enrollment No : 210090107120
Class : III-A Batch - A3
Dataset Used : Online Retail 2
Dataset description : A real online retail transaction data set of two years.

a. Implement a python program to find factorial of a given number, if the given


number is even. (Using while loop)

In [ ]: def factorial(n):
result = 1
while n > 0:
result *= n
n -= 1
return result

number = int(input("Enter a number: "))

if number % 2 == 0:
fact = factorial(number)
print(f"Factorial of {number} is {fact}")
else:
print(f"{number} is not even, so its factorial is not calculated.")

b. Implement a python program to find Fibonacci series of a given number. (Using


for loop)

In [ ]: def fibonacci_series(n):
fib_series = [0, 1]
for i in range(2, n):
next_fib = fib_series[i - 1] + fib_series[i - 2]
fib_series.append(next_fib)
return fib_series

number = int(input("Enter the number of Fibonacci terms: "))


series = fibonacci_series(number)
print("Fibonacci Series:", series)

Enter the number of Fibonacci terms: 12


Fibonacci Series: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

c. Implement a python program the following pattern for n number of lines:


*
##
***
####

In [ ]: def print_pattern(n):
for i in range(1, n + 1):
print("*" * (2 * i - 1))

file:///C:/Users/Priyansh/Desktop/python assignments/Priyansh/210090107120_Assignment1.html 1/3


9/21/23, 9:42 AM 210090107120_Assignment1

lines = int(input("Enter the number of lines: "))


print_pattern(lines)

Enter the number of lines: 4


*
***
*****
*******

d. Implement a python function to check whether given number is prime or not.

In [ ]: def is_prime(number):
if number <= 1:
return False # Numbers less than or equal to 1 are not prime

if number <= 3:
return True # 2 and 3 are prime

if number % 2 == 0 or number % 3 == 0:
return False # Numbers divisible by 2 or 3 are not prime

i = 5
while i * i <= number:
if number % i == 0 or number % (i + 2) == 0:
return False # Numbers divisible by i or i+2 are not prime
i += 6

return True # If no divisors found, the number is prime

if __name__ == "__main__":
# Input a number to check if it's prime
num = int(input("Enter a number: "))

if is_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")

e. Write a program to implement the following using operators in Python. start


no=110, result=550.

In [ ]: import random

# List of mathematical operators


operators = ['+', '-', '*', '/']

def perform_operation(operator, start_no, result_no):


if operator == '+':
return start_no + result_no
elif operator == '-':
return start_no - result_no
elif operator == '*':
return start_no * result_no
elif operator == '/':
if result_no == 0:
return None # Avoid division by zero
return start_no / result_no
else:

file:///C:/Users/Priyansh/Desktop/python assignments/Priyansh/210090107120_Assignment1.html 2/3


9/21/23, 9:42 AM 210090107120_Assignment1

return None # Invalid operator

def main():
start_no = random.randint(3, 9)
result_no = random.randint(3, 9)

while True:
selected_operator = random.choice(operators)
result = perform_operation(selected_operator, start_no, result_no)

if result is not None:


print(f"{start_no} {selected_operator} {result_no} = {result}")
break

if __name__ == "__main__":
main()

9 + 3 = 12

file:///C:/Users/Priyansh/Desktop/python assignments/Priyansh/210090107120_Assignment1.html 3/3

You might also like