Understanding Python Variables
Ohidur Rahman Bappy
MAR 22, 2025
Understanding Python Variables
Variables are reserved memory locations to store values. Creating a variable reserves space in memory.
Assigning Values to Variables
Python variables do not require explicit declaration. Assignment occurs automatically. Use the equal sign (=
) to assign values.
The left of =
is the variable name, the right is the stored value. For example:
#!/usr/bin/python3
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print(counter)
print(miles)
print(name)
Multiple Assignment
Assign a single value to multiple variables:
a = b = c = 1
Assign multiple objects to multiple variables:
a, b, c = 1, 2, "john"
Standard Data Types
Data in memory can be of many types, such as numeric or alphanumeric. Python supports:
- Numbers
- String
- List
- Tuple
- Dictionary
Python Numbers
Number data types store numeric values, created upon assignment:
var1 = 1
var2 = 10
Delete number objects using the del
statement:
del var1, var2
Python supports:
int
: integersfloat
: floating-point valuescomplex
: complex numbers (x + yj
)
Python Strings
Strings are character sets enclosed in quotes. Use the slice operator to obtain sub-strings:
#!/usr/bin/python3
str = 'Hello World!'
print(str) # Prints complete string
print(str[0]) # Prints first character
print(str[2:5]) # Prints from 3rd to 5th
print(str[2:]) # Prints from 3rd char
print(str * 2) # Prints twice
print(str + "TEST") # Concatenates
Python Lists
Lists contain items separated by commas and enclosed in brackets. Items can be of different types:
#!/usr/bin/python3
list = ['abcd', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john']
print(list) # Complete list
print(list[0]) # First element
print(list[1:3]) # From 2nd to 3rd
print(list[2:]) # From 3rd
print(tinylist * 2) # Twice
dprint(list + tinylist) # Concatenated
Python Tuples
Tuples, similar to lists, are enclosed in parentheses and immutable:
#!/usr/bin/python3
tuple = ('abcd', 786, 2.23, 'john', 70.2)
tinytuple = (123, 'john')
print(tuple) # Complete tuple
print(tuple[0]) # First element
print(tuple[1:3]) # From 2nd to 3rd
print(tuple[2:]) # From 3rd
print(tinytuple * 2) # Twice
dprint(tuple + tinytuple) # Concatenated
Python Dictionary
Dictionaries are key-value pairs enclosed in braces:
#!/usr/bin/python3
mydict = {}
mydict['one'] = "This is one"
mydict[2] = "This is two"
tinydict = {'name': 'john', 'code': 6734, 'dept': 'sales'}
print(mydict['one']) # For 'one' key
print(mydict[2]) # For 2 key
print(tinydict) # Complete dictionary
print(tinydict.keys()) # All keys
print(tinydict.values()) # All values
Data Type Conversion
Convert between types using functions:
int(x [,base])
: To integer.float(x)
: To float.complex(real [,imag])
: To complex.str(x)
: To string.repr(x)
: To expression string.eval(str)
: Evaluate string.tuple(s)
: To tuple.list(s)
: To list.set(s)
: To set.dict(d)
: To dictionary.frozenset(s)
: To frozen set.chr(x)
: Integer to character.ord(x)
: Character to integer.hex(x)
: To hexadecimal.oct(x)
: To octal.