Fundamentals 3 min read

Common Python Data Types and Their Usage

This article introduces Python’s fundamental data types—including integers, floats, booleans, and strings—explaining their characteristics, representation formats, common pitfalls such as floating‑point precision, and provides concise code examples illustrating each type’s usage in Python.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Common Python Data Types and Their Usage

Data types are essential attributes of any programming language; assigning explicit types enables computers to process data correctly. This article outlines the most commonly used Python data types.

Integer (int) : Represents whole numbers in decimal, binary (prefix 0b ), octal (prefix 0o ), and hexadecimal (prefix 0x ) formats. Example:

print('十进制', 118) print('二进制', 0b10101111) print('八进制', 0o176) print('十六进制', 0x1EA3)

Floating‑point (float) : Consists of an integer part and a fractional part, but suffers from precision issues. Direct addition may yield unexpected results, e.g., n1 = 1.1 n2 = 2.2 print(n1 + n2) # 3.3000000000000003 . Using the decimal module resolves this:

from decimal import Decimal print(Decimal('1.1') + Decimal('2.2')) # 3.3

Boolean (bool) : Represents truth values True and False (capitalized). Booleans can be cast to integers ( True → 1 , False → 0 ). Example:

f1 = True f2 = False print(int(f1)) # 1 print(int(f2)) # 0

String (str) : Immutable sequence of characters. Can be defined with single quotes, double quotes, or triple quotes for multi‑line strings. Example:

str1 = '我还年轻,吃苦趁现在' str2 = "我还年轻,吃苦趁现在" str3 = """我还年轻,吃苦趁现在"""

pythondata typesintString()boolfloat
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login 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.