Before Start Before writing the first Python program, let's review what is the command line mode and the Python interactive mode. Command line mode Select "Command Prompt" from the Windows Start menu to enter the command line mode. Its prompt is similar to C: >: Interactive mode Type the command python in the command line mode, you will see a bunch of text output similar to the followin...
Python is a computer programming language. Computer programming languages are different from the natural languages we use every day. The biggest difference is that natural languages have different understandings in different contexts.
For a computer to perform tasks according to a programming language, it must ensure that the program written by the programming language is not ambiguous. Therefore, any programming language has its own set of syntax. The compiler or interpreter is responsible for converting the code into excutable machine code.
Python's syntax is relatively simple, and the code is written as follows:
# print absolute value of an integer: a = 100 if a >= 0: print(a) else: print(-a)
Statements that begin with
the indented statement is treated as a code block.
are comments. Comments are human-readable and can be anything. The interpreter ignores comments. Each other line is a statement, and when the statement ends with a
There are pros and cons to indentation. The advantage is that it forces you to write formatted code, but it doesn't specify whether the indentation is a few spaces or a tab. As per convention, you should always stick to a 4-space indent.
Another advantage of indentation is that it forces you to write less indented code. You will tend to split a long piece of code into several functions to get less indented code.
The disadvantage of indentation is that the
is disabled, which is the worst place. When you refactor your code, the pasted code must be rechecked for correct indentation. In addition, it is difficult for the IDE to format Python code in the same way as Java code.
Finally, please note that Python programs are case sensitive. If you write the wrong case, the program will report an error.
Python uses indentation to organize code blocks. Be sure to follow conventions and stick to a 4-space indentation.
In a text editor, you need to set the tab to be automatically converted to 4 spaces to ensure that tabs and spaces are not mixed.