2.3 Variables in Python

mohit355

Mohit Raj

Posted on November 3, 2019

2.3 Variables in Python

variables :

  1. Variables are nothing but reserved memory locations to store values. It means that when you create a variable, you reserve some space in the memory.
  2. Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory.
  3. There are different types of data types in python:
    a. Numbers
    b. String
    c. List
    d. Tuple
    e. Dictionary
    Note: I will discuss these all in separate section

  4. A variable can hold different types of values.

  5. for example if you assign a string to a variable then that variable belong to string data types,and if you assign number to the same variable the its belong to number data types.

  6. Means to say that you can store any type of variable in any variable,that variable behaves according to that particular data type. need not to declare data type before the variable name (as done in language like C and C++).

How to declare variable

  1. For declaration of variable in python no need to declare data type of it.
  2. Syntax : variable_name = assigned_value
    example :
    name="abc"
    x=23
    roll=1 etc

  3. You may assign values to more than one variable at a time.
    example : a=b=c=2 means a=2,b=2,c=2.

  4. You may also assign like this_
    a,b=2,3 i.e. a=2 and b=3 (this is not possible in language like C or C++)
    Alt Text

Rules for declaration of a variable

  1. Variable name not start with a number (show syntax error) but you can use number inside the variable name. Alt Text

Alt Text

  1. Variable name only starts with any letters ,underscore(_) . this is for the first letter of the variable name
    Alt Text

  2. Not use any special symbol ($,^,@,%) in the variable name neither at the start nor in the middle or end.
    Alt Text

  3. keywords are not variables. They just have special meaning to the interpreter..
    See below list for the python keywords these are not used as a variable :
    Alt Text

=>> This is all about variable as we continue we know more about variables as we go deeper in it.

Thank You....

💖 💪 🙅 🚩
mohit355
Mohit Raj

Posted on November 3, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related