Python Programming Variable Tutorial with Stone River eLearning

In our previous blog post on Python language, we discussed how Python mathematical functions could be easily performed using a simple print statement in Python. The user friendliness and amazing dynamic data type availability and checking make it possible to do even the complex operations with utmost efficiency and ease. In this particular blog, we will be discussing about a more basic and fundamental aspect of computer programming that is a common and necessary element of all programming languages; the variables. As we all know, variables are used to store values which can then be used for operations and calculations. A variable allows the user to minimize the time spent on hard coding each and every line of the program. Assigning values to the variable gives the programmer the chance to easily use the same value at multiple locations of the program.

How are variables declared in Python?

Declaring a variable in Python is far easier and structured as compared to any other object oriented programming language that you might have used. All we have to do is write the name of the variable and assign values to that variable. Unlike other programming languages like C, C++; we do not to declare the variable before we assign values to it. Let us, with the help of an example, understand the process of assigning values to a variable in Python.

Eg:

var1 = 100

print(var1)

Output:

100

The same code when written in C would have required the following statements.

Eg:

#include <stdio.h>

int main()

{

int x;

x=100;

printf(“%d”, &x);

return 0;

}

You can clearly notice the difference in the length of the code between C and Python. This code was just for printing a value in a variable and still it had a prominent difference; now imagine the difference for a code that involves more complex functions and operations.

As we discussed above, in python, the variable and its values can also be directly used in any other variable for performing calculations or operations. We will now demonstrate the same using the example we have discussed above.

Eg:

var1 = 100

print(var1)

var2= var1/5

print(var2)

Output:

100

20

There are a few things that we must keep in mind regarding variables to ensure that we do not encounter any error while working with variables on Python.

  1. The name of the variable could be anything that starts with a character; however it should not start with a number. The name of the variable can begin with an underscore.
  2. Strings are declared inside either single or double quotations. However; it is better to use double quotations if your text includes apostrophe marks in it.
  3. A floating point value is declared either as “a = 3.5” or if it is an integer value that you want to declare as a float, you can write “a = float(7)”
  4. Just in case you want your print statement to display some text along with the value in a variable, then in such a case, the print statement in Python looks similar to the print statement used in C programming language.

Eg:

var1 = 100

print(“The value stored in the variable is %d” % var1)

The text comes inside the double quotes and the variable name after the end of the quotation marks. A “%” sign must precede the name of the variable and a formatter (“%d” in this case) must be written with the text inside the print command. We will learn more about variables, formatters and their use as we move to the next blog posts in this series on python programming.

GET YOUR FREE PYTHON EBOOK!

(Visited 43 times, 1 visits today)