Python Variables
Variables are data storage containers.
Creating Variables
Python doesn't have a commanding for declaring variables.
When you first assign a value to variable, it is created.
Example 1 :-
x = 5
y = "John"
print(x)
print(y)
Output :-
John
Variables with no particular type must be declared, and after they are specified they can even change type.
Example 2 :-
x = 4
x = "Sally"
print(x)
Output :-
Casting
Casting is a technique for specifying the data type of a variable.
Example :-
x = str(3)
y = int(3)
z = float(3)
print(x)
print(y)
print(z)
Output :-
3
3.0
Get the Type
The type()
function can be used to determine the data type of a variable.
Example :-
x = 5
y = "John"
print(type(x))
print(type(y))
Output :-
<class 'str'>
Single or Double Quotes?
Example :- Single or double quotes are used to declare string variables :
x = "John"
print(x)
#double quotes are the same as single quotes:
x = 'John'
print(x)
Output :-
John
Case-Sensitive
The case-sensitive is important in variable names.
Example :- This will result in the following two variables :
a = 4
A = "Sally"
print(a)
print(A)
Output :-
Sally
Related Links
Variable Names and Naming Rules
A variable will have a short name (such as x
and y
) or a longer name (such as age, carname, or total volume). Python variables have rules :
- A variable name must begin with a letter or an underscore.
- A number cannot be the first character in a variable name.
- Only alpha-numeric characters and underscores (A-z, 0-9, and_ ) are used in variable names.
- Case matters when naming variables (age, Age and AGE three distinct variables).
Example :- A few variable names are :
# Valid Variable Names
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
# Invalid Variable Names
2myvar = "John"
my-var = "John"
my var = "John"
Note:- Always keep in mind that variable names are case sensitive.
Multi Words Variable Names
It can be hard to read names that contain more than one word.
If you want to make them more readable, you can use a variety of methods.
Camel Case
A capital letter appears at the beginning of every word except first one :
myVariableName = "John"
Pascal Case
A capital letter is used to begin every word :
MyVariableName = "John"
Snake Case
The underscore character is used to divide each word in a variable name :
my_variable_name = "John"
Python Variables - Assign Multiple Values
You can declare multiple variable and define their value at once in many ways.
Many Values to Multiple Variables
Example :- In Python, you can assign values to multiple variables in a single line by using a function :
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Output :-
Banana
Cherry
Note :- You'll get an error if the number of variables doesn't equal the number of values.
One Value to Multiple Variables
Example :- It's also possible to assign the same value to many variables in a single line :
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output :-
Orange
Orange
Unpack a Collection
Python allows you to extract values from a list, tuple, etc. Unpacking is the process of removing the contents of a package.
Example :-
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Output :-
banana
cherry
Related Links
Output Variables
print
statements are commonly used to output variables in Python.
Example 1 :- Python +
character is used to combine text and a variable :
x = "awesome"
print("Python is " + x)
Output :-
Example 2 :- Add a variable to another by using the +
symbol :
x = "Python is "
y = "awesome"
z = x + y
print(z)
Output :-
Example 3 :- The +
character is a mathematical operator for numbers :
x = 5
y = 10
print(x + y)
Output :-
Example 4 :- Python will throw an error if you try to combine a string and a number :
x = 5
y = "John"
print(x + y)
Output :-
Global Variables
Global variables are variables that are created outside of a function (as in the examples above).
Everybody, both within and outside of functions, can use global variables.
Example 1 :- Create a variable outside of a function and utilise it within the function :
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Output :-
Creating a variable with the same name inside a function makes it local, meaning it may only be used within the function. There will be no change to the original global variable with the same name.
Example 2 :- Create a variable with the same name as the global variable within a function :
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
Output :-
Python is awesome
The global Keyword
A variable created within a function is normally local and can only be utilised within that function.
The global
keyword can be used to define a global variable within a function.
Example 1 :- The variable is in the global scope if you use the global
keyword :
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Output :-
If you want to alter a global variable within a function, use the global
keyword.
Example 2 :- Use the global
keyword to modify the value of a global variable within a function :
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Output :-