Image

Anyone Can Code.





Image


Variables



Jyotirmay Zamre, Sammarth Kumar



Variables are containers that hold data values. The variable itself can be named using any assortment of uppercase/lowercase letters, numbers and underscores, known as an identifier. Identifiers are case-sensitive so 'NAME' and 'name' will be considered different identifiers. Identifiers cannot begin with numbers either.

Unlike other languages, Python has no specific command to declare variables and their types. Variables are created, and their types are identified when you assign a value to them. Because of this, Python is known as a dynamically-typed language

Let's look at an example:


    x = 'Hello World'
    print(x)
                      
Hello World

Here, x is the variable identifier and "Hello World is the content of the variable also known as the literal

Literals are simply the values stored in the variables and can be of several data types, which we will cover in the next tutorial.

Variables can be assigned any data type - integers, floats, strings, boolean etc. For strings, the value has to be placed inside single or double quotes. It is important to note that the data type of the value assigned to the variable doesn't have to be constant. A variable can hold a number and can be changed in the next line to hold a string.


    x = 'Hello World'
    x = 25
    print(x)
                      
25

Adding Variables

Two variables can be combined into one variable by using the '+' operator. However, the data types of the values assigned to the two variables have to be the same. If they aren't, Python will give an error. When both the variables contain numbers, the '+' operator will add the two numbers together. If there are two strings, it will squash them together to output a new string. We will talk more about this in our operators tutorial.


    x = 'Hello'
    y = 'World'
    z = x + y
    x2 = 5
    y2 = 10
    z2 = x2 + y2
    print(z)
    print(z2)
                      
HelloWorld 15

Variables with text assigned to them can also be combined with other text. They can be combined by assigning the value to a new variable or by combining them within a print statement. However, you can combine variables only with variables of the same data type.


    y = 'World'
    print('Hello'+ y)
                                          
HelloWorld

Naming Variables

Although we just used identifiers such as x, while naming variables, make sure that the names are representative of what data the variable stores. For example if my variable stores my bank account balance and i name it x, it will be hard for me to understand that line of code. Instead I should use a good identifier such as balance.

Identifier Rules

When creating an identifier, there are some rules we must follow:

  1. An identifier can consist of alphanumeric characters and underscores (_), and can be of any length
  2. An identifier cannot start with a digit. An identifier 2var would be invalid, but var2 is allowed
  3. Keywords cannot be identifiers (keywords to be covered in the next tutorial)
  4. Special characters such as @, #, $ are not allowed in identifiers

Defining Multiple Variables

We can also define multiple variables at a time in a single line. On the left hand side, we keep identifiers, separated by commas, and on the right hand side we have their respective literals, separated by commas as well.


    a,b,c = 1,2,3
    print(a,b,c)
                                          
1 2 3

Swapping Values of Variables - a Trick

There's a cool trick in Python by which we can swap the values of variables in only one line. All you have to do is keep the identifiers on the left and then on the right, type the identifier of the variable you want to swap values with, for each identifier. Let's see how.


    a,b,c = 1,2,3
    a,b,c = b,a,c
    print(a,b,c)
                                          
2 1 3

This trick is helpful in some situations, so you might as well know about it.

In this page, we used terms such as strings, which you may not have heard before. Don't worry we will be covering all this in the next tutorial.


That's it for this tutorial! Click Next to access the next tutorial!

❮ Back

Next ❯









Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar