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
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
- Create a name that makes sense. Suppose, vowel makes more sense than v.
- Use camelCase notation to declare a variable. It starts with lowercase letter. For example:
myName myAge myAddress
- Use capital letters where possible to declare a constant. For example:
PI G MASS TEMP
- Never use special symbols like !, @, #, $, %, etc.
- Don't start name with a digit.
- Constants are put into Python modules and meant not be changed.
- 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
A 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:
- String literals :: "halo" , '12345'
- Int literals :: 0,1,2,-1,-2
- Long literals :: 89675L
- Float literals :: 3.14
- Complex literals :: 12j
- Boolean literals :: True or False
- Special literals :: None
- Unicode literals :: u"hello"
- List literals :: [], [5,6,7]
- Tuple literals :: (), (9,),(8,9,0)
- Dict literals :: {}, {'x':1}
- Set literals :: {8,9,10}
Thank You :)
Nice Blog
ReplyDeletePython Online Training Hyderabad
Best Python Online Training
I really liked your blog post.Thanks for sharing this info
ReplyDeleteBest Python Online Training
Python Online Classes