Hello world python интерпретатор

Python Hello World Program

If you landed here, I am assuming you heard about Python programming and want to learn it. Well, that’s great. And the first step in learning any new programming language is to write the infamous Hello World program.

Let’s write our first Python program to print “Hello World” on the console. Let’s first get to the program and then we will learn how a Python program works. But, before that, we have to install Python on our computer.

Downloading and Installing Python

Python comes with different installers for different operating systems. So, the installation process also differs slightly, but it’s super easy and quick to install Python on any operating system.

1. Installing Python on Windows

Python provides a UI-based full installer for Windows. Go to the Python Releases for Windows page and from the “Stable Releases” section, download the Windows Installer EXE file. Just run the installer and it will install Python in Windows. Make sure to check the option to add Python binary folder to PATH to save you extra effort.

Читайте также:  Insert url in html code

2. Installing Python on Mac OS

Go to the Python Releases for macOS page and from the “Stable Releases” section, download the macOS 64-bit universal2 installer package. Run the installer and it will be done in a jiffy.

3. Installing Python on Linux/Unix

Most of the Linux operating systems comes pre-installed with Python. Just run the “python3 –version” command to confirm it and check the version. If you want to use the latest version, follow the instructions on this page on Python Docs.

PyCharm IDE

If you are serious about learning Python, PyCharm IDE is a must. Go to the PyCharm Downloads page and download the free Community version for your operating system and install it. It’s super easy and it will save you a lot of time.

Python Hello World Program

Now that we have installed Python in our system, we are ready to write our Python program to print Hello World. Here is the python script to print “Hello World” on the console.

Yes, that’s it. It could not have been simpler than this.

Here is the output when this script is executed from the PyCharm IDE.

Python Hello World Program Output

It’s the simplest program we will ever write. Let’s execute it from the python command line interpreter too.

$ cat hello_world.py print("Hello World") $ python3.7 hello_world.py Hello World $

Python Hello World Program Execute From Terminal

Python comes with an interpreter, which is a shell-like interface to run python scripts. Let’s see how to print the “Hello World” message on the console from the python interpreter.

$ python3.7 Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello World") Hello World >>>

Python Interpreter Hello World Example

Understanding Python Hello World Program

  • The print() function is one of the built-in functions. This function prints the argument to the console.
  • We are calling the print() function with the string argument “Hello World” so that it would get printed on the console.
  • When we are executing a python script file, statements get executed one by one. The print() statement gets executed and it prints the “Hello World” message onto the console.

Summary

We started our journey to learn Python programming with the conventional “Hello World” program. We installed Python and PyCharm IDE in our system. We learned that the python program can be executed from the PyCharm IDE and terminal. We also got some idea about the python interpreter tool, which is very useful in running small python code snippets.

What’s next?

If you have reached this far, you are destined to learn Python. I would suggest you go through these 5 tutorials next.

Источник

Первая программа на Python «Hello world»

В настоящее время используются только версия Python 3. Разработка и поддержка Python 2 прекращены, так что мы работаем с третей версией во всех статьях.

В какой-то момент ваших приключений в программировании вы, в конце концов, столкнетесь с Python 2. Не беспокойтесь. Есть несколько важных отличий, о которых вы можете почитать здесь.

Если вы используете Apple или Linux, у вас уже установлен Python 2.7 и 3.6+ (в зависимости от версии OS). Некоторые версии Windows идут в комплекте с Python, но вам также потребуется установить Python 3.

Он очень прост в установке на Linux. В терминале запустите:

$ sudo apt-get install python3 idle3

Это установит Python и IDLE для написания кода.

Для Windows и Apple я предлагаю вам обратиться к официальной документации, где вы найдете подробные инструкции: https://www.python.org/download

Запуск IDLE

IDLE означает «Интегрированная среда разработки». В вашей карьере программирования вы столкнетесь с многочисленными интегрированными средами разработки, хотя за пределами Python они называются IDE.

  • Если у вас Windows, выберите IDLE из меню «Пуск».
  • Для Mac OS, вы можете найти IDLE в приложениях > Python 3.
  • Если у вас Linux, выберите IDLE из меню > Программирование > IDLE (используя Python 3.*).

Для Mac OS и Linux, в терминале, воспользуйтесь:

Когда вы впервые запускаете IDLE, вы видите примерно следующее на экране:

Первая программа на Python

Это оболочка Python. А три стрелки называются шевронами.

Они означают приглашение интерпретатора, в который вы вводите команды.

Как написать “Hello, World!”

Классическая первая программа — «Hello, World!» . Давайте придерживаться традиции. Введите следующую команду и нажмите Enter:

Источник

What is Python? How the Interpreter Works and How to Write «Hello World» in Python

Michael Para

Michael Para

What is Python? How the Interpreter Works and How to Write

In this article, I am going to explain what Python is and how the Python interpreter works. Then you’ll write your first «Hello World» program.

What is Python?

Python is a high-level programming language designed to do many tasks. It’s based on the CPython interpreter which translates the Python code into something the machine can read.

Python gives us the ability to use a lot of modules and packages with our code, which are standard libraries built in with the interpreter.

You can use Python to do many tasks such as:

  • Machine Learning
  • Artificial Intelligence
  • Data Visualization
  • Programming Applications
  • Web Applications
  • Language and Game Development
  • Data Analytics

Additionally, Python’s syntax is pretty simple and easy to learn – often it seems that you are just writing a message to someone else. Just make sure you know the indentation rules :).

We can compare Python with other interpreted programming languages such as Java, JavaScript, PHP, and others. But you might be wondering – what is CPython?

In the following section, I am going to focus on the history of the Python interpreter in-depth, then I’ll answer this question.

Python History Overview

The first appearance of the Python programming language was in late 1980s. It was created by Guido van Rossum.

Python was designed to replace the ABC programming language which worked with Amoeba operating system.

Rossum started the implementation in 1989 and he worked on Python alone until 2018.

He named it Python because the first version of it was able to read the BBC comedy script “Monty Python’s Flying Circus”.

The first release was in 1994 under version 1.0 (Python 1.0) and the second release was in 2000, named version 2.0.

In version 2.0, van Rossum added minor features such as collection systems and comprehensions.

The third version was released in 2008 and fixed a basic flaw of the language. They named this version “Py3K”, or Python 3.0.

How Does the Python Interpreter Work?

The Python interpreter is called “CPython” and it’s written in the C programming language. This is the default implementation for Python.

In the following sections, you will understand how the Python interpreter works behind the scenes.

Source Code Analysis

Actually, any translator starts with the source code analysis. Here the Python interpreter receives the source code and initializes some instructions to do the following things:

It follows the indentation rule and checks the Python syntax. Maybe there are some incorrect lines, so it will stop the program from the execution to show the error message.

This phase is called lexical analysis, which means dividing the source code files into a list of tokens

In the following step, the interpreter will generate byte codes. Let’s see how that works.

Byte Code Generation

Once the parser of the Python interpreter receives the tokens, it starts to manipulate the lexical tokens. It generates a big structure called the AST (Abstract Syntax Tree).

The interpreter converts this AST to byte code which means machine language. In Python, the byte code can be saved in a file ending with the “.pyc” extension.

In the following section, you will see how the python interpreter executes these byte codes.

The Python Virtual Machine (PVM)

The Python interpreter initializes its runtime engine called PVM which is the Python virtual machine.

The interpreter loads the machine language with the library modules and inputs it into the PVM. This converts the byte code into executable code such as 0s and 1s (binary).

And then it prints the results.

Note that if an error happens during the PVM process, the executor will terminate the operation immediately to display the error.

Now you will learn how to install Python on your operating system.

If you have no Python software or you’re using a mobile device, you can use any online Python compiler.

How to Install Python

To install Python on your Ubuntu Linux operating system, follow these instructions:

Open your terminal and run the following command to update the Ubuntu local system repository:

image-67

In the next section, you will learn how to write your first program with Python.

How to Write Your First Python Program

In this program, you’re going to print the classic “Hello World” message using the Python programming language.

First, create a folder and name it “CodedTag” then create a file inside and name it as a “page.py”.

Then copy and paste the following Python code:

image-68

Congratulations – you just wrote your first Python program.

Wrapping Up

In this article, you learned what Python is and a bit of its history. You also learned how the Python interpreter works.

Let’s summarize it in a few points:

  1. The interpreter checks and searches for syntax errors and verifies indentation rules. Then it converts the source code via tokenization.
  2. The parser receives the lexical tokens and generates an Abstract Syntax Tree.
  3. The interpreter converts the AST to Byte Code and initializes the Python Virtual machine to execute the byte code and send back the final result.

Thank you for reading, If you’d like to read more of my articles, you can find them on CodedTag. Stay tuned for my next articles.

Источник

Оцените статью