Python Variables
Abdulmutalib Idris
Head of IT and Media / Full Stack Developer @ Agro Preciso LTD
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
The following variable names are valid:
thevar = "Aisha"
the_var = "Aisha"
thevar = "Aisha"
THEVAR = "Aisha"
thevar2 = "Aisha"
领英推荐
theVariableName = "Halima"
TheVariableName = "Muhammed"
the_variable_name = "John"
x, y, z = "Rice", "Maize", "Paddy Rice"
print(x)
print(y)
print(z)
x = y = z = "Paddy Rice"
print(x)
print(y)
print(z)
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)
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
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)