Sunday, August 5, 2018

Variable Declaration in Python Programming Language



Unlike other programming languages, Python do not need explicit variable declaration. Explicit declaration means where variables are to be declared with 'data type' before using. Suppose we need a variable named 'num' in C++ language to store fractional value, then the variable must be declared explicitly using the statement 'float num' before using it. 

This is not the case with Python. Python language supports implicit variable declaration where variables are directly assigned values without declaring like 'x=10'. Depending upon the value assigned, type of the variable is set by Python. 

Example: num = 123 # This will create a variable 'num' and assign the value 123 on it.

We can change the type of a already created variable by assigning value of different type like num = 'Suddha' # the `num` variable is now a string type.


Rules and conventions of variable declaration


Like all other programming languages, Python programming language also follows few rules and conventions in variable declaration.

Rules

All variable names must start with alphabet and underscore ( _ ) and numeric value may come afterwards.

Apart from the above three characters, no other characters can be used in variable names.

Variable names are case sensitive in Python

No keywords can be used in variable names in Python programming language

Conventions

Variable names should be meaningful

Python variables are normally written in lower case character

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner