Python Program List 3
121. Write a Python program to convert the distance (in feet) to
inches, yards, and miles.
d_ft = int(input("Input distance in feet: "))
d_inches = d_ft * 12
d_yards = d_ft / 3.0
d_miles = d_ft / 5280.0
print("The distance in inches is %i inches." % d_inches)
print("The distance in yards is %.2f yards." % d_yards)
print("The distance in miles is %.2f miles." % d_miles)
Output:
Input distance in feet: 100
The distance in inches is 1200 inches.
The distance in yards is 33.33 yards.
The distance in miles is 0.02 miles.
222. Write a Python program to convert seconds to day, hour,
minutes and seconds.
time = float(input("Input time in
seconds: "))
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
print("d:h:m:s-> %d:%d:%d:%d"
% (day, hour, minutes, seconds))
Output:
Input time in
seconds: 1234565
d:h:m:s->
14:6:56:5
323. Write a Python program to calculate the sum of the digits in
an integer.
num = int(input("Input a four digit
numbers: "))
x =
num //1000
x1 = (num - x*1000)//100
x2 = (num - x*1000 - x1*100)//10
x3 = num - x*1000 - x1*100 - x2*10
print("The sum of digits in the number
is", x+x1+x2+x3)
Output:
Input a four digit numbers: 5245
The sum of digits in the number is 16
424. Write a Python program to get the command-line arguments
(name of the script, the number of arguments, arguments) passed to a script.
import sys
print("This is the name/path of the
script:"),sys.argv[0]
print("Number of
arguments:",len(sys.argv))
print("Argument
List:",str(sys.argv))
Output:
python test.py arg1 arg2 arg3
This is the name/path of the script:
test.py
('Number of arguments:', 4)
('Argument List:', "['test.py',
'arg1', 'arg2', 'arg3']")
525. Write a Python program to swap two variables.
a = 30
b = 20
print("\nBefore swap a = %d and b =
%d" %(a, b))
a, b = b, a
print("\nAfter swapping a = %d and b =
%d" %(a, b))
print()
Output:
Before swap a = 30 and b = 20
After swapping a = 20 and b = 30
626. Write a Python program to check if a number is positive,
negative or zero.
num = float(input("Input a number:
"))
if num > 0:
print("It is positive number")
elif num == 0:
print("It is Zero")
else:
print("It is a negative number")
727. Write a Python program to compute the product of a list of integers
(without using for loop).
from functools import reduce
nums = [10, 20, 30,]
nums_product = reduce( (lambda x, y: x *
y), nums)
print("Product of the numbers :
", nums_product)
Product of the numbers : 6000
828. Write a python program to convert decimal to hexadecimal.
x = 30
print(format(x, '02x'))
x = 4
print(format(x, '02x'))
1e
04
929. Write a Python program to check if lowercase letters exist in
a string.
str1 = 'A8238i823acdeOUEI'
print(any(c.islower() for c in str1))
True
130. Write a Python function to check whether a number is
divisible by another number. Accept two integer’s values form the user.
def multiple(m, n):
return
True if m % n == 0 else False
print(multiple(20, 5))
print(multiple(7, 2))
True
False
Comments
Post a Comment