Variable Declaration in python
Variable Declaration: A variable is nothing but a name given to a storage area that our programs can manipulate. A Variable is the heart of every good programming language, including Python. Variables are changed time to time, because they have dynamic nature.
A variable is nothing but a reserved memory location to store values. In other words, a variable in a program gives data to the computer to work on. Every value in Python has a data type. Different data types used in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables can be declared by any name or even alphabets like total_amount, Marks, Sname, etc.
These are the following rules of writing variable in python programming-
· Every variable starts with a character, underscore "_" or a upper-case or lower-case letter.
· Always variable must be start with alphabet after that we can use number.
· No commas and blank space (white space) allowed in variable name.
· Keywords are not used as name of the variable.
- Python also uses a variable to store the information.
- Variables can be re-declared even after you have declared it them for once.
- Declare local variable when you want to use, it for current function.
- Declare Global variable when you want to use the same variable for whole of the program.
- To delete variable it uses keyword "del".
Assigning Values to Variables: Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
Total = 50 # An integer assignment
Rate = 100.0 # A floating point assignment
name = "Jaypal" # A string assignment
Multiple Assignment: Python programming language allow, you to declaration of multiple variable and its assign multiple value to the variables.
For Example:
X=y=z=10
Here- x , y and z are the variables with the value 10, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables.
x,y,z = 25,3.5,”Ramakant”
Here, x is integer type variable, y is float type variable and z is string type variable, they are assigned to variables x, y and Z respectively.
For Example: Write a program demonstrate the use of variable.
a = 55 #here a is variable of integer type
b = 35
c = a + b
print("Addition of two Number is=",c)
welcome = "Welcome" #here welcome is string variable
aryan = " Aryan Computers,Barshi"
welcome = welcome + " to " + aryan
print(welcome)
Output:
How you cast one variable type into another. Most of the other variable types you typically would not want to cast to. These are following a few of the important variable types in Python-
int(variable) - casts variable to integer
str(variable) - casts variable to string
float(variable) - casts variable to float (number with decimal)
Comments
Post a Comment