Python Variables
Contents
2.4. Python Variables#
Defination: A variable is a container for a value, which can be of various types.
A variable is created the moment you first assign a value to it.
Variable names are case-sensitive.
Variable names can be short (like x and y) or more descriptive (age, carname, total_volume).
2.4.1. Rules for Python variables:#
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
2.4.2. Creating Varibales#
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
# Example
x = 4 # x is of type int
print(x)
4
Python allows you to assign values to multiple variables in one line:
# Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Orange
Banana
Cherry
And you can assign the same value to multiple variables in one line:
# Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Orange
Orange
Orange
To combine both text and a variable, Python uses the + character:
# Example
x = "awesome"
print("Python is " + x)
Python is awesome
You can also use the + character to add a variable to another variable:
# Example
x = "Python is "
y = "awesome"
z = x + y
print(z)
Python is awesome
For numbers, the + character works as a mathematical operator:
# Example
x = 5
y = 10
print(x + y)
15
If you try to combine a string and a number, Python will give you an error:
# Example
x = 5
y = "John"
print(x + y)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [7], line 4
2 x = 5
3 y = "John"
----> 4 print(x + y)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
To add a space between them, add a ” “:
# Example
x = 5
y = "John"
print(x + y)
2.4.3. Casting#
If you want to specify the data type of a variable, this can be done with casting.
# Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
2.4.4. Get the Type#
You can get the data type of a variable with the type() function.
# Example
x = 5
y = "John"
print(type(x))
print(type(y))
<class 'int'>
<class 'str'>
2.4.5. Single or Double Quotes?#
String variables can be declared either by using single or double quotes:
# Example
x = "John"
# is the same as
x = 'John'
2.4.6. Case-Sensitive#
Variable names are case-sensitive.
# Example
a = 4
A = "Sally"
#A will not overwrite a
print(a)
print(A)
4
Sally
2.4.7. Legal variable names#
myvar
my_var
_my_var
myVar
MYVAR
myvar2
# Legal variable names with examples
myvar = "Apple"
print(myvar)
my_var = "Apple"
print(my_var)
_my_var = "Apple"
print(_my_var)
myVar = "Apple"
print(myVar)
MYVAR = "Apple"
print(MYVAR)
myvar2 = "Apple"
print(myvar2)
Apple
Apple
Apple
Apple
Apple
Apple
2.4.8. Illegal variable names#
2myvar
my-var
my var
# Illegal variable names with examples
2myvar = "Apple"
print(2myvar)
Input In [18]
2myvar = "Apple"
^
SyntaxError: invalid syntax
my-var = "Apple"
print(my-var)
Input In [33]
my-var = "Apple"
^
SyntaxError: cannot assign to operator
my var = "Apple"
print(my var)
Input In [41]
my var = "Apple"
^
SyntaxError: invalid syntax
2.4.9. Unpacking a Collection#
Unpacking a collection of values into variables is called unpacking.
# Example
animals = ["Dog", "Cat", "Bird"]
(x, y, z) = animals
print(x)
print(y)
print(z)
Dog
Cat
Bird
2.4.10. Python Output Variables#
Defination: The Python print statement is often used to output variables.
Python uses the print statement to output data to the standard output device (screen).
# Example:
x = "awesome"
print("Python is " + x)
Python is awesome
We can also output multiple variables at the same time
# Example:
x = "Python is "
y = "awesome"
z = x + y
print(x + y)
Python is awesome
2.4.11. Python Global Variables#
Defination: Variables that are created outside of a function (as in all of the examples above) are known as global variables.
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Python is awesome
Global variables can be used by everyone, both inside of functions and outside.
# Example:
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
Python is fantastic
Python is awesome
Defining a global variable inside a function is done by using the global keyword.
# Example:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python is fantastic