Hintor
Python Basics for Programmers

Variables and Dynamic Typing

Python for Developers/Python Basics for Programmers/

Variables and Dynamic Typing

Python

Lesson explanation

Variables and Dynamic Typing


🎯 Learning Objective

After this lesson, you will be able to:

  • Assign and reassign values without type declarations.
  • Read dynamic types with type() and avoid changing a variable's meaning carelessly.
  • Convert numeric text with int() and float().
  • Use multiple assignment and Python-style names clearly.

Short Explanation

Python variables are names that refer to values. You do not declare them with String, int, let, or const:

Python
service_name = "billing-api"
worker_count = 6

The value determines the current type, and type() lets you inspect it. A name can later refer to another type, but that flexibility should not make the code unpredictable. Reassign a variable when its value changes; use a new name when its meaning changes.

Configuration files, forms, and environment variables often provide numbers as text. Convert them before numeric work:

Python
max_workers = int("8")
timeout_seconds = float("2.5")

Python also supports multiple assignment. You can assign several values in one line or exchange two values without a temporary variable.

Use descriptive snake_case names such as request_timeout, not Java-style declarations or vague names such as x1.

💡 Tip: int() accepts whole-number text such as "42". Use float() when the text may contain a decimal point.


Clean Code Examples

1. Assign values and inspect their types

Python infers the type from each value.

Python
region = "eu-central"
replicas = 3

print(type(region))
print(type(replicas))

Output:

TEXT
<class 'str'>
<class 'int'>

2. Convert imported numeric text

Convert external text before treating it as a number.

Python
timeout_text = "1.75"
timeout_seconds = float(timeout_text)

print(f"Timeout: {timeout_seconds:.2f}s")

Output:

TEXT
Timeout: 1.75s

3. Reassign a changing state

Reassignment keeps the same name and gives it a new value.

Python
build_state = "queued"
build_state = "running"

print(f"State: {build_state}")

Output:

TEXT
State: running

4. Assign and exchange multiple values

Python can unpack values and exchange them in one statement.

Python
primary_host, backup_host = "db-a", "db-b"
primary_host, backup_host = backup_host, primary_host

print(f"Primary: {primary_host}")
print(f"Backup: {backup_host}")

Output:

TEXT
Primary: db-b
Backup: db-a


⚠️ Common Mistakes

  1. Do not write declarations such as String name = ... or let count = ....
  2. Do not expect "8" to behave like the number 8 until it is converted.
  3. Do not repeatedly change one variable between unrelated meanings just because Python allows it.
  4. Do not unpack a different number of values than the number of target names.
Start Practice

Python for Developers

Variables and Dynamic Typing

4 tasks
Lesson progress0 / 4 tasks completed

Tasks

Practice progress

Convert an imported batch size

View task

Complete the conversion so the imported batch size is stored as a number.

Requirements

  • Fill only the missing function name.
  • Keep the existing value unchanged.
  • Print exactly the expected output.

Expected Output

TEXT
Batch size: 250

Expected output

Batch size: 250
Code: PythonTask 1

Convert an imported batch size

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3
4

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 4 tasks completed

Previous lessonBack to unitBack to track
GoalPython
Assign a valuename = value
Inspect the current typetype(value)
Convert whole-number textint(text)
Convert decimal textfloat(text)
Assign several valuesa, b = value_a, value_b
Exchange two valuesa, b = b, a