Link for installation of anaconda - https://docs.anaconda.com/anaconda/install/windows/
A.
Without Function
Write and execute a simple Python code cell (e.g., print "Hello, Data Science!")
# Print the message
print("Hello, Data Science!")
2. Program to display system version
import sys
print(sys.version)
print(sys.version_info)
OR
import platform
# Get the system version
system_version = platform.version()
# Print the system version
print("System version:", system_version)
3. Program to display pandas Library version
import pandas as pd
print(pd.__version__)
OR
import pandas as pd
# Get the pandas version
pandas_version = pd.__version__
# Print the pandas version
print("Pandas version:", pandas_version)
4. Write a program to display Python Version
import sys
# Get the Python version
python_version = sys.version
# Print the Python version
print("Python version:", python_version)
5. Program to display data and time
from datetime import datetime
current_time = datetime.now()
print(current_time.date())
print(current_time.time())
6. Write a Python program to find the factorial of a number
Without Function
Iterative Approach
# Take input from user
try:
number = int(input("Enter a number to calculate its factorial: "))
# Check if the number is negative
if number < 0:
print("Factorial is not defined for negative numbers")
else:
result = 1
for i in range(1, number + 1):
result *= i
print(f"The factorial of {number} is {result}.")
except ValueError:
print("Invalid input. Please enter an integer.")
Recursive Approach
# Take input from user
try:
number = int(input("Enter a number to calculate its factorial: "))
# Check if the number is negative
if number < 0:
print("Factorial is not defined for negative numbers")
else:
# Initialize the result variable
result = 1
7. Write a Python program to find the a number is prime or not
Without Function
# Input: number to check
num = int(input("Enter a number: "))
# Prime numbers are greater than 1
if num > 1:
# Check for factors
is_prime = True
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break
# Output the result
if is_prime:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
else:
print(f"{num} is not a prime number")
8. Write a Python program to find the a Fibonaccie Series
Without Function
# Input: number of terms in the Fibonacci series
n = int(input("Enter the number of terms: "))
# Initialize the first two terms
a, b = 0, 1
# Print the Fibonacci series
print("Fibonacci series:")
for i in range(n):
print(a, end=" ")
# Update the terms
a, b = b, a + b
With Function
Write a Python program to find the factorial of a number using recursion
With Function
# Function to calculate factorial of a number
def factorial(n):
"""
This function returns the factorial of a given number n.
Factorial of n (n!) is the product of all positive integers less than or equal to n.
"""
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Example usage
number = 5 # You can change this number to test other values
result = factorial(number)
result
Write a Python program to find the factorial of a number using Iterative approach
With Function
# Function to calculate factorial of a number iteratively
def factorial_iterative(n):
"""
This function returns the factorial of a given number n using an iterative approach.
Factorial of n (n!) is the product of all positive integers less than or equal to n.
"""
if n < 0:
return "Factorial is not defined for negative numbers"
result = 1
for i in range(1, n + 1):
result *= i
return result
# Example usage
number = 5 # You can change this number to test other values
result = factorial_iterative(number)
result
# Function to calculate factorial of a number iteratively and read number from user
def factorial_iterative(n):
"""
This function returns the factorial of a given number n using an iterative approach.
Factorial of n (n!) is the product of all positive integers less than or equal to n.
"""
if n < 0:
return "Factorial is not defined for negative numbers"
result = 1
for i in range(1, n + 1):
result *= i
return result
# Take input from user
try:
number = int(input("Enter a number to calculate its factorial: "))
result = factorial_iterative(number)
print(f"The factorial of {number} is {result}.")
except ValueError:
print("Invalid input. Please enter an integer.")
2. Write a Python program to find the a number is prime or not
With Function
def is_prime(num):
# Prime numbers are greater than 1
if num <= 1:
return False
# Check for factors
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
# Input: number to check
num = int(input("Enter a number: "))
# Output the result
if is_prime(num):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
3. Write a Python program to find the a Fibonaccie Series
With Function
def fibonacci_series(n):
# Initialize the first two terms
a, b = 0, 1
# Generate the Fibonacci series
for i in range(n):
print(a, end=" ")
a, b = b, a + b
# Input: number of terms in the Fibonacci series
n = int(input("Enter the number of terms: "))
# Call the function to print the Fibonacci series
print("Fibonacci series:")
fibonacci_series(n)