0% found this document useful (0 votes)
3 views21 pages

Python Programs for Data Types & Operations

The document contains a series of programming tasks in Python, covering various topics such as data types, arithmetic operations, string manipulation, and control structures. Each task includes a code snippet and its expected output, demonstrating fundamental programming concepts. Tasks range from basic operations like finding the largest of three numbers to more complex string and list manipulations.

Uploaded by

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

Python Programs for Data Types & Operations

The document contains a series of programming tasks in Python, covering various topics such as data types, arithmetic operations, string manipulation, and control structures. Each task includes a code snippet and its expected output, demonstrating fundamental programming concepts. Tasks range from basic operations like finding the largest of three numbers to more complex string and list manipulations.

Uploaded by

lovekhush630
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2332385 BTCS 513-18

TASK 1: Write a program to demonstrate different number data types in


Python.
Code:
Number1 = input("enter a number")
TypeOfNumber1 = type(Number1)
print(TypeOfNumber1)

OUTPUT :

1
2332385 BTCS 513-18

TASK 2: Write a program to perform different Arithmetic Operations on


numbers in Python.
Code:
while True :

Number1= int(input("Enter the First number: "))


Number2= int(input("Enter the Second number: "))
print("1) + For Addititon \n2) - For subtraction \n3) * For Multiplication\n4) // For int
Divide \n5) / For int Divide \n6) ** For Power \n7) Type Exit to Exit the program ")

Operator = (input("Enter the arithmetic operator:"))

if(Operator== "+" ):
print(Number1, "+", Number2, "=", Number1+Number2 )
elif(Operator == "-"):
print(Number1, "-",Number2,"=",Number1-Number2)
elif(Operator == "*"):
print(Number1, "*",Number2,"=",Number1*Number2)
elif(Operator == "//"):
print(Number1, "//",Number2,"=",Number1//Number2)
elif(Operator == "/"):
print(Number1, "/",Number2,"=",Number1/Number2)
elif(Operator == ""):
print(Number1, "",Number2,"=",Number1**Number2)
elif(Operator == "Exit"):
print("Exited!!!")
else:

2
2332385 BTCS 513-18

print("Something went wrong!!")

print("If you wish to continue pree 0\nIf you wish to exit press 1")
Choice = int(input("Type 0 or 1:"))

Output :

3
2332385 BTCS 513-18

TASK 3: Write a program to create, concatenate and print a string and


accessing substring from a given string.

while True:
MainString = input("Enter a String : ")
print("1) Create a String\n2) Concatinate a String \n3)Acquiring a SubString \n4)Exit")
Choice = int(input("Enter Your Choice : "))
if(Choice==1):
print("You choose to create a string")
print(MainString)
elif(Choice==2):
print("You choose to Concatinate a String")
PlusString=input("String to Concatinate")
print(f"The concatinated String {MainString+PlusString}")
elif(Choice==3):
print("You choose to access a Substring ")
SubString=input("Enter the substring to check :")
if (SubString in MainString) :
print(f"SubString {SubString} is Present in String {MainString}")
else:
print("Its not prasent")
elif(Choice==4):
print("Exit the main Menu")
else:
print("Something went wrong!!")
print("If you wish to continue press 0\nIf you wish to exit press 1")
Choice = int(input("Type 0 or 1:"))

4
2332385 BTCS 513-18

Output :

5
2332385 BTCS 513-18

TASK 8: Write a python program to find largest of three numbers.


Code:

X = int(input("Enter a Number :"))


Y = int(input("Enter 2nd Number :"))
Z = int(input("Enter 3rd Number :"))

if(X>=Y and X>=Z):


print(f" {X} is largest of the three ")
elif(Y>=X and Y>=Z):
print(f"{Y} is largest of the three ")
elif(Z>=X and Z>=Y):
print(f"{Z} is largest of the three ")
else:
print("Something Went wrong!!!!")

Output :

6
2332385 BTCS 513-18

TASK 9: Write a Python program to convert temperature to and from celsius,


Fahrenheit [ c/5=f-32/9]
Code:
X = int(input("For Celsius to Fahrenheit = 0\nFor Fahrenheit to Calsius = 1\nYour Preferance:"))
if(X==0):
TC = int(input("Enter Temperature in Celsius:"))
TF = 9/5*TC+32
print(TF,"Is the Temperature in Fahrenheit")
elif(X==1):
FT = int(input("Enter Temperature in Fahrenheit:"))
CT = (FT-32)*5/9
print(CT,"Is the Temperature in Celsius")
else:
print("something went wrong!!!!")

Output :

7
2332385 BTCS 513-18

TASK 10: Write a program to construct the following using nested for loop
*
**
***
****
*****
****
***
**
*
Code :
for i in range(1,5):
print(" ")
for j in range(i):
print(" *",end="")
for i in range(5,0,-1):
print(" ")
for j in range(i):
print(" *",end="")

Output :

8
2332385 BTCS 513-18

9
2332385 BTCS 513-18

TASK 11: Write a Python script that prints prime numbers less than 20
Code:
for num in range(2,20):
itsPrime = True
for i in range(2, num):
if num % i == 0:
itsPrime = False
break
if itsPrime:
print(num)

Output:

10
2332385 BTCS 513-18

TASK 21: A) Print Pattern 1: using a for loop and range function
1
22
333
4444
55555
Code :
for i in range (1,6):
print(" ")
for j in range(i):
print(i,end=" ")

Output :

TASK 21: B) Print Pattern 1: using a for loop and range function
11
2332385 BTCS 513-18

1
21
321
4321
54321
Code:
for i in range(1,6):
for j in range(i, 0, -1):
print(j, end=" ")
print()

Output :

TASK 22: To check whether a give number is even or odd

12
2332385 BTCS 513-18

Code:
x = int(input("Enter a Numner : "))

if(x%2==0):
print("It is an even number")
else:
print("It is an odd number")

Output :

TASK 23: To find sum of all the digits of a given number

13
2332385 BTCS 513-18

Code:
num = int(input("Enter a number: "))
DigitSum = 0

while num>0:
LastDigit = num%10
DigitSum = DigitSum + LastDigit
num = num//10
print(DigitSum)

Output:

TASK 24: To check whether a number is palindrome or not.

14
2332385 BTCS 513-18

X = int(input("enter a number: "))

originalX= X
ReverseX = 0
while X >0 :
lastdigit = X%10
ReverseX = ReverseX*10 + lastdigit
X = X//10
if originalX == ReverseX:
print("It's a palindrome!")
else:
print("Not a palindrome.")

Output:

TASK 25: Check if a Substring is Present in a given String.

15
2332385 BTCS 513-18

Code:
A = input("Enter Main string:")
B = input("Enter sub string: ")

if(B in A):
print("Is present ")
else:
print("not present ")

Output:

16
2332385 BTCS 513-18

TASK 26: Program to find even length words in a string


Code:
string = input("Enter a string: ")
words = [Link]()
print("Even length words are:")
for word in words:
if len(word) % 2 == 0:
print(word)

Output :

Task 27: Program to swap all dots and commas in a string.

17
2332385 BTCS 513-18

Code:
s = input("Enter a string: ")

s = [Link](".", "#")

s = [Link](",", ".")

s = [Link]("#", ",")

print("Swapped string:", s)

Output :

Task 28: Given a string “F.R.I.E.N.D.S” , change the string to “friends”


18
2332385 BTCS 513-18

Code :
String = "F.R.I.E.N.D.S"

UpdateString = [Link]()

Frinds = [Link](".","")
print(Frinds)

Output:

Task 29: Extract digit string from a given string .

19
2332385 BTCS 513-18

Code :
DigiString = input("Enter a Digit-String")
DigitStrings= ""
for i in DigiString:
if [Link]() is True:
DigitStrings += i
print(DigitStrings)

Output:

TASK 30: Program for Insertion in a list before any element .

20
2332385 BTCS 513-18

Code:
X = [1,2,3,4,5,6]

[Link](0,76)

print(X)

Output :

21

You might also like