Python Program List 1


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

import sys
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)

                Output:
Python version                                                                                               
3.5.2 (default, Sep 10 2016, 08:21:44)                                                                       
[GCC 5.4.0 20160609]                                                                                          
Version info.                                                                                                
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

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

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

Output:

Current date and time :
2014-07-05 14:34:14

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

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))

Output:

Input the radius of the circle : 1.1                                                                         
The area of the circle with radius 1.1 is: 3.8013271108436504

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

Python has two functions designed for accepting data directly from the user:
·         Python 2.x. -> input()
·         Python 3.x. -> raw_input()

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

Output:

Input your First Name : Piyush                                      
Input your Last Name : Patel                                       
Hello  Patel Piyush

55.      Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.

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

Output:

Input some comma separated numbers:  3, 5, 7, 23                      
List :  ['3', '5', '7', '23']                                     
Tuple :  ('3', '5', '7', '23')

66.    Write a Python program to display the first and last colors from the following list.
color_list = ["Red","Green","White" ,"Black"]

color_list = ["Red","Green","White" ,"Black"]
print( "%s %s" %(color_list[0], color_list[-1]))

Output:
Red Black

77.      Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.

     a = int(input("Input an integer : "))
n1 = int( "%s" % a )
n2 = int( "%s%s" % (a,a) )
n3 = int( "%s%s%s" % (a,a,a) )
print (n1+n2+n3)
Output:
                Input an integer : 5
                615

88.      Write a Python program to print the calendar of a given month and year. (Note: Use 'calendar' module.)

import calendar
y = int(input("Input the year : "))
m = int(input("Input the month : "))
print(calendar.month(y, m))

Output:
Input the year : 2018
Input the month : 12
December 2018
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
99.      Write a Python program to get the the volume of a sphere. (V = 4/3 × π × r3)

pi = 3.1415926535897931
r = float(input("Input the year : "))
V= 4.0/3.0*pi* r**3
print('The volume of the sphere is: ',V)

Output:
Input the year : 6
The volume of the sphere is:  904.7786842338603

110.  Write a Python program to calculate the sum of three given numbers, if the values are equal then return thrice of their sum.

def sum_thrice(x, y, z):

     sum = x + y + z
 
     if x == y == z:
      sum = sum * 3
     return sum

print(sum_thrice(1, 2, 3))
print(sum_thrice(3, 3, 3))

Output:
6
27

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes