VARIABLES IN PYTHON
A name is assigned for a storage location is called a variable. A Python variable, which is used to store value, is also referred to as an identifier. When we are using variables in the python we need not to specific type variable because python dynamic type programming language.
Variable names must start with a letter or an underscore and can contain both letters and numbers.
In python always variables starts with an alphabetic-letters like A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Punctuation characters are not allowed in python such as @, $, and % within identifiers.
Example:
valid variables |
invalid variables |
x |
1x |
age |
*age |
Total |
Total$ |
Amount12 |
X%6 |
_a |
1_a |
a_6 |
X 6 |
VARIABLE DECLARATION & ASSIGNING VALUES
Python is a case sensitive language. Thus, Avatar and avatar are two different identifiers in Python.
· The use of a variable in an app is not required to be declared before using it in Python. It enables us to construct a variable when it is needed.
· In Python, variables don't need to be declared explicitly. The variable is automatically declared whenever we give it any value.
· One uses the equal (=) operator to give a variable a value.
Example:
Ø x = 30 correct
Ø car$ =60 wrong
Ø 123age =20 wrong
Ø age123=12.00 correct
Ø amount=30 /// print(amount) #30
Ø AMOUNT=9999 /// print(AMOUNT) #9999
Here are naming conventions for Python identifiers −
Every time Class names begin with an uppercase letter. Remaining identifiers start with a lowercase letter. Starting an identifier with a single leading underscore (_) indicates that the identifier is private. Starting an identifier with two leading underscores (__) indicates that the identifier is strongly private. If the identifier starts and also ends with two underscores symbols, then the identifier in python language-defined special name called magic method example: ___sum__
Variable Names
How to declare a valid variable has already been covered above in detail. Variable names may contain uppercase, lowercase letters (A to Z), digits (0–9), and the underscore character ( ) of any length. Think about the names of the following valid variables.
Example:1
stdname=”maneesh”
stdage=20
stdmarks=87.07
stdgrade=”A”
Print(stdname)
Print(stdage)
Print(stdmarks)
Print(stdgrade)
Output:
maneesh
20
87.07
A
Lines and Indentation
¢ Python does not use braces ({}) to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.
¢ The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount.
For example-
>>if True:
print ("True") else:
print ("False")