Followers

Latest techys

Part9- Python Fundamentals | Start from the Basics

If u fell any difficulty in watching the content, try to reduce the size of this page or rotate your portable device if you are using it


In the previous article, we learnt about Python Styles and Conventions. Today, we shall learn about Other Components of a Python Program. So, let's start.

Variables and Assignments: A variable in Python represents named location that refers to a value and whose values can be used & processed during program run. For instance, to store name of a person and his income during a program run, some labels would be required to refer to the income to make it distinguishable.

* Variables are also called symbolic variables as these are named labels.

    Creating a variable: It is not at all complicated. For the above case of a person & his income, we just need to assign a numeric value with an identifier name and Python will create the variable of the type similar to that of the value assigned. 

        For example: 

name='Rohan'          #name is a variable assigned with a string type value

income=120000     #income is another variable assigned with a numeric type value

    Important point to remember:

        Unlike other programming languages, Python variables do not have fixed storage locations. The location they refer to, changes everytime the values change(this rule is not for all types of variables, though)


    Lvalues and Rvalues: Expressions that come on the lhs(left hand side) of an assignment are said to be lvalues & expressions that come on the rhs(right hand side) of an assignment are said to be rvalues.

    For example: a=20    here a is the lvalue and 20 is the rvalue.

                          20=a    here lvalue cannot have numeric datatype so this cannot happen.


Multiple Assignment: Python is very versatile with assignments. 

    There are many ways to assign in Python, they are:

    1. Assigning same value to multiple variables: You can assign same value to multiple variables in a single statement.

        For example: a=b=c=17

    2. Assigning multiple values to multiple variables: Values will be assigned order wise i.e. first variable is given first value, second variable is given second value & so on...

         For example: x, y, z=10, 20, 30


Variable Definition: In Python, a variable is created when you first assign a value to it. It also means that a variable is not created until some value assigned to it. If a variable is not assigned with a value, then it will give an error.


We shall learn about Dynamic Typing in the next article.

No comments