Variable in python


Variable

A variable is a named location used to store data   in the memory. it is helpful to think of variables as a container that holds data which can be changed later during programming.

Example:- number=10

Here we have created a named number to assign value 10 to the variable.

Assigning a value to a variable in python

As you can see from the above example you can use the assignment operator (=) to assign a value to a variable.

Example  

website="apple"

print(website)

Output           

apple

Note :- Python is a type inferred language; It can automatically know that "apple" is a string and declare website as a string.


Changing the value of a variable

we can change the value of variable like this.  

 Example           

website="apple.com"

 print(website)

 # Assigning a new value to variable website

website="google.com"

print(website)

Output        

apple.com

google.com

In the above program, we have assigned apple.com to the website variable initially, then it's value is changed to google.com.

Assigning multiple values to multiple variables

We can assign multiple values to multiple variables at once like this.

Example

a,b,c=5,3.5,"hello"

print(a)

print(b)

print(c)

Output      

5

3.5

hello

NOTE: If we want to assign the same value to multiple variables at once, we can do this also.

Example          

x=y=z="same"

print(x)

print(y)

print(z)

Output     

same

same

same

                                           

No comments:

Post a Comment