Variables and Types
Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.
Identifier Naming
Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.
- The first character of the variable must be an alphabet or underscore ( _ ).
- All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore or digit (0-9).
- Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
- Identifier name must not be similar to any keyword defined in the language.
- Identifier names are case sensitive for example my name, and MyName is not the same.
- Examples of valid identifiers : a123, _n, n_9, etc.
- Examples of invalid identifiers: 1a, n%4, n 9, etc.
Declaring Variable and Assigning Values
Python does not bound us to declare variable before using in the application. It allows us to create variable at required time.
We don't need to declare explicitly variable in Python. When we assign any value to the variable that variable is declared automatically.
The equal (=) operator is used to assign value to a variable.
Eg:
a=10
name = 'Manma'
salary = 800000.98
print(a)
print(name)
print(salary)
>>>Output
10
Manma
800000.98
Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement which is also known as multiple assignment.
1. Assigning single value to multiple variables
x=y=z=50
print x
print y
print z
>>>Output
50
50
50
2. Assigning multiple values to multiple variables:
a,b,c=5,10,15
print a
print b
print c
>>>Output
5
10
15
No comments:
Post a Comment