Master Python’s Core Data Types: Numbers, Strings, Lists, and More
This article introduces Python’s fundamental data types—including numbers, strings, lists, tuples, sets, and dictionaries—explains their mutability, demonstrates variable assignments, type checking, and common built‑in functions with clear code examples, helping beginners quickly grasp and apply these essentials in their programs.
Fundamental Data Types in Python
Introduction
Hello everyone, when first learning Python you may encounter many difficulties. This article summarizes Python’s basic data types for easy reference and memorization.
Python Data Types
Number (numeric)
String (text)
List
Tuple
Set
Dictionary
Classification
Immutable: Number, String, Tuple
Mutable: List, Dictionary, Set
Variable Operations in Python
Python supports multiple assignment, e.g.:
a = b = c = 1</code>
<code>print(a)</code>
<code>print(b)</code>
<code>print(c)Now a, b, c are all 1.
Multiple assignment with different values:
a, b, c = 1, 2, 3</code>
<code>print(a)</code>
<code>print(b)</code>
<code>print(c)Swap variables without a temporary variable:
a = 1</code>
<code>b = 2</code>
<code>a, b = b, a</code>
<code>print(a)</code>
<code>print(b)Other languages often need a temporary variable:
a = 1 # assign</code>
<code>b = 2 # assign</code>
<code>c = a # store a</code>
<code>a = b # a gets b</code>
<code>b = c # b gets original a</code>
<code>print(a)</code>
<code>print(b)Inspecting Data Types
Use type(x) to view a variable’s type:
a, b, c = 1, True, 1.0</code>
<code>print(type(a), type(b), type(c)) # <class 'int'> <class 'bool'> <class 'float'> a, b, c = '1', 'True', '1.0'</code>
<code>print(type(a), type(b), type(c)) # <class 'str'> <class 'str'> <class 'str'>Number
Conversion examples:
temp = '123' # non‑numeric string will cause error if converted directly</code>
<code>print(type(temp)) # <class 'str'></code>
<code>res = int(temp)</code>
<code>print(type(res), res) # <class 'int'> 123 temp = "b"</code>
<code>res = int(temp, base=16) # specify base, default is 10</code>
<code>print(type(res), res)Comparison examples:
print(1 == True) # True</code>
<code>print(0 == False) # TrueCommon numeric functions:
abs(x) # absolute value</code>
<code>ceil(x) # round up</code>
<code>exp(x) # e to the power x</code>
<code>fabs(x) # absolute value (float)</code>
<code>floor(x) # round down</code>
<code>log(x) # natural logarithm (x > 0)</code>
<code>log10(x) # base‑10 logarithm</code>
<code>max(... ) # maximum value</code>
<code>min(... ) # minimum value</code>
<code>pow(x, y) # x**y</code>
<code>round(x, n) # round to n decimal places</code>
<code>sqrt(x) # square rootString
Length
temp = 'hahahahahahaha'</code>
<code>print(len(temp)) # 14Slicing
# [start:stop:step]</code>
<code>temp = "abcdefg"</code>
<code>res1 = temp[::] # abcdefg</code>
<code>res2 = temp[0:-1:2] # aceFormatting
# default positional formatting</code>
<code>temp1 = "I am {}, I am {} years old!"</code>
<code>res1 = temp1.format('Xianyu', 22)</code>
<code>print(res1) # I am Xianyu, I am 22 years old!</code>
<code># indexed formatting</code>
<code>temp2 = "I am {1}, I am {0} years old!"</code>
<code>res2 = temp2.format('Xianyu', 22)</code>
<code>print(res2) # I am 22, I am Xianyu years old!</code>
<code># named formatting</code>
<code>temp3 = "I am {name}, I am {age} years old!"</code>
<code>res3 = temp3.format(name='Xianyu', age=22)</code>
<code>print(res3) # I am Xianyu, I am 22 years old!find
# find first occurrence, returns index or -1</code>
<code>temp = "hahe"</code>
<code>res = temp.find("h", 1, 3)</code>
<code>print(res)lower
temp = "AAAaaaBBbb"</code>
<code>res = temp.lower()</code>
<code>print(res) # aaaaaabbbbupper
temp = "abc"</code>
<code>res = temp.upper()</code>
<code>print(res) # ABCstrip
# remove leading/trailing whitespace (including newlines and tabs)</code>
<code>temp = " a aa
"</code>
<code>res = temp.strip()</code>
<code>print(res) # a aajoin
# join characters with a separator</code>
<code>temp1 = 'abcd'</code>
<code>temp2 = '-'</code>
<code>res1 = temp2.join(temp1)</code>
<code>print(res1) # a-b-c-d</code>
<code># simple concatenation</code>
<code>temp3 = 'abc'</code>
<code>temp4 = '123'</code>
<code>res3 = temp3 + temp4</code>
<code>print(res3) # abc123replace
# replace substring</code>
<code>temp = 'hahahaha'</code>
<code>res = temp.replace("a", "e", 50)</code>
<code>print(res) # heheheheSigned-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.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
