In python variables are the reserved memory locations used for storing values. In python a built-in id() function returns the address where the variable is stored.
For example:
week = "Week 30"
print(id(week))
Python has no command for declaring a variable. A Python variable is created automatically when you assign a value to it. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable.
age = 52 # Creates an integer variable
distance = 16.2 # Creates a floating point variable
name = "Ttalib" # Creates a string variable
print(age)
print(distance)
print(name)
Casting
If you want to specify the data type of a variable, this can be done with casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Get the Type
You can get the data type of a variable with the in-built type() function.
x = 5
y = "John"
print(type(x)) # <class 'int'>
print(type(y)) # <class 'str'>
Single or Double Quotes?
String variables can be declared either by using single or double quotes:
x = "John"
# is the same as
x = 'John'
print(x)
Case-Sensitive
Variable names are case-sensitive.
a = 4
A = "Sally"
#A will not overwrite a
print(a)
print(A)
How to Name Your Variable
The following rules must be followed assigning names to 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)
- A variable name cannot be any of the Python keywords.
The following variable names are valid:
thevar = "Aisha"
the_var = "Aisha"
thevar = "Aisha"
THEVAR = "Aisha"
thevar2 = "Aisha"
Techniques For Multi-Words Variable Names
- Camel Case: Each word, except the first, starts with a capital letter:
theVariableName = "Halima"
- Pascal Case: Each word starts with a capital letter:
TheVariableName = "Muhammed"
- Snake Case: Each word is separated by an underscore character:
the_variable_name = "John"
Assigning Multiple Values To variables
- Assign Many Values to Multiple Variables
x, y, z = "Rice", "Maize", "Paddy Rice"
print(x)
print(y)
print(z)
- Assign One Value to Multiple Variables
x = y = z = "Paddy Rice"
print(x)
print(y)
print(z)
- Unpack a Collection
If you have a collection of values in a list. Python allows you to extract the values into variables. This is called unpacking.
commodity = ["Rice", "Maize", "Paddy Rice", "Cocoa"]
w, x, y, z = commodity
print(w)
print(x)
print(y)
print(z)
Global & Local Variables
Variables that are created outside of a function are global variable while those created inside of function are local variables (unless is created using the global keyword). Global variables can be used by everyone, both inside of functions and outside while local variables can only be used inside the function.
x = "Abdulmutalib"
def nameFunc():
print("My name is " + x)
nameFunc()
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
x = "Abdulmutalib"
def nameFunc():
x = "Muhammed"
print("My name is " + x)
nameFunc()
print("My name is " + x)
The global Keyword
To create a global variable inside a function, you can use the global keyword.
def nameFunc():
global x
x = "Abdulmutalib"
nameFunc()
print("My name is " + x)
Also, use the global keyword if you want to change a global variable inside a function.
x = "Abdulmutalib"
def myfunc():
global x
x = "Muhammed"
myfunc()
print("My name is " + x)
Comments