Python Variables, Data Types, Scope, and Type Casting


Python is a dynamically typed language, which means you don’t need to declare the type of variable before using it. This article will walk you through some fundamental concepts such as variable declaration, data types, f-strings, type casting, and the scope of variables.

Variable Declaration and f-String Formatting

You can store different types of values in variables such as strings, integers, floats, and booleans. Python’s f-string syntax makes it easy to embed variables into strings for readable output.

name = "Alex"  # String
age = 10       # Integer
weight = 109.6 # Float
is_resident_of_baltimore = True  # Boolean

print(f"Master {name} is {age} years old and his weight is {weight} lb")
print(f"{name} is the resident of Baltimore, is this assumption correct ?", is_resident_of_baltimore)

Output:
Master Alex is 10 years old and his weight is 109.6 lb
Alex is the resident of Baltimore, is this assumption correct ? True

Checking Data Types Using type()

You can inspect the data type of a variable using Python’s built-in type() function.

print("Data type of name:", type(name))
print("Data type of age:", type(age))
print("Data type of weight:", type(weight))
print("Data type of is_resident_of_baltimore:", type(is_resident_of_baltimore))

Output:
Data type of name: <class ‘str’>
Data type of age: <class ‘int’>
Data type of weight: <class ‘float’>
Data type of is_resident_of_baltimore: <class ‘bool’>

Type Casting (Changing Variable Types)

Python allows you to convert one data type to another using constructor functions like str(), int(), and float().

var1 = 100
print(type(var1), var1)

var2 = str(100)
print(type(var2), var2)

var3 = float(100)
print(type(var3), var3)

var4 = int(50.23)
print(type(var4), var4)

Output:
<class ‘int’> 100

<class ‘str’> 100
<class ‘float’> 100.0
<class ‘int’> 50

Local Scope (Inside a Function)

Variables created inside a function are local and only accessible within that function. They don’t affect variables outside the function with the same name.

person_name = "Peter"

def when_super_hero_mode_is_active():
    person_name = "Spidy"
    print("Name:", person_name)

print("Name:", person_name)
when_super_hero_mode_is_active()
print("Name:", person_name)

Output:
Name: Peter
Name: Spidy
Name: Peter

Even though we change person_name inside the function, it doesn’t affect the global person_name.

Global Scope with global Keyword

To modify a global variable from within a function, use the global keyword.

name_of_woman = "Diana"

def super_heroin_mode_is_on():
    global name_of_woman
    name_of_woman = "Black Widow"
    print("Name:", name_of_woman)

print("Name:", name_of_woman)
super_heroin_mode_is_on()
print("Name:", name_of_woman)

Output:
Name: Diana
Name: Black Widow
Name: Black Widow

Dynamic Typing in Python

Python allows variables to change their data type over time. You can assign a number and later assign a string to the same variable.

x = 5
print(x)

x = "a"
print(x)

Output:
5
a

Variable Names Are Case Sensitive

Python treats uppercase and lowercase letters as different. So y and Y are two distinct variables.

y = 100
Y = "Hello"

print(y)
print(Y)

Output:
100
Hello