Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Assignment 1 and 2 Combine Python

The document contains a series of Python programs that perform various tasks such as checking if a number is even or odd, determining leap years, and calculating telephone bills. It also includes functions for converting temperatures, counting vowels in a string, and finding the maximum of three numbers. Additionally, it demonstrates list operations like removing duplicates, counting occurrences, and generating multiples of a number.

Uploaded by

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

Assignment 1 and 2 Combine Python

The document contains a series of Python programs that perform various tasks such as checking if a number is even or odd, determining leap years, and calculating telephone bills. It also includes functions for converting temperatures, counting vowels in a string, and finding the maximum of three numbers. Additionally, it demonstrates list operations like removing duplicates, counting occurrences, and generating multiples of a number.

Uploaded by

Pankaj Mahanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Write a program that prompts the user to input a number and display if the number is even or odd

num=float(input("enter the number= "))


if num%2==0:
print(num,"is a even number")
else:
print(num,"is a odd number")

enter the number= 9


9.0 is a odd number

2.Write a program that prompts the user to input a year and determine whether the year is a leap year or not.Leap Years are any year
that can be evenly divided by 4. A year that is evenly divisible by 100 is a leap year only if it is also evenly divisible by 400.

year = int(input("enter the year: "))


if(year%4==0 and year%100!=0)or(year%400==0):
print("It is a leap year")
else:
print("it is not a leap year")

enter the year: 2000


It is a leap year

3.Check whether the entered no is +/-/Zero

number=float(input("enter the number: "))


if number>0:
print(number,"is a psoitive number")
elif number<0:
print(number,"is a negative number")
else:
print("number is zero")

enter the number: -2


-2.0 is a negative number

4.Check whether the entered no is one digit/two digit/three digit

number=float(input("enter the number: "))


if number<10:
print("Number is 1 digit")
if number>=10 and number<100:
print("Number is 2 digit")
elif number>100 and number<1000:
print("Number is 3 digit")

enter the number: 7


Number is 1 digit

1. Write a program that prompts the user to input number of calls


and calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for up to 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls.

bill=200
calls=int(input("enter the number of calls "))
if calls>100 and calls<=150:
bill=bill+(calls-100)*0.60
elif calls>150 and calls<=200:
bill=bill+(50*0.60)+(calls-150)*0.50
elif calls>200:
bill=bill+(50*0.60)+(50*0.50)+(calls-200)*0.40
print("Your monthly telephone bill is ",bill)

enter the number of calls 100


Your monthly telephone bill is 200

1. Write a program that prompts the user to input a number. Program should display the corresponding days to the number. For
example if user type 1 the output should be sunday. If user type 7 the output should be Saturday.

day = int(input("Enter a number"))


if day==1:
print("sunday")
elif day==2:
print("Monday")
elif day==3:
print("Tuesday")
elif day==4:
print("Wednesday")
elif day==5:
print("Thrusday")
elif day==6:
print("Friday")
elif day==7:
print("Saturday")

Enter a number5
Thrusday

7 Write a program to find Profit or Loss

CP=float(input("Enter the cost price"))


SP=float(input("Enter the selling price"))
if SP>CP:
print("It's a Profit")
elif SP<CP:
print("It's a Loss")
else:
print("No Profit or Loss")

Enter the cost price32


Enter the selling price10
It's a Loss

8.Write a program to print Salary Slip of an employee.

BA=float(input("Enter the basic salary"))


HRA=0.2*BA
DA=0.1*BA
PF=0.05*BA
GrossSalary = BA+HRA+DA-PF
print("The Gross Salary is Rs",GrossSalary)

Enter the basic salary12000


The Gross Salary is Rs 15000.0

1. Convert celcius to Farenheit

celsius = float(input("Enter temperature in Celsius"))


fahrenheit = (celsius * 9/5) + 32
print("Temperature to fahrenheit",fahrenheit)

Enter temperature in Celsius13


Temperature to Celsius 55.4

12.Convert kilometres to mile.

kilometers = float(input("Enter distance in kilometers"))


miles = kilometers * 0.621371
print("The distance in Mile is",miles)

Enter distance in kilometers12


The distance in Mile is 7.4564520000000005

1. Swap entered nos

a=10
b=20
c=a
a=b
b=c
print(a,b)

20 10

Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js
Calculate the average sales from a list.
sales=[20000,50000,100000,55000,72000,120000,66000,22000,44000]
print(sum(sales)/len(sales))

61000.0

Check if a particular sale value exists in the list.


sales=[20000,50000,100000,55000,72000,120000,66000,22000,44000]
a=int(input("Enter the sales value you want to check from the list : "))
if a in sales:
print("Value is present")
else:
print("Value is not present.")

Enter the sales value you want to check from the list : 20000
Value is present

Create a new list where each sale is doubled.


sales=[20000,50000,100000,55000,72000,120000,66000,22000,44000]
doubled_sales=[]
for i in sales:
doubled_sales.append(i*2)
print("The new doubled sales values are :",doubled_sales)

The new doubled sales values are : [40000, 100000, 200000, 110000, 144000, 240000, 132000, 44000, 88000]

Multiply all elements in a list together.


sales=[200,500,1000,550,720,1200,660,220,440]
mul=1
i=1
for i in sales:
mul=i*i
print("The multiplied elements in the list are :",mul)

The multiplied elements in the list are : 193600

Find the highest sale from a list of sales.


sales=[200,500,1000,550,720,1200,660,220,440]
highest=max(sales)
print("The highest value in the list is : ",highest)

The highest value in the list is : 1200

Sum of All Even Numbers in a List


sales=[200,500,1000,550,720,1200,660,220,440]
i=0
for i in sales:
if(i%2==0):
i=i+i
print("The sum of all even numbers in the list is :",i)

The sum of all even numbers in the list is : 880

Generate a List of Multiples of a Number


a=int(input("Enter the number for which you want to make multiples of : "))
x=int(input("Enter the limit for which you want your multiples to be found : "))
l=[]
for i in range(1,x+1):
l.append(a*i)
print("The first",x,"multiples of",a,"are",l)

Enter the number for which you want to make multiples of : 12


Enter the limit for which you want your multiples to be found : 15
The first 15 multiples of 12 are [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180]

Remove Duplicates from a List


Remove Duplicates from a List
l=[10,20,30,40,50,60,60]
x=[]
for i in l:
if i not in x:
x.append(i)
print("The new list before removing all the duplicates are : ",l)
print("The new list after removing all the duplicates are : ",x)

The new list before removing all the duplicates are : [10, 20, 30, 40, 50, 60, 60]
The new list after removing all the duplicates are : [10, 20, 30, 40, 50, 60]

Counting Occurrences of a Value in a List


l=[10,20,30,40,50,60,60]
x=int(input("Enter the value you want to search from the list : "))
c=0
for i in l:
if(x==i):
c=c+1
print("The count of occurences of the value",x,"is :",c)

Enter the value you want to search from the list : 20


The count of occurences of the value 20 is : 1

Write a function celsius_to_fahrenheit() that takes a temperature


in Celsius and returns the equivalent temperature in Fahrenheit.
def celsius_to_fahrenheit(temp):
return ((9*temp/5)+32)
c=int(input("Enter the temparature in celcius that you want to convert into farenheit : "))
print("The converted temparature from ",c,"C is :",celsius_to_fahrenheit(c),"F")

Enter the temparature in celcius that you want to convert into farenheit : 10
The converted temparature from 10 C is : 50.0 F

Write a function count_vowels() that takes a string as input and


returns the number of vowels (a, e, i, o, u) in the string.
def count_vowels(string):
vowels="aeiouAEIOU"
count=0
for char in string:
if char in vowels:
count=count+1
return count
x=input("Enter a string : ")
print("The number of vowels in the string are :",count_vowels(x))

Enter a string : Riya


The number of vowels in the string are : 2

Write a function find_max that takes three numbers as input and


returns the largest of the three.
a= lambda x,y,z : max(x,y,z)
u=int(input("Enter first number :"))
i=int(input("Enter second number :"))
x=int(input("Enter third number :"))
print(a(u,i,x))

Enter first number :10


Enter second number :20
Enter third number :30
30

Create a lambda function that takes a number as input and


returns its square.
n=lambda x: x*x
a=int(input("Enter a number :"))
print("The squared version of that number is : ",n(a))
Enter a number :20
The squared version of that number is : 400

Write a lambda function that checks if a given number is divisible


by 3.
div=lambda x: x%3==0
no=int(input("Enter the number to check whether it is divisible by 3 or not :"))
if (div(no)==True):
print("The number is divisible by 3")
else:
print("The number is not divisible by 3")

Enter the number to check whether it is divisible by 3 or not :1600


The number is not divisible by 3
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like