SoFunction
Updated on 2024-10-30

Python basic learning functions + modules + classes

Preface:

Python Basics + Structures + Data Types

Python Basic Learning Lists + Tuples + Dictionaries + Collections

Today's is the third Python basic learning, the previous knowledge points to you on the above, zero basis of the partners can get their own hands, learn the basics of Python on our late

Go to the realization of the Python case helps a lot, know it in order to better solve the problem, words do not say, directly began.

I. Functions

print("------------- defines the function -------------");
def print_info():
# Print the prompt message to return to the input information
    print("Welcome to the Student Information Management System, please follow the prompts to enter the operation!")
    print("1. Add student information")
    print("2. Deletion of student information")
    print("3. Modification of student information")
    print("4. Access to student information")
    print("5. Browsing student information")
    print("6. Exit the system")
print("------------- call function -------------");
print_info()
D:\workspace\Python\venv\Scripts\ D:/workspace/Python/
-------------Defining Functions-------------
-------------call function-------------
Welcome to the Student Information Management System,Please follow the prompts to enter the operation!
1.Adding Student Information
2.Deleting Student Information
3.Modify student information
4.Access to student information
5.Browse Student Information
6.Log out of the system
The process has been completed,exit code0

II. Modules

III. Classes and objects

object of a class

print("------------- create class -------------");
class Stu:
    name = "Zhang San"
print()
D:\workspace\Python\venv\Scripts\ D:/workspace/Python/
-------------Creating Classes-------------
John Doe
The process has been completed,exit code0

instance object

print("------------- instance object -------------");
class Stu:
    # 'Define an attribute
    name = "Zhang San"
    age = 19
# Create an object of the Stu class
stu = Stu()
print("Student name:%s,age:%d" % (, ))
D:\workspace\Python\venv\Scripts\ D:/workspace/Python/
-------------instance object-------------
Student Name:John Doe,(a person's) age:19
The process has been completed,exit code0

Class methods

print("------------- constructor ------------");
class Stu:
    # Constructor
    def __init__(self):
         = "Zhang San"
         = 1
    def displayCount(self):
        print("Student name: %s, student number %d" % (, ))
stu = Stu()
()
print("------------- destructor methods ------------");
class Stu:
    # Constructor
    def __init__(self, name, stuid):
         = name
         = stuid
    # Analyzing methods
    def __del__(self):
        print("Resources have been released")
stu = Stu("Zhang San", 1)
del stu  # Delete object Trigger destructor method
# del # This is a deletion of an attribute, not a deletion of an entire instance.
print("To carry out garbage collection.")
print("------------- encapsulates ------------");
# coding=utf-8
class JustCounter:
    __secretCount = 0  # Private variables
    publicCount = 0  # Public variables
    def count(self):
        self.__secretCount += 1
         += 1
        print(self.__secretCount)
counter = JustCounter()
()
()
print()
print(counter._JustCounter__secretCount)
D:\workspace\Python\venv\Scripts\ D:/workspace/Python/
-------------constructor method------------
Student Name:John Doe,student number1
-------------destructuring method------------
Released resources
Carry out garbage collection
-------------seal inside------------
1
2
2
2
The process has been completed,exit code0

Class Inheritance

print("Inheritance of class ------------- ------------");
# coding=utf-8
class Parent:  # Define the parent class
    parentAttr = 100
    def __init__(self):
        print("Call the parent class constructor.")
    def parentMethod(self):
        print("Calling parent class methods")
    def setAttr(self, attr):
         = attr
    def getAttr(self):
        print("Parent Class Attributes :", )
class Child(Parent):  # Define subclasses
    def __init__(self):
        print("Calling subclass constructor methods")

    def childMethod(self):
        print("Call child method child method")
c = Child()  # Instantiate subclasses
()  # Call methods of subclasses
()  # Call parent class methods
(200)  # Call the parent class method again
()  # Call the parent class method again
D:\workspace\Python\venv\Scripts\ D:/workspace/Python/
-------------Class Inheritance------------
Calling subclass constructors
Calling subclass methods child method
Calling parent class methods
Parent Class Attributes : 200
The process has been completed,exit code0

III. Summary

That's all I have to share today, and again, the basics are really important to learn.

To this point this article on Python basic learning functions + modules + classes of the article is introduced to this, more related to the basic content of Python please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!