0% found this document useful (0 votes)
49 views

DSP LABs

The document contains a syllabus for a Python lab practical with 13 programming problems covering topics like square roots, random number generation, prime numbers, factorials, and more. Each problem is presented with a short description, sample code, and example output to demonstrate the code.

Uploaded by

durga bhavani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

DSP LABs

The document contains a syllabus for a Python lab practical with 13 programming problems covering topics like square roots, random number generation, prime numbers, factorials, and more. Each problem is presented with a short description, sample code, and example output to demonstrate the code.

Uploaded by

durga bhavani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Science Using Python; Lab (Practical) Syllabus

1. Python Program to Find the Square Root


2. Python Program to Swap Two Variables
3. Python Program to Generate a Random Number
4. Python Program to Check if a Number is Odd or Even
5. Python Program to Find the Largest Among Three Numbers
6. Python Program to Check Prime Number
7. Python Program to Display the multiplication Table
8. Python Program to Print the Fibonacci sequence
9. Python Program to Find the Sum of Natural Numbers
10. Python Program to Find Factorial of Number Using Recursion
11. Python Program to work with string methods.
12. Python Program to create a dictionary and print its content.
13. Python Program to create class and objects.

1. Python Program to Find the Square Root

Program Code:

print("Enter a Number: ")


num = int(input())

squareroot = num ** 0.5

print("\nSquare Root =", squareroot)

output:

Enter a Number:
4

Square Root = 2.0

2. Python Program to Swap Two Variables

Program Code:

# To take inputs from the user


x = input('Enter value of x: ')
y = input('Enter value of y: ')
print('The value of x before swapping: {}'.format(x))
print('The value of y before swapping: {}'.format(y))

# create a temporary variable and swap the values


temp = x
x=y
y = temp

print('The value of x after swapping: {}'.format(x))


print('The value of y after swapping: {}'.format(y))

Output:

Enter value of x: 55
Enter value of y: 77
The value of x before swapping: 55
The value of y before swapping: 77
The value of x after swapping: 77
The value of y after swapping: 55

3. Python Program to Generate a Random Number

Program code:

import random
rand_list = []
#Generating 50 Random numbers
for i in range(0,50):
n = random.randint(1,50)
rand_list.append(n)
print(rand_list)

Output:

[7, 28, 7, 1, 8, 50, 4, 19, 16, 37, 45, 45, 9, 48, 40, 35, 25, 29, 20, 7, 24, 27, 45, 45, 37, 6, 43,
30, 37, 19, 21, 7, 19, 9, 22, 41, 43, 45, 23, 35, 6, 32, 4, 24, 15, 40, 1, 40, 40, 34]

4. Python Program to Check if a Number is Odd or Even

Program code:

num = int(input("Enter a Number:"))


if num % 2 == 0:
print("Given number is Even")
else:
print("Given number is Odd")

Output:

Enter a Number:15
Given number is Odd

Enter a Number:78
Given number is Even

5. Python Program to Find the Largest Among Three Numbers

Program code:

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))
num3 = int(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: 56


Enter second number: 43
Enter third number: 98

The largest number is 98

6. Python Program to Check Prime Number

Program Code:

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

# define a flag variable


flag = False

if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True


if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

Output:

Enter a number: 55
55 is not a prime number

Enter a number: 13
13 s a prime number

7. Python Program to Display the multiplication Table

Program Code:

max=int(input("How many terms do you want to display"))


table=int(input("Enter Table Number:"))
print("The", table,"table is")
for n in range(1,max+1):
print(table,'x',n,'=',table*n)

Output:

How many terms do you want to display10


Enter Table Number:7
The 7 table is
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

8. Python Program to Print the Fibonacci sequence

Program Code:

n= int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if n <= 0:
print("Please enter a positive integer")

elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1

Output:

How many terms? 8


Fibonacci sequence:
0
1
1
2
3
5
8
13

9. Python Program to Find the Sum of Natural Numbers

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

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num=num-1
print("The sum is",sum)

Output:
Enter a number: 5
The sum is 15
10. Python Program to Find Factorial of Number Using Recursion

def fact(n):
if n == 1:
return n
else:
return n*fact(n-1)

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

# check if the number is negative


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", fact(num))

output:

Enter a number 5
The factorial of 5 is 120

You might also like