Fundamentals 10 min read

Master Python Variables and Data Types in Just 5 Minutes

This article explains how Python stores variables in memory, demonstrates single and multiple assignments, outlines the five built‑in data types (numbers, strings, lists, tuples, dictionaries), and provides conversion functions with clear code examples for beginners.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Variables and Data Types in Just 5 Minutes

Variable Storage in Memory

When a variable is created, Python allocates a space in memory to hold its value. The interpreter decides the amount of memory based on the variable's data type, allowing the variable to store integers, floats, or characters.

Variable Assignment

Python does not require explicit type declarations. Each variable in memory has an identifier, a name, and the stored data. A variable must be assigned a value before it can be used; the equal sign (=) assigns the value on the right to the variable on the left.

Example:

counter = 100
miles = 1000.0
name = "John"

Running this code outputs:

100
1000.0
John

Multiple Variable Assignment

Python allows assigning the same value to several variables at once: a = b = c = 1 This creates a single integer object with value 1 and lets three variables reference the same memory location.

You can also assign different values to multiple variables in one statement: a, b, c = 1, 2, "john" Here, two integer objects (1 and 2) are assigned to a and b, and the string "john" is assigned to c.

Standard Data Types

Numbers

String

List

Tuple

Dictionary

Python Numbers

Numeric types store numeric values and are immutable; modifying a number creates a new object. You can create numbers directly:

var1 = 1
var2 = 10

Python supports four numeric types:

int (signed integer)

long (long integer, also used for octal and hexadecimal)

float (floating‑point number)

complex (complex number)

You can delete references to objects with the del statement:

del var1
del var_a, var_b

Python Strings

A string is a sequence of characters, digits, or underscores, written as s = "a1a2...an". Strings support slicing using [start:stop], where indices start at 0 (or -1 from the end). Example:

s = 'ilovepython'
print(s[1:5])  # outputs: love

The plus sign ( +) concatenates strings, and the asterisk ( *) repeats them.

Python Lists

Lists are mutable sequences that can contain numbers, strings, or even other lists (nested). They are defined with square brackets []. List slicing works the same way as string slicing.

Example:

my_list = [1, 2, 3, 4]
print(my_list[1:3])  # outputs: [2, 3]

The plus sign concatenates lists, and the asterisk repeats them.

Python Tuples

Tuples are ordered collections defined with parentheses (). Elements are separated by commas. Unlike lists, tuples are immutable (read‑only).

Example output (illustrative):

(1, 2, "john")

Python Dictionaries

Dictionaries are unordered collections of key‑value pairs, defined with curly braces {}. Keys are used to retrieve values, unlike list indices.

Example:

person = {"name": "John", "age": 30}
print(person["name"])  # outputs: John

Data Type Conversion Functions

int(x [,base])

: Convert x to an integer. long(x [,base]): Convert x to a long integer. float(x): Convert x to a floating‑point number. complex(real [,imag]): Create a complex number. str(x): Convert x to a string. repr(x): Convert x to a string representation. eval(str): Evaluate a Python expression contained in a string. tuple(s): Convert sequence s to a tuple. list(s): Convert sequence s to a list. set(s): Convert s to a mutable set. dict(d): Create a dictionary from a sequence of (key, value) pairs. frozenset(s): Convert s to an immutable set. chr(x): Convert an integer to a character. unichr(x): Convert an integer to a Unicode character. ord(x): Convert a character to its integer Unicode code point. hex(x): Convert an integer to a hexadecimal string. oct(x): Convert an integer to an octal string.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData TypesVariables
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.