1. Write a Python program to get the Python version you are using.
import sys
print("Python version")
print ([Link])
print("Version info.")
print (sys.version_info)
help('modules')
List all modules in Python
2. Write a Python program to display the current date and time.
Sample Output :
Current date and time :
2014-07-05 [Link]
Options 1
import datetime
x = [Link]()
print(x)
Options 2
import datetime
now = [Link]()
print ("Current date and time : ")
print ([Link]("%Y-%m-%d %H:%M:%S"))
strftime() is a Python date method you can use to convert dates to strings. It
doesn't just convert to strings but also allows you to format your dates in a readable
way
Available Date Formats
3. Write a Python program which accepts the radius of a circle from the user
and compute the area.
Sample Output :
r = 1.1
Area = 3.8013271108436504
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))
[Link] a Python program which accepts the user's first and last name and
print them in reverse order with a space between them
fname = input("Input your First Name : ")
lname = input("Input your Last Name : ")
print ("Hello " + lname + " " + fname)
[Link] 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')
The split() method splits a string into a list.
You can specify the separator; default separator is any whitespace.
Syntax
[Link](separator, maxsplit)
values = input("Input some comma separated numbers : ")
list1 = [Link](",")
tuple1 = tuple(list1)
print('List : ',list1)
print ('Tuple : ',tuple1)
Other examples -1
txt = "hello, my name is Peter, I am from UK"
x = [Link](", ")
print(x)
Other examples -2
txt = "apple#banana#cherry#orange"
x = [Link]("#")
print(x)
Other examples -3
txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter to 1, will return a list with 2
elements!
x = [Link]("#", 1)
print(x)
[Link] a Python program to accept a filename from the user and print the
extension of that.
Sample filename : [Link]
Output : java
The repr() function returns a printable representational string of the given
object.
filename = input("Input the Filename: ")
f_extns = [Link](".")
print ("The extension of the file is : " + repr(f_extns[-1]))
str() displays today’s date in a way that the user can understand the date and
time.
repr() prints “official” representation of a date-time object (means using the
“official” string representation we can reconstruct the object).
The __repr__ method returns the string representation of an object.
Typically, the __repr__() returns a string that can be executed and yield the
same value as the object. In other words, if you pass the returned string of the
object_name.
built-in str() and repr() functions both produce a textual representation of an
object. The difference between str() and repr() is: The str() function returns a
user-friendly description of an object. The repr() method returns a developer-friendly
string representation of an object.
Example
import datetime
today = [Link]()
print (today)
print (str(today))
#prints the official format of date-time object
print (repr(today))
[Link] 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]))
[Link] a Python program that accepts an integer (n) and computes the value
of n+nn+nnn.
Sample value of n is 5
Expected Result : 615
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)
[Link] a Python program to get the volume of a sphere with radius 6.
pi = 3.1415926535897931
r= 6.0
V= 4.0/3.0*pi* r**3
print('The volume of the sphere is: ',V)
10. Write a Python program to convert seconds to day, hour, minutes and
seconds.
time = float(input("Input time in seconds: "))
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
print("d:h:m:s-> %d days :%d hours:%d minutes:%d seconds" % (day,
hour, minutes, seconds))