Sie sind auf Seite 1von 11

Name-Pratiksha D.

Lalda

SE A

Roll No-53

Assignment No-1

1) A Python program to convert numbers from octal,binary and hexadecimal


system into decimal number system.

Code:

oc=0o123
bin=0b0101
hex=0xFC1
print("Octal value 123 in Decimal is ",int(oc))
print("Binary value 0101 in Decimal is ",int(bin))
print("Hexadecimal value FC1 in Decimal is ",int(hex))
Output:
Octal value 123 in Decimal is 83
Binary value 0101 in Decimal is 5
Hexadecimal value FC1 in Decimal is 4033

2) A python program to convert from octal,binary and


hexadecimal system into decimal number system using int()
function

(Take input from user)

Code:
b=input("Enter an Binary value ")
o=input("Enter an Octal value ")
h=input("Enter an Hexadecimal value ")
print("Binary value ",b," in decimal is",int(b,2))
print("Octal value ",o," in Decimal is",int(o,8))
print("Hexadecimal value ",h," in decimal is",int(h,16))

Output
Enter an Binary value 0101
Enter an Octal value 123
Enter an Hexadecimal value fc1
Binary value 0101 in decimal is 5
Octal value 123 in Decimal is 83
Hexadecimal value fc1 in decimal is 4033

3) A python program to accept three integers separated by


commas and display their average.

Code:

a,b,c=(int(x) for x in input("Enter a three number


").split(","))
sum=(a+b+c)/3
print("Average is",sum)

Output:

Enter a three number 1,2,3

Average is 2.0
4) A python program to accept a group of strings separated
by commas and display them again.

Code-

a,b,c=(x for x in input("Enter a names ").split(","))


print(a,b,c)

Output-

Enter a names Pratiksha,Diwakar,Laldas

Pratiksha Diwakar Laldas

5a) A python program to accept a list and display it

Code:

x=[int(x) for x in input(“Enter a list:”).split(“ ”)]

print(“The list is”,x)

Output:

Enter a list 1 2 3 4

The list is [1, 2, 3, 4]

5b)A python program to accept a tuple and display it.

Code:

x=eval(input(“Enter a tuple: ”))

Print(“Entered tuple is:”,x)

Output:

Enter a tuple: (1,2,3)


Entered tuple is: (1, 2, 3)

6) A python program to accept a number from keyboard and


test whether it is odd or even.

Code-

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


if a%2==0:
print("Number is even")
else:
print("Number is odd")

Output-

Enter a number 8

Number is even

Enter a number 7

Number is odd

7) A python program to accept a numeric digit from keyboard


and display in words(Between1 to 5).

Code:

a= (int(x) for x in input("Enter a number ").split(" "))


for x in a:
if x==1:
print("One")
elif x==2:
print("Two")
elif x==3:
print("Three")
elif x==4:
print("Four")
elif x==5:
print("Five")
else:
print("Oops Ivalid! ")

Output-

Enter a number 1 2 3 4 5

One

Two

Three

Four

Five

OR

Code:
d={1:"One",2:"Two",3:"Three",4:"Four",5:"Five"}
key=int(input("Enter any number in between 1-5 (both inclusive)"))
if key<1 or key>5:
print("Invalid Enter Positive Number")
else:
print(key," in words is ",d[key])
Output
Enter any number in between 1-5 (both inclusive) 3
3 in words Three
8) A python program to display each character from a string
using sequence index.

code-

s=input("Enter a string ")


i=0
while(i<len(s)):
print(s[i],end= ' ')
i+=1

Output-

Enter a string: pratiksha

p r a t i k s h a

9) A python program to display sum of number of list using


while loop.

Code:

x=[int(x) for x in input(“Enter a list:”).split(“ ”)]

l=len(x)

i=0

sum=0

while i<l:

sum+=x[i]
i+=1

print(“Sum of numbers is:”,sum)

Output:

Enter a list:1 2 3

Sum of numbers is: 6

10a)A Python program to retrieve only negative numbers from a list of


numbers.
Code:
list=[int(x) for x in input("Enter elements").split()]
for i in list:
if i<0:
print(i)
Output
Enter elements -96 5 3 -7 -5 1 2 8 3
-96
-7
-5

10.b) A Python program to assert if a user enters a number greater than zero.
Code:
x=int(input(“Enter a number:”))
assert x>0,”Only positive number allowed”
print(“You Entered :”,x)

Output:
Enter a number:2
You Entered : 2

Enter a number:-9
Traceback (most recent call last):
File "C:/Users/prati/PycharmProjects/Hello/app.py", line 2, in <module>
assert x>0,"only positive number allowed"
AssertionError: only positive number allowed
11) A Python program to test whether a number is even or odd using functions.
Code:
def check(x):
if x%2==0:
print(x," is an even number")
else:
print(x," is an odd number")

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


check(a)
Output:
Enter a number: 2
2 is an even number
Enter a number: 3
3 is an odd number

12) A Python program to check if a given number is prime or not using


functions.
Code:
def prime(a):
for i in range(2,a):
if a%i==0:
print(a,"is not a prime number")
break
else:
print(a,"is a prime number")
x=int(input("Enter a number"))
prime(x)
Output:
Enter a number 3
3 is a prime number
Enter a number 12
12 is not a prime number

13) A Python program to multiply all the numbers in a list using functions.
Code:
def multipy(x):
prod=1
for i in x:
prod*=i
return prod
lst=[int(x) for x in input("Enter list elements").split()]
print(“Product of all numberin list is: ”,multipy(lst))
Output:
enter a list: 1 4 3
Product of all number in list is: 12

14) A Python program to design a basic calculator using variable length


function.
Code:
def add(*a):
sum=0
for i in a:
sum+=i
return sum
def mul(*m):
prod=1
for i in m:
prod*=i
return prod

def sub(*s):
su=2
for i in s:
su-=i
return su
a=add(1,2,3,4,5)
print("sum of 1 2 3 4 5 is:",a)
subs=sub(1,2,3,4,5)
print("subraction of 1 2 3 4 5 is:",subs)
m=mul(1,2,3,4,5)
print("product of 1 2 3 4 5 is : ",m)
Output
sum of 1 2 3 4 5 is : 15
subraction of 1 2 3 4 5 is: -13
product of 1 2 3 4 5 is : 120

Das könnte Ihnen auch gefallen