0% found this document useful (0 votes)
28 views

Python Programming Dhaval Patel L D College of Engineering Python Programming

The document contains Python code examples for arithmetic operators, comparison operators, logical operators, assignment operators, and membership operators. It also includes programs to check if a number is odd or even, positive/negative/zero, swap two numbers, find the largest of three numbers, calculate sum of odd and even numbers from 1 to 10, make a basic calculator, and calculate SPI from marks. The code examples demonstrate the use of basic Python operators and control flow statements like if/else. The programs take user input and print output.

Uploaded by

Dharmisha panjri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Python Programming Dhaval Patel L D College of Engineering Python Programming

The document contains Python code examples for arithmetic operators, comparison operators, logical operators, assignment operators, and membership operators. It also includes programs to check if a number is odd or even, positive/negative/zero, swap two numbers, find the largest of three numbers, calculate sum of odd and even numbers from 1 to 10, make a basic calculator, and calculate SPI from marks. The code examples demonstrate the use of basic Python operators and control flow statements like if/else. The programs take user input and print output.

Uploaded by

Dharmisha panjri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Muskan gidwani 210280111509

Exercise 1

PYTHON PROGRAMMING
Dhaval Patel
L D College of Engineering
Python programming

 Arithmatic Operator

1. Multiply 10 with 5, and print the result.


INPUT :
a=10
b=5
c=a*b
print(c)
print(a*b)

Output :

2. Add 20 with 66, and print the result.


Input :
3. a=20
b=66
c=a+b
print(c)
print(a+b)

Output :

4. Subtract 88 with 52, and print the result.


Input :
a=88
b=52
c=a-b
print(c)
print(a-b)
Output :
1
Muskan gidwani 210280111509

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 :

7. Modulo 52 with 4, and print the result.


Input :
8.
a=52
b=4
c=a%b
print(c)
print(a%b)

Output :

9. Exponantion 5 with 66, and print the result.


Input :
10.
a=66
b=5
c=a**b
print(c)
print(a**b)
print(66**5)
print(5**66)

Output :

3
Muskan gidwani 210280111509

Exercise 1

11.Float division of 20 with 6, and print the result.


Input :
a=20
b=6 c=a/b
print(c)
print(a/b)
print(20/6)

Output :

 What will be the output of following expression in python?

Input : 4 + 5.2 – 0.2

a=4
b=5.2
c=0.2
print(a+b-c)
print(4+5.2-0.2)

Output :

Input : 9.8 – 2.1 * 3

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 :

Input : (True or False) and (not (True and False))

print((True or False) and (not (True and False))) a=True


b=False
print((a or b)and(not(a and b)))

Output :

5
Muskan gidwani 210280111509

Exercise 1

Input : (2**3)%2==4

a=2 b=3 c=2 d=4


print((a**b)%c==d)
print((2**3)%2==4)

Output :

Input :((2*5)+4)-8%3**2

a=2 b=5 c=4 d=8 e=3 f=2


print(((a*b)+c)-d%e**f)
print(((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 :

1. Write a program to use comparison operator to find the relation


between given two numbers 55 and 33.
(Greater than, Less than, Equal to, not equal to, Greater than or equal to,
Less than or equal to).
Input :
#comparsion operator
a=55
b=33
print("the output of 55 greater than 33 is : ", a>b )
print("the output of 55 less than 33 is : ", a<b )
print("the output of 55 greater than or equal to 33 is : ", a>=b )
print("the output of 55 less than or equal to 33 is : ", a<=b )
print("the output of 55 not equal to 33 is : ", a!=b )
print("the output of 55 equal to 33 is : ", a==b )
Output :

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

Input :'hello' in 'hello world'

# membership operator
a=("hello world")
print('hello 'in a)

Output :

Input : 'name' in 'hello world'


a=("hello world")
print('name'in a)

Output :

Input : b=[1,2,3,4,5] 5 not in b


b = [1, 2, 3, 4, 5]
print(5 not in b) print(7 not in b)

Output :

11
Muskan gidwani 210280111509

Python Lab work


EXERCISE 2

Write a program to check the given number is Odd or Even.


Input :
# program to check the given number is Odd or Even. num=int(input("enter
the number to check for even/odd:")) if num%2==0:
print(num,"is even")
else:
print(num,"is odd")

Output :

Write a program to check if a given number is positive, negative or


zero.
Input :
# program to check if a given number is positive, negative or zero. num=
int(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 nummber ")

Output :

1
Muskan gidwani 210280111509

Write a program to swap the two numbers without using temporary


variable.
Input :
# program to swap the two numbers without using temporary variable.
x=int(input("enter the first number:"))
y=int(input("enter the second number:")) x,y=y,x
print(" after swap first number is",x,"and second number is",y)

Output :

Write a program to find Largest among given Three Numbers.


Input :
#program to find Largest among given Three Numbers a=int(input("enter
the first number:")) b=int(input("enter the second number:"))
c=int(input("enter the third number:"))
if (a>b) and (a>c):
print("largest number is:",a) elif(b>a) and (b>3):
print("largest number is:",b)
else:
print("largest number is :",c)

Output :

2
Muskan gidwani 210280111509

Write a program to sum of ODD and EVEN numbers from 1 to 10.


Input :
#program to sum of ODD and EVEN numbers from 1 to 10. a=
int(input('enter a number :'))
list1=[] list2=[]
for i in range(a,a+11): if i%2==0:
list1.append(1)
else:
list2.append(1)
print("sum of even number is :",sum(list1)) print("sum of odd numbers is :",
sum(list2))

Output :

Write a program make a simple calculator that can add, subtract,


multiply and divide using user choice.
Input :
# program make a simple calculator that can add, subtract, multiply and
divide using user choice.
x=int(input("enter the first number:")) y=int(input("enter the second
number:"))
operation=input(' select operation from following +,-,*,/ : ') if operation
=="+":
print("sum of numbers is : ",x+y) elif operation =="-":
print("subtraction of numbers is:",x-y) elif operation =="*":
print("multiplication of numbers is :",x*y) elif operation=="/":
print("division of numbers is : ",x/y)

3
Muskan gidwani 210280111509

Output :

Write a program to calculate SPI using given marks.


Input :
# program make a simple calculator that can add, subtract, multiply and
divide using user choice.
x=int(input("enter the first number:")) y=int(input("enter the second
number:"))
operation=input(' select operation from following +,-,*,/ : ') if operation
=="+":
print("sum of numbers is : ",x+y) elif operation =="-":
print("subtraction of numbers is:",x-y) elif operation =="*":
print("multiplication of numbers is :",x*y) elif operation=="/":
print("division of numbers is : ",x/y)

4
Muskan gidwani 210280111509

python1=(int(python)*int(7)) elif grade=="cc":


python1=(int(python)*int(6)) elif grade=="cd":
python1=(int(python)*int(5)) elif grade=="dd":
python1=(int(python)*int(4)) elif grade=="ff":
python1=(int(python)*int(0))

average2=((adc1+python1)/8) print("your spi is :",average2)

Output :

5
MUSKAN GIDWANI 210280111509

EXERCISE 3
EXERCISE 3

1) Write a Python program to find out the sum of all digits of


a number. INPUT :
# Python program to find out the sum of all digits of a number.
num=input("enter the number:")
sum=0
for i in num:
sum = sum + int(i)
print(sum)

Output :

1
MUSKAN GIDWANI 210280111509

EXERCISE 3

2) Write a Python program to print all combinations of three


numbers. Input :
#Python program to print all combinations of three numbers.
a=int(input("enter the first number :"))
b=int(input("enter the second number :")) c=int(input("enter the second
number :")) combination=[]
combination.append(a) combination.append(b) combination.append(c) for i
in range(0,3):
for j in range(0,3):
for k in range(0,3): if(i!=j&j!=k&k!=i):
print(combination[i],combination[j],combination[k])

Output :

2
MUSKAN GIDWANI 210280111509

EXERCISE 3

3) Write a Python program to calculate total vowels in


a string . Input :
#Python program to calculate total vowels in a string . x=input("enter the
string :")
vowels=0
for i in str(x):
if(i=='a' or i=='A' or i=='e' or i=='E' or i=='i' or i=='I' or i=='o' or i=='O'or
i=='u' or i=='U'):
vowels=vowels+1 print('number of vowels are :') print(vowels)

Output :

3
MUSKAN GIDWANI 210280111509

EXERCISE 3

4) Python program to find the smallest and largest divisor of


a number. Input :
#Python program to find the smallest and largest divisor of a number
a=int(input(" enter the integer number :"))
b=[]
for i in range(1,a+1): if a%i==0:
b.append(i) print('smallest divisior of',a,"is : 1") print("largest divisor is
:",a)
print(" all possible divisors are :",b)

Output :

4
MUSKAN GIDWANI 210280111509

EXERCISE 3

5) Write a python program to reverse a


number. Input :
#python program to reverse a number. num=input("enter the number :")
b=(num[::-1])
print("the reverse number is : ",b)

Output :

5
MUSKAN GIDWANI 210280111509

EXERCISE 3

6) Write a python program to check if a number is


abundant/excessive or not. Input :
#python program to check if a number is abundant/excessive or not.
a=int(input(" enter the integer number :"))
b=[]
for i in range (1,a+1):
if a%i==0 and i!=a: b.append(i)
print("proper divisors are :" ,b) if a>sum(b):
print(a," is not abundent number sum of divisor :", sum(b))
else:
print(a,"is abundent number because sum of divisior:", sum(b))

Output:

6
MUSKAN GIDWANI 210280111509

EXERCISE 3

7) Write a python program to check if a year is a leap


year or not. Input :
# python program to check if a year is a leap year or not. year=int(input("
Enter year to be check :"))
if((year % 400 == 0) or (year % 100 != 0) and (year % 4==0)): print("given
year is a leap year")
else:
print(" given year is not a leap year")

Output :

7
MUSKAN GIDWANI 210280111509

EXERCISE 3

8) Write a python program to display all the prime numbers


within an interval range given by user.
Input :
# python program to display all the prime numbers within an interval
range given by user.
a= int(input("enter starting number:" )) b= int(input(" enter ending number
:")) c=[]
for num in range (a,b+1): if num>1:
for i in range(2,num): if(num%i)==0:
break
else:
c.append(num)
print("prime numbers between",a,"and",b,"is :",c) d=(len(c))
print("totsl prime numbers between",a,"and",b,"is :",d)

Output :

8
MUSKAN GIDWANI 210280111509

EXERCISE 3

9)Write a python program to check if a string is


palindrome or not. Input :
#python program to check if a string is palindrome or not x=input("enter
the string :")
y=x[::-1]
if x==y:
print(" string is palindrome")
else:
print("string is not palindrome")

Output :

9
MUSKAN GIDWANI 210280111509

EXERCISE 3

10) Write a program to print the following pattern in


output console window.

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

1) Write a Python program to find the LCM of two numbers.


Input :
# Python program to find the LCM of two numbers a=int(input("enter
the first number is :")) b=int(input("enter the second number is :"))
if a>b:
number= a
else:
number= b while 1:
if(number%a ==0 and number%b ==0): lcm=number
break number+=1
print('the lcm is: ',lcm)

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

6) Write a python program to print one identity matrix.


Input :
#a python program to print one identity matrix. size = int(input("Enter
the size of the matrix :")) for x in range(0,size):
for y in range(0,size): if (x==y):
print("1",end =" ")
else:
print("0",end =" ")
print()

Output :

You might also like