Getting started with Python
Python as a calculator
At first, we can use Python like a simple calculator because it makes getting started in Python easier.
We can also use IDLE as a Python shell called a calculator. First we start the Python shell. After the start we see the following presentation.
To do small calculations, we do not have to write and start complete programs. We can enter the desired arithmetic operations directly at the entry point, recognizable by the characters >>>. A final press with the Enter key leads to the immediate calculation and output of the result.
Addition, subtraction and multiplication
This is followed by input, processing and output of some simple calculations:
Division, integer division and modulo
Here are two division tasks:
As we can see in the picture, Python can solve the division of natural numbers but also of real numbers. Of course you can not divide in mathematics by the number zero, which is why after the third calculation error is reported.
An example of the percent operator (modulo):
The percent operator calculates the remainder of a division. The division 22/8 returns 2 remainder 6. The modulo operator returns the remainder 6.
Ranking and parentheses
It applies, as in the mathematics point before line calculation. Multiplication and division take precedence over addition and subtraction. Setting the brackets causes the expressions within brackets to be calculated first. That's what Python does in practice. Python first parses the expressions in parentheses, if any, otherwise calculates the expressions by multiplication and division first.
Variables and assignment
So far, we have only counted with the numbers. Like any programming language, Python can store numbers in variables if they are needed more than once during a calculation.
The factor for conversion from miles to kilometers per hour is stored in the variable "mi".
We can choose the name of a variable in compliance with the rules of the Java programming language. For example, they must not be keywords or characters.
First program
A first Python program is entered, saved and called up. The output of the first Python program is Hello World.
So the program outputs the text Hello World on the screen. This is often the program you write while learning any new programming language.
We also experienced that with the programming language Java.
The input of the new program happens, in which we start the development environment IDLE. We go up on the rider to FILE then under NEW FILE. A new window opens with the title UNTITLED. In the new window we give the new program:
The built-in function "print ()" outputs text or the values of the variables on the screen. As we can see, here we need a very short command to compare with the Java programming language, we do not have a long syntax.
Save and run the program
To save the program, we call the menu command SAVE in the current window in the FILE menu. The program is saved in the file named hello.py.
After pressing the SAVE button the storage is completed. We can freely choose the name of the file. The file with the extension .py is intended for Python programs must not be forgotten.
We can invoke Windows, Mac and Linux in two different ways. The first is where we can call the program within the development environment. The second option is to start the program from the command line. The command line is called Windows CMD and Unix systems like macOS or Linux called Terminal.
If we want to run the program within the development environment we just go to RUN on the tab and tap RUN MODULE. The program is then executed immediately if no errors are included.
For the execution of a program in the command line, we will have to skip first so that the initial is not too difficult.
Comments
In the case of extensive programs, it is very useful to include comments for explanation in the program text. One-line comments are initiated by the hash sign and extend to the end of the line. Multi-line comments begin and end with three double quotes. Comments are not considered as programming steps by the system and are therefore not executed.
At first, we can use Python like a simple calculator because it makes getting started in Python easier.
We can also use IDLE as a Python shell called a calculator. First we start the Python shell. After the start we see the following presentation.
To do small calculations, we do not have to write and start complete programs. We can enter the desired arithmetic operations directly at the entry point, recognizable by the characters >>>. A final press with the Enter key leads to the immediate calculation and output of the result.
Addition, subtraction and multiplication
This is followed by input, processing and output of some simple calculations:
Division, integer division and modulo
Here are two division tasks:
As we can see in the picture, Python can solve the division of natural numbers but also of real numbers. Of course you can not divide in mathematics by the number zero, which is why after the third calculation error is reported.
An example of the percent operator (modulo):
The percent operator calculates the remainder of a division. The division 22/8 returns 2 remainder 6. The modulo operator returns the remainder 6.
Ranking and parentheses
It applies, as in the mathematics point before line calculation. Multiplication and division take precedence over addition and subtraction. Setting the brackets causes the expressions within brackets to be calculated first. That's what Python does in practice. Python first parses the expressions in parentheses, if any, otherwise calculates the expressions by multiplication and division first.
Variables and assignment
So far, we have only counted with the numbers. Like any programming language, Python can store numbers in variables if they are needed more than once during a calculation.
The factor for conversion from miles to kilometers per hour is stored in the variable "mi".
We can choose the name of a variable in compliance with the rules of the Java programming language. For example, they must not be keywords or characters.
First program
A first Python program is entered, saved and called up. The output of the first Python program is Hello World.
So the program outputs the text Hello World on the screen. This is often the program you write while learning any new programming language.
We also experienced that with the programming language Java.
The input of the new program happens, in which we start the development environment IDLE. We go up on the rider to FILE then under NEW FILE. A new window opens with the title UNTITLED. In the new window we give the new program:
The built-in function "print ()" outputs text or the values of the variables on the screen. As we can see, here we need a very short command to compare with the Java programming language, we do not have a long syntax.
Save and run the program
To save the program, we call the menu command SAVE in the current window in the FILE menu. The program is saved in the file named hello.py.
After pressing the SAVE button the storage is completed. We can freely choose the name of the file. The file with the extension .py is intended for Python programs must not be forgotten.
We can invoke Windows, Mac and Linux in two different ways. The first is where we can call the program within the development environment. The second option is to start the program from the command line. The command line is called Windows CMD and Unix systems like macOS or Linux called Terminal.
If we want to run the program within the development environment we just go to RUN on the tab and tap RUN MODULE. The program is then executed immediately if no errors are included.
For the execution of a program in the command line, we will have to skip first so that the initial is not too difficult.
Comments
In the case of extensive programs, it is very useful to include comments for explanation in the program text. One-line comments are initiated by the hash sign and extend to the end of the line. Multi-line comments begin and end with three double quotes. Comments are not considered as programming steps by the system and are therefore not executed.
Kommentare
Kommentar veröffentlichen