Basic Program
Basic Program
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: -
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
# Use the sys.version attribute to get the Python version and print it
print(sys.version)
# 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.
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().
# 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:
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)
Write a Python program to display the first and last colors from the following list.
Write a Python program that prints the calendar for a given month and year.
Note: Use 'calendar' module.
Write a Python program to calculate the number of days between two dates.
# Calculate the difference between the end date and start date
total_days = l_date - f_date