# Read a blog post, write a piece of code, and write Python every day, day 1

Posted on May 28, 2023, 2:45 a.m. by 王家銘
Category: The back-end

"Offer comes, ask friends to take it! I am participating in the 2022 Spring Recruit Punch card campaign. Click here for more details."

This series of columns was created to get the Python feel right, and you can use it to learn Python as well, because it's difficult to jump over, so it's best to start with the Snowball series.

Learn a little each day, so each column in this series should be no more than 3000 words (including code)

Write to write to discover, these may be interview questions ~

## Inversion of digital

The problem scenario converts the number 789 to 987.

Typical number flipping problem.

Take the hundreds place, the tens place, and you can solve it

Code is as follows

``````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)
Copy the code``````

## Class documentation

Problem scenario Adds documentation to the newly created class

Use triple quotes around the class name to add the documentation. Use the class name to call the documentation

Code is as follows

``````class My_Class(object) :
"" "hello "" "

print(My_Class.__doc__)
Copy the code``````

## Sets the encoding of Python files

Problem Scenario Python files set the default encoding.

Python script files are encoded in UTF-8 by default. You do not need to specify the encoding. Python2 code files are usually annotated with a line. The setting mode is a one-line comment that must meet the following regular expression format.

Code is as follows

``````coding[=:]\s*([-\w.]+)
Copy the code``````

The declaration location is on either the first or second line of the Python file, with no Spaces before: or =. For example:

``````# -*- coding:utf-8 -*-
Copy the code``````

You can also use uppercase

``````# -*- coding:UTF-8 -*-
Copy the code``````

## Rotation string

Scenario One string and one number perform the following operations

• The string for`abcde`If the number is 3, the output is displayed`cdeab`;
• The string for`abcde`Is 1, the output is displayed`eabcd`;
• The string for`abcde`Is 0, the output is displayed`abcde`;
• ...

Solve the problem of using a string slice.

Code is as follows

``````def reverse_str(my_str,offset) :
# If the number is 0, the order defaults to the same
if offset ==0:
return my_str

left = my_str[:len(my_str)-offset]
right = my_str[len(my_str)-offset:]
return right + left
Copy the code``````

## Implement the console scrollbar

Problem scenario outputs scrollbars on the console.

The solution uses string formatting, such as to indicate progress, / to indicate unfinished progress, ljust() to return a left-aligned original string, and padding characters (default Spaces) to a new string of specified length.

The coding is implemented step by step as follows

``````# Print a line
progress_str = "" * 100
print(progress_str)
Copy the code``````

Fill one part , and fill the other part /.

``````# Print a line
progress_str = ""*20
# Fill 50 -
progress_str = progress_str.ljust(100.'/')
print(progress_str)
Copy the code``````

Implement loop operation

``````import time

for i in range(0.11):
time.sleep(0.3)

current = i/10

# the progress bar
progress_str = '{0:s}{1:.0%}'.format((int(current*10) *'').ljust(10.'/'), current)
print(progress_str)
Copy the code``````

To make it appear on a line, you can change the code to the following format, noting the last line of code.

``````import time

for i in range(0.11):
time.sleep(0.3)

current = i/10

# the progress bar
progress_str = '{0:s}{1:.0%}'.format((int(current*10) *'').ljust(10.'/'), current)
print(f'\r{progress_str}',end=' ')
Copy the code``````

## `print`The function writes directly to the file

Question Scenario What should I do if I do not want to print the output of the print function to the console, but input it directly to a file?

The print() function has an argument called file.

Code is as follows

``````file = open('runtime.log'.'a+', encoding='utf-8')
print('Test Log', file=file)
Copy the code``````

## Merge two lists

The problem scenario merges two lists and requires that the merged lists be in order. For example, my_list1 = [1,2,3], my_list2 = [1,3,5], and my_list = [1,1,2,3,3,5]

First merge two lists, and then judge the size of each element. Bubble sort can be used to complete the task. Code is as follows

``````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)
Copy the code``````

## Recording time

Today is the 281/365 day of continuous writing. You can follow me, like me, comment on me, favorites me.

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.