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

Python Practical

a python code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Practical

a python code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Write a program to perform different Arithmetic operations in python.

program:

#store first input


x = int(input("enter the number:"))
#store operator
operator = input("operator:")
#store second input
y = int(input("enter the second number:"))
#operation
if operator == '+':
print("sum=",x+y)
elif operator == '-':
print("sum=",x-y)
elif operator == '*':
print("sum=",x*y)
elif operator == '/':
print("sum=",x/y)
elif operator == '%':
print("sum=",x%y)

Output:
2. Write a python program to check whether the given number is even or
odd

Program:

#input value
num = int(input("enter the number:"))
#comparison
if num % 2 == 0:
print(num,"is even")
else :
print(num,"is odd")

Output:
3.1 To Implement Python Programs for Looping and control structure.

Program:

#loop for range


for i in range(5):
print(f"number={i}")
#loop for fruit list
fruits = ["apple","banana","grapes"]
for fruit in fruits:
print("name=",fruit)

Output:
3.2 To Implement Python Programs for control structure.

Program:

#initialise counter
counter = 0
#loop for counter
while counter < 5:
print("count=",counter)
#increase counter
counter= counter+1

Output:
4.1 Write a python program to find following pattern
1) Left Triangle Star Pattern In Python
*
**
***
****
*****
Program :

#initialise i
i=0
#loop for pattern
while i<=5:
print(i*'*')
#i increment
i+=1

Output:
4.2 Write a python program to find following pattern
1) Left Triangle Star Pattern In Python
*
**
***
****
*****

Program :

rows = 5
k = 2 * rows - 2
for i in range(0, rows):
# process each column
for j in range(0, k):
# print space in pyramid
print(end=" ")
k=k-2
for j in range(0, i + 1):
# display star
print("* ", end="")
print("")

Output:
1. Write a python program to find the length of a given string and reverse
the same.

Program:

#define reverse
def reverse(string):
#start from back side
string = string[::-1]
return string
s = input("enter the string:")
#original string
print("before reverse string:",s)
#reverse string
print("after reverse string:",revers(s))

Output:
2. Write a python program to check whether the string is in
uppercase,lowercase and capitalise first
letter or not

Program :

#input string
s = input("enter the string:")
#check the string is in upper case or not
if s.isupper():
print("string is in upper case")
else:
print("string is in lower case")
#check the first letter is in capital or not
if s[0].isupper():
print("the first letter is capital")
else:
print("the first letter is not capital")

Output:

You might also like