Python Variables, Constants,literals.

Variable

In most of the programming languages a variable is a named location used to store data in the memory. Each variable must have a unique name called identifier. It is helpful to think of variables as container that hold data which can be changed later throughout programming.
Non technically, you can suppose variable as a bag to store books in it and those books can be replaced at anytime.
Note: In Python we don't assign values to the variables, whereas Python gives the reference of the object (value) to the variable.

Declaring Variables in Python

In Python, variables do not need declaration to reserve memory space. The "variable declaration" or "variable initialization" happens automatically when we assign a value to a variable.

Assigning value to a Variable in Python

You can use the assignment operator = to assign the value to a variable.

Example 1: Declaring and assigning a value to a variable


When you run the program, the output will be:
kishanvegad.blogspot.com
In the above program, we assigned a value kishanvegad.blogspot.com to the variable Website. Then we print the value assigned to website i.e  kishanvegad.blogspot.com
Note : Python is a type inferred language, it can automatically infer (know)  kishanvegad.blogspot.com is a String and declare website as a String.

Example 2: Assigning multiple values to multiple variables

If we want to assign the same value to multiple variables at once, we can do this as

Constants

A constant is a type of variable whose value cannot be changed. It is helpful to think of constants as containers that hold information which cannot be changed later.
Non technically, you can think of constant as a bag to store some books and those books cannot be replaced once placed inside the bag.

Assigning value to a constant in Python

In Python, constants are usually declared and assigned on a module. Here, the module means a new file containing variables, functions etc which is imported to main file. Inside the module, constants are written in all capital letters and underscores separating the words.

Example 3: Declaring and assigning value to a constant

Create a constant.py
Create a main.py

When you run the program, the output will be:
3.14
9.8
In the above program, we create a constant.py module file. Then, we assign the constant value to PI and GRAVITY. After that, we create a main.py file and import the constant module. Finally, we print the constant value.
Note: In reality, we don't use constants in Python. The globals or constants module is used throughout the Python programs.

Rules and Naming convention for variables and constants

  1. Create a name that makes sense. Suppose, vowel makes more sense than v.
  2. Use camelCase notation to declare a variable. It starts with lowercase letter. For example:
    myName
    myAge
    myAddress
  3. Use capital letters where possible to declare a constant. For example:
    PI
    G
    MASS
    TEMP
  4. Never use special symbols like !, @, #, $, %, etc.
  5. Don't start name with a digit.
  6. Constants are put into Python modules and meant not be changed.
  7. Constant and variable names should have combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example:
    snake_case
    MACRO_CASE
    camelCase
    CapWords

Literals

literal is a succinct and easily visible way to write a value. Literals represent the possible choices in primitive types for that language. Some of the choices of types of literals are often integers, floating point, Booleans and character strings. Python support the following literals:
  1. String literals   ::   "halo" , '12345'
  2. Int literals   ::   0,1,2,-1,-2
  3. Long literals   ::   89675L
  4. Float literals   ::   3.14
  5. Complex literals   ::   12j
  6. Boolean literals   ::   True or False
  7. Special literals   ::   None
  8. Unicode literals   ::   u"hello"
  9. List literals   ::   [], [5,6,7]
  10. Tuple literals   ::   (), (9,),(8,9,0)
  11. Dict literals   ::   {}, {'x':1}
  12. Set literals   ::   {8,9,10}



Thank You :)







Comments

Post a Comment

Popular posts from this blog

Python Matrix

Angular : List of Topics

What are the steps involved in the concreting Process?