Sharpen Your Skills: Programming Languages Practice Q&A

Programming practice questions

Python Programming Language

 
1

Question- Write a Python program to print the following string in a specific format. Sample String: "Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are!"



Answer-

print("Twinkle, twinkle, little star, \n\tHow I wonder what you are! \n\t\tUp above the world so high, \n\t\tLike a diamond in the sky. \nTwinkle, twinkle, little star, \n\tHow I wonder what you are!")
2

Question- Write a Python program to get the Python version you are using.



Answer-
  import sys;
 print("Python version");
 print (sys.version);
 print("Version info.");
 print (sys.version_info);
3

Question- Write a Python program which accepts the user's first and last name and print them in reverse order with a space between them.



Answer-
 fname = input("Input your First Name : ");
 lname = input("Input your Last Name : ");
 print ("Hello  " + lname + " " + fname);
4

Question- Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers. Sample data : 3, 5, 7, 23 Output : List : ['3', ' 5', ' 7', ' 23'] Tuple : ('3', ' 5', ' 7', ' 23')



Answer-
  values = input("Input some comma seprated numbers : ")
  list = values.split(",")
  tuple = tuple(list)
  print('List : ',list)
  print('Tuple : ',tuple)

5

Question- write a Python program to find the addition of these two numbers.



Answer-
  #Method1
num1 = 15
num2 = 12
  
# Adding two nos
sum = num1 + num2
  
# printing values
print("Sum of ", sum)


#Method2
n1=input("Enter 1st number");
n2=input("Enter 2nd number");
add=int(n1)+int(n2);
print("Addition is ",add);
6

Question- Write a Python program which accepts the radius of a circle from the user and compute the area.



Answer-
       from math import pi;
       r = float(input ("Input the radius of the circle : "))
       print ("The area of the circle with radius " + str(r) + " is: " + str(pi * r**2));

7

Question- Write a Python program to display the current date and time.



Answer-
   import datetime

  now = datetime.datetime.now()

  print ("Current date and time : ")

  print (now.strftime("%Y-%m-%d %H:%M:%S"))
8

Question- Write a Python program for swap two integer number which is enter by user and by using Third variable.



Answer- n1=int(input("Enter a number"))
n2=int(input("Enter 2nd number"))
t=n1
n1=n2
n2=t
print("swap values are::")
print("N1 is",n1);
print("N2 is",n2);
9

Question- Write a Python program for swap two integer number which is enter by user and without using Third variable.



Answer- n1=int(input("Enter 1st number"));
n2=int(input("Enter 2nd number"));
n1=n1+n2;
n2=n1-n2;
n1=n1-n2;
print("Swap value is: n1 is ",n1," And n2 is ",n2);
10

Question- Write a Python program to enter cost of a pen and calculate similar 100 pen cost with 20% discount.



Answer- p=int(input("Enter cost of a pen"))
t=100*p
d=t*20/100
pay=t-d
print("100 pen cost is =",t)
print("Discount amt is:: ",d)
print("Paying Amt is Rs.  ",pay)