Learning how to use variables in Python is a fundamental skill for any aspiring coder. In this tutorial, we’ll focus on how to use the variable ‘N’ in a Python program. We’ll walk you through creating a simple script and show how variables can be used to store, manipulate, and print data.
Step 1: Defining the Variable ‘N’
In Python, we can assign any value to a variable. To start with, we will assign an integer value to ‘N’ which we’ll use in our script later. See the code below:
1 |
N = 5 |
Step 2: Manipulating the Variable ‘N’
We can now manipulate the value of ‘N’. This demonstrates the concept of variables which act as containers for information.
1 |
N = N * 2 |
Step 3: Printing the Value of ‘N’
Python has built-in functions for displaying output. We use the print() function to display the value of ‘N’.
1 |
print(N) |
Step 4: Using the Variable ‘N’ Inside a Function
We can also use the variable ‘N’ inside a function. As an example, we’ll use the range() function to create a list of numbers from 0 to N-1.
1 2 |
for i in range(N): print(i) |
Full Code
1 2 3 4 5 |
N = 5 N = N * 2 print(N) for i in range(N): print(i) |
Output
10 0 1 2 3 4 5 6 7 8 9
Interpreting the Output
In the output, you can see that the value of ‘N’ was first multiplied by 2 resulting in 10, which is the first number printed. The subsequent numbers are generated by the range() function, starting from 0 all the way up to ‘N’-1 or 9 in our case.