Master Python Variables and Data Types: From Assignment to Conversion
This article explains how Python stores variables in memory, covers variable assignment, multiple assignments, the five standard data types (numbers, strings, lists, tuples, dictionaries), and shows how to convert between types using built‑in functions, illustrated with code examples and output images.
Variable Assignment
In Python, variable assignment does not require type declaration. Each variable created in memory includes an identifier, name, and data. A variable must be assigned before use; the equal sign (=) assigns a value to the variable, with the left side being the variable name and the right side the stored value.
Example:
counter = 100 miles = 1000.0 name = "John"Running the program outputs:
100 1000.0 JohnMultiple Variable Assignment
Python allows assigning the same value to several variables simultaneously: a = b = c = 1 This creates a single integer object with value 1, and all three variables reference the same memory.
Different values can also be assigned to multiple variables: a, b, c = 1, 2, "john" Here, integers 1 and 2 are assigned to a and b, and the string "john" to c.
Standard Data Types
Python defines five standard data types for storing various kinds of data:
Numbers
String
List
Tuple
Dictionary
Numbers
Numeric types store numeric values and are immutable; changing a number creates a new object.
Creating numbers:
var1 = 1 var2 = 10Delete references with del:
del var1 del var_a, var_bPython supports four numeric types:
int (signed integer)
long (long integer, also represents octal and hexadecimal)
float (floating‑point)
complex (complex number)
Strings
A string is a sequence of characters, numbers, or underscores, written as s = "a1a2...an". Substrings can be obtained using slicing s[start:end], where indices may be positive or negative.
Example:
s = 'ilovepython' s[1:5]yields love.
Concatenation uses + and repetition uses *.
Lists
Lists are the most frequently used mutable sequence type, defined with square brackets [ ]. They can contain heterogeneous elements, including other lists.
List slicing works like string slicing: list[start:end]. Concatenation uses + and repetition uses *.
Tuples
Tuples are immutable sequences defined with parentheses ( ). Elements are separated by commas. Because they cannot be modified, they are often used for read‑only data.
Dictionaries
Dictionaries are unordered collections of key‑value pairs, defined with braces { }. Keys are used for lookup instead of numeric indices.
Data Type Conversion
Built‑in functions can convert between types, returning a new object: 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): Return a string representation of x. eval(str): Evaluate a Python expression contained in a string. tuple(s): Convert a sequence to a tuple. list(s): Convert a sequence to a list. set(s): Convert to a mutable set. dict(d): Create a dictionary from a sequence of (key, value) pairs. frozenset(s): Convert 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 code point. hex(x): Convert an integer to a hexadecimal string. oct(x): Convert an integer to an octal string.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
