Newby Coder header banner

Python Variables

What are Variables in Python?

A variable is a name (or identifier) for a value (or object), which can vary (get changed)

Value of a variable can be a literal(raw Python object) or an object

For example, in the statement name="Derp", name is a variable whose value is assigned a string literal "Derp"

This value can be changed, with statement like name="June"

Literals

Literal is Python's raw data which can be assigned to a variable or used as it is

Literals can be numeric literals like 12, 56.13 , or string literals which can contain words or sentences like "word" "more than a word" etc

If a phrase like "hold my beer"(which is a string literal) is to be displayed to screen then it can directly be used in a print() method as

print("hold my beer")

This literal can also be assigned to a variable which can be used to do the same

what_you_gotta_do = "hold my beer"
print(what_you_gotta_do)

Both examples result in the string getting printed in the screen, when executed

In above example, what_you_gotta_do is a variable which can be changed to some other literal like

what_you_gotta_do = "crack no bottles"
what_you_gotta_do = ["take a number", 13, "give me my number back"]

Following are different types of literals in Python :

Numeric Literals

Numeric literals can belong to 3 different numerical types: Integer, Float, and Complex

Numeric Literals are immutable (unchangeable), though a variable holding the number can be assigned to something else

Examples

bin_a = 0b1001 #Binary Literal
dec_a = 34 #Decimal Literal
hex_a = 0x3a2 #Hexadecimal Literal
float_b = 10.57

String literals

A string literal is a sequence of characters inside quotes (which can be single, double or triple quotes)

Strings are also immutable

empty_a = '' #Empty string
char_a = 'c' #String with single character
str_a = "a string"
multiline_a = """a multiline
    string"""

Boolean literal

A boolean literal is either of True or False

Numerical equivalent of True is 1 and False is 0

Although, when used in a condition, any number except zero or a non-empty string evaluates to True

None

None is a literal used to denote something of no value or type

That mentioned, 0 is not None and empty objects are not None since they have a type

print(None)
print("")
print(0==None)

Output

None

False

Literal Collections

There are four different type of collections of literals, which can contain multiple literals :

Collections like List, Tuple and Dict and also hold other collections

whisky = ["red label", "blue label", "black label"] #list
prime_numbers = (2, 3, 5, 7, 11) #tuple
a_dictionary = {'pi': 3.14, 'twinkle twinkle': 'little star'} # dict
vowels = {'a', 'e', 'i' , 'o', 'u'} #set
Check Python Datatypes Python Sets

Python variables

Variables are identifiers which are used to identify literals or objects assigned to them

Since most things in Python are objects, literals are also objects

Python Identifiers Python Objects

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory

A variable can be considered as the object itself, which is assigned to it, until it is assigned to some other object

Python Datatype

Assigning Values to Variables

Values are assigned to a variable by using an equal sign(or operator) =

Python handles memory allocation automatically

#!/usr/bin/python
name     = "Peter"       # string
age      = 22            # integer
average_marks   = 65.13         # float
subjects = ["Physics", "Chemistry", "Mathematics"] #list

print name
print age
print average_marks
print subjects 

Output

Peter
22
65.13
["Physics", "Chemistry", "Mathematics"] 

Python is a dynamically typed language, which means that a given variable can be bound to values of different types

Assigning same value to multiple variables

Python allows to assign a single value to several variables simultaneously

For example

x = yz = xyz = 1 

Here, three variables are assigned to the same memory location of integer object 1

Assigning different value to multiple variables

Multiple objects can be assigned to multiple variables in same statement

>>> n, price, item = 1, 2.55, "Black Pen"
>>> n
1
>>> price
2.55
>>> item
'Black Pen' 

Here, variable n is assigned with value of integer 1, variable price is assigned with value 2.55 and a string "Black Pen" is assigned to variable item

Delete a variable in Python

A variable can be deleted with del keyword like del variable_name

it = "what is it?"
what_it_holding = "my bottle"
del it

Above example deletes variable it