Python Basics: Number Reversal, Docstrings, Encoding, String Rotation, Progress Bar, File Output, and List Merging
This article presents a series of Python fundamentals tutorials, covering number reversal, adding class docstrings, setting file encoding, rotating strings, implementing a console progress bar, redirecting print output to files, and merging two lists with sorting, each accompanied by concise code examples.
The article encourages daily Python practice, offering short coding exercises to reinforce basic programming skills.
Reverse Number – Convert a three‑digit integer (e.g., 789) to its reversed form (987) by extracting hundreds, tens, and units. The solution is demonstrated with the following function:
def reverse_number(number):
baiwei = int(number/100)
shiwei = int(number%100/10)
gewei = int(number%10)
return gewei*100 + shiwei*10 + baiwei
new_number = reverse_number(789)
print(new_number)Class Docstring – Add documentation to a newly created class using triple quotes. The docstring can be accessed via ClassName.__doc__ :
class My_Class(object):
"""你好"""
print(My_Class.__doc__)Setting Python File Encoding – Python source files default to UTF‑8, but an explicit encoding declaration can be added on the first or second line using a comment that matches the regular expression coding[=:]\s*([-\w.]+) . Examples:
# -*- coding:utf-8 -*-
# -*- coding:UTF-8 -*-Rotate String – Given a string and an integer offset, rotate the string accordingly (e.g., abcde with offset 3 becomes cdeab ). The implementation uses slicing:
def reverse_str(my_str, offset):
if offset == 0:
return my_str
left = my_str[:len(my_str)-offset]
right = my_str[len(my_str)-offset:]
return right + leftConsole Progress Bar – Display a progress bar in the console by formatting a string with completed ( > ) and remaining ( / ) characters, using ljust() for padding. A loop updates the bar in real time:
# Print a line of progress symbols
progress_str = ">" * 100
print(progress_str)
import time
for i in range(0, 11):
time.sleep(0.3)
current = i/10
progress_str = '{0:s}{1:.0%}'.format((int(current*10)*'>').ljust(10, '/'), current)
print(f'\r{progress_str}', end='')Redirecting print to a File – Use the file parameter of print() to write output directly to a file instead of the console:
file = open('runtime.log', 'a+', encoding='utf-8')
print('测试日志', file=file)Merge Two Lists – Combine two lists and sort the result using bubble sort to maintain order. The following function merges and sorts the lists, then prints the merged list:
def merge(l1, l2):
my_list = l1 + l2
n = len(my_list)
for i in range(n):
for j in range(0, n - i - 1):
if my_list[j] > my_list[j + 1]:
my_list[j], my_list[j + 1] = my_list[j + 1], my_list[j]
print(my_list)
if __name__ == '__main__':
my_list1 = [4, 2, 6]
my_list2 = [1, 3]
merge(my_list1, my_list2)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.