python practice questions you fraud
Write a Python program to create a copy of its own source code
#quine print() print((lambda str='print(lambda str=%r: (str %% str))()': (str % str))()) print() ??????????
Write a Python program to sort three integers without using conditional statements and loops
a1 = min(x,y,z) a3 = max(x,y,z) a2 = (x+y+z) - a1 - a3) print
what does \n\t do? how to add even more?
adds tab in string \n\t\tHow I wonder what you are!
Write a Python program to display the first and last colors from the following list. color_list = ["Red","Green","White" ,"Black"] Pictorial Presentation:
color_list = ["Red","Green","White" ,"Black"] print( "%s %s"%(color_list[0],color_list[-1]))
Write a Python program to concatenate N strings e.x. this list list_of_colors = ['Red', 'White', 'Black'] #uses one line
colors = '-'.join(list_of_colors) print(colors)
Write a Python program to test whether all numbers of a list is greater than a certain number. #one line
def check(list, n): print(all(x > n for x in list))
Write a Python program to concatenate all elements in a list into a string and return it.
def concatenate_list_data(list): result= "" for element in list: result += str(element) return result
Write a Python program to count the number 4 in a given list.
def countfour(list): count=0 for i in list: if i == 4: count+=1
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
Write a Python program to compute the greatest common divisor (GCD) of two positive integers.
def gcd(x, y): gcd = 1 if x % y == 0: return y for k in range(int(y / 2), 0, -1): #range(start,stop,step) if x % k == 0 and y % k == 0: gcd = k break return gcd #this works regardless whether x>y or y<x
Write a Python program to create a histogram from a given list of integers.
def histogram( items ): for n in items: output = ''" times = n while( times > 0 ): output += '*' times = times - 1 print(output)
Write a Python program to get a new string from a given string where "Is" has been added to the front. If the given string already begins with "Is" then return the string unchanged.
def isstring(string): if (string[0:2] == "Is"): return string; else: return "Is "+string;
Write a Python program to get a string which is n (non-negative integer) copies of a given string.
def larger_string(str, n): result = "" for i in range(n): result = result + str return result
Write a Python program to get the least common multiple (LCM) of two positive integers.
def lcm(x, y): if x > y: z = x else: z = y while(True): if((z % x == 0) and (z % y == 0)): lcm = z break z += 1 return lcm
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))
Write a Python program to get the volume of a sphere with radius 6.
def spherevol(r){ return (4/3)pi*(r**3)
Write a Python program to calculate the sum of the digits in an integer.
def sum_digits(n): s = 0 while n: s += n % 10 n = n// 10 return s
Write a python program to find the sum of the first n positive integers
def sumfirst(list): stopindex = 0 for i in range(0,len(list)-1): if list[i] < 0: stopindex = i sum = 0 for i in range(0,stopindex): sum = sum + list[i] print sum
Write a Python program to swap two variables.
def swapper(a,b): i = a a = b b = i
Write a Python program to display the examination schedule. (extract the date from exam_st_date). exam_st_date = (11, 12, 2014) Sample Output: The examination will start from : 11 / 12 / 2014
exam_st_date = (11,12,2014) print( "The examination will start from : %i / %i / %i"%exam_st_date)
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 : " + f_extns[-1])
Write a Python program to print all even numbers from a given numbers list in the same order and stop the printing if any numbers that come after 237 in the sequence.
for x in numbers: if x == 237: print(x) break; elif x % 2 == 0: print(x)
Write a Python program to calculate number of days between two dates.Sample dates : (2014, 7, 2), (2014, 7, 11)
from datetime import date f_date = date(2014, 7, 2) l_date = date(2014, 7, 11) delta = l_date - f_date print(delta.days)
tell user to input radius and print area
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))
find current date and time
import datetime now = datetime.datetime.now() print ("Current date and time : ") print (now.strftime("%Y-%m-%d %H:%M:%S"))
how to find out what python version you are using?
import sys #access to variables in the interpreter print(sys.version)
Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn
n = int(input("Input an integer : ")) n1 = int( "%s" % n ) n2 = int( "%s%s" % (n,n) ) n3 = int( "%s%s%s" % (n,n,n) ) print (n1+n2+n3)
Write a Python program to get the identity of an object
obj1 = object() print(id(obj1)
Write a Python program to print the documents (syntax, description etc.) of Python built-in function(s).
print(abs.__doc__)
Write a Python program to print out a set containing all the colors from color_list_1 which are not present in color_list_2.
print(color_list_1.difference(color_list_2)) #these are sets
Write a Python program to convert a byte string to a list of integers.
str = "asdf" b = bytes(str, 'utf-8') print(list(b))
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)