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)06/02 Report--
This article mainly explains "what are the basic knowledge points of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic knowledge points of Python"?
Set (Set) and its functions
A collection is a sequence of unordered, non-repeating elements.
List = {1, 3, 6, 5, 7, 9, 11, 3, 7} # define collection mode-list1 = set ([1, 3, 6, 5, 7, 9, 11, 3, 7]) # define set mode 2 list2 = set () # define an empty set print (list1, list) # after printing, you can see that the elements in the set have been automatically deduplicated print (3 in list) # to determine whether an element is in the set or not Return Bool value print (20 not in list1) # to determine whether an element is not in the collection, return Bool value list1.add (99) # New element list1.update ([10,20,30,2]) # add multiple items list1.remove (3) # Delete an element, if the element does not exist, report an error print (list1.discard (8)) # Delete an element If the element does not exist, do nothing print (len (list1)) # calculate the number of elements in the collection print (list1.pop ()) # an element list.clear () # randomly pops up from the collection and empties the collection
Operation of a set
List1 = set ([1,3,6,5,7,9,11,3,7]) list2 = set ([2,4,6,8,3,5]) print (list1 List2) # intersection print (list1.intersection (list2)) print (list1 & list2) # Union print (list1.union (list2)) print (list1 | list2) # difference print (list1.difference (list2)) print (list1-list2) # symmetric difference print (list1.symmetric_difference (list2) print (list1 ^ list2) # whether it is a subset or not list3 = set ([9) 11]) print (list3.issubset (list1)) print (list1.issuperset (list3)) # if the intersection of two sets is empty, return true list4 = set ([20,30]) print (list1.isdisjoint (list4)) print (list1.isdisjoint (list2))
File (File) operation
In development, there is often a need to read and write files, and the related code implementation is as follows:
The open mode of the file
File read manipulation, write operation, append operation, read file by line
# read read the full text of the file directly f = open ('test', 'ritual, encoding='utf-8') # File handle data = f.read () print (data) # write write f = open (' test1', 'walled, encoding='utf-8') f.write ('I love Beijing Tiananmen Square) to the file On Tiananmen Square, the sun rises) # append appends to the file f = open ('test1',' ahem, encoding='utf-8') f.write ('ah ho hee') # loop reads the file by line # high bigger reads the file as an iterator and prints one line only one line in memory f = open ('test', 'r') Encoding='utf-8') count = 0 for l in f: if count = 9: print ('-') count + = 1 continue print (l.strip ()) count + = 1 # low loop reads all the contents of the file into memory Low efficiency f = open ('Sonnet',' ritual, encoding='utf-8') for index, line in enumerate (f.readlines ()): if index = = 9: print ('-') continue print (line.strip ())
Functions of files
F = open ('test', 'r' Encoding='utf-8') # File handle read mode Open file print (f.tell ()) # get the current cursor position print (f.readline ()) print (f.readline ()) print (f.tell ()) print (f.readline ()) f.seek (10) # Jump the cursor to the 10th character print (f.readline ()) print (f.encoding) # get the file code print (f.fileno () # i don't know what it is print (f.isatty ()) # determine whether the file is a tty terminal print (f.readable ()) # determine whether the file is readable print (f.writable ()) # determine whether the file is writable print (f.seekable ()) # determine whether the file is a reloadable cursor (tty cannot jump f.flush () # when opening a text in write mode If you need to refresh the contents of the file on your hard disk in time, you can call this function f.close () # close the file print (f.closed) # to determine whether the file is closed.
Modification of the file
# it is difficult to modify the file directly. It is difficult to write the changes into another file. If you need it, you can write it back to the file itself f = open ('test',' ritual, encoding='utf-8') f_new = open ('test.bak',' walls, encoding='utf-8') for line in f: if'I have dreamed of'in line: line = line.replace ('I have dreamed of it a million times' 'i don't want to dream a million times') f_new.writelines (line) f.close () f_new.close ()
A progress bar example is used to understand the mechanism of the flush function. This example can achieve the progress bar effect.
Import sys import time f = open ('Sonnet1',' writing, encoding='utf-8') # opening a file in handle writing mode creates a new file. If a file with the same name exists, it will directly overwrite for i in range (10): sys.stdout.write ('#') sys.stdout.flush () time.sleep (0.2)
Character coding conversion
The most important thing about character transcoding is to remember that unicode is the transfer station between encodings. If unicode is not the target code or the original code, then any two encodings need to be converted to each other through unicode (see figure below).
It is important to note that the default encoding for python is ASCII,python3 and the default encoding is unicode.
In python3, encode will change string into bytes type while transcoding, and decode will change bytes back to string while decoding.
Function
A function is a code snippet that is organized, reusable and used to implement a single, or associated function.
Functions can improve the modularity of the application and the reuse of the code. Python provides many built-in functions (such as print ()); you can also create your own functions, that is, user-defined functions.
To define a function with its own desired function, you need to follow the following rules:
The function code block begins with the def keyword, followed by the function identifier name and parentheses ().
Any incoming parameters and arguments must be placed in the middle of parentheses, which can be used to define parameters.
The * line statement of the function can optionally use the document string-- used to store the function description.
The function content starts with a colon and is indented.
Return [expression] ends the function and optionally returns a value to the caller. Return without an expression is equivalent to returning None.
Knowledge to be added: function parameters, variable scope, recursion, high-order functions.
Thank you for your reading, the above is the content of "what are the basic knowledge points of Python". After the study of this article, I believe you have a deeper understanding of what the basic knowledge points of Python have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.