Master Python Variables: Types, Assignment, and Common Pitfalls
This article provides a comprehensive guide to Python variables, covering their characteristics, how to assign and reassign values, multiple assignment techniques, the five built‑in data types, numeric and string operations, list, tuple, dictionary usage, and type conversion functions.
1. Introduction to Python Variable Types
Python variables do not require explicit declaration; you can create a variable simply by assigning a value, and Python automatically determines its type. The built‑in type() function can be used to query a variable’s type. Variables can be reassigned without deletion, and the interpreter allocates memory based on the variable’s data type, allowing storage of integers, floats, or strings.
2. Variable Assignment
Assigning a value to a variable in Python simultaneously declares and defines it. Each variable has an identifier, a name, and associated data. A variable must be assigned before it can be used, and the equal sign ( =) assigns the value on the right to the name on the left.
Example:
The program prints the assigned values for counter, miles, and name.
3. Multiple Variable Assignment
Python allows simultaneous assignment to several variables.
Example:
Three variables share the same integer object, and their memory addresses can be inspected.
Another example assigns distinct objects to multiple variables:
4. Standard Data Types
Python defines five fundamental data types:
Numbers
String
List
Tuple
Dictionary
5. Python Numbers
Numeric types store numeric values and are immutable; modifying a number creates a new object. Numbers are created by assigning a literal value.
The del statement can delete object references:
Python supports four numeric types: int (signed integer), long (long integer, also used for octal/hex), float (floating‑point), and complex (complex numbers).
6. Python Strings
Strings are sequences of characters enclosed in quotes. They support indexing from left to right (starting at 0) and from right to left (starting at -1). Slicing uses the syntax string[start:stop], where the start index is inclusive and the stop index is exclusive.
Example of slicing:
The plus sign ( +) concatenates strings, while the asterisk ( *) repeats them.
7. Python Lists
Lists are mutable sequences defined with square brackets []. They can contain heterogeneous elements, including other lists (nested). List concatenation uses +, and repetition uses *.
Example:
8. Python Tuples
Tuples are immutable sequences defined with parentheses (). Elements are separated by commas, and once created, they cannot be modified.
Example:
9. Python Dictionaries
Dictionaries are unordered collections of key‑value pairs, defined with curly braces {}. Keys are used to retrieve values.
Example:
10. Python Type Conversion
Built‑in conversion functions (e.g., int(), float(), str()) create a new object of the desired type from an existing value.
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.
