[go: up one dir, main page]

0% found this document useful (0 votes)
28 views8 pages

Practical-1: Aim: Write A Function To Find All Prime Numbers Occur Between 1 To 100

The document contains 7 code snippets and outputs for practical problems involving prime number checking, palindrome checking, Fibonacci sequence, subject filtering from a list, frequency counting with zip, password validation, and exploring the Scipy library.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Practical-1: Aim: Write A Function To Find All Prime Numbers Occur Between 1 To 100

The document contains 7 code snippets and outputs for practical problems involving prime number checking, palindrome checking, Fibonacci sequence, subject filtering from a list, frequency counting with zip, password validation, and exploring the Scipy library.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Enrollment No:202103103510078

Practical-1
Aim: Write a function to find all prime numbers occur between 1 to 100.

Code:-

def is_prime(num):

if num < 2:

return False

for i in range(2, int(num**0.5) + 1):

if num % i == 0:

return False

return True

print("Prime numbers are:")

for i in range(1, 100 + 1):

if is_prime(i):

print(i, end=" ")

Output:-

UTU/CGPIT/CS/SEM-5/ Programming with Python 1


Enrollment No:202103103510078

Practical-2
Aim: Write a python program to check whether the given string is a
palindrome or not.

Code:-

def isPalindrome(s):
return s == s[::-1]

s = str(input("Enter String: "))


ans = isPalindrome(s)

if ans:
print(f"{s} is a palindrome.")
else:
print(f"{s} is NOT a palindrome.")

Output:-

UTU/CGPIT/CS/SEM-5/ Programming with Python 2


Enrollment No:202103103510078

Practical-3
Aim: Using concept of list comprehension, write a python program to
print the Fibonacci sequence form with a given n input by console.

Code:-

n = int(input("Give length: "))

mylist=[0,1]

[mylist.append(mylist[-2]+mylist[-1]) for n in range(n)]

print("Fibonacci sequence form:", mylist)

Output:-

UTU/CGPIT/CS/SEM-5/ Programming with Python 3


Enrollment No:202103103510078

Practical-4
Aim: Find out the list of subjects of a particular semester from the input
provided as a list of dictionaries using lambda map and filter together.
i/p:- [{'sem':6,'sub':'python'},{'sem':6,'sub':'cns'}, {'sem':5,'sub':'java'},
{'sem':5,'sub':'daa'}] o/p:- sem 6 subjects:['python','cns']

Code:-

subject = [{'sem':6,'sub':'python'},{'sem':6,'sub':'cns'},

{'sem':5,'sub':'java'}, {'sem':5,'sub':'daa'}]

a = list(filter(lambda x : x['sem'] == 6, subject))

b = set(map(lambda x : x['sem'], a))

print("sem:", list(b))

c = list(map(lambda x : x['sub'], a))

print("subject:", c)

Output:-

UTU/CGPIT/CS/SEM-5/ Programming with Python 4


Enrollment No:202103103510078

Practical-5
Aim: Write a program to count frequency of elements and storing in
dictionary using zip().

Code:-

def CountFrequency(my_list):

a = set(my_list)

b = []

for item in a:

b.append(my_list.count(item))

c = {a: b for a,

b in zip(a, b)}

print("Frequency count:", c)

if __name__ == "__main__":

my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]

print("Your list:", my_list)

CountFrequency(my_list)

Output:-

UTU/CGPIT/CS/SEM-5/ Programming with Python 5


Enrollment No:202103103510078

Practical-6
Aim: Write a program to check the validity of password input by users.
Following are the criteria for checking the password:
1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
3. At least 1 letter between [A-Z]
4. At least 1 special character
5. Min. length of transaction password: 6
6. Max. Length of transaction password: 12

Code:-

import re

v = input("Enter Password: ")

flag = 1

while True:

if not re.search("[a-z]", v):

flag = 1

break

elif not re.search("[0-9]", v):

flag = 1

break

elif not re.search("[A-Z]", v):

flag = 1

break

elif not re.search("@", v):

flag = 1

UTU/CGPIT/CS/SEM-5/ Programming with Python 6


Enrollment No:202103103510078

break

elif len(v) < 6 or len(v) > 12:

flag = 1

break

else:

print("Valid Password")

flag = 0

break

if flag == 1:

print("Invalid Password")

Output:-

UTU/CGPIT/CS/SEM-5/ Programming with Python 7


Enrollment No:202103103510078

Practical-7
Aim: Write a program that can explore Scipy python library.

Code:-

from scipy.special import cbrt

# cube root of 64

print(cbrt(64))

# cube root of 78

print(cbrt(78))

# cube root of 128

print(cbrt(128))

Output:-

UTU/CGPIT/CS/SEM-5/ Programming with Python 8

You might also like