Python hello world command line

How To Write Your First Python 3 Program

The “Hello, World!” program is a classic and time-honored tradition in computer programming. Serving as a simple and complete first program for beginners, as well as a good program to test systems and programming environments, “Hello, World!” illustrates the basic syntax of programming languages.

This tutorial will walk you through writing a “Hello, World” program in Python 3.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Writing the “Hello, World!” Program

To write the “Hello, World!” program, let’s open up a command-line text editor such as nano and create a new file:

Once the text file opens up in the terminal window we’ll type out our program:

Let’s break down the different components of the code.

print() is a function that tells the computer to perform an action. We know it is a function because it uses parentheses. print() tells Python to display or output whatever we put in the parentheses. By default, this will output to the current terminal window.

Some functions, like the print() function, are built-in functions included in Python by default. These built-in functions are always available for us to use in programs that we create. We can also define our own functions that we construct ourselves through other elements.

Inside the parentheses of the print() function is a sequence of characters — Hello, World! — that is enclosed in quotation marks. Any characters that are inside of quotation marks are called a string.

Once we are done writing our program, we can exit nano by typing the control and x keys, and when prompted to save the file press y .

Once you exit out of nano you’ll return to your shell.

Running the “Hello, World!” Program

With our “Hello, World!” program written, we are ready to run the program. We’ll use the python3 command along with the name of our program file. Let’s run the program:

Читайте также:  Composite pattern java примеры

The hello.py program that you created will cause your terminal to produce the following output:

Let’s go over what the program did in more detail.

Python executed the line print(«Hello, World!») by calling the print() function. The string value of Hello, World! was passed to the function.

In this example, the string Hello, World! is also called an argument since it is a value that is passed to a function.

The quotes that are on either side of Hello, World! were not printed to the screen because they are used to tell Python that they contain a string. The quotation marks delineate where the string begins and ends.

Since the program ran, you can now confirm that Python 3 is properly installed and that the program is syntactically correct.

Conclusion

Congratulations! You have written the “Hello, World!” program in Python 3.

From here, you can continue to work with the print() function by writing your own strings to display, and can also create new program files.

Keep learning about programming in Python by reading our full tutorial series How To Code in Python 3.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Tutorial Series: How To Code in Python

Python banner image

Introduction

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Источник

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.

Читайте также:  Read file java from end

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.

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.

Читайте также:  List and arraylist java example

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 Getting Started

Many PCs and Macs will have python already installed.

To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe):

To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:

If you find that you do not have Python installed on your computer, then you can download it for free from the following website: https://www.python.org/

Python Quickstart

Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.

The way to run a python file is like this on the command line:

Where «helloworld.py» is the name of your python file.

Let’s write our first Python file, called helloworld.py, which can be done in any text editor.

Simple as that. Save your file. Open your command line, navigate to the directory where you saved your file, and run:

Congratulations, you have written and executed your first Python program.

The Python Command Line

To test a short amount of code in python sometimes it is quickest and easiest not to write the code in a file. This is made possible because Python can be run as a command line itself.

Type the following on the Windows, Mac or Linux command line:

From there you can write any python, including our hello world example from earlier in the tutorial:

C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type «help», «copyright», «credits» or «license» for more information.
>>> print(«Hello, World!»)

Which will write «Hello, World!» in the command line:

C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type «help», «copyright», «credits» or «license» for more information.
>>> print(«Hello, World!»)
Hello, World!

Whenever you are done in the python command line, you can simply type the following to quit the python command line interface:

Источник

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