Python programs are written in a text editor, such as Notepad, PyCharm, or the code editor in Python’s integrated development environment (IDLE), and saved with a .py file extension.
You then use the Python interpreter to execute the code saved in the file.
Let’s start at the beginning.
Language Classification
There are different levels of programming language: low-level languages and high-level languages.
Low-Level Language
A low-level language is a programming language whose functions often refer directly to the processor’s instructions and is commonly written in machine code or assembly language. Assembly language is known as a second-generation programming language , machine code being the first generation.
Each assembly language instruction corresponds to a sequence of binary numbers in machine code. The numbers, characters, addresses, and other data are converted into their machine code equivalents.
So, LDA could be represented by the binary code 11000011; the number 1210 is 00001100 in binary.
The assembled machine code is then executed by the processor.
High-Level Language
Python is an example of a high-level language . Rather than dealing directly with processor registers and memory addresses, high-level languages deal with variables, human-readable statements, loops, and functions.
Python is an interpreted language, meaning the code you write is translated into machine code directly, making it well suited to web development.
When you attempt to run your program, the interpreter will convert and execute your code, but will only do this if it doesn’t contain any errors.
If there are syntax errors, an error in the Python grammar, the interpreter will stop and highlight the error.
If your program runs, there could still be errors. These could be logical errors and can produce unexpected results and are sometimes called bugs in the program. This could be a divide by 0 error which can cause the program to crash.
The process of eliminating these errors is called debugging.
Python Language Syntax
The syntax defines how a program is written and interpreted and forms the basis of writing code.
Reserved Words
These are words reserved by the programming language that define the syntax and structure. Here are some of the most common ones:
For example , the word “while” indicates a while loop. The word “if” defines an “if statement.” You can’t use a reserved word as a variable name or function name.
Identifiers
Indentation
Most other programming languages such as C and C++ use braces { } to define a block of code. Python uses indentation . Use the tab key.
Comments
A comment is an explanation or annotation in the source code of a computer program for the purpose of making the source code easier for other programmers to understand. Comments are intended to be human readable for the programmer’s benefit and are ignored by the Python interpreter during execution.
Comments are very important while writing a program. You should clearly document all your code using comments, so other developers working on a project can better understand what your code is doing.
If you need to write a block describing the functionality, then use a triple quote before and after the comment block.
Input
Output
Escape Characters
An escape character tells the interpreter to perform a specific operation such as a line break or tab or a reserved character such as a quote mark or apostrophe.
Escape characters
Escape Character | Function |
---|---|
| Line break |
| Tab (horizontal indentation) |
New line in a multiline string | |
\ | Backslash |
’ | Apostrophe or single quote |
” | Double quote |
Writing a Program
A new blank window will appear. This is the code editor. Here, you can write all your Python code.
Arrange your windows as shown here, with the Python Shell on the left-hand side (or right if you prefer) – this is where you’ll see the results of your programs. Put the code editor window next to the Python Shell window.
For our first program, we’re going to write something that adds two numbers together and then displays the result.
Let’s put it all together in a program.
To run the program , press F5, or go to the “Run” menu in your code editor and click “Run Module.”
You can see in the following image the output of the program, in this case “10.”
This particular program isn’t very useful. It would be much better if we could allow the user to enter the numbers they want to add together. To do this, we’ll need to add a function that will prompt the user for a value.
You can see in the following image the output of the program . The program prompted the user for two numbers, added them together, and then displayed the result underneath.
Lab Exercises
How do you write comments in your code? Explain with an example.
Why should you include comments?
Summary
Python programs are written in a text editor, such as Notepad, PyCharm, or the code editor in Python’s development environment (IDLE), and saved with a .py file extension.
Python is an example of a high-level language.
Python is an interpreted language, meaning the code you write is translated into machine code directly, making it well suited to web development.
An identifier is a name given to a class, function, or a variable.
Python uses indentation to mark a block of code. Use the tab key to indent.
A comment is an explanation or annotation in the source code of a computer program for the purpose of making the source code easier for other programmers to understand.
You can obtain input from the user using the input( ) function.
You can display information on the screen with the print( ) function.