In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to set the coding of Python files". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to set the coding of Python files" can help you solve the problem.
1. Reverse a number
Problem scenario: convert the number 789 to 987.
A typical digital flip problem.
Solution: take apart the 100 digits and 10 digits of the number and unravel it.
The code is as follows:
Def reverse_number (number): baiwei = int (number/100) shiwei = int (number0/10) gewei = int (number) return gewei*100+shiwei*10+baiweinew_number = reverse_number (789) print (new_number) 2. Documentation for the class
Problem scenario: add documentation to the newly created class
Solution: use three quotation marks under the class name to add the documentation, and use the class name, which can be called by _ _ doc__
The code is as follows:
Class My_Class (object): "Hello" and "print (My_Class.__doc__) 3. Set the encoding of the Python file
Problem scenario: the Python file sets the default encoding.
Solution: Python script files are encoded using UTF-8 encoding format by default, generally do not need to specify the encoding, but we can also set the encoding, Python2 code files are generally with this line of comments. The setting method is to use one-line comments, which requires that the format satisfies the following regular expression.
The code is as follows:
Coding [=:]\ s * ([-\ w.] +)
The declaration is located on the first or second line of the Python file. Note: there is no space before or =.
For example, the following code:
#-*-coding:utf-8-*-
You can also use uppercase:
#-*-coding:UTF-8-*-4 Rotate string
Problem scenario: a string, a number, complete the following operations
If the string is abcde and the number is 3, the output cdeab
If the string is abcde and the number is 1, the output eabcd
If the string is abcde and the number is 0, the output abcde
……
Solution: a problem of string slicing.
The code is as follows:
Def reverse_str (my_str,offset): # if the number is 0, the order remains the same by default if offset = = 0: return my_str left = my_str [: len (my_str)-offset] right = my_ strlen (my_str)-offset:] return right + left5. Implement the console scroll bar
Problem scenario: output scroll bars in the console.
Solution: use string formatting for implementation, such as > for progress, / for unfinished progress, use the ljust () method to return a left alignment of the original string, and populate the new string of specified length with padding characters (default spaces).
The code is as follows: step by step
# print a line > progress_str = ">" * 100print (progress_str)
Fill part >, and then fill another part /.
# print one line > progress_str = ">" * 2mm fill 50-progress_str = progress_str.ljust (100djinzzag') print (progress_str)
Implement the loop operation:
Import timefor i in range (0,11): time.sleep (0.3) current = iUniverse 10 # progress bar progress_str ='{0purs} {1pur.0%} '.format ((int (current*10) *' >'). Ljust (10,'/'), current) print (progress_str)
To make it appear on one line, you can modify the code to the following format, paying attention to the last line of code.
Import timefor i in range (0,11): time.sleep (0.3) current = iAccord 10 # progress bar progress_str ='{0purs} {1pur.0%} '.format ((int (current*10) *' >'). Ljust (10,'/'), current) print (f'\ r {progress_str}', end='') the 6.print function writes directly to the file
Problem scenario: what if you don't want to print the output of the print function to the console, but input it directly to the file?
Solution: the print () function has an argument called file, so just use it.
The code is as follows:
File= open ('runtime.log',' a log, encoding='utf-8') print ('test log', file=file) 7. Merge 2 lists
The problem scenario merges two lists and requires the merged lists to be in order. For example, my_list1 = [1JEI 2jue 3], my_list2 = [1JEI 3JEI 5], and after the merger, it is my_list = [1Jing 1Jing 2jue 3JI 5]
The solution is to merge two lists first, then judge the size of each element, and then use bubble sorting to complete the task. The 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) about "how to set the encoding of Python files" ends here Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.