Search
You exceeded the request by
0 characters

2. First introduction to Python

The test was not passed
0
0

Summary

- Python is a popular programming language with a wide range of applications.

- Services such as Dropbox and Google are written in Python.

- Python allows you to work with xml/html files, develop robots, GUI, web scripts, FTP, mathematical and scientific calculations.

- Python installation on Windows, Mac OS and Linux is possible from the official website.

- Visual Studio Code is a convenient Python-enabled code editor.

- IDLE Python is an integrated development and learning environment for novice programmers.

- Debugging of the code is possible using debugging tools in the Python shell.

- You can practice Python on the Codebra website.

Introduction

A favorite question before learning a language is: "What can I write on it?". We interpret it into a more correct question: "What is the use of the Python language?". I will immediately write that services such as Dropbox and Google are written in Python, and this indicates a good quality of the language. In Python, you can:

  • Immerse yourself in working with artificial intelligence and machine learning;
  • Develop a robot;
  • Develop a GUI (graphical interface);
  • Develop a game;
  • Develop a CAD application;
  • Create various web scenarios;
  • Work with FTP;
  • Work with the network;
  • Process text;
  • Process images;
  • Visualize data;
  • Automate any actions on the computer;
  • Programming mathematical and scientific calculations;
  • Work with xml/html files;
  • And a lot more.

Now let's write a simple Hello World program. Let's look at the example below:

Example (python)
print("Hello World!") 

Please note that the text is inside the quotation marks (they can be both paired and single). We will look at the lines in more detail in the following lessons. Those who are familiar with C++, Java or PHP languages may be surprised by the fact that a semicolon is not placed at the end, that is, the end of the line is the end of the instruction. We will also talk about the syntax of the Python language in the following lessons. Our phrase Hello World!, in addition to quotation marks, is written inside parentheses, which have become mandatory since the third version of Python.

Let's get acquainted with the input() function. Data can be entered into the program using the input function, which reads information from the console. Next is an example of how to save the received data to a variable (about variables you will find out later):

Example (python)
a = input() 

When the interpreter reaches this line, the program execution will stop, and data input to the console will be expected. After entering into the console, the program will continue to work. For example, let's output what was written in the console:

Example (python)
a = input()
print(a) 

Or, output the values of two variables separated by commas:

Example (python)
a = input()
b = input()
print(a + ', ' + b) 

Now let's try to add up the two numbers entered into the console:

Example (python)
a = input()
b = input()
print(a + b) 

If you enter the numbers 2 and 4, the program returns 24. Why is that? The fact is that Python put them together as two strings, not two numbers. In Python, all data is called objects that belong to some type. The type of the object determines what actions can be performed with it. For example, there are objects str (strings), int (integers) and `float' (real numbers).

So, to convert a string from digits to a number, you need to use the int() function. For example, int('2') will return the digit 2. Let's change our program:

Example (python)
a = int(input())
b = int(input())
print(a + b) 

This basic information will help you further learn Python.

Installing Python on Windows

Before you start practicing Python, you need to install it on your computer (if you haven't done it before). Most people use the Windows operating system, so let's start by installing it on it.

To get started, download the installer from the [this link is available to authorized users].

Here is an image or screenshot.
To access, you must log in on the Codebra website.

After downloading the latest version, run the file. The Python 3.9.7 installation window will appear in front of you (at the time of writing the lesson, the latest version). First of all, check the box next to "Add Python 3.9 to PATH", then click "Install Now".

Here is an image or screenshot.
To access, you must log in on the Codebra website.

After the installation is complete, the program will offer to remove the restrictions on the length of the path. You can remove this restriction to run Python from almost anywhere.

Open the command prompt. To do this, write cmd in the search or press the keyboard shortcut "Win + R", type cmd in the window that appears and press Enter. At the command prompt, type py or python. If you did everything right, Python will write its own version and build in response and you can start working with it.

Yes, you can already work with Python, but using the console is not very convenient. Alternatively, write code using notepad (perfect for this [this link is available to authorized users]) and run through the console with the command py or python:

Example (cmd)
py path/name.py 

Installing VS Code

Visual Studio Code is a convenient code editor, moreover, it is free. To get started, you need to download it from the [this link is available to authorized users] and install it.

To make VS Code understand Python, the simplest solution is to install the plugin. To do this, click on the Extensions tab, and write in the search:

Example ()
tag:debuggers @sort:installs 

After that, we find Python and click Install.

Here is an image or screenshot.
To access, you must log in on the Codebra website.

Then download the plugin for syntax highlighting. To do this, enter ext:py3 in the search and install MagicPython. Now restart VS Code and open our file with the extension .py and press F5. The program will ask: what do you run, click `Python File'.

Installing Python on Mac OS

Installing Python on Mac OS is very similar to the steps in Windows. Before installing, you need to check that you can install packages downloaded from third-party resources (not the App Store) in your Mac's settings. To do this, go to system settings, click "Protection and Security" in the "Programs" folder and select the "General" tab. There, find the item "Allow downloads from:" and change it to "App Store for Mac and from the installed developer".

Now download the file from the official website and install it.

Installing Python on Linux

If you have a Linux operating system, then you probably know how to install Python yourself. First, you need to check if Python is already installed on your computer. Open a terminal and write the command python3 --version. If Python is installed, you will receive its version in response.

How to work with IDLE Python

After installing Python on your computer, the IDLE shortcut will appear in the Python folder. IDLE is an integrated development and learning environment that is perfect for a novice programmer. After running IDLE Python, you will see the following window:

Here is an image or screenshot.
To access, you must log in on the Codebra website.

This opened an interactive interpreter – a convenient thing for small experiments. It is also called a shell. The shell is based on REPL. REPL is a cycle of three steps: reading, calculating and output. That is, the Python interpreter shell reads the entered command, executes it and immediately outputs the result.

The Python shell is well suited for small code snippets, but we need to learn how to create and edit text files in the .py format.

To create a file, click the File tab and the New File button in it to create a new file (or just the keyboard shortcut Ctrl + N, before that do not forget to enable the English layout).

A new file will open, which you can save wherever you want with the extension `.py'.

Now try writing the following code:

Example (python)
a = int(input())
b = int(input())
print(a + b) 

To run the code, click the Run tab and the Run Module button in it. Before running the code, Python IDLE will ask you to save the file. After that, the Python shell opens, in which you need to enter one number, which will be saved to the variable a, press the Enter key, then enter the second number and press Enter, it will be written to the variable b. At the very end, the Python interpreter will reach the print() function and add up the two entered numbers.

Despite its simplicity, IDLE Python has a very user-friendly editor. The first thing that catches your eye is the code highlighting. There is also an auto-indentation that appears when a new block of code should start. Most often this happens after writing a colon (:) and pressing the Enter key (line feed).

IDLE Python has code completion if you press the Tab or Ctrl + Space key while typing names, for example, functions or variables.

In Python, as you will learn later, there are a huge variety of functions and methods. Try to write the print function, put an opening parenthesis and wait a bit. A tooltip will appear that specifies the parameters of the print() function.

In the Options tab, click Show Code Context and Show Line Numbers. Now you will have line numbers and a gray box above the code. This field indicates in which block of code the top line is located when scrolling the page.

Errors are something that every programmer encounters. Sometimes mistakes are obvious, sometimes they don't come up for years. Some errors can be noticed while reading the code, and some with the help of debugging tools.

To enable the interpreter debugging mode, select the Debug tab in the Python shell and click the Debugger button, after which the inscription [DEBUG ON] will appear in the console. The last entry means waiting for the interpreter. The Debug Control window will also appear:

Here is an image or screenshot.
To access, you must log in on the Codebra website.

In this window, you can check the value of local and global variables during code execution. We'll talk in more detail about debugging the code later.

In this lesson, we got very superficially acquainted with the Python language, learned how to install it and other auxiliary tools. In the next lesson we will continue learning the basics and move on to variables and comments.

Test task

Two seconds...
For the first time on the Codebra website?

Sorry about this pop-up, they annoy me too.

The codebra.ru educational resource is entirely dedicated to programming and computer security. All courses and lessons are on the main page. For the sake of interest, you can look at the content of courses on Active Directory Pentest, Python, HTML and CSS, JavaScript, C++ and others posted on the main page.

If you haven't found something, then use the site search, which is located on the main page at the very top.

Good luck learning!

Close the window