How to Add in Python

In Python, you can add numbers, strings, lists, and other data types using various operators and methods.

The following examples show how to add in Python using different ways.

Using + Operator to Add Numbers

You can use the + operator to add numbers in Python:

# Declare variables
a = 5
b = 7

# Do addition
c = a + b

# Show result
print(c)

Output: 👇️

12

In this example, we use the + operator to add two numbers.

Using + Operator to Concatenate Strings

You can use the + operator to concatenate strings in Python:

# Declare strings
a = "Hello"
b = "World"

# Add strings
c = a + " " + b

# Show strings
print(c)

Output: 👇️

Hello World

In this example, we use the + operator to concatenate two strings with a space in between.

Using + Operator to Add Elements to List

You can use the + operator to add elements to a list in Python:

# Declare lists
a = [1, 2, 3]
b = [4]

# Add lists
c = a + b

# Show result
print(c)

Output: 👇️

[1, 2, 3, 4]

In this example, we use the + operator to add an element to a list.

Using append() Function to Add Elements to List

You can use the append() function to add elements to a list in Python:

# Declare list
a = [1, 2, 3]

# Add element to list
a.append(4)

# Show result
print(a)

Output: 👇️

[1, 2, 3, 4]

In this example, we use the append() function to add an element to a list.