This is the third day of my participation in Gwen Challenge
The introduction
The Python environment is installed and ready to program Python, but where do you write Python programs? Is the Python interactionator opened by typing Python in a CMD window? Or in a notepad? Just entry can be, but it is recommended to use a notebook first. PyCharm, a Python development tool, is available later.
Python source program concepts
-
A Python source program is a specially formatted text file that can be developed using any text-editing software
-
The file extension for Python programs is usually.py
createPython-Basic
Working directory
Create a python-basic working directory on your desktop, where python-Basic means Python basics. This directory is dedicated to files written to practice the basics of Python.
create01-hello.py
file
Create the 01-hello.py file in the python-basic directory and open it with Notepad and write the following code
#! /usr/bin/python3
# -*- coding:utf-8 -*-
print('hello python')
print('hello world')
Copy the code
Where #! /usr/bin/python3 is a declaration to run the file in Linux using the PYTHon3 interpreter.
# -*- coding: UTF-8 -*- indicates that the file is encoded in UTF-8. In Python3, this line is optional, because Python3 stores strings in Unicode by default, whereas Python2 stores strings in ASCII, and Python2 produces garbled characters if Chinese characters appear in a file. So declare how the file is encoded at the beginning.
We are using the Python3 interpreter on a Windows platform, so we don’t have to write these two lines, but I personally recommend including them for good code practice. Coding habits and styles can’t be learned overnight, so start with the basics. Maybe you don’t understand now, it’s okay, a lot of things are hindsight, there’s no need to get clear at the beginning, such as smiling back, can make you suddenly enlightened is enough. So you can just copy and paste it and copy it.
Python3, Python2 refers to the version of the Python interpreter.
Python3 is a Python 3.x interpreter and Python2 is a Python 2.x interpreter.
Python 3.x is the current and future mainstream version
With that said, it’s time to get back to the subject of how Python code works once you’ve written it.
You can try double-clicking, but this simple double-clicking will not work
CMD window to run Python programs
As mentioned earlier, Python code requires a Python interpreter to interpret and run. Since we added the Path of the Python interpreter to the system environment variable -path when we installed the Python environment, we can call the Python interpreter by typing Python in a CMD window.
Open the CMD window in the current python-basic directory
Enter the following commands to call the Python interpreter and run Python programs.
python 01-hello.py
Copy the code
Python is followed by the path to the file to run, and since I’m opening a CMD window in the current directory, I just type the file name. You do not need to enter a complete file name. For example, enter 01 and press TAB to automatically complete the file name. If you do not open CMD in the current directory, remember to write out the path to the file.
The function print() prints the contents of “” onto the screen.
Double click on thepython
File to run
If you have a Python environment and you have system environment variables configured, you can run it, but the effect is so fast that you can’t see the result. So if you add delays, blocks, loops, etc. to the file, you can see the effect. Here’s how to use input() to block the program.
Create a new file called 02-input.py and enter the following code
#! /usr/bin/python3
# -*- coding:utf-8 -*-
print('Input () function exercise')
ret = input('Please enter test content:')
print(ret)
input('Enter any character to exit the program')
Copy the code
Double-click the program to see the following effect
The input() function will block the Python program, waiting for you to input data and treating it as the return value, which I received using the RET variable.
If you double-click the file and it still doesn’t work, it may be because you have multiple Python interpreters or because the.py file is not a Python interpreter by default. This can be done by specifying the opening mode as the Python interpreter (python.exe) by default.
BUG walkthroughs
About the error
- We write programs that don’t execute properly, or that don’t produce the results we expect
- Commonly known as
BUG
, is a very common programmer in the development, beginners common mistakes may include the following reasons:- Hand by mistake
- There is still a lack of understanding of the knowledge that has been learned
- There is still something to learn and improve about the language
- When learning a language, we should not only learn the grammar of the language, but also learn how to recognize mistakes and how to solve mistakes
Every programmer grows up fixing mistakes. Failure is the mother of success. Don’t be afraid of mistakes.
Common mistakes
✍ hand error, for example using pirnt(” Hello python”)
NameError: name 'pirnt' is notDefined error -> Name error:'pirnt'Name not definedCopy the code
✍ write multiple print on one line print(‘hello’) print(‘python’)
SyntaxError: invalid syntaxCopy the code
Each line of code is responsible for one action
✍ indentation error
IndentationError: unexpected indent error: unexpected indentCopy the code
- Python is a very rigidly formatted programming language
- For now, remember not to add Spaces before each line of code
Common wrong words
* error * name name * defined * syntax * invalid * Index Index * Indentation * unexpected Undesirable * character * line * encoding * declared * details * ASCII an encoding * FileNotFound The file could not be foundCopy the code
The tail language
✍ Code writes the world and makes life more interesting. ❤ ️
✍ thousands of rivers and mountains always love, ✍ go again. ❤ ️
✍ code word is not easy, but also hope you heroes support. ❤ ️