← All articles
Python

Day 1: Python Fundamentals — Variables and Data Types

What a Python variable actually is, why dynamic typing catches beginners off guard, and a practical settings loader that converts environment variables safely.

Today covers variables as names, basic types (int, float, str, bool), type(), dynamic typing, and converting types. Tomorrow is lists and tuples.


Post 1: Conceptual Understanding

Hook: Most Python bugs I see from beginners start with one wrong picture: a variable is not a box that holds a value.

In Python, a variable is a name that points to an object. Assignment binds the name to an object. It does not copy anything into a container. This matters once you reassign a variable, because the old object is not changed.

x = 10
y = x
y = y + 5

print(x)         # 10
print(y)         # 15
print(type(x))   # <class 'int'>

x = "ten"
print(type(x))   # <class 'str'>

Here, y = x makes both names point to the same int object with the value 10. y + 5 creates a new int object with the value 15, and y is rebound to it. x still points to 10.

Python also does not attach a type to the name. The object carries the type. The same name x can point to an int first and a str later. This is called dynamic typing. It makes code short, but it also means type mistakes show up at runtime.

A common mistake:

age = "25"
print(age + 5)
# TypeError: can only concatenate str (not "int") to str

Python does not convert "25" to a number for you. You need an explicit conversion with int(age).

Takeaway: Think of variables as labels on objects. Use type() to check what a value actually is when something behaves unexpectedly.

#python #learnpython #programming #softwaredevelopment


Post 2: Practical Application

Almost every app reads its configuration from environment variables, and every value you read arrives as a string.

Here is a small settings loader that reads values from the environment and converts them to the types the rest of the code needs:

import os

def load_settings():
    port = int(os.getenv("APP_PORT", "8000"))
    timeout = float(os.getenv("REQUEST_TIMEOUT", "2.5"))

    raw_debug = os.getenv("APP_DEBUG", "false")
    debug = raw_debug.strip().lower() in {"1", "true", "yes"}

    return {"port": port, "timeout": timeout, "debug": debug}

print(load_settings())

Expected output, assuming none of the three variables are set:

{'port': 8000, 'timeout': 2.5, 'debug': False}

The important parts:

  • os.getenv() always returns a string, or the default you pass in. Note that the defaults here are strings too.
  • int() and float() do the conversion. If APP_PORT is set to "abc", you get a ValueError at startup, which is the behavior you want.
  • The debug flag is parsed by checking against a set of accepted words. I don’t use bool() for this, because bool("false") returns True. Any non-empty string is truthy in Python.

Two ways to handle this:

  1. Convert inline wherever a value is used. This is quick for a short script, but the conversion logic gets repeated, and a bad value fails somewhere deep in the code.
  2. Parse everything once at startup into one dictionary or object, as shown above. The conversion rules live in one place, and bad input fails early with a clear error.

For small scripts, inline conversion is fine. For anything that runs as a service, parse once at startup. Later in this series, we’ll look at libraries that can validate these values for you.

Takeaway: Treat types at the edges of your program, such as config, user input, and network data, as something you convert on purpose. Don’t assume the type you need.

#python #backenddevelopment #programming #softwareengineering

Read more articles