DEV Community

Cover image for 2.3 Variables in Python
Mohit Raj
Mohit Raj

Posted on • Updated on

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....

Top comments (2)

Collapse
 
mburszley profile image
Maximilian Burszley • Edited

Might want to fix the formatting of your post.. especially those codeblocks.

Additionally, your expanded explanation of chained assignment is incorrect. When dealing with references, it points all the variables at the same reference:

>>> a=b=c={}
>>> c['test'] = 5
>>> a
{'test': 5}

The example before that is known as unpacking and happens with collections (you're using implicit tuples):

(a, b) = (2, 3)

Lastly, keywords are not variables. They just have special meaning to the interpreter.

Collapse
 
mohit355 profile image
Mohit Raj • Edited

Thanks for your suggestion for the post formatting.

Regarding the chained assignment

=>>Actually i start python from basics and i not want to confuse readers bu using tuples,list,dictionary . these are just basics of variable and if u read the whole post then you also read the last line where i write "This is all about variable as we continue we know more about variables." that means the its not the end as we continue we discuss more about declaration of variable. you use unpacking concept and i think this is not the time to teach unpacking. After completing the basics when we go deep inside list ,tuples then we teach these points. you are also right.

=>> a,b=2,3 here i am using using implicit tuples but not telling everyone about that because it is just basics and i only taught here what are the ways to declare.

Lastly

=>> you told that "Lastly, keywords are not variables. They just have special meaning to the interpreter" . If you read my post clearly then same point is there but the way of writing is somewhat wrong so i changed it.

Thanks for your suggestion