CS 1 Ans - Introduction To Python
CS 1 Ans - Introduction To Python
CS 1 Ans - Introduction To Python
import sys
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)
help('modules')
Options 1
import datetime
x = datetime.datetime.now()
print(x)
Options 2
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%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
Sample Output :
r = 1.1
Area = 3.8013271108436504
4.Write a Python program which accepts the user's first and last name and
print them in reverse order with a space between them
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.
Syntax
string.split(separator, maxsplit)
values = input("Input some comma separated numbers : ")
list1 = values.split(",")
tuple1 = tuple(list1)
print('List : ',list1)
Other examples -1
x = txt.split(", ")
print(x)
Other examples -2
txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)
Other examples -3
txt = "apple#banana#cherry#orange"
print(x)
6.Write a Python program to accept a filename from the user and print the
extension of that.
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).
Example
import datetime
today = datetime.datetime.now()
print (today)
print (str(today))
7.Write a Python program to display the first and last colors from the
following list.
color_list = ["Red","Green","White" ,"Black"]
8.Write 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)
10. Write a Python program to convert seconds to day, hour, minutes and
seconds.