← Back to Index

Python Programming: Data Types, Variables, Scope & Practical Guide

A complete technical reference guide covering core Python syntax, variable manipulation, and scope fundamentals.

4. Data Types & Examples

Python has several built-in data types used to define various operations and storage behaviors.

Data Type Description Example Code Output
int Integer numbers a = 25
print(type(a))
<class 'int'>
25
float Decimal numbers b = 3.14
print(type(b))
<class 'float'>
3.14
str Text / String s = "Hello"
print(type(s))
<class 'str'>
Hello
bool True or False flag = False
print(type(flag))
<class 'bool'>
False
list Ordered collection (mutable) lst = [1, 2, 3]
print(type(lst))
<class 'list'>
[1, 2, 3]
tuple Ordered collection (immutable) tup = (1, 2, 3)
print(type(tup))
<class 'tuple'>
(1, 2, 3)
dict Key-Value pairs d = {"a": 1, "b": 2}
print(type(d))
<class 'dict'>
{'a': 1, 'b': 2}

5. Assigning Multiple Variables

Python allows flexible assignment patterns for initializing and manipulating values across multiple variables simultaneously.

Assign same value to multiple variables:

a = b = c = 100
print(a, b, c)  # Output: 100 100 100

Assign different values to multiple variables:

a, b, c = 10, 20, 30
print(a, b, c)  # Output: 10 20 30

Swap two variables:

a, b = 5, 10
a, b = b, a
print(a, b)  # Output: 10 5

6. Variable Scope

Scope determines the visibility and lifecycle of variables within your code blocks.

x = "Global"  # Global variable

def func():
    y = "Local"  # Local variable
    print("Inside function, y =", y)

func()
# print(y)  # Error! y is not accessible outside the function
print("Outside function, x =", x)

Output:

Inside function, y = Local
Outside function, x = Global

7. Type Conversion

You can explicitly convert data from one type to another using built-in casting functions.

x = "100"
print(int(x) + 50)  # Output: 150

y = 3.99
print(int(y))       # Output: 3

z = 10
print(str(z) + " is number")  # Output: 10 is number

flag = "True"
print(bool(flag))   # Output: True

Common Type Conversion Functions

8. Constants (By Convention)

PI = 3.14159     # Constant (by convention - use UPPERCASE)
MAX_USERS = 100
Note: Python does not enforce true immutable constants syntactically, but developers use UPPERCASE naming conventions to indicate values that should remain constant.

9. Delete Variables

You can remove variables from memory using the del keyword.

a = 10
print(a)  # Output: 10

del a     # Delete variable reference
# print(a)  # Error: name 'a' is not defined

10. Check if Variable Exists

You can verify whether a variable has been defined in the global scope using globals().

x = 50

if 'x' in globals():
    print("x exists")
else:
    print("x does not exist")

Output:

x exists

11. Practical Example

Bringing concepts together with basic variable declarations and formatted strings (f-strings):

name = "John"
age = 25
is_emp = True
salary = 55000.75

print(f"Name: {name}")
print(f"Age: {age}")
print(f"Employed: {is_emp}")
print(f"Salary: {salary}")

Output:

Name: John
Age: 25
Employed: True
Salary: 55000.75