How to add numbers in python

How to add numbers in a list Python | Example code

Use append() function to add numbers in a list Python. Syntax of adding a single number into a given list.

Python Example add numbers in a list

Use list.append()

It will append new elements into a list.

a_list = [1, 2, 3] new_num = 4 a_list.append(new_num) print(a_list)

Output: [1, 2, 3, 4]

Use enumerate()

Use it if you have an iterable object to add-in list. It will add value to the existing list number.

a_list = [1, 0, 0] new_nums = [1, 2, 3] for index, integer in enumerate(new_nums): a_list[index] += integer print(a_list) 

Output: [2, 2, 3]

Add user input numbers into the list

lst = [] num = int(input('How many numbers: ')) for n in range(num): numbers = int(input('Enter number ')) lst.append(numbers) print("Sum of elements in given list is :", sum(lst))

How to add numbers in a list Python

Do comment if you have any doubts or suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Источник

How to add two numbers in Python [6 different methods]

In this python tutorial, you will learn about Python programs to add two numbers.

There are six methods to add two numbers in Python, which are shown below.

  • Using the arithmetic operator ‘+’
  • Using ‘+=’
  • Using the function reduce() and operator.add
  • Using the add function
  • Using the user input
  • Using the sum() method

How to add two numbers in Python

Here we will discuss all 6 methods to add two numbers in Python/ Let us first start with the arithmetic operator.

Читайте также:  Бэкенд php или python

Method-1: How to add two numbers in Python using the arithmetic operator ‘+’

This is the most basic and straightforward method to add two numbers in Python. Simply use the ‘+’ operator between the two numbers you want to add, and Python will return the sum.

# Calculating the sum of two numbers num1 = 5 # First number num2 = 10 # Second number # Adding the two numbers sum = num1 + num2 # Printing the result print("The sum of", num1, "and", num2, "is", sum)

The above code calculates the sum of two numbers, num1, and num2, and stores the result in a variable named sum. The result is then printed to the console with a message. num1 is assigned the value of 5, and num2 is assigned the value of 10.

Python-add two numbers using the operator

Method-2: How to add two numbers in Python using ‘+=’

The ‘+=’ operator is a shorthand operator in Python that can be used to add a value to a variable. This operator adds the value on the right to the variable on the left, and the result is stored in the left variable.

# Addition of two variables num1 = 5 num2 = 10 # Perform addition using '+=' operator num1 += num2 # Print the result print("The sum of", num1, "and", num2, "is", num1) 

The above code adds two numbers num1 and num2 using the “+=” operator. The result of the addition is then stored in the num1 variable, which is then printed as the “The sum of 5 and 10 is 15”.

Output: The sum of 15 and 10 is 15

Method-3: How to add two numbers in Python using the function reduce() and operator.add

The reduce() function is a built-in function in Python that can be used to apply a given function to all elements of a list. To add two numbers, you can use the reduce() function along with the operator.add function.

# Importing the operator module and the reduce function from functools library import operator from functools import reduce # Initializing two numbers num1 = 5 num2 = 10 # Calculating the sum of the two numbers using reduce() and operator.add sum = reduce(operator.add, [num1, num2]) # Printing the result print("The sum of", num1, "and", num2, "is", sum) 

The above code imports the operator module and the reduce function from the functools module.

  • It defines two variables num1 and num2 with values 5 and 10 respectively.
  • Then it calculates the sum of the two variables using the reduce function with operator.add as the operator to apply to the elements of the list [num1, num2].
  • The result is printed with a message that states “The sum of 5 and 10 is 15”.
Output: The sum of 5 and 10 is 15

Method-4: How to add two numbers in Python using the add function

# Define a function that takes in two parameters, num1 and num2 def add(num1, num2): # Return the result of adding the two numbers return num1 + num2 # Store the result of calling the add function with arguments 5 and 10 sum = add(5, 10) # Print the sum of 5 and 10 print("The sum of 5 and 10 is", sum) 

The above code defines a function add() that takes two arguments, num1 and num2.

  • The function returns the sum of the two arguments by using the “+” operator.
  • Then the function is called with arguments 5 and 10 and the result is stored in the sum variable. Finally, the sum is printed.
Output: The sum of 5 and 10 is 15

Method-5: How to add two numbers in Python using the user input

To add two numbers provided by the user, you can use the input() function to get the input from the user and then use any of the methods mentioned above to add the two numbers.

# This code takes two input numbers from the user # And calculates their sum and prints the result num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) # Sum of the two input numbers sum = num1 + num2 # Output the result print("The sum of", num1, "and", num2, "is", sum)

The above code takes two integer inputs from the user and stores them as num1 and num2.

  • Then, it calculates the sum of these two numbers and stores it in the variable sum. Finally, it prints the result of the sum by including the values of num1, num2, and sum in the output string.
Output: Enter the first number: 3 Enter the second number: 8 The sum of 3 and 8 is 11

Method-6: How to add two numbers in Python using the sum() method

The sum() function is a built-in function in Python that can be used to add numbers. You can pass a list of numbers to this function, and it will return the sum of all numbers in the list.

# Program to calculate the sum of two numbers # using the sum() function # Define the two numbers num1 = 5 num2 = 10 # Calculate the sum of the two numbers # by passing them as a list to the sum() function sum = sum([num1, num2]) # Print the sum of the two numbers print("The sum of", num1, "and", num2, "is", sum) 

The above code calculates the sum of two numbers num1 and num2. The numbers are assigned the values of 5 and 10, respectively.

  • The sum function from the builtins of the module is then used to find the sum of the two numbers and store it in the sum variable.
Output: The sum of 5 and 10 is 15

Addition of two numbers in Python using a function

Python also allows you to define your own functions. We can create a function to add two numbers:

def add_two_numbers(num1, num2): return num1 + num2 print('The sum is', add_two_numbers(3, 5))

In this script, we first defined a function add_two_numbers which takes two arguments and returns their sum. We then called this function with the numbers 3 and 5, and printed the result.

Читайте также:  Php регулярные выражения начало строки

When you run the above Python addition code, you can see the output like below:

python code for adding two numbers

Sum of two numbers in Python using Lambda Functions

Lambda functions are a way of creating small, anonymous functions in Python. They can be used in places where you need a function, but don’t want to bother defining it with a full def statement.

Here’s how to add two numbers using a lambda function in Python:

# define the lambda function add_two_numbers = lambda num1, num2: num1 + num2 # use the lambda function print('The sum is', add_two_numbers(3, 5))

You may also like to read the following Python tutorials.

In this Python tutorial, we have learned How to add two numbers in Python by using the below methods:

  • Using the arithmetic operator ‘+’
  • Using ‘+=’
  • Using the function reduce() and operator.add
  • Using the add function
  • Using the user input
  • Using the sum() method

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Источник

Оцените статью