Function in Python

Python program to add two number using function

Python program to add two number using function

In this tutorial, we will discuss Python program to add two number using function

In this topic, we will learn a simple concept of how to add two numbers using the function in the Python programming language

already we learned the same this concept using the operator in a simple way.

if you know click here Python program to add two numbers

Program 1

Add two integer number using the function

#Python program to add two numbers using function

def add_num(a,b):#function for addition
    sum=a+b;
    return sum; #return value

num1=25  #variable declaration
num2=55
print("The sum is",add_num(num1,num2))#call the function

When the above code is compiled and executed, it produces the following results

The sum is 80

 

Add two integer number using the function – get input from the user

Program 2

#Python program to add two numbers using function

def add_num(a,b):#function for addition
    sum=a+b;
    return sum; #return value
num1=int(input("input the number one: "))#input from user for num1
num2=int(input("input the number one :"))#input from user for num2

print("The sum is",add_num(num1,num2))#call te function

When the above code is compiled and executed, it produces the following results

input the number one: 34
input the number one: 45
The sum is 79

This is the program asked input from user two numbers and displays the sum of two numbers entered by the user

We can use pre-defined python function input() to takes input from the user. Input() function returns a string value. So we can use int() function to convert from string to int data type (shown in line 6 and 7).

Add two floating point number using the function

Program 3

#Python program to add two numbers using function

def add_num(a,b):#function for addition
    sum=a+b;
    return sum; #return value

num1=2.456 #variable declaration
num2=55.54
print("The sum is",add_num(num1,num2))#call te function

 

the sum is 57.996

 

Add two float numbers using the function – get input from the user

#Python program to add two numbers using function

def add_num(a,b):#function for addition
    sum=a+b;
    return sum; #return value
num1=float(input("input the number one: "))#input from user for num1
num2=float(input("input the number one: "))#input from user for num2

print("The sum is",add_num(num1,num2))#call te function

 

input the number one: 34.4
input the number one: 32.45
The sum is 66.85

This is the program asked input from user two numbers and displays the sum of two numbers entered by the user

We can use pre-defined python function input() to takes input from the user. Input() function returns a string value. So we can use float() function to convert from string to float data type (shown in line 6 and 7).

 

 

Suggested for you

 largest number of three number in C

 largest number of three number in C++

 largest number of three number in Python

 largest number of three number in C using function

 largest number of three number in C++ using function

 largest number of three number in Python using function

 largest number of three number in Java using method

 

Function in Python language

Operator in Python language

Python data types

C program to calculate sum in array elements
Java program to calculate sum in array elements
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • your ideas regarding explaining python programming is realy very easy to understand and surely very helpful for skillfull learning

  • def addition(x,y):
    sum=x+y;
    return sum;
    a=int(input("please enter first number: "))
    b=int(input("please enter second number: "))
    print("sum is :",addition(a,b))

    Please tell mistake in this code.

    • No error but indent mistake

      here, the code

      def addition(x,y):
      sum=x+y //indent inside
      return sum //indent inside
      a=int(input("please enter first number"))
      b=int(input("please enter second number"))

      print("Sum is: ",addition(a,b))

  • def add_num(a,b):#function for addition
    sum=a+b;
    return sum; #return value
    b =float(input("input the number one: "))#input from user for num1
    c =float(input("input the number two: "))#input from user for num2
    print("The sum is",add_num(b,c))#call te function

    input the number one: Traceback (most recent call last):
    File "/tmp/pyrunnerwQyCdwJM/code.py", line 4, in <module>
    b =float(input("input the number one: "))#input from user for num1
    ValueError: could not convert string to float: 'NO_INPUT'

  • Take two numbers (integers) as one input and print the addition in one line .can any one give answer for this question

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago