You are on page 1of 15

In [2]: # Given Year is Leap Year or not

year=int(input(" Enter the Year :"))

if year%4==0:
if year % 100==0:
if year % 400==0:
print("{} is Leap Year".format(year))
else:
print("{} is not Leap Year".format(year))
else:
print("{} is Leap Year".format(year))
else:
print("{} is not Leap Year".format(year))

Enter the Year :2021


2021 is not Leap Year

In [3]: # Given Number is Palendrome or not

In [4]: num= int(input(" Enter the number :"))


m=num
rev=0

while num>0:
rem=num%10
rev=rev*10+rem
num=num//10

if m==rev:
print("{} is Palendrom".format(m))
else:
print("{} is not Palendrom".format(m))

Enter the number :12321


12321 is Palendrom

In [5]: # Given String is Palendrome or not

In [6]: str1=input(" Enter the String :")

str2=""
length = len(str1)

for i in range(length-1,-1,-1):
str2=str2+str1[i]

if str1==str2:
print("{} is Palendrom".format(str1))
else:
print("{} is not Palendrom".format(str1))

Enter the String :MALAYALAM


MALAYALAM is Palendrom
In [3]: # Febinocii Series using for loop
n=int(input("Enter the number :"))
a,b=0,1

for i in range(n):
print(a, end =" ")
a,b=b,a+b

Enter the number :10


0 1 1 2 3 5 8 13 21 34

In [4]: # Febinocii Series using while loop

n=int(input("Enter the number :"))


a,b=0,1

while n>0:
print(a, end=" ")
a,b=b,a+b
n-=1

Enter the number :10


0 1 1 2 3 5 8 13 21 34

In [5]: n=int(input("Enter a number :"))


fact=1
for i in range (1, n+1):
fact=fact*i
print("factorial of {} is {}".format(n,fact))

Enter a number :10


factorial of 10 is 3628800

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

In [1]: s=1
for r in range(0,5):
for c in range(0,7):
print("{:02}".format(s) , end=" ")
s+=1
print('\r')

01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
In [2]: s=1
for r in range(0,5):
for c in range(0,7):
if s>31:
break
else:
print("{:02}".format(s) , end=" ")
s+=1
print('\r')

01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

In [7]: def dectoBin(n):


if n>1:
dectoBin(n//2)
print(n%2, end=" ")

a=34
dectoBin(a)

1 0 0 0 1 0

In [8]: # Febinocii series using Generator


def febi(n):
a,b=0,1
while n>=1:
yield a
a,b=b,a+b
n=n-1

x=febi(10)
for i in x:
print(i , end=" ")

0 1 1 2 3 5 8 13 21 34

In [11]: # Febinocii Series using Generator


def febi(n):
a,b=0,1
while n>=1:
yield a
a,b=b,a+b
n=n-1

for i in febi(10):
print(i , end =" ")

0 1 1 2 3 5 8 13 21 34
In [12]: def febi(n):
a,b=0,1
while n>=1:
yield a
a,b=b,a+b
n=n-1

x= febi(10)
print(next(x))
print(x.__next__())

0
1

In [13]: def fibonacci_numbers(nums):


x, y = 0, 1
while nums>=1:
x, y = y, x+y
nums-=1
yield x

def square(nums):
for num in nums:
yield num**2

print(sum(square(fibonacci_numbers(10))))

4895

In [15]: def fact(n):


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

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


x=fact(n)
print(x)

Enter a number :10


3628800
In [16]: def fact(n):
while n>=1:
x=1
y=n
while y>=1:
x=x*y
y-=1
yield x
n=n-1

num= fact(10)
for i in num:
print(i)

3628800
362880
40320
5040
720
120
24
6
2
1

In [17]: lst=[0,1,5,4,6,8]
lst1=iter(lst)
print(next(lst1), end=" ")
print(next(lst1), end=" ")
print(next(lst1), end=" ")
print(next(lst1), end=" ")
print(lst1.__next__())

0 1 5 4 6
In [18]: # Guess the no:
import random
x=random.randint(1,20)
chances=3
print(x)
while(chances !=0):
guess=int(input("Enter the Guessing Number : "))
if (int(guess)==x):
print("Well Done !")
print("Your Guessing Number is :", x)
break
elif (int(guess) < x):
print(" Guessing number is small ")
chances-=1
elif (int(guess) > x):
print(" Guessing number is Large ")
chances-=1

else:
print(" Chances are Over .. Better Luck Next Time")

9
Enter the Guessing Number : 5
Guessing number is small
Enter the Guessing Number : 10
Guessing number is Large
Enter the Guessing Number : 9
Well Done !
Your Guessing Number is : 9

In [20]: minimum=-1
maximum=-1
list1=[]
while(1):
n=input('enter the numbers :')
list1.append(n)
if (n=="done"):
print(" Done !")
break
elif maximum < int(n):
maximum = int(n)

elif minimum == -1:


minimum = int(n)

elif int(n) <minimum:


minimum = int(n)

print(" The numbers you entered :", list1)


print(" Minimum Number :", minimum)
print(" Maxiimum Number :", maximum)

enter the numbers :5


enter the numbers :6
enter the numbers :9
enter the numbers :8
enter the numbers :0
enter the numbers :-1
enter the numbers :done
Done !
The numbers you entered : ['5', '6', '9', '8', '0', '-1', 'done']
Minimum Number : -1
Maxiimum Number : 9
In [21]: string= input("Enter the string :")
str=string.split()
str.sort()
lst=[]
dict={}
for i in str:
if i not in lst:
c= str.count(i)
lst.append(i)
print(i,c, sep=":")
dict[i]=c
print(dict)

Enter the string :SRI VASAVI ENGG COLLEGE IS ONE OF THE REPUTED ENGG COLLEGE IN THE
ANDHRA PRADESH
ANDHRA:1
COLLEGE:2
ENGG:2
IN:1
IS:1
OF:1
ONE:1
PRADESH:1
REPUTED:1
SRI:1
THE:2
VASAVI:1
{'ANDHRA': 1, 'COLLEGE': 2, 'ENGG': 2, 'IN': 1, 'IS': 1, 'OF': 1, 'ONE': 1, 'PRADES
H': 1, 'REPUTED': 1, 'SRI': 1, 'THE': 2, 'VASAVI': 1}

In [22]: import random

print(random.randrange(10, 20))

x = ['a', 'b', 'c', 'd', 'e']

# Get random choice


print(random.choice(x))

# Shuffle x
random.shuffle(x)

# Print the shuffled x


print(x)

# Print random element


print(random.random())

19
d
['a', 'e', 'd', 'c', 'b']
0.4240809725114303
In [23]: txt= "Rajesh KOne"
print("{:20}".format(txt))
print("{:*^20}".format(txt))
print("{:*<20}".format(txt))
print("{:*>20}".format(txt))
print("{:^20}".format(txt))
print("{:<20}".format(txt))
print("{:>20}".format(txt))

Rajesh KOne
****Rajesh KOne*****
Rajesh KOne*********
*********Rajesh KOne
Rajesh KOne
Rajesh KOne
Rajesh KOne

In [24]: str = "Hello"


str1 = " world"
print(str*3) # prints HelloHelloHello
print(str+str1) # prints Hello world
print(str[4]) # prints o
print(str[2:4]); # prints ll
print('w' in str) # prints false as w is not present in str
print('wo' not in str1) # prints false as wo is present in str1.
print(R"//\\//\\//\\") # prints //\\//\\//\\
print("The string str : %s"%(str)) # prints The string str : Hello

HelloHelloHello
Hello world
o
ll
False
False
//\\//\\//\\
The string str : Hello

In [25]: list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


list1 = [123, 'john']
print (list) # Prints complete list
print (list[0]) # Prints first element of the list
print (list[1:3]) # Prints elements starting from 2nd till 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (list1 * 2) # Prints list two times
print (list + list1) # Prints concatenated lists

['abcd', 786, 2.23, 'john', 70.2]


abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
In [26]: tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tuple1 = (123, 'john')
print (tuple) # Prints complete tuple
print (tuple[0]) # Prints first element of the tuple
print (tuple[1:3]) # Prints elements starting from 2nd till 3rd
print (tuple[2:]) # Prints elements starting from 3rd element
print (tuple1 * 2) # Prints tuple two times
print (tuple + tuple1) # Prints concatenated tuple

('abcd', 786, 2.23, 'john', 70.2)


abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')

In [27]: tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )


list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]

list[2] = 1000 # Valid syntax with list


tuple[2] = 1000 # Invalid syntax with tuple

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-ecbe5ba87f64> in <module>
3
4 list[2] = 1000 # Valid syntax with list
----> 5 tuple[2] = 1000 # Invalid syntax with tuple

TypeError: 'tuple' object does not support item assignment

In [28]: lst =[]


while(1):
a = input("enter a number / 'done' for exit : ")
if (a == 'done'):
break
else:
lst.append(a)
print("Numbers you entered :", lst)

enter a number / 'done' for exit : 1


enter a number / 'done' for exit : 2
enter a number / 'done' for exit : 6
enter a number / 'done' for exit : 9
enter a number / 'done' for exit : 10
enter a number / 'done' for exit : done
Numbers you entered : ['1', '2', '6', '9', '10']
Pre defined functions
print()
input()
type()
int()
str()
len()
sum()
max()
min()
ord()
chr()

In [29]: balance=0

pin=int(input("Enter PIN :"))


if pin==1234:
while(1):
choice=input("Do u want to Diposit / Withdraw / Exit '\n' D / W / X :" )
if choice == 'D':
dipo=float(input("Enter Deposit Amount :"))
balance=balance+dipo

if choice == 'W':
withdr=float(input("Enter Withdraw Amount : "))
if withdr > balance:
print("Insufficient Funds")
break
else:
balance=balance-withdr

if choice== 'X':
print("Thank You! ")
break
print ("Balance is :", balance)
else:
print("Invalid PIN")

Enter PIN :1234


Do u want to Diposit / Withdraw / Exit '
' D / W / X :D
Enter Deposit Amount :10000
Do u want to Diposit / Withdraw / Exit '
' D / W / X :W
Enter Withdraw Amount : 1000
Do u want to Diposit / Withdraw / Exit '
' D / W / X :X
Thank You!
Balance is : 9000.0
3.Write a program that calculates and prints the value according to the given
formula:
Q = Square root of [(2 C D)/H]
Following are the fixed values of C and H:
C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated sequence.
Example
Let us assume the following comma separated input sequence is given to the program:
100,150,180
The output of the program should be:
18,22,24

In [30]: import math


C=50
H=30
n= input("Enter the inputs :")
lst=n.split(',')
for i in lst:
D= int(i)
Q= math.sqrt((2 * C * D)/H)
print(round(Q), end=" ")

Enter the inputs :100,150,180


18 22 24

4.Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional
array. The element value in the i-th row and j-th column of the array should be i*j.
- Note: i=0,1.., X-1; j=0,1,…Y-1.
- Example
- Suppose the following inputs are given to the program:
- 3,5
- Then, the output of the program should be:
- [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

In [32]: X= int(input("Enter the input X :"))


Y= int(input("Enter the input Y :"))
lst=[]
for i in range(0,X):
lst.append([])
for j in range(0,Y):
lst[i].append(i*j)
print(lst)

Enter the input X :3


Enter the input Y :5
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
5.Write a program that accepts a comma separated sequence of words as input and
prints the words in a comma-separated sequence after sorting them
alphabetically.
- Suppose the following input is supplied to the program:
- Input : without,hello,bag,world

- OUTPUT: bag,hello,without,world

In [33]: str= input(" Enter the words : ")


lst=str.split(',')
lst.sort()
# print(lst)
# (or) print(sorted(lst))
sort= ','.join(lst)
print(sort)

Enter the words : without,hello,bag,world


bag,hello,without,world

In [34]: binary=input("Enter binary number :")


length=len(binary)-1
decimal=0
for i in binary:
if length >=0:
value=int(i)*( 2**length)
decimal=decimal+value
length -=1
print("Decimal value of \" {} \" is \" {} \"".format(binary,decimal))

Enter binary number :10101


Decimal value of " 10101 " is " 21 "

In [1]: # convert to decimal to binary


n=int(input("Enter Decimal Number :"))
str1=""
while n>0:
r=n%2
n=n//2
str1=str(r)+str1
print(int(str1))

Enter Decimal Number :10


1010

In [2]: # convert to decimal to octal


n=int(input("Enter Decimal Number :"))
str1=""
while n>0:
r=n%8
n=n//8
str1=str(r)+str1
print(int(str1))

Enter Decimal Number :8


10
Write a program to compute the frequency of the words from the input. The output
should output after sorting the key alphanumerically.
Suppose the following input is supplied to the program:
Input: New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
Then, the output should be:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1

In [35]: str1=input("Enter the string :")

lst1=[]
lst=str1.split()
lst.sort()
for i in lst:
if i not in lst1:
lst1.append(i)
count=str1.count(i)
print("{} : {}".format(i,count))
else:
continue

Enter the string :New to Python or choosing between Python 2 and Python 3? Read Pyth
on 2 or Python 3.
2 : 2
3. : 1
3? : 1
New : 1
Python : 5
Read : 1
and : 1
between : 1
choosing : 1
or : 2
to : 1
In [36]: # Given number is Prime or not

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

for i in range(2,n):
if n%i==0:
print("{} is not prime".format(n))
break
else:
print("{} is prime".format(n))

Enter a number :7
7 is prime

In [37]: # Average of Prime numbers in given range

def avgprime(lst):
print(lst)
l=len(lst)
avg= sum(lst)/l
print(avg)

n=int(input("enter range : "))


lst=[]
for i in range(2,n+1):
c=0
for j in range(1,i+1):
if i%j==0:
c+=1
else:
continue
if c==2:
lst.append(i)

avgprime(lst)

enter range : 10
[2, 3, 5, 7]
4.25

In [40]: binary="1010"
n=4
print(binary.zfill(4 + len(binary)))

00001010

In [1]: num = 10

# check and print type of num variable


print(type(num))

# convert the num into string


converted_num = str(num)

# check and print type converted_num variable


print(type(converted_num))

<class 'int'>
<class 'str'>
In [7]: roll="18A81A04"
for i in range(1,61):
if i<10:
print(roll+'0'+str(i), '\t', end=" ")
else:
print(roll+str(i), '\t', end=" ")

18A81A0401 18A81A0402 18A81A0403 18A81A0404 18A81A0405 18A


81A0406 18A81A0407 18A81A0408 18A81A0409 18A81A0410 18A
81A0411 18A81A0412 18A81A0413 18A81A0414 18A81A0415 18A
81A0416 18A81A0417 18A81A0418 18A81A0419 18A81A0420 18A
81A0421 18A81A0422 18A81A0423 18A81A0424 18A81A0425 18A
81A0426 18A81A0427 18A81A0428 18A81A0429 18A81A0430 18A
81A0431 18A81A0432 18A81A0433 18A81A0434 18A81A0435 18A
81A0436 18A81A0437 18A81A0438 18A81A0439 18A81A0440 18A
81A0441 18A81A0442 18A81A0443 18A81A0444 18A81A0445 18A
81A0446 18A81A0447 18A81A0448 18A81A0449 18A81A0450 18A
81A0451 18A81A0452 18A81A0453 18A81A0454 18A81A0455 18A
81A0456 18A81A0457 18A81A0458 18A81A0459 18A81A0460

In [ ]:

In [ ]:

You might also like