• LearnElectronics

Operators and Functions in Python - Examples

Updated: Oct 17, 2020

In this video, I am giving you 4 examples to try on your own and test your learning. So, do watch the video carefully, note down all the example questions and before scrolling down the video and seeing the solution for each example, I want you to try solving each solution on your own. After that, you may check the solution and see if you have written the code similar to what I have written or not.


Example 1: Area of triangle


In this example, firstly, we take 3 inputs from the user as sides of the triangle as s1,s2 & s3.

s1 = float(input("Enter first side = "))
s2 = float(input("Enter second side = "))
s3 = float(input("Enter third side = "))

Then we call the function - "areafn(s1,s2,s3)" to calculate the area of the triangle. The sides of the triangle are sent as parameters of the function when called.

area = areafn(s1,s2,s3)

This will move to the line where we have defined the function(first line of code in our case). In the first line, the parameters a,b,c have the values of sides s1,s2,s3, and using the formula we calculate the Semi-parameter(s) and then using s, we calculate the area of the triangle(a). Then we return the area of the triangle to the variable 'area' by writing 'return a'.

Heron's formula - area of triangle

Note: Always define the function before you call the function.

def areafn(a,b,c):
     s = (a+b+c)/2
     a = (s*(s-a)*(s-b)*(s-c))**0.5
     return a

And finally, we display the value of the area of the triangle using the Built-in-Function print( ).

print("Area of Triangle = ",area)

You may see the complete code below with the output and let us know if you got the answer correct or not.

Code:

def areafn(a,b,c):
    s = (a+b+c)/2
    a = (s*(s-a)*(s-b)*(s-c))**0.5
    return a

s1 = float(input("Enter first side = "))
s2 = float(input("Enter second side = "))
s3 = float(input("Enter third side = "))

area = areafn(s1,s2,s3)
print("Area of Triangle = ",area)

Output:

Enter first side = 3
Enter second side = 5
Enter third side = 4
Area of Triangle =  6.0

Example 2: Convert 'X' days in years, months and day format

The way the code is written is almost similar to the first example and need not requires a line by line explanation. We simply take the input from the user to enter the total number of days you want to be calculated in the form of years, months, and days. Then the function 'ansfn' is called and the value returned by the function is stored in 3 variables - dd, mm & yy. Then the value of dd, mm and yy is printed using the print ( ).

days = int(input("Enter the number of days = "))
dd,mm,yy = ansfn(days)
print(days,"days =", end=" ") print(yy,"years,",mm,"months,",dd,"days")

In the function, d is the user input and using which we calculate the number of years, months, and days. All of this is done using 5 lines of maths and the use of arithmetic operators.

def ansfn(d):
     yy = d//365
     r  = d%365
     mm = r//30
     r  = r% 30
     dd = r
     return dd,mm,yy 

One year has 365 days and to calculate the no. of years divide the total days by 365 and the quotient would give us the no. of years. So to get the quotient, we do floor division i.e. 'd//365' and store the value in 'yy' variable. There are chances that we may not get a multiple of 365 as the number of days and it means when we divide the days - d/365, we may also get remainder using which we can calculate the no. of months. So, the remainder - r = d%365, and then comes the time to calculate the no. of months.


One month has 30 days and similarly, like finding no. of years, we can find no. of months by floor diving remainder with 30 i.e. r//30. And whatever the remainder is left are the no. of days and should be stored in dd as dd = r where r = r%30.


Time to check the code and the output of the example one by one.


Code:

def ansfn(d):
    yy = d//365
    r  = d%365
    mm = r//30
    r  = r%30
    dd = r
    return dd,mm,yy

days = int(input("Enter the number of days = "))
dd,mm,yy = ansfn(days)
print(days,"days =", end=" ")
print(yy,"years,",mm,"months,",dd,"days")

Output:

Output 1:
Enter the number of days = 569
569 days = 1 years, 6 months, 24 days

Output 2:
Enter the number of days = 730
730 days = 2 years, 0 months, 0 days

Example 3: Convert 'X' rupees in notes format


This example is similar to the previous example and just have the context different. I won't be explaining this example, still, if you have any doubts then you may ask them in the comments section.


Code:

def notes(amt):
    K2 = amt//2000
    r  = amt%2000
    H5 = r//500
    r  = r%500
    H2 = r//200
    r  = r%200
    H1 = r//100
    r  = r%100
    F  = r//50
    return K2,H5,H2,H1,F

amount = int(input("Enter the amount you want to withdraw = "))
K2,H5,H2,H1,F = notes(amount)


print(amount,"=",K2,"INR2000 notes,",H5,"INR500 notes,",H2,"INR200 notes,",H1,"INR100 notes &",F,"INR50 notes")

Output:

Output 1:
Enter the amount you want to withdraw = 3650
3650 = 1 INR2000 notes, 3 INR500 notes, 0 INR200 notes, 1 INR100 notes & 1 INR50 notes

Output 2:
Enter the amount you want to withdraw = 2850
2850 = 1 INR2000 notes, 1 INR500 notes, 1 INR200 notes, 1 INR100 notes & 1 INR50 notes

Example 4: Reverse and sum of digits


To reverse a 3 digit number you need to separate all the 3 digits i.e. ld,m & rd (left digit, middle & right digit), and interchange then. But that is not possible in programming and we need some logic to do this task.


In the function, sumrev(x), we first find the 3rd digit(ld) of the input number(x) by finding the remainder when input is divided by 10, hence, ld = x%10. Then the quotient(r) has the other two digits i.e. the 1st and 2nd which we find when we do floor division of input by 10 i.e. r = x//10.

ld = x%10
r = x//10

Again, from the double-digit quotient(r), you can find the middle digit(m) by finding the remainder when you divide it by 10 and the quotient now is your 1st digit(rd).

m = r%10
rd = r//10

You got all the 3 digits in 3 different variables - ld, m & rd and to reverse the number you use the below formula. The formula multiples 'ld' by 100 to bring it to the hundreds position, then multiplies 'm' with 10 to get it to tens position and add the result of the above two with 'rd' and 'rd' in on the zeros position to make our reverse 3-digit number.

rev = ld*100 + m*10 + rd

The sum of digits is very simple and can be found just by simply adding all the separated 3 digits stored in different variables-

s = rd + m + ld

Now, time to see the complete code and try it in your Python IDLE. Also, let me know if you got the code correct.


Code:

def sumrev(x):
    ld = x%10
    r = x//10
    m = r%10
    rd = r//10
    rev = ld*100 + m*10 + rd
    s = rd + m + ld
    return rev,s

number = int(input("Enter any 3 digit number = "))
r,s = sumrev(number)
print("The number you entered is",number,end=" ")
print(" , it's reverse =",r," and sum of it's digits =",s)

Output:

Enter any 3 digit number = 456
The number you entered is 456  , it's reverse = 654  and sum of it's digits = 15

I hope you tried all the examples on your own and got all the answers correct. If not, no big deal, try again the same example and also try more examples of your own. One relevant example that I can give you is-


Example 5 -

Take birth date as input from the user i.e. year, month, & date, and ask today's date(or any other date) as another input. Calculate the no. of years, months, and days in between those two dates.


That's all for examples on Operators and Functions, check other topics in the python course, and continue learning.

57 views
  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us
© Copyright - LearnElectronics 2020.