Python Algorithms Flashcards
Write a Python program to print the following string in a specific format (see the output). Go to the editor
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 :
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 get the Python version you are using.
import sys print("Python version") print (sys.version) print("Version info.") print (sys.version_info)
Python version
3.5.2 (default, Sep 10 2016, 08:21:44)
[GCC 5.4.0 20160609]
Version info.
sys.version_info(major=3, minor=5, micro=2, releaselevel=’final’, serial=0)
Write a Python program to display the current date and time.
Sample Output :
Current date and time :
2014-07-05 14:34:14
import datetime
now = datetime.datetime.now()
print (“Current date and time : “)
print (now.strftime(“%Y-%m-%d %H:%M:%S”))
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))
Write 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)
Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.
values = input("Input some comma seprated numbers : ") list = values.split(",") tuple = tuple(list) print('List : ',list) print('Tuple : ',tuple)
Sample Output:
Input some comma seprated numbers : 3,5,7,23
List : [‘3’, ‘5’, ‘7’, ‘23’]
Tuple : (‘3’, ‘5’, ‘7’, ‘23’)
Write a Python program to accept a filename from the user and print the extension of that.
filename = input(“Input the Filename: “)
f_extns = filename.split(“.”)
print (“The extension of the file is : “ + repr(f_extns[-1]))
Sample Output:
Input the Filename: abc.java
The extension of the file is : ‘java’
Write 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]))
Red Black
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)
Write a Python program to print the documents (syntax, description etc.) of Python built-in function(s).
Sample function : abs()
Expected Result :
abs(number) -> number
Return the absolute value of the argument.
print(abs.__doc__)
Sample Output:
Return the absolute value of the argument.
Write a Python program to print the calendar of a given month and year.
Note : Use ‘calendar’ module.
import calendar
y = int(input(“Input the year : “))
m = int(input(“Input the month : “))
print(calendar.month(y, m))
Sample Output:
Input the year : 2017
Input the month : 04
April 2017
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Write a Python program to calculate number of days between two dates.
Sample dates : (2014, 7, 2), (2014, 7, 11)
Expected output : 9 days
from datetime import date f_date = date(2014, 7, 2) l_date = date(2014, 7, 11) delta = l_date - f_date print(delta.days)
Sample Output:
9
Write a Python program to get the volume of a sphere with radius 6.
volume = 4/3pi * r^3 pi = 3.1415926535897931 r= 6.0 V= 4.0/3.0*pi* r**3 print('The volume of the sphere is: ',V)
Sample Output:
The volume of the sphere is: 904.7786842338603
Write a Python program to get the difference between a given number and 17, if the number is greater than 17 return double the absolute difference.
def difference(n): if n <= 17: return 17 - n else: return (n - 17) * 2
print(difference(22))
print(difference(14))
Sample Output:
10
3
Write a Python program to test whether a number is within 100 of 1000 or 2000
def near_thousand(n): return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100)) print(near_thousand(1000)) print(near_thousand(900)) print(near_thousand(800)) print(near_thousand(2200))
Sample Output: