Python Programming Dhaval Patel L D College of Engineering Python Programming
Python Programming Dhaval Patel L D College of Engineering Python Programming
Exercise 1
PYTHON PROGRAMMING
Dhaval Patel
L D College of Engineering
Python programming
Arithmatic Operator
Output :
Output :
Exercise 1
2
Muskan gidwani 210280111509
Exercise 1
5. Divide 20 with 6, and print the result.
Input :
6.
a=20
b=6
c=a/b
print(c)
print(a/b)
Output :
Output :
Output :
3
Muskan gidwani 210280111509
Exercise 1
Output :
a=4
b=5.2
c=0.2
print(a+b-c)
print(4+5.2-0.2)
Output :
a=9.8
b=2.1
c=3
print(a-b*c)
print(9.8-2.1*3)
Output :
4
Muskan gidwani 210280111509
Exercise 1
Input : 2 * 3 – 4 // 2
a=2 b=3 c=4 d=2
print(a*b-c//d)
print(2*3-4//2)
Output :
Input: (10 + 4 * 2) / 9 + 1
a=10
b=4
c=2
d=9
e=1
print((a+b*c)/d+e)
print((10+4*2)/9+1)
Output :
Output :
5
Muskan gidwani 210280111509
Exercise 1
Input : (2**3)%2==4
Output :
Input :((2*5)+4)-8%3**2
Output :
Input : 2.5/0.5/2
print(2.5/0.5/2) a=2.5
b=0.5
c=2 print(a/b/c)
Output :
6
Muskan gidwani 210280111509
Exercise 1
Input : 2.5/0.5//2
print(2.5 / 0.5 // 2) a=2.5
b=0.5
c=2 print(a/b//c)
Output :
Input : 5**3+3**4>=200
print(5**3+3**4>=200) a=5
b=3 c=3 d=4 e=200
print(a**b+c**d>=e)
Output :
7
Muskan gidwani 210280111509
Exercise 1
Compare operator :
8
Muskan gidwani 210280111509
Exercise 1
Logical operator
If A=True and B=False than print the result of and, or, not of given
operands
Input :
#logical operator
a=True
b=False c= a and b print(c)
print("the output of a and b :", a and b) print("the output of a or b ;", a
or b) d=a or b
print(d)
print(" the output of not a:", not a) e=not a
print(e)
print(" the output of not b:", not b) e=not b
print(e)
Output :
9
Muskan gidwani 210280111509
Exercise 1
Assignment operators
For A=66 and B=8, use all the different assignment operator and print the
result.
Input :
# assigmment operator x=66
y=8
x+=y # x=x+y , current value of x is 66 and y is 8 print(x)
x-=y # x=x-y, current value of x is 74 and y is 8 print (x)
x*=y # x=x*y , current value of x is 66 and y is 8 print (x)
x/=y # x=x/y, current value of x is 528 and y is 8 print(x)
x%=y # x= x%y, current value of x is 66 and y is 8 print (x)
x**=y # x= x**y , current value of x is 2 and y is 8 print(x)
x//=y # x= x//y , current value of x is 256 and y is 8 print(x)
Output :
10
Muskan gidwani 210280111509
Exercise 1
Membership Operator
# membership operator
a=("hello world")
print('hello 'in a)
Output :
Output :
Output :
11
Muskan gidwani 210280111509
Output :
Output :
1
Muskan gidwani 210280111509
Output :
Output :
2
Muskan gidwani 210280111509
Output :
3
Muskan gidwani 210280111509
Output :
4
Muskan gidwani 210280111509
Output :
5
MUSKAN GIDWANI 210280111509
EXERCISE 3
EXERCISE 3
Output :
1
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output :
2
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output :
3
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output :
4
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output :
5
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output:
6
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output :
7
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output :
8
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output :
9
MUSKAN GIDWANI 210280111509
EXERCISE 3
Patterns:
1
12
123
1234
Input :
print("pattern") for i in range(5):
for j in range(1,i+1): print(j,end="")
print() print()
Output :
1
22
333
4444
Input :
print("pattern") for i in range(5):
for j in range(i):
print(i,end="") print()
print()
Output :
10
MUSKAN GIDWANI 210280111509
EXERCISE 3
*****
****
***
**
*
Input
:
print("pattern") for i in range(5):
for j in range(5-i): print("*",end="")
print() print()
Output :
p
* r
** i
*** n
**** t
(
****
"
p
Input a
print("pattern") for i t
in range(5): t
for j in range(5-i): print(" e
",end="") r
for j in range(i+1): n
print("*",end="") "
for j in range(1,i+1): )
print("*",end="")
print() f
print() o
r
i
n
r
11 a
n
g
MUSKAN GIDWANI 210280111509
EXERCISE 3
Output:
12
MUSKAN GIDWANI 210280111509
EXERCISE 3
*****
**
**
**
*****
Input :
print("pattern") for i in range(5):
for j in range(5):
if (i==0 or i==4 or j==0 or j==4): print("*",end=" ")
else:
print(" ",end=" ")
print()
Output :
13
MUSKAN GIDWANI 210280111509
EXERCISE 4
EXERCISE 4
Output :
1
MUSKAN GIDWANI 210280111509
EXERCISE 4
2) Write a Python program to find if a number is Armstrong or not.
INPUT :
#Python program to find if a number is Armstrong or not.
a=int(input('enter the number:'))
b=a total=0 while b> 0:
answer = b % 10 total +=answer ** 3 b //=10
if a==total:
print('the number is armstrong number')
else:
print('the number is not armstrong number')
Output :
2
MUSKAN GIDWANI 210280111509
EXERCISE 4
3)Write a Python program to find the total number of lowercase
characters in a string.
Input :
Python program to find the total number of lowercase characters in a
string.
string=input("Enter the string:") count = 0
for a in string:
if(a.islower()):
count = count + 1 if(count == 0):
print(" No lower case character is found in string.")
else:
print("Total number of Lower Case Character:",count)
Output :
3
MUSKAN GIDWANI 210280111509
EXERCISE 4
4)Write a program to remove characters from odd or even index of a
string.
Input :
# python program to remove characters from odd or even index of a
string.
string= input('Enter the string:') output = ""
odd_Or_even = str(input("Enter 'a' if you want to remove even position
characters ,'b'for odd position character: "))
if odd_Or_even == 'a':
print(" The string after removing characters on even position:") for x in
range (len(string)) :
if x%2 == 0:
output = output+string[x] elif odd_Or_even =='b':
print(" The string after removing characters on odd position :") for x in
range (len(string)) :
if x%2 != 0 :
output=output+string[x]
print (output)
Output :
4
MUSKAN GIDWANI 210280111509
EXERCISE 4
5) Write a python program to merge two lists and sort it.
Input :
# python program to merge two lists and sort it x=[]
y=[]
list1=int(input('Enter the number of elements :')) for i in range(1,list1+1):
a=int(input('Enter the element')) x.append(a)
list2=int(input('Enter the number of elements:')) for i in range(1,list2+1):
b=int(input('Enter the elements')) y.append(b)
output=x+y output.sort()
print('sorted list is the:',output)
Output :
5
MUSKAN GIDWANI 210280111509
EXERCISE 4
Input using sorted ()method:
#python program to merge two lists and sort it using sorted() method
list1=[4,6,7,8]
list2=[45,76,89,56]
output=sorted(list1+list2) print("sorted list is the :",output)
Output ;
6
MUSKAN GIDWANI 210280111509
EXERCISE 4
Output :