Python Fundamentals: Problem Solving, Strings, Lists, Functions, OOP, and PEP Guidelines
This article introduces Python fundamentals by explaining problem‑solving techniques such as the XY problem, demonstrating string and list operations, showing how to write functions and classes, and emphasizing the importance of following PEP‑8 style conventions for clean code.
Python is a beginner‑friendly language with simple syntax and a rich ecosystem of libraries, making it an ideal choice for learning programming fundamentals.
Problem solving and the XY problem – Effective programmers first identify and articulate the real issue before seeking solutions; the XY problem illustrates how asking about a proposed solution (Y) can obscure the original problem (X).
Example: extracting a file extension can be done by directly slicing the last three characters or by using def extract_ext(filename): return filename[-3:] print(extract_ext('photo_of_sasquatch.png')) # → png or by splitting on the dot: def extract_ext(filename): return filename.split('.')[-1] print(extract_ext('photo_of_sasquatch.png')) # → png
Understanding why code works – Debugging and reading error messages are essential; tools like python tutor or IDE code folding help visualize execution flow.
String manipulation – Strings are sequences of characters that can be indexed or sliced: word = 'supergreat' print(word[0]) # → s print(word[0:5]) # → super
List operations – Lists store heterogeneous items; iterating, filtering, and sorting are common tasks. Example of separating numbers and strings: my_list = ['a','b','n','x',1,2,3,'a','n','b'] number_list = [] string_list = [] for item in my_list: if not isinstance(item, str): number_list.append(item) else: string_list.append(item)
List comprehensions provide concise alternatives: my_list = [letter for letter in my_list if isinstance(letter, str)]
Functions vs. procedures – Functions return values while procedures only perform actions. Example of a simple function that prints each element of a list: def print_list(input_list): for each in input_list: print(each) print()
Parameters (formal arguments) and arguments (actual values) are distinguished when calling functions.
Object‑oriented programming (OOP) – Classes define blueprints for objects. Example class definition: class Student: def __init__(self, name): self._name = name self._subject_list = [] def add_subject(self, subject_name): self._subject_list.append(subject_name) def get_student_data(self): print(f'Student: {self._name} is assigned to:') for subject in self._subject_list: print(subject)
Instances are created with student1 = Student('Martin Aaberge') and can be used across modules via imports.
Respecting PEP‑8 – Following the official style guide (e.g., using snake_case for identifiers) improves code readability and maintainability.
The article also includes links to additional Python resources and a QR‑code promotion for a free Python course.
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.
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.