0% found this document useful (0 votes)
314 views17 pages

Assignment No: 01 Salary Calculation

The document contains Python code for multiple programming assignments. The code includes functions to calculate employee salary, momentum of an object, find maximum and minimum numbers in a list, calculate student results, build a simple calculator, reverse a number, convert binary to decimal, generate random numbers, copy and modify file contents, and control home appliances using an Arduino and Bluetooth.

Uploaded by

vedic1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
314 views17 pages

Assignment No: 01 Salary Calculation

The document contains Python code for multiple programming assignments. The code includes functions to calculate employee salary, momentum of an object, find maximum and minimum numbers in a list, calculate student results, build a simple calculator, reverse a number, convert binary to decimal, generate random numbers, copy and modify file contents, and control home appliances using an Arduino and Bluetooth.

Uploaded by

vedic1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Assignment No : 01 Salary Calculation

'''
Assignment No: 1
To calculate salary of an employee given his basic pay (take as input from user).
Calculate gross salary of employee. Let HRA be 10 % of basic pay and TA be 5% of
basic pay.
Let employee pay professional tax as 2% of total salary.
Calculate net salary payable after deductions.
'''
basic_pay = input("Enter your basic pay: ")
try:
basic_pay = float(basic_pay)
if basic_pay<0: # basic pay cannot be less than zero 
print("Basic pay can't be negative.")
exit()
hra = basic_pay*0.1 # hra is 10 % of basic pay
ta = basic_pay*0.05 # ta is 5 % of basic pay
total_salary = basic_pay + hra + ta 
professional_tax = total_salary*0.02 # professional tax is 2 % of total salary
salary_payable = total_salary - professional_tax
print("Salary Payable is",salary_payable)
except ValueError:
print("Enter amount in digits.")
Assignment No: 02 Momentum calculation

Assignment No: 2
To accept an object mass in kilograms and velocity in meters per second and
display its momentum.
Momentum is calculated as e=mc2 where m is the mass of the object and c is its
velocity.

try:
mass = float(input("Enter mass in kgs: ")) # m is mass in kgs
velocity = float(input("Enter velocity in m/s: ")) # c is velocity in m/s
momentum = mass*(velocity**2) # e=mc2
print('Momentum of object is', momentum)
except ValueError:
print("Enter mass and velocity in int/float")
Assignment No: 03 Finding Max No & Min No in List Using Built-In
Function

Assignment No:3
To accept N numbers from user. Compute and display maximum in list, minimum in
list,
sum and average of numbers using built-in function.

try:
n = int(input("Enter no of elements: "))
nos_list = [] # nos_list list is created
for i in range(n): # for loop
num = int(input("Enter no: "))
nos_list.append(num) # numbers are added in nos_list list
maxNo = max(nos_list) # maximum number in list
minNo = min(nos_list) # minimum number in list
sumNos = sum(nos_list) # sum of numbers in list
average = sumNos/len(nos_list) # average of numbers in list
print('Max no is ',maxNo)
print('Min no is ',minNo)
print('Count: ',len(nos_list)) # length of list
print('Sum of nos is ',sumNos)
print('Average of nos is ',average)
except ValueError:
print('Enter n and all elements as integer.')
Assignment No: 4 Student Result Calculation

Assignment No: 4
To accept students five courses marks and compute his/her result.
Student is passing if he/she scores marks equal to and above 40 in each course.
If student scores aggregate greater than 75%, then the grade is distinction.
If aggregate is 60>= and <75 then the grade if first division.
If aggregate is 50>= and <60, then the grade is second division.
If aggregate is 40>= and <50, then the grade is third division.

class Student: # creation of class

def __init__(self): #  __init__() function to assign values to object properties


self.marks = []
self.passed = True

def input(self): # input function is defined using the def keyword. 


          #Self parameter is a reference to the current instance of the class, and is used
to access variables that belong to the class.
try:
for i in range(5):
mark = float(input("Marks: "))
self.marks.append(mark)
if mark < 40:
self.passed = False
except:
print("Enter marks in digits.")

def grade(self): #grade function is defined


if self.passed:
average = sum(self.marks)/5
if average>75:
print("Distinction")
elif average >= 60 and 75> average:
print("First Division")
elif average >= 50 and 60> average:
print("Second Division")
else: 
print("Third Division")
else:
print("Failed")

if __name__ == '__main__':
student = Student()
student.input() # function calling
student.grade()
Assignment No: 6 Simple Calculator

Assignment No: 6
To simulate simple calculator that performs basic tasks such
as,addition,subtraction,multiplication and division.

def add(x, y): # function definition of addition


   return x + y

def subtract(x, y): # function definition of subtraction


   return x - y

def multiply(x, y): # function definition multiplication


   return x * y

def divide(x, y): # function definition of divide


   return x / y

def main():
while(1):
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user 


choice = input("Enter choice(1/2/3/4):")
try:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
except:
print("Enter numbers as integers")
continue

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':


   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':


   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")

if __name__ == '__main__':
main()
Assignment No: 9 Printing Reverse No using Function

Assignment No: 9
To accept a number from user and print digits of number in a reverse order using
built-in function.

Number = int(input("Please Enter any Number: "))    


Reverse = 0    
while(Number > 0):    
    Reminder = Number %10    
    Reverse = (Reverse *10) + Reminder    
    Number = Number //10    
   
print("\n Reverse of entered number is = %d" %Reverse)  
Assignment No: 10 Binary to Decimal Conversion

Assignment No: 10
To input binary number from user and convert it into decimal number.

def binaryToDecimal(binary): # function definition


      
    binary1 = binary 
    decimal, i, n = 0, 0, 0
    while(binary != 0): 
        dec = binary % 10
        decimal = decimal + dec * pow(2, i) 
        binary = binary//10
        i += 1
    print(decimal)     
      
  
# Driver code 
if __name__ == '__main__': 
    binaryToDecimal(100) 
    binaryToDecimal(101) 
    binaryToDecimal(1001)
Assignment NO: 11 To generate pseudo random numbers

Assignment NO: 11
To generate pseudo random numbers.

import random
try:
start = int(input("Enter start point: "))
end = int(input("Enter end point: "))
num = random.randint(start,end)
print(num)
except:
print("Enter start and end point as integer")
Assignment No: 16 Character Conversion UPPER to lower and lower
to UPPER case

Assignment No: 16
To copy contents of one file to other. While copying a) all full stops are to be
replaced with commas b) lower case are to be replaced with upper case c) upper
case
are to be replaced with lower case.

try:
fin = open("input.txt",'r') # file opening in read mode
fout = open("output.txt",'w') # file is created 

for line in fin:


line = line.swapcase()  #Replace lower case with upper and upper with lower
line = line.replace(".","*") #Replace full stops with *
fout.write(line)
except:
print("File error occured")
Home Automation using
Arduino and Bluetooth module
Arduino Code
Given below is the Arduino code that you can compile and
program in your Arduino UNO.
//using ports 10, 11, 12, 13
int relay1 = 10;
int relay2 = 11;
int relay3 = 12;
int relay4 = 13;
int val;

void setup() {

Serial.begin(9600);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);

}
void loop() {

//check data serial from bluetooth android App


while (Serial.available() > 0){
val = Serial.read();
Serial.println(val);
}

//Relay is on
if( val == 1 ) {
digitalWrite(relay1,HIGH); }
else if( val == 2 ) {
digitalWrite(relay2,HIGH); } else if( val == 3 ) {
digitalWrite(relay3,HIGH); }
else if( val == 4 ) {
digitalWrite(relay4,HIGH); }

//relay all on
else if( val == 0 ) {
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
}
//relay is off
else if( val == 5 ) {
digitalWrite(relay1,LOW); }
else if( val == 6 ) {
digitalWrite(relay2,LOW); }
else if( val == 7 ) {
digitalWrite(relay3,LOW); }
else if( val == 8 ) {
digitalWrite(relay4,LOW); }

//relay all off


else if( val == 10 ) {
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
}
}
print('Hello, world!')

# This program adds two numbers

num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

# Taking kilometers input from the user


kilometers = float(input("Enter value in kilometers: "))

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac
print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))

# Python program to find the factorial of a number provided by the user.

# change the value for a different result


num = 7

# To take input from the user


#num = int(input("Enter a number: "))

factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

# Python program to check if year is a leap year or not

year = 2000

# To get year (integer input) from the user


# year = int(input("Enter a year: "))

if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))

# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user


# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter


s = (a + b + c) / 2

# calculate the area


area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

You might also like