Saturday, 11 March 2017

Python: variables in python

What is Variable?


  • A variable is simply a name that one creates to store something  for later use.

Variable names in python follow the rules:

  • Upper and lower case letters and numbers are allowed, no symbols except underscore symbol( _ ). 
  • Variable names must start with a letter or _ symbol.
  • Variable names are case sensitive (MyVar and myvar are different)
  • The length of the variable name is limited.
  • You cannot use python special words/keywords as variable names.

A value associated with a variable by using = (Equal) operator.

No declaration is needed for what type of values is stored.

Example1:
Variable1=10
print(Variable1)

Output:
10

Example2:
vaRiaBle2=20
print(vaRiaBle2)

Output:
20

Example3:
x=2
Y=4
XYZ=5
abc=7
print(x)
print(Y)
print(XYZ)
print(abc)

Output:
2
4
5
7

Example4:
a_b=10
print(a_b)

_ab=20
print(_ab)

Output:
10
20

Example5:


var@3=30
print(var@3)

Error will be displayed

Example6:
myvar=10
print(myvar)
print(MyVar)

Output:
10

Traceback (most recent call last):
  File "E:/python_practice/variables.py", line 29, in <module>
    print(MyVar)
NameError: name 'MyVar' is not defined

Example7:

and=20
print(and)

Output:
invalid syntax

No comments:

Post a Comment