Uploaded by santhosh pawar

PYTHON ASSIGNMENT 1

advertisement
PYTHON ASSIGNMENT 1
1. Identifier rules in Python
●
●
●
●
●
●
Identifiers cannot be a keyword.
Identifiers are case-sensitive.
It can have a sequence of letters and digits.
It's a convention to start an identifier with a letter rather than _.
White spaces are not allowed.
We cannot use special symbols like !, @, #, $, and so on.
—----------------------------------------------------------------------------------------------------------------------------
2.Sum of 3 numbers
a=int(input("enter number1\n"))
b=int(input("enter number2\n"))
c=int(input("enter number3\n"))
print ("sum of three numbers is ")
print (a+b+c)
—-----------------------------------------------------------------------------------------------------------------3. Number is prime or not
a=int(input("enter number1\n"))
if a==2 :
print ("prime number \n")
temp=0
for i in range(2,a):
if a%i==0 :
temp=1
if temp==1:
print (a,"is not a prime number")
else :
print (a,"is prime number ")
—-----------------------------------------------------------------------------------------------------------------4.Fibonacci series
n=int(input("enter number\n"))
a,b=0,1
print("Fibonacci series is :")
fibo=[a,b]
for i in range(3,n+1):
t=a+b
fibo.append(t)
a=b
b=t
print(fibo)
—-----------------------------------------------------------------------------------------------------------------5.max of given numbers
n=int(input("how many numbers u want to enter\n"))
print("enter numbers")
arr=[]
for j in range(0,n):
a=int(input())
arr.append(a)
arr.sort()
print("max of given numbers is")
print(arr[-1])
—-----------------------------------------------------------------------------------------------------------------6.prime numbers in a range
low=int(input("enter the lower range\n"))
up=int(input("enter the upper range\n"))
print("prime numbers in range are: ")
count=0
for i in range(low,up+1):
temp=0
for j in range(2,i):
if i%j==0:
temp=1
if temp==0:
count+=1
print(i,end=" ")
print("\ntotal no of prime numbers are: ",count)
—------------------------------------------------------------------------------------------------------------------
7.string to array and vice versa
str=input("enter the string\n")
arr=str.split(" ")
print("String to array: ",arr)
strg=" "
str1=strg.join(arr)
print("array to string: ",str1)
—-----------------------------------------------------------------------------------------------------------------8. Random IP address
import random
ip=[]
for i in range(0,4):
ip.append(str(random.randint(0,255)))
ipd=".".join(ip)
print("Random ip adress genarated is: ")
print(ipd)
—-----------------------------------------------------------------------------------------------------------------10. Checking for valid ip address
ip=input("enter the ip adress\n")
ipd=ip.split('.')
if len(ipd)==4:
for i in range(0,4):
temp=1
if not(int(ipd[i])>=0 and int(ipd[i])<256) :
temp=0
break
if temp==1:
print("its valid ip adress")
else:
print("not a valid ip")
else:
print("not in valid format")
—-----------------------------------------------------------------------------------------------------------------11. Binary operations
binary=input("enter the binary number\n")
dec=int(binary,2)
octal=oct(dec)
print("binary to octal:",octal[2:])
print("binary to decimal:",dec)
bi=[bit for bit in binary]
ones=[]
for i in range(0,len(bi)):
if int(bi[i])==0:
ones.append("1")
else:
ones.append("0")
one="".join(ones)
print("ones compliment:",one)
c=1
twos=[]
ron=ones[::-1]
for j in ron:
j=int(j)
if (j==1 and c==1):
twos.append("0")
c=1
elif (j==1 and c==0):
twos.append("1")
c=0
elif (j==0 and c==1):
twos.append("1")
c=0
elif (j==0 and c==0):
twos.append("0")
c=0
two=twos[::-1]
print("twos compliment:","".join(two))
—-----------------------------------------------------------------------------------------------------------------12. String sorting
stri=input("enter the string\n")
words=stri.split()
words.sort(key=str.lower)
print("String in ascending order: ", " ".join(words))
desc=words[::-1]
print("String in descending order: ", " ".join(desc))
—------------------------------------------------------------------------------------------------------------------
Download