Master Python Variables and Data Types: A Complete Beginner’s Guide
This article explains how Python stores variables in memory, assigns values without type declarations, supports multiple assignments, and covers the five standard data types—numbers, strings, lists, tuples, and dictionaries—along with type conversion functions and practical code examples.
Variable Storage and Assignment
Variables are stored in memory; creating a variable allocates space. Python assigns values without type declarations. The equals sign (=) assigns the value on the right to the variable name on the left. A variable must be assigned before use.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
counter = 100 # integer
miles = 1000.0 # float
name = "John" # string
print counter
print miles
print nameMultiple assignment is supported:
a = b = c = 1
a, b, c = 1, 2, "john"Standard Data Types
Python defines five standard data types: Numbers, Strings, Lists, Tuples, Dictionaries.
Numbers
Numbers are immutable; assigning creates a new object. Python supports int, long, float, complex. Examples:
int: 10, 100, -786, 0x69
long: 51924361L, -0x19323L, 0122L
float: 0.0, 15.20, -21.9, 32.3+e18
complex: 3.14j, 45.j, 9.322e-36j
Objects can be deleted with the del statement, e.g., del var or del var_a, var_b.
Strings
Strings are sequences of characters. They can be indexed from left (0) or right (-1). Slicing s[ start : end ] extracts substrings. Concatenation uses +, repetition uses *.
s = "ilovepython"
print s[1:5] # loveLists
Lists are mutable ordered collections, defined with brackets []. They support indexing, slicing, concatenation (+) and repetition (*).
mylist = ["runoob", 786, 2.23, "john", 70.2]
print mylist[0] # runoob
print mylist[1:3] # [786, 2.23]Tuples
Tuples are immutable ordered collections, defined with parentheses (). They cannot be reassigned.
mytuple = ("runoob", 786, 2.23, "john", 70.2)
print mytuple[0] # runoobDictionaries
Dictionaries are unordered key‑value mappings, defined with braces {}. Keys are used for access.
person = {"dept": "sales", "code": 6734, "name": "john"}
print person["name"] # johnData Type Conversion
Built‑in functions convert between types, returning new objects. Common functions include int(), long(), float(), complex(), str(), repr(), eval(), tuple(), list(), set(), dict(), chr(), ord(), hex(), oct().
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.
