1. Write a Python program to find out what version of Python you are using.
Script Soln
import sys # Import the sys module to access system-
specific parameters and functions
# Print the Python version to the console
print("Python version")
# Use the sys.version attribute to get the Python version
and print it
print(sys.version)
# Print information about the Python version
print("Version info.")
# Use the sys.version_info attribute to get detailed
version information and print it
print(sys.version_info)
Command Line Soln
user@machine1:~$ python --version
Python 2.7.17
user@machine1:~$ python -V
Python 2.7.17
user@machine1:~$ python3 --version
Python 3.6.9
user@machine1:~$ python3 -V
Python 3.6.9
2. Write a Python program to display the current date and time.
Sample Output :
Current date and time :
2014-07-05 14:34:14
Soln
# Import the 'datetime' module to work with date and time
import datetime
# Get the current date and time
now = datetime.datetime.now()
# Create a datetime object representing the current date
and time
# Display a message indicating what is being printed
print("Current date and time : ")
# Print the current date and time in a specific format
print(now.strftime("%Y-%m-%d %H:%M:%S"))
# Use the 'strftime' method to format the datetime object
as a string with the desired format
3. Write a Python program that calculates the area of a circle based on the radius
entered by the user.
Sample Output :
r = 1.1
Area = 3.8013271108436504
Soln
# Import the 'pi' constant from the 'math' module to
calculate the area of a circle
from math import pi
# Prompt the user to input the radius of the circle
r = float(input("Input the radius of the circle : "))
# Calculate the area of the circle using the formula: area
= π * r^2
area = pi * r ** 2
# Display the result, including the radius and calculated
area
print("The area of the circle with radius " + str(r) + "
is: " + str(area))
4. Write a Python program that accepts the user's first and last name and prints
them in reverse order with a space between them.
Soln
# Prompt the user to input their first name and store it in
the 'fname' variable
fname = input("Input your First Name : ")
# Prompt the user to input their last name and store it in
the 'lname' variable
lname = input("Input your Last Name : ")
# Display a greeting message with the last name followed by
the first name
print("Hello " + lname + " " + fname)
5. Write a Python program that accepts a sequence of comma-separated
numbers from the user and generates a list and a tuple of those numbers.
Sample data : 3, 5, 7, 23
Output :
List : ['3', ' 5', ' 7', ' 23']
Tuple : ('3', ' 5', ' 7', ' 23')
Soln
# Prompt the user to input a sequence of comma-separated
numbers and store it in the 'values' variable
values = input("Input some comma-separated numbers: ")
# Split the 'values' string into a list using commas as
separators and store it in the 'list' variable
list = values.split(",")
# Convert the 'list' into a tuple and store it in the
'tuple' variable
tuple = tuple(list)
# Print the list
print('List : ', list)
# Print the tuple
print('Tuple : ', tuple)