Python Variables

Introduction

 
In this article, I will explain the python variables.
 

Creating variable

 
Variables are boxes for storing facts values. Unlike different programming languages, Python has no command for affirming a variable. A variable is created the instant you first assign a cost to it.
 
Example
  1. x = 5 #int variable  
  2. y = "c# corner" #string variable  
  3. print(x)  
  4. print(y)  
Output
 
Python Variables
 

String

 
String variables use single or double quotes.
 
Example
  1. x = "c# corner is double quotes."#double quotes.  
  2. print(x)  
  3. x = 'c# corner is single quotes.'#single quotes.  
  4. print(x)  
 Output
 
Python Variables

Variables names

 
Variables are used (age, char name, total volume, etc..). Rules for Python variables.
  1. The variable name must start with a letter (a, b, c...).
  2. The variable name is the underscore character(_variable_).
  3. The variable name cannot start with a number (0,1,2, 3...).
  4. The Variable names are case-sensitive. (variable, Variable and VARIABLE are three different variables)
Example
  1. #Legal variable names are displayed in the output screen.  
  2. myvar = "variable name is myvar"  
  3. my_var = "variable name is my_var"  
  4. _my_var = "variable name is _my_var"  
  5. myVar = "variable name is myVar"  
  6. MYVAR = "variable name is MYVAR"  
  7. myvar2 = "variable name is myvar2"  
  8. print(myvar)  
  9. print(my_var)  
  10. print(_my_var)  
  11. print(myVar)  
  12. print(MYVAR)  
  13. print(myvar2)  
  14. #Illegal variable names are not displayed in the output screen.  
  15. #2myvar = "variable name is 2myvar"  
  16. #my-var = "variable name is my-var"  
  17. #my var = "variable name is my var"  
Output
 
Python Variables
 

Multiple variables

 
Python allows one or more variables in one line.
 
Example
  1. a, b, c = "red""yellow""green"#more variables in one line.  
  2. print(a)  
  3. print(b)  
  4. print(c)  
Output
 
Python Variables
 

Multiple variables in one line

 
The same value is used for multiple variables in one line.
 
To combine both string and an int, we use the + character.
 
Example
  1. x = y = z = "green"#The same value is multiple variables.  
  2. print("x value is "+x)#+ character is used to combine two variables.  
  3. print("y value is "+y)  
  4. print("z value is "+z)  
Output
 
Python Variables
 

Global variables

 
Variables created outside of a function are known as global variables. Global variables are used both inside and outside of the function.
 
Example
  1. x = "hello c# corner"#global variables  
  2. def myfunc():  
  3.   print("inside of function " + x)#inside of function  
  4. myfunc()  
  5. print("outside of function " + x)#outside of function  
Output
 
Python Variables
 

How to find variable type

 
In Python, the “type” keyword is used to find the variable type.
 
Example
  1. a = 5  
  2. b ="c# corner"  
  3. c=3.3  
  4. print(type(a))#variable type is displayed in the output screen  
  5. print(type(b))#variable type is displayed in the output screen  
  6. print(type(c))#variable type is displayed in the output screen  
Output
 
Python Variables
 

Conclusion

 
In this article, we have seen python variables. I hope this article was useful to you. Thanks for reading!


Similar Articles