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

7. If else statement & functions

The document provides examples of Python code demonstrating the importance of indentation, conditional statements, and functions. It includes a mini calculator project with functions for addition, subtraction, multiplication, and division, along with user input handling. The document also highlights common errors such as indentation issues and shows how to use functions to eliminate repetitive tasks.

Uploaded by

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

7. If else statement & functions

The document provides examples of Python code demonstrating the importance of indentation, conditional statements, and functions. It includes a mini calculator project with functions for addition, subtraction, multiplication, and division, along with user input handling. The document also highlights common errors such as indentation issues and shows how to use functions to eliminate repetitive tasks.

Uploaded by

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

In [1]: A = 36

A = 18

In [5]: if A>=35:
print("The Student is passed")
else:
print("The Student has failed")

Cell In[5], line 2


print("The Student is passed")
^
IndentationError: expected an indented block after 'if' statement on line 1

In [ ]: ### Indentation is important in python


#let's write the same code with indentation

In [4]: if A>=35:
print("The Student is passed")
else:
print("The Student has failed")

The Student has failed

In [24]: a=60

In [25]: if a<35:
print("failed")
elif a>=35 and a<=60:
print("passed")
elif a>60 and a<=75:
print("first_class")
else:
print("the student is topper")

passed

In [33]: a= 350

In [34]: if a<=200:
print("cheap")
elif a>200 and a<=400:
print("affordable")
else:
print("expensive")

affordable

In [35]: print("Good morning Sunil")

Good morning Sunil

In [36]: print("Good morning anil")

Good morning anil

In [ ]: ### to eliminate such repetative tasks functions are used

In [ ]: ### Functions
# write some logic (write it once) and use it multiple times
# function always starts with 'def' which means define

In [37]: def greeting(name):


print("hello! Good Morning", name)

In [38]: greeting("sunil")

hello! Good Morning sunil

In [39]: greeting("anil")

hello! Good Morning anil

In [43]: def greeting(name):


print("Hello! Wel Come to KSR", name)

In [44]: greeting("sunil")

Hello! Wel Come to KSR sunil

In [45]: a=3
b=4
c=(a+b)

In [46]: print(c)

In [47]: a=5
b=7
c=(a*b)

In [48]: print(c)

35

In [ ]: ### to eliminate the repetition we can use functions

In [49]: def add(a,b):


c=a+b
print(c)

In [50]: add(7,2)

In [51]: add(99,101)

200

In [52]: def mul(a,b):


c=a*b
print(c)

In [53]: mul(3,4)

12

In [54]: mul(12,6)

72

In [ ]: #### Project ####

In [ ]: #Mini Calculator

In [30]: def addition(a,b):


c= a+b
print("the addition is: ",c) #### both print and return can be used to display the result

def subtraction(a,b):
c= a-b
print("the subtraction is: ",c)

def multiplication(a,b):
c= a*b
print("the multiplication is: ", c)

def division(a,b):
c=a/b
print("the division is: ", c)

File <string>:15
print("the division is: ", c)
^
IndentationError: unindent does not match any outer indentation level

In [8]: add(8,2)

Out[8]: 10

In [9]: sub(8,2)

Out[9]: 6

In [10]: mul(8,2)

Out[10]: 16

In [15]: div(8,2)

Out[15]: 4.0

In [20]: print("enter the value of a: ")


a=int(input())
print("enter the value of b: ")
b=int(input())
add(a,b)

enter the value of a:


enter the value of b:
Out[20]: 13

In [24]: print("enter the value of a: ")


a=int(input())
print("enter the value of b: ")
b=int(input())
print("the addition is", add(a,b))

enter the value of a:


enter the value of b:
the addition is 14

In [47]: def addition(a,b):


c= a+b
print("the addition is: ", c)
def subtraction(a,b):
c= a-b
print("the subtraction is: ", c)
def multiplication(a,b):
c= a*b
print("the multiplication is: ", c)
def division(a,b):
c=a/b
print("the division is: ", c)

In [51]: print("enter the first element: ")


a=int(input()) #### need to write mention datatype everytime when we collect data as an input
print("enter second element: ")
b=int(input())
print("enter the operation: add/sub/div/mul: ")
operation=input()

if operation == "add":
addition(a,b)
elif operation =="sub":
subtraction(a,b)
elif operation =="mul":
multiplication(a,b)
elif operation =="div":
division(a,b)

enter the first element:


enter second element:
enter the operation: add/sub/div/mul:
the multiplication is: 12

In [ ]:

You might also like