Baisc Programmes - 1
Baisc Programmes - 1
com/python-exercises/python-basic-
exercises.php#EDITOR
2. Write a Python program to get the Python version you are using.
import sys as s
print(s.version())
print(s.version_info())
import datatime as dt
t = dt.datetime.now()
a = t.strftime("%Y-%M-%D")
print(a)
u = input('yournumber :')
list1 = u.split(',')
tuple = tuple(list1)
print(list1)
print(tuple)
Write a Python program to accept a filename from the user and print the
extension of that. Go to the editor
Sample filename : abc.java
Output : java
i = input()
p = i.split('
print(p[-1])
8. Write a Python program to display the first and last colors from the
following list. Go to the editor
color_list = ["Red","Green","White" ,"Black"]
i = ["Red","Green","White" ,"Black"]
print(i[0],i[-1])
Write a Python program that accepts an integer (n) and computes the value
of n+nn+nnn. Go to the editor
Sample value of n is 5
Expected Result : 615
i = input()
z = int(i)
p = z + int(i+i) + int(i+i+i)
print(p)
12. Write a Python program to print the calendar of a given month and
year.
Note : Use 'calendar' module.
import calendar
i = input()
j = input()
i = int(i)
j = int(j)
z = calendar.month(i,j)
print(z)
14. Write a Python program to calculate number of days between two dates.
Sample dates : (2014, 7, 2), (2014, 7, 11)
Expected output : 9 days
a = (2014, 7, 2)
b = (2014, 7, 11)
print("no. of days between {0} {1} is {2} :".format(a,b,b[-1]-a[-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.
give = int(input())
num = 17
diff = num - give
if num < give:
print(abs(diff)*2)
else:
print(diff)
def difference(n):
if n <= 17:
return 17 - n
else:
return (n - 17) * 2
print(difference(22))
print(difference(14))
18. Write a Python program to calculate the sum of three given numbers,
if the values are equal then return three times of their sum.
def sum_of(givennumber):
asd = str(givennumber)
b = int(asd[0])+ int(asd[1]) + int(asd[2])
if asd[0] == asd[1] == asd[2]:
return b*3
else:
return b
print(sum_of(333))
sum = x + y + z
if x == y == z:
sum = sum * 3
return sum
print(sum_thrice(1, 2, 3))
print(sum_thrice(3, 3, 3))
19. 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
i = "Isasdl asdlfjIsasdlf"
j = "Is"
if i.startswith(j):
print(i)
else:
print('%s %s'%(j,i))
def new_string(str):
if len(str) >= 2 and str[:2] == "Is":
return str
return "Is" + str
print(new_string("Array"))
print(new_string("IsEmpty"))
print(larger_string('abc', 2))
print(larger_string('.py', 3))
Write a Python program to find whether a given number (accept from the
user) is even or odd, print out an appropriate message to the user.
def mainstr(list1):
z = 0
i = int(input('give a no. that you need count'))
for j in list1:
if i != j:
pass
else:
z = z + 1
return z
print(mainstr((1,2,3,4,5,4)))
def list_count_4(nums):
count = 0
for num in nums:
if num == 4:
count = count + 1
return count
print(list_count_4([1, 4, 6, 7, 4]))
print(list_count_4([1, 4, 6, 4, 7, 4]))
result = ""
for i in range(n):
result = result + substr
return result
print(substring_copy('abcdef', 2))
print(substring_copy('p', 3));
def vovel(char):
vovels = 'aeiou'
vovelss = vovels.upper()
if char in vovels or char in vovelss:
return True
else:
return False
print(vovel('E'))
def is_vowel(char):
all_vowels = 'aeiou'
return char in all_vowels
print(is_vowel('c'))
print(is_vowel('e'))
def vovel(list1,x):
for i in list1:
if x == i:
return True
else:
return False
print(vovel([1, 5, 8, 3],-1))
def values(list1):
sum = 0
for i in list1:
if i > sum:
sum = sum + i
makkalu = sum * '*'
print(makkalu)
sum = 0
print(values([10,2,35,1,]))
def histogram( items ):
for n in items:
output = ''
times = n
while( times > 0 ):
output += '*'
times = times - 1
print(output)
histogram([2, 3, 6, 5])
def values(list1):
stp = ""
for i in list1:
i = str(i)
stp = stp + i
return stp
print(values([10,2,35,1,]))
def concatenate_list_data(list):
result= ''
for element in list:
result += str(element)
return result
28. 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. Go to the editor
Sample numbers list :
numbers = [
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615,
953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949,
687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831,
445, 742, 717,
958,743, 527
]
def values(list1):
stp = []
for i in list1:
if i%2 == 0:
stp.append(i)
elif i == 237:
break
else:
pass
return stp
print(values([386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978,
328, 615, 953, 345,399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866,
950, 626, 949, 687, 217,815, 67, 104, 58, 512, 24, 892, 894, 767, 553,
81, 379, 843, 831, 445, 742, 717,958,743, 527]))
numbers = [
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615,
953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949,
687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831,
445, 742, 717,
958,743, 527
]
for x in numbers:
if x == 237:
print(x)
break;
elif x % 2 == 0:
print(x)
29. 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. Go to the editor
Test Data :
color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])
Expected Output :
{'Black', 'White'}
#print(color_list_1)
seet = {}
seet = set(seet)
for i in color_list_1:
for j in color_list_2:
if i == j:
break
else:
for z in color_list_2:
if z == j:
break
else:
if i not in seet:
seet.add(i)
else:
if i in seet:
break
else:
seet.add(i)
continue
print(seet)
0. Write a Python program that will accept the base and height of a
triangle and compute the area. Go to the editor
a = int(input())
b = int(input())
print(a*b/2)
31. Write a Python program to compute the greatest common divisor (GCD)
of two positive integers.
import math as m
a = abs(int(input()))
b = abs(int(input()))
print(m.gcd(a,b))
import math as m
a = abs(int(input()))
b = abs(int(input()))
print(m.lcm(a,b))
34. Write a Python program to sum of two given integers. However, if the
sum is between 15 to 20 it will return 20.
def sum(x,y):
a = x + y
if a in range(15,21):
return 20
else:
return a
print(sum(2,11))
def sum(x, y):
sum = x + y
if sum in range(15, 20):
return 20
else:
return sum
print(sum(10, 6))
print(sum(10, 2))
print(sum(10, 12))
35. Write a Python program that will return true if the two given integer
values are equal or their sum or difference is 5.
Sample Solution:
Python Code:
amt = 10000
int = 3.5
years = 7
future_value = amt*((1+(0.01*int)) ** years)
print(round(future_value,2))
40. Write a Python program to compute the distance between the points
(x1, y1) and (x2, y2)
print(points((4,0),(6,6)))
import os.path
print(os.path.isfile('main.txt'))
print(os.path.isfile('main.py'))
import platform
import os
print("Name of the operating system:",os.name)
print("\nName of the OS system:",platform.system())
print("\nVersion of the operating system:",platform.release())
import os
import sys
import platform
import sysconfig
print("os.name ", os.name)
print("sys.platform ", sys.platform)
print("platform.system() ", platform.system())
print("sysconfig.get_platform() ", sysconfig.get_platform())
print("platform.machine() ", platform.machine())
print("platform.architecture() ", platform.architecture())
import site;
print(site.getsitepackages())
Write a python program to get the path and name of the file that is
currently executing.
import os
print("Current File Name : ",os.path.realpath(__file__))
Sample Solution:-
Python Code:
import multiprocessing
print(multiprocessing.cpu_count())