Fundamentals 12 min read

Comprehensive Python Tutorial: Data Types, Containers, Strings, and Custom Classes

This tutorial walks through Python's core data types—including numeric, container, and string objects—demonstrates common operations and examples such as hexadecimal literals, list manipulation, 99 multiplication table, regex password checks, and shows how to define custom classes, attributes, methods, and properties for clean, object‑oriented code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Comprehensive Python Tutorial: Data Types, Containers, Strings, and Custom Classes

Learning Python is a continuous process; this article reinforces the four major data types and demonstrates practical examples.

Numeric Types

Python treats all data as objects; common numeric objects are int , float , and bool .

<code>0xa5  # equals decimal 165</code>
<code>1.05e3  # 1050.0</code>

Container Types

Containers can hold multiple elements: list , tuple , dict , and set .

<code>lst = [1, 3, 5]  # list variable</code>
<code>tup = (1, 3, 5)  # tuple variable</code>
<code>dic = {'a': 1, 'b': 3, 'c': 5}  # dict variable</code>
<code>s = {1, 3, 5}  # set variable</code>

Examples include removing the smallest and largest values from a list and computing the mean, printing a 99‑multiplication table, and random sampling from a list.

<code>def score_mean(lst):
    lst.sort()
    lst2 = lst[1:-1]
    return round(sum(lst2) / len(lst2), 1)

lst = [9.1, 9.0, 8.1, 9.7, 19, 8.2, 8.6, 9.8]
score_mean(lst)  # 9.1</code>
<code>for i in range(1, 10):
    for j in range(1, i+1):
        print('%d*%d=%d' % (j, i, j*i), end='\t')
    print()</code>
<code>from random import randint, sample
lst = [randint(0, 50) for _ in range(100)]
lst_sample = sample(lst, 10)</code>

String Operations

Common string methods include strip , replace , join , title , and find .

<code>'  I love python\t\n  '.strip()  # 'I love python'</code>
<code>'i love python'.replace(' ', '_')  # 'i_love_python'</code>
<code>'_'.join(['book', 'store', 'count'])  # 'book_store_count'</code>
<code>'i love python'.title()  # 'I Love Python'</code>
<code>'i love python'.find('python')  # 7</code>

To check if one string is a rotation of another, test whether str1 is a substring of str2+str2 :

<code>def is_rotation(s1: str, s2: str) -> bool:
    if s1 is None or s2 is None:
        return False
    if len(s1) != len(s2):
        return False
    def is_substring(a, b):
        return a in b
    return is_substring(s1, s2 + s2)</code>

Regular‑expression password validation (6‑20 alphanumeric characters, no underscore):

<code>import re
pat = re.compile(r'[\da-zA-Z]{6,20}')
pat.fullmatch('n0passw0Rd')  # matches</code>

Custom Types

Define a class with class , use self for instance attributes, and explore magic methods.

<code>class Dog(object):
    pass

wangwang = Dog()</code>

Adding an __init__ method creates attributes:

<code>def __init__(self, name, dtype):
    self.name = name
    self.dtype = dtype</code>

Define methods such as shout :

<code>def shout(self):
    print("I'm %s, type: %s" % (self.name, self.dtype))</code>

Private attributes are prefixed with __ ; they can be accessed via property decorators for read‑only or read‑write behavior.

<code>class Book(object):
    def __init__(self, name, sale):
        self.__name = name
        self.__sale = sale
    @property
    def name(self):
        return self.__name
    @name.setter
    def name(self, new_name):
        self.__name = new_name</code>

Using property makes name behave like a regular attribute while controlling access.

- END -

PythonData Typesprogramming fundamentalsClassesstrings
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.