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

Python Programming Assignment Solutions

Uploaded by

aanyapal699
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)
3 views9 pages

Python Programming Assignment Solutions

Uploaded by

aanyapal699
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

ASSIGNMENT-1

1. What is slicing operator how can you extract a substring


from a given string?
Ans:- Slicing operator ([:]) extracts a part of a sequence (string,
list, tuple).
Example:
text = "PythonProgramming"
print(text[0:6]) # Python
print(text[7:]) # Programming
print(text[-6:]) # mming
2. Write a program to swap two numbers using a temporary
variables.

Ans:- x = int(input(“Enter first number :”)


y = int(input(“Enter second number:”)
temp = x
x=y
y = temp
print(“After swapping: x =”, x, “,y =”,y)
3. Write a program that prompts user to enter two
integers x and y and then display x to the power y.

Ans:- x = int(input("Enter first integer: "))

y = int(input("Enter second integer:”))

print("You entered: x =", x, "and y =", y)


4. Write a program to calculate the salary of an employee
given his basic pay . define HRA and TA as consants and
calculate the salary of an employee.

Ans:- BASIC_PAY = float(input("Enter Basic Pay: "))

HRA = 0.20 * BASIC_PAY # 20% of basic

TA = 0.10 * BASIC_PAY # 10% of basic

salary = BASIC_PAY + HRA + TA

print("Total Salary = ", salary)

5. Write a program to print the ascii value of a character.


Ans:- ch = input("Enter a character: ")

print("ASCII value of", ch, "is", ord(ch))


6. Write a program to calculate the no of seconds in a
day.

Ans:- seconds = 24 * 60 * 60
print("Seconds in a day:", seconds)
7. Momentum is calculated E=mc^2 where m is the mass
c is the velocity write a program that accepts objects
mass and velocity and display momentum.

Ans:- m = float(input("Enter mass of object (kg): "))

c = float(input("Enter velocity of object (m/s): "))


E = m * (c ** 2)

print("Momentum (E) =", E)


D

You might also like