In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the basic knowledge of Python". In daily operation, I believe many people have doubts about what basic knowledge of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what basic knowledge of Python?" Next, please follow the editor to study!
1. Function function
In programming, the function that needs to be rewritten can be packaged as a function.
1) define the function
Before defining a function, implement the operation of adding a prefix and suffix to a string:
F1 = "F1" f2 = "f2" F1 + = ".txt" F1 = "my_" + f1f2 + = ".txt" f2 = "my_" + f2print (F1, f2) my_f1.txt my_f2.txt
After defining the function:
Def modify_name (filename): filename + = ".txt" filename = "my_" + filename print (filename) modify_name ("F1") modify_name ("f2") my_f1.txtmy_f2.txt
Parameters are used in the above functions. Parameters need to be passed when calling, but sometimes there can be no parameters. Eg:
Def modify_name (): filename = "F1" filename + = ".txt" filename = "my_" + filename print (filename) modify_name () my_f1.txt
A function can also have a return value. After processing the function, you can return the result you want to return.
Def modify_name (filename): filename + = ".txt" filename = "my_" + filename return filenamenew_filename = modify_name ("F1") print (new_filename) my_f1.txt2) parameter setting
When calling a function, if the function has parameters, you need to pass parameters to the function. The original parameters of the function are called arguments, and the passed parameters are called arguments. You can pass parameters in the following ways:
Def f (x, a, b, c): return a*x**2 + Backx + c*1print (f (2, 1, 1, 0)) # ignores the parameter names and needs to write the parameter names one by one in order to correspond to print (f (x, x, a, b, c)) # and pass the parameter print (f (a, x, c, x, b, b) by name) # if you use the parameter name, you can pass the parameter 666 in disorder.
In addition, for parameters that do not change all the time, you can set a default value for the parameter. If the default value is set, the parameter can be passed without changing the value of the parameter, otherwise each parameter must be passed successfully before it can be called
Def f (x, axi1, baux1, cym0): return a*x**2 + bauxx + c*1print (f (2, aq2)) print (f (2)) 106
Tip: parameters with default values should be separated from those without default values, that is, parameters that are not set by default values cannot be followed by parameters with default values
3) Global and local variables
Global variable (global): can be used both inside and outside the function (common)
Local variable (local): used only within functions (private)
Def modify_name (): filename = "f1.txt" # here is a local variable print ("local filename:", filename) modify_name () print ("global filename:", filename) # there will be an error filename = "f1.txt" # here is a global variable def modify_name (): print ("local filename:", filename) modify_name () print ("global filename:", filename)
When the global variable conflicts with the local variable, the local variable takes priority!
Filename = "f1.txt" def modify_name (): filename = "f2.txt" print ("local filename:" filename) modify_name () print ("global filename:", filename) local filename: f2.txtglobal filename: f1.txt
Special case: allow the internal to modify the external value, must use the global declaration, which is equivalent to submitting an application
Filename = "f1.txt" def modify_name (): global filename # apply filename = "f2.txt" print ("local filename:", filename) modify_name () print ("global filename:", filename) local filename: f2.txtglobal filename: f2.txt2, Class
Purpose: in order to describe a specific object or class of objects, eg: cat characteristics, movements, etc.
1) define class
Use class File to create a class. Note that the first letter of the class name is usually capitalized.
Then use my_file = File () to create an instance, each of which inherits the basic properties of a class
Class File: def _ _ init__ (self): self.name = "F1" self.create_time = "today" my_file = File () print (my_file.name) print (my_file.create_time) f1today
Self is an index of the class itself. No matter what attributes or functions of the class you want to obtain when you define the class, you can get it through self.
_ _ init__ (): when an instance is created, the class is initialized automatically, that is, the _ _ init__ () function is run once
Modify the value of the property: (but the value modified in this way is valid only for the current instance)
My_file.name = "new_name" print (my_file.name) new_name2) class function
_ _ init__ (): you can also pass parameters to this function:
Class File: def _ init__ (self, name, create_time= "today"): self.name = name self.create_time = create_timemy_file = File ("my_file") print (my_file.name) print (my_file.create_time) my_filetoday
You can also define more functions, such as renaming, etc.
Class File: def _ init__ (self, name, create_time= "today"): self.name = name self.create_time = create_time def change_name (self, new_name): self.name = new_namemy_file = File ("my_file") my_file.change_name ("new_name") print (my_file.name) new_name
And the function of the class can also have a return value.
Class File: def _ init__ (self, name, create_time= "today"): self.name = name self.create_time = create_time def get_info (self): return self.name + "is created at" + self.create_timemy_file = File ("my_file") print (my_file.get_info () my_file is created at today3) inheritance
Write the two classes separately, eg:
Class Video: def _ init__ (self, name, window_size= (1080): self.name = name self.window_size = window_size self.create_time = "today" class Text: def _ init__ (self, name, language= "zh-cn"): self.name = name self.language = language self.create_time = "today"
It can be found that these two classes have something in common, such as name, and create_time.
Therefore, you can write an underlying class so that the two classes inherit the underlying class, thus reducing the repeated development of common attributes / functions between classes!
One class can inherit another class, making the class a subclass and the inherited class the parent class
The subclass can inherit the function / function of the parent class, and the subclass can redefine the function of the parent class
Class File: def _ init__ (self, name, create_time= "today"): self.name = name self.create_time = create_time def get_info (self): return self.name + "is created at" + self.create_timeclass Video (File): def _ init__ (self, name, window_size= (1080) ): # Import the settings of common attributes into the File parent class super (). _ _ init__ (name=name, create_time= "today") self.window_size = window_sizeclass Text (File): def _ init__ (self, name, language= "zh-cn"): super (). _ _ init__ (name=name) Create_time= "today") self.language = language # can also reuse the parent class function def get_more_info (self): return self.get_info () + "in the subclass Using language of "+ self.languagev = Video (" my_video ") t = Text (" my_text ") print (v.get_info ()) # call parent function print (t.create_time) # call parent attribute print (t.language) # call your own attribute print (t.get_more_info ()) # call your own reuse parent function my_video is created at todaytodayzh-cnmy_text is created at today Using language of zh-cn4) Private attributes and functions
Private: _ at the beginning of an underscore, weakly hidden, do not want others to use it, but others can still use it if necessary
Private: _ _ at the beginning of two underscores, strongly hidden, not allowed to be used by others
Class File: def _ _ init__ (self): self.name = "F1" self.__deleted = False # do not let others use this variable self._type = "txt" # do not want others to use this variable def delete (self): self.__force_delete () def _ _ force_delete (self): # do not let others use this Function self.__deleted = True return True def _ soft_delete (self): # do not want others to use this feature self.__force_delete () # although it is strongly hidden But you can call return Truef = File () print (f._type) # internally to get the value, but there will be ⚠️ print (f._soft_delete ()) # that can be called. But there will be ⚠️ print (f.__deleted) # will directly report an error print (f.__force_delete) # will directly report an error 5) Special method definition meaning def _ init__ () initialize the "official" representation of the instance def _ _ repr__ () string the "informal" value def _ _ iter__ () string traverses a sequence def _ _ next_ _ () gets the next value from the iterator. 3. Module module
Moudel is mainly for a relatively large project, involving the mutual invocation relationship between multiple files.
For a project, it needs to implement a lot of functions. at this time, if a certain function or a certain type of function can be classified and written into a file, when merging, I do not need to know the specific code in the file. I just need to call the functions in this file you gave me! Also
That is to say, as a user of the function in the file you give, I don't care about the code in which you implement the function. I just care that I can use the function to call your function.
# file.pydef create_name (): return "new_file.txt"
Eg: for example, the above file.py file implements the function of create_name. However, as a user of this function, I do not need to understand the code used for this function. I only care about how to call and use this function.
1) reference module
For example, if you create a new me.py, you need to reference the function of create_name () in file.py in this file.
# me.pyimport fileprint (file.create_name ()) new_file.txt
Or you can call it this way
# me.pyimport file as f1print ("F1:", f1.create_name ()) class File: def create_name (self): return "new_file.txt" f2 = File () print ("f2:", f2.create_name ()) F1: new_file.txtf2: new_file.txt
It can be found that this is very similar to the class function call!
There are more ways to quote:
# me.pyfrom file import create_nameprint (create_name ()) new_file.txt
Suppose there is another function create_time () in file.py.
# me.pyfrom file import create_name, create_timeprint (create_name ()) print (create_time ()) new_file.txttoday
If there are too many functions in file.py to remember clearly, you can quote:
# me.py# the first kind of import fileprint ("1", file.create_name ()) # the second kind of from file import * print ("2", create_name ()) print ("2", create_time () 2) module management of large projects
In regular module, we often see a _ _ init__.py file, like def _ _ init__ (self) in class, in which you can write how to initialize your files directory and set the relationship between the elements in the directory.
# files/__init__.pyfrom .video import get_video_size
Once the _ _ init__.py is set, you can directly import get_video_size the video.py function from files.
# me.pyfrom files import get_video_sizeprint (get_video_size ())
However, text.py at the same level as video.py cannot obtain the functionality in text.py through import because it is not declared in files/__init__.py
# me.pyfrom files import create_name
If you do not declare it in files/__init__.py but still want to call it, you can use:
# me.pyimport files.textprint (files.text.create_name) # or from files import textprint (text.create_name) at this point, the study on "what are the basics of Python" is over, hoping to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.