0% found this document useful (0 votes)
46 views5 pages

Practical: 2 Aim 2.1: Check Whether The Number Is Armstrong or Not?

The document contains code snippets and outputs demonstrating various Python programming concepts including: 1) Checking if a number is Armstrong or prime using functions and conditionals. 2) Checking if a number is positive, negative or zero using conditionals. 3) Demonstrating list methods like insert, remove, append, length, pop and clear. 4) Demonstrating dictionary methods like printing, accessing values, changing values and finding length. 5) Demonstrating tuple methods like adding items, finding length, checking for items and accessing items. 6) Demonstrating set methods like adding items and finding length.

Uploaded by

Harsh More
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)
46 views5 pages

Practical: 2 Aim 2.1: Check Whether The Number Is Armstrong or Not?

The document contains code snippets and outputs demonstrating various Python programming concepts including: 1) Checking if a number is Armstrong or prime using functions and conditionals. 2) Checking if a number is positive, negative or zero using conditionals. 3) Demonstrating list methods like insert, remove, append, length, pop and clear. 4) Demonstrating dictionary methods like printing, accessing values, changing values and finding length. 5) Demonstrating tuple methods like adding items, finding length, checking for items and accessing items. 6) Demonstrating set methods like adding items and finding length.

Uploaded by

Harsh More
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/ 5

PRACTICAL : 2

Aim 2.1: Check whether the number is Armstrong or not?

Code:

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

sum = 0

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if num == sum:

print(num,"is an Armstrong number")

else:

print(num,"is not an Armstrong number")

Output:

Enter a number: 153

153 is an Armstrong number

Aim 2.2 : Check whether the number is prime or not?

Code:

def test_prime(n):

if (n==1):

return False

elif (n==2):

return True;

else:

for x in range(2,n):

if(n % x==0):

return False
return True

a = int(input("enter the value:"))

print(test_prime(a))

Output:

enter the value:12

False

enter the value:3

True

Aim 2.3 : Check whether the number is positive or negative?

Code:

num = float(input("Input a number: "))

if num > 0:

print("It is positive number")

elif num == 0:

print("It is Zero")

else:

print("It is a negative number")

Output:

Input a number: -6

It is a negative number

Input a number: 0

It is Zero

Input a number: 200

It is positive number
Aim 2.4 : Create a list and perform the following methods.

1) insert() 2) remove() 3) append() 4) len() 5) pop() 6) clear()

Code:

My_list = [1, 2 , 3, 4, 6, 7, 8]

My_list.insert(0, 5)

print("after the value inserted list is", My_list)

My_list.remove(3)

print("after the value removing list is", My_list)

My_list.append(6)

print("list is", My_list)

x = len(My_list)

print(x)

My_list.pop(4)

print("list is", My_list)

My_list.clear()

print("list is", My_list)

Output:

after the value inserted list is [5, 1, 2, 3, 4, 6, 7, 8]

after the value removing list is [5, 1, 2, 4, 6, 7, 8]

list is [5, 1, 2, 4, 6, 7, 8, 6]

list is [5, 1, 2, 4, 7, 8, 6]

list is []
Aim 2.5 : Create a dictionary and apply the following methods.

1) Print the dictionary items 2) access items 3) use get()

4) change values 5) use len()

Code:

thisdict = {

"name": "Harsh",

"Lname": "More",

"Byear": 2001,

"Degree": "computer",

"erno": 191210107031

print(thisdict)

print(thisdict["name"])

x = thisdict["Byear"]

print(x)

x = thisdict.keys()

print(x)

thisdict["name"]= "ram"

print(thisdict)

print(len(thisdict))

Output:

{'name': 'Harsh', 'Lname': 'More', 'Byear': 2001, 'Degree': 'computer', 'erno': 191210107031}

Harsh

2001

dict_keys(['name', 'Lname', 'Byear', 'Degree', 'erno'])

{'name': 'Harsh', 'Lname': 'More', 'Byear': 2001, 'Degree': 'computer', 'erno': 191210107031}

5
Aim 2.6 : Create a tuple and perform the following methods.

1)Add items 2) len() 3) check for item in tuple

4) access items

Code:

thistuple = ( "harsh", "veer", "het", "sahil")

print(thistuple)

thistuple = ( "harsh", "veer", "het", "sahil")

y = list(thistuple)

y.append("yash")

thistuple = tuple(y)

print(len(thistuple))

print(thistuple[3])

Output:

(‘harsh’, ‘veer’, ‘het’, ‘sahil’)

(‘harsh’, ‘veer’, ‘het’, ‘sahil’, 'yash')

sahil

Aim 2.7 : Create a set and perform the following methods.

1)Add items 2) len() 3) check for item in tuple

4) access items

Code:

thisset = {"a", "b", "c"}

print(thisset

You might also like