0% found this document useful (0 votes)
7 views

Python Prog

Python programming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Prog

Python programming
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 1: Suggest Footwear

def sunny():
print("It is sunny day, good to wear sneaker!")

def rainy():
print("Oops! its raining, better wear gumbot")

def snowy():
print("Wow! its snowing, better wear boot")

weather_today = input("Whats the weather today:")


if weather_today == "sunny":
sunny()
elif weather_today == "rainy":
rainy()
elif weather_today == "snowy":
snowy()
else:
print(f"No footwear avaibale for {weather_today}")

Question 2: Simple Calculator using function


num1 = float(input("Enter number 1:"))
2
num2 = float(input("Enter number 2:"))
3
4
def to_add():
5
tot = num1 + num2
6
print(f"{num1} + {num2} = {tot}")
7
8def to_sub():
9 sub = num1 - num2
10 print(f"{num1} - {num2} = {sub}")
11
12def to_mul():
13 mul = num1 * num2
14 print(f"{num1} x {num2} = {mul}")
15
16def to_div():
17 div = num1 / num2
18 print(f"{num1} / {num2} = {div}")
19
20operation = input("Choose an operation(+,-,*, /):")
21if operation == "+":
22 to_add()
23elif operation == "-":
24 to_sub()
25elif operation == "*":
26 to_mul()
27elif operation == "/":
28 to_div()
29else:
30 print(f"No operation for {operation} for now! sorry!”)

Coding challenge: Leap year


def is_leap(year):
if year % 4 == 0 and year % 100 != 0:
print(f"{year} is a leap year.")
elif year % 400 == 0:
print(f"{year} is a leap year.")
elif year % 100 == 0:
print(f"{year} is not a leap year.")
else:
print(f"{year} is not a leap year.")
year = int(input("Enter a year:"))
is_leap(year)

Write a function that inputs a number and prints the


multiplication table of that number
def mul(num):
"""
Prints the multipliaction table of a given number
"""
for i in range(1, 11):
print("{multiplier} * {multiplicand} = {multiplicantion}".format(
multiplier=num, multiplicand=i, multiplicantion=num * i))

mul(9)

Write a program to implement these formulae of permutations


and combinations.
import operator as op

def factorial(num):
"""
Returns the factorial of a number
"""

if num == 1:
return num
return num * factorial(num-1)

def permutation(n, r):


"""
Returns the permutation of a number
"""
return int(factorial(n) / factorial(n-r))

def combination(n, r):


"""
Returns the combinations of a number
"""
return int(factorial(n) / (factorial(r) * factorial(n-r)))

print("Permutation: ", permutation(15,4))


print("Combination: ", combination(15,4))
Write a function that converts a decimal number to binary
number
def decToBin(num):
"""
Prints the binary number of a given decimal number using recursion
"""
if num > 1:
decToBin(num//2)
print(num % 2, end="")

decToBin(11)
Write a program which can filter odd numbers in a list by using
filter function
def filterOdd(lst):
"""
Filter odd numbers from given list
"""
return list(filter(lambda num: (num%2 != 0), lst))

filterOdd([0,2,5,8,19,20,34,95])

Write a program which can map() to make a list whose elements


are cube of elements in a given list
def cube(lst):
"""
Returns the list of cubes of given number
"""
return list(map(lambda x: x**3, lst))

cube([1, 3, 5, 9, 15])

You might also like