Variables are containers for storing data values. Here we will discuss about Python Variables.
Name conventions for Variables –
- Variable name cannot start with a number
- Variable name must start with a letter or the underscore character
- Variable name can only contain alpha-numeric characters and underscores
- Variable names are case-sensitive. For example , Firstname and firstname are two different variables.
1. A Variable is a name that is used to refer to memory location.
x = 10 y = "Hello Python" print(x) print(y) Result - 10 Hello Python
2. Variables are declared without any type and can be changed.
x = 10 #variable type is int x = "Hello Python" #now variable type is string,written as str print(x) #It will result Hello Python
Note - String variable can be declared by using single quotes or double quotes.
3. If you want to specify the type of a variable, this can be done with casting.
x = int(10) #here x is 10 x = str(10) #here x is '10' x = float(10) #here x is 10.0
4. Check type of a variable
x = 10 y = "Hello Python" print(type(x)) #here type is int print(type(y)) #here type is str
5. One value can be assigned to multiple variables
x = y = z = 10 print(x) print(y) print(z)
6. Multiple values can be assigned to multiple variables in one line
x , y , z = 10 , 20 , 30 print(x) print(y) print(z)
Hash(#) is used for single line comment in Python. """ This is a comment written in more than just one line """