Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Write a Python program to print the following string in a specific format (see the output).

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!"

Output Sample: -

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!

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

Write a Python program to find out what version of Python you are using.

A string containing the version number of the Python interpreter plus additional information on the build number and
compiler used. This string is displayed when the interactive interpreter is started.

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)

Explanation:

The said code imports the "sys" module and then uses it to print the version of Python currently being used, as well as
additional version information.

 The first line imports the "sys" module provides access to some variables used or maintained by the interpreter
and to functions that interact strongly with the interpreter. It is always available.
 The second line prints the string "Python version".
 The third line prints the version of Python currently being used to the console.
 The fourth line prints the string "Version info.".
 The fifth line prints version information of the current Python interpreter. It returns a named tuple (version_info)
containing the five components of the version number: major, minor, micro, releaselevel, and serial.

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

Python datetime:

The datetime module supplies classes for manipulating dates and times in both simple and complex ways.
datetime.now(tz=None) returns the current local date and time. If optional argument tz is None or not specified, this is
like today().

# 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

Explanation:

The said code imports the "datetime" module, gets the current date and time, and finally prints it in a formatted string.

 The first line import datetime imports the datetime module which supplies classes for manipulating dates and
times.
 The second line now = datetime.datetime.now() creates a datetime object for the current date and time.
 The third line print ("Current date and time : ") prints the string "Current date and time : " to the console.
 The fourth line print (now.strftime("%Y-%m-%d %H:%M:%S")) uses the strftime() method of the datetime object
to format the date and time as a string in the format "YYYY-MM-DD HH:MM:SS" and prints it to the console.

The strftime() method returns a string representing the date, controlled by an explicit format string. The format codes
used in this example are:

 %Y: year with century as a decimal number.


 %m: month as a zero-padded decimal number.
 %d: day of the month as a zero-padded decimal number.
 %H: hour (24-hour clock) as a zero-padded decimal number.
 %M: minute as a zero-padded decimal number.
 %S: second as a zero-padded decimal number.

This code can be useful for logging or timestamping events in a program, or for displaying the current date and time in a
user interface.
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

Sample data : 3, 5, 7, 23
Output :
List : ['3', ' 5', ' 7', ' 23']
Tuple : ('3', ' 5', ' 7', ' 23')

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

Write a Python program to display the first and last colors from the following list.

color_list = ["Red","Green","White" ,"Black"]

# Create a list called 'color_list' containing color names


color_list = ["Red", "Green", "White", "Black"]
# Print the first and last elements of the 'color_list' using string formatting
# values of 'color_list[0]' (Red) and 'color_list[-1]' (Black)
print (color_list[0], color_list[-1])

Write a Python program that prints the calendar for a given month and year.
Note: Use 'calendar' module.

# Import the 'calendar' module


import calendar

# Prompt the user to input the year and month


y = int(input("Input the year : "))
m = int(input("Input the month : "))
d = int(input("Input the date: "))

# Print the calendar for the specified year and month


print(calendar.month(y, m, d))
Write a Python program to print the following 'here document'.
Sample string:
a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example

# Use triple double-quotes to create a multi-line string


print("""
a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example
""")

Write a Python program to calculate the number of days between two dates.

# Import the 'date' class from the 'datetime' module


from datetime import date

# Define a start date as July 2, 2014


f_date = date(2014, 7, 2)

# Define an end date as July 11, 2014


l_date = date(2014, 7, 11)

# Calculate the difference between the end date and start date
total_days = l_date - f_date

# Print the number of days in the time difference


print(total_days.days)

You might also like