[Reprinted] What are the classifications of python modules_ compiled a list, quick answer packages for common Python questions

Reference link: Python implementation of automatic tic-tac-toe game using random numbers

Python continues to be popular. At the same time, the demand in fields such as web development, data science and machine learning is still growing, and Python is the general programming language in these fields.

 As the demand for Python increases, both beginners and advanced programmers need more resources to master this on-demand language. Therefore, we have compiled a list of the most common questions about Python developers like you. Everything from the for loop to the document to the GUI.

 Today, we will introduce the following: What is Python?

 What is the history of Python?

 What are the main functions of Python?

 Should I use Python 2 or Python 3?

 How to install Python?

 What is the best Python IDE?

 What are the best Python resources?

 What are the basic concepts of Python?

 What are the rules for local and global variables?

 What are the best practices for using imports in modules?

 What are the classes in Python?

 How to use strings to call functions or methods?

 How to delete files in Python

 Can I generate random numbers in Python?

 Can I read or write binary data in Python?

 What GUI toolkits are there for Python?

 Master Python through hands-on projects.

 After completing these courses, you are ready to apply for any high-paying Python position.

 Foundation and history

 What is Python?

 Python is an object-oriented, interpreted high-level programming language. In addition to object-oriented programming, Python also provides examples of procedural and functional programming. It uses modules, exceptions, dynamic types, data types and classes.

 This language is powerful and clear, and it combines the interfaces of many system classes and libraries. Python can also be used as an extension language for applications that require a programmable interface.

 What is the history of Python?

 Python was founded by Guido Van Rossum in Centrum Wiskunde & Informatica in the Netherlands in the 1980s. Python was originally created as the successor of the ABC language, which will be able to handle exceptions and interface with the Amoeba operating system.

 Before July 12, 2018, he was the sole leader of the Python project. In January 2019, the core developers elected Brett Cannon, Nick Coghlan, Barry Warsaw, Carol Willing and Van Rossum to lead the project.

 Python 2.0 was released on October 16, 2000, with new features, such as cycle detection garbage collector and support for Unicode. Python 3.0 was released on December 3, 2008.

 What are the main functions of Python? Easy to learn and use: Since Python's syntax is simple and clear and usually similar to English, Python is considered an easy-to-learn language. Python uses bonus semicolons and braces to define code blocks. As an advanced implementation, it is a programming language recommended by beginners.

 Expressive: Python can perform complex tasks with just a few lines of code. For example, a hello world is just one line: print("Hello World). Although Python only needs one line to execute, languages ​​like Java or C require more lines.

 Interpreted language: Python is an interpreted language, which means that Python programs are executed line by line. One advantage of interpreted languages ​​is that they are easy to debug and portable.

 Cross-platform language: Python can run equally on Widows, Linux, UNIX, macOS and other operating systems, thus making the language portable. This allows engineers to use a program to create software on competing platforms.

 Free and open source: Python is free and open to the public, you can download it on python.org. It has a large global community dedicated to creating more python packages and functions through a dedicated team.

 Object-oriented language: Python is an object-oriented programming language that uses classes and objects. It also allows features such as inheritance polymorphism and encapsulation. This makes it easier for programmers to write reusable code.

 Should I use Python 2 or Python 3?

 Although there are many versions of Python, the main comparison is Python 2 and Python 3. Python3 was originally released in December 2008 to correct some basic design flaws introduced by Python 2.

 The guiding principle of Python 3 is: "Reduce function duplication by eliminating old ways of doing things." Python 2 was created in a way that supports multiple ways of performing the same task.

 Python 2: Still ingrained in many companies’ software

 Many older libraries for Python 2 are not compatible with forwarding

 By default, strings are stored as ASCII

 Python 3: Will replace Python 2 by 2020

 More and more libraries are created strictly for Python 2

 The text string defaults to Unicode

 Now, it is clear that Python 3 is the most popular choice because Python 2 is no longer supported by the Python Software Foundation. With this change, the entire community has largely turned to Python 3, which means there is no reason to learn Python 2.

 How to install Python?

 Python requires about 25 MB of disk space, so make sure you have enough space. After installation, Python requires an additional 90 MB of space. You can download Python here.

 Click "Download Python 3.8.5"

 Scroll down and click "[your operating system] 64-bit installer".

 After clicking the button, follow the instructions of the installer and you are done!

 What is the best Python IDE?

 IDE (Integrated Development Environment) is a program dedicated to software development. In this case, we are looking for an IDE dedicated to python development. Some features of the IDE include: an editor designed to process code

 Build, execute and debug tools

 Some form of source control

 A good IDE for the Python environment has some important features: saving and reloading code files, running code in the environment, debugging support, syntax highlighting, and automatic code formatting.

 General IDEs with Python support:Eclipse + PyDev

 Sublime Text

 Atom

 Python-specific editors and IDEs:PyCharm

 Spyder

 Thonny

 I recommend PyCharm, it provides some amazing features, such as type checking, code inspection, automatic refactoring, easy navigation in larger projects, integration with debugger and version control. The list continues.

 What is the best resource for learning Python?

 The best way to learn Python is to practice. Python is very intuitive, so focusing on coding challenges will improve your skills. You can get ideas about these ideas on GitHub, the official Python website or online courses.

 Programming problem

 What are the basic concepts of Python?

 semicolon

 Let's start with Python first, unlike most programming languages, which do not use semicolons to end lines. The newline is enough to make the interpreter detect the new command.

 In the example using the print() method, we can see an example.

 print('First command')

 print('Second command')

 indentation

 Most languages ​​use curly braces to define the scope of a code block, but the Python interpreter will simply determine this through indentation. This means that you must be especially careful about spaces in the code, which can break your application. Below is an example.

 def my_function():

 print('Hello world')

 Comment

 To comment out something in the code, you only need to use a hash sign #. Below is an example.

 this is a comment that does not influence the program flow

 def my_function():

 print('Hello world')

 variable

 Using python, you can store and manipulate data in the program. Variables store data, such as numbers, user names, passwords, etc. To create (declare) variables, you can use the = symbol.

 name='Bob'

 age=32

 Note that in Python, for example, you don't need to tell the program whether a variable is a string or an integer. This is because Python has dynamic typing, where the interpreter will automatically detect the data type.

 type of data

 To store data in Python, we have determined that you need to use variables. However, every variable will have a data type. Examples of data types include string, integer, boolean and list.

 A Boolean type can only hold the value True or False.

 my_bool = True

 print(type(my_bool))

 my_bool = bool(1024)

 print(type(my_bool))

 An integer is a type of three values, including float and complex one. The integer is a positive or negative number.

 my_int = 32

 print(type(my_int))

 my_int = int(32)

 print(type(my_int))

 String is one of the most common data types.

 my_city = "New York"

 print(type(my_city))

 Single quotes have exactly

 the same use as double quotes

 my_city = 'New York'

 print(type(my_city))

 Setting the variable type explicitly

 my_city = str("New York")

 print(type(my_city))

 Operators are symbols that can be used in your values ​​and variables to perform comparisons and mathematical operations.

 Arithmetic operator: +: addition

 -: Subtraction

 *:multiplication

 /:department

 **: exponentiation

 %: Modulus, give you the remainder of the division

 Comparison operator: ==: equal

 !=: Not equal

 >: better than

 >=: greater than or equal to

 <=: less than or equal to

 What are the rules for local and global variables?

 In Python, variables referenced in functions are implicitly global. If a variable is assigned a value in the function body, it is local unless you explicitly declare it as a global variable.

 What are the best practices for using imports in modules?

 Generally, do not use from modulename import *. This will confuse the importer's name space, which makes it more difficult for Linter to detect undefined names.

 Import the modules at the top of the file so that you can clearly know which modules your code requires. Import once per line.

 Generally, it is good practice to import modules in the following order: standard library modules

 Third-party library module

 Locally developed modules

 Only when you need to solve problems such as avoiding circular imports or trying to reduce module initialization time, you should move imports to the local scope.

 What are the classes in Python?

 Essentially, everything in Python is an object, which has attributes and methods. A class is an object constructor that serves as a blueprint for creating objects.

 Here, we create a class named after the MyClass attribute X. Then, we create a p1 object and print the value of X.

 class MyClass:

 x = 5

 p1 = MyClass()

 print(p1.x)

 When a class is created, a new object type is created, which allows new instances of that type. Each class will have its unique attributes. Compared with other programming languages, Python's class merging uses minimal syntax and semantics.

 How to use strings to call functions or methods?

 There are multiple techniques to achieve this, but the best way is to use a dictionary that maps strings to functions. With this method, the string does not need to match the function name. This is also the main technique used to simulate case construction:

 def a():

 pass

 def b():

 pass

 dispatch = {'go': a, 'stop': b} # Note lack of parens for funcs

 dispatch[get_input()]() # Note trailing parens to call function

 How to delete files in Python Open the "Python File" window.

 Enter the following code

 import them

 os.remove("ChangedFile.csv")

 print("File Removed!")

 This task seems very simple. All you have to do is to call os.remove() with the file name and path. Python defaults to the current directory. Run the application and you should see the File Removed! message.

 How to generate random numbers in Python?

 To generate random numbers in Python, you can use the randint() function.

 Program to generate a random number between 0 and 9

 importing the random module

 import random

 print(random.randint(0,9))

 Can I read or write binary data in Python?

 For complex and unconventional data formats, the struct module should be used. This allows you to take a string containing binary data and convert it to a Python object, and vice versa.

 In the following example, the code reads two 2-byte integers and a 4-byte big-endian integer from the file:

 f = open(filename, "rb") # Open in binary mode for portability

 s = f.read(8)

 x, y, z = struct.unpack(">hhl", s)

 What GUI toolkits are there for Python? Tkinter: The standard build of Python includes tkinter, which is the easiest to install and use. You can learn more here.

 Kivy: Kivy is a cross-platform GUI library for desktop operating systems and mobile devices. It is written in Python and Cithon. It is free and open source software under the MIT license.

 Gtk+: Python's GObject introspection binding allows you to write GTK+ 3 applications.

 wxWidgets: wxWidgets is a free and portable GUI written in C++. wxPython is a Python binding for wxwidgets, which provides many functions through pure Python extensions, while other bindings do not.

 Author: 10 years of IT workers

 Link: https://juejin.im/post/688144...

 Source: Nuggets

 The copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/u013946150/article/details/112976683