Increment by 1 in Python – multiple ways
Hey fellow Python coder! In this tutorial, we will be covering various methods in which we can increment various objects by 1 in Python.
Incrementing Numberic Variables
by 1 in Python
In Python, Variables are simply containers that are used to store and manage data. They can be used to hold various types of data be it numbers, strings, or any other custom object.
Also Read: Python Variables Naming Rules
Let’s declare a few numeric variables which we will be incrementing by 1 in the sub-sections later. We will be studying various methods to increment numeric variables by 1.
integerVariable = 934 decimalVariable = 9.34
Method 1: Using +
Operator to Increment by 1
The simplest way to increment a variable by 1 is by using the addition operator simply as shown below.
print("Old Integer Value : " , integerVariable) integerVariable = integerVariable + 1 print("New Integer Value : ", integerVariable) print("Old Decimal Value : " , decimalVariable) decimalVariable = decimalVariable + 1 print("New Decimal Value : ", decimalVariable)
The output of the code comes out to be this:
Old Integer Value : 934 New Integer Value : 935 Old Decimal Value : 9.34 New Decimal Value : 10.34
As you can see integer and decimal values are incremented by 1.
Method 2 – Using +=
Operator to Increment by 1
The +=
operator is an assignment operator that adds the right-hand value to the left value. It is a short-hand combination of addition and assignment operations.
Also Read: Working of ‘+=’ operator in Python with examples
Let’s learn how to increment the variables by 1 using the code below.
print("Old Integer Value : " , integerVariable) integerVariable += 1 print("New Integer Value : ", integerVariable) print("Old Decimal Value : " , decimalVariable) decimalVariable += 1 print("New Decimal Value : ", decimalVariable)
The output of the code comes out to be this:
Old Integer Value : 935 New Integer Value : 936 Old Decimal Value : 10.34 New Decimal Value : 11.34
As you can see integer and decimal values are incremented by 1.
Incrementing String Variables
by 1
Strings are sequences of characters forming either a word or a sentence. Before incrementation, let’s declare a string variable using the code below.
charString = 'f'
In this tutorial, we will learn how to increment both a character and a bunch of characters. In Python, there are multiple ways to increment a string by 1. Let’s explore them in the next sections.
Method 1: Using chr
and ord
Methods to Increment by 1
The chr
method is used to get the string value behind an ASCII code. And ord
method does the opposite and gives the ASCII code for a SINGLE character.
Also Read: Show the ASCII value of a character in Python
Let’s see how both methods work and add the charString
variable by one and convert f
to g
. In the code first, we extract the ASCII code of the letter f and then increment the ASCII by 1. Lastly, convert the ASCII value to the character using the chr
method.
oldASCII = ord(charString) print("Old Character is : ", charString , " with ASCII value as : ", oldASCII) newASCII = oldASCII + 1 newcharString = chr(newASCII) print("New Character is : ", newcharString , " with ASCII value as : ", newASCII)
The output of the code is as follows:
Old Character is : f with ASCII value as : 102 New Character is : g with ASCII value as : 103
As you can see the character is incremented by 1 and now points to g instead of f.
Also Read: How to concatenate two strings in Python
Incrementing List Variable
by 1
A list in Python is like a container that allows you to store multiple items in a single variable.
Also Read: List in Python and operations
In the case of Lists, instead of directly incrementing it by 1 we add 1 to each element present in the numeric lists.
Have a look at the code below:
listVariable = [1, 2, 3, 4, 5] newListVariable = [] for i in listVariable: newListVariable.append(i+1) print("Old List: ", listVariable) print("New List: ", newListVariable)
The output of the code is as follows:
Old List: [1, 2, 3, 4, 5] New List: [2, 3, 4, 5, 6]
As you can see using the simple assignment operation, we incremented each element of the list by 1.
Incrementing Tuple Variable
by 1
Tuples are very similar to Lists but the only difference between the two is the use of brackets and the fact that they are immutable.
Also Read: Tuples in Python with examples
Similar to lists, tuples can’t just be incremented by 1. In this case as well we will increment each element by 1 as shown in the code below.
tupleVariable = (1, 2, 3, 4, 5) newTupleVariable = () for i in tupleVariable: newTupleVariable += (i+1), print("Old Tuple: ", tupleVariable) print("New Tuple: ", newTupleVariable)
The output of the code is as follows:
Old Tuple: (1, 2, 3, 4, 5) New Tuple: (2, 3, 4, 5, 6)
As you can see using the simple addition of elements to the tuple, we incremented each element of the tuple by 1. It’s that easy!
In this tutorial, we covered incrementing 1 to various Python Objects in their respective ways.
I hope you find this tutorial useful and fun to learn! Happy Coding!
Leave a Reply