DEV Community

loading...

๐Ÿ Python: Crash course

zigabrencic profile image Ziga Brencic ใƒป3 min read

๐Ÿ Python series:

Python is a scripting language that gained in popularity in recent years.

Official site here.

How language came to be?

Python was created in the late 1980s by Guido van Rossum as a Christmas project. Heh like this newsletter :P

The language was then officially released for the first time to the public in 1991.

Python is a scripting language that is interpreted, rather than compiled into the machine code, which is one of the major reasons why python is by design slower than C++.

Sure there's the option of converting python to "C" with Cython to achieve speedups. But pure C and C++ will always be faster than the snake ๐Ÿ .

Python versions

There are two major versions of python out there at the moment:

  • python 2 which you should use only if you have to work with legacy code.
  • python 3 with the latest version 3.9.1. You can find the docs here.

Side note. macOS still ships with python2 but is finally switching from python2 so if you run:

> python2
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Enter fullscreen mode Exit fullscreen mode

How to start python?

To access python3 from the command line simply run:

> python3
Enter fullscreen mode Exit fullscreen mode

This will open the following command line interpreter:

> python3
Python 3.9.0 (default, Nov 21 2020, 14:01:50)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Enter fullscreen mode Exit fullscreen mode

in which you can run commands.

Hello world

To run the classical hello world type into the terminal:

>>> print("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

and press enter. That's it.

That's one way of using python, but I'm personally not a big fan of it. The alternative is to write scripts, which is a way to go if you indent to write more then five lines of code.

Run a script from the command line

You can write scripts and run them directly from the terminal.

Create a file called hello_world.py and write to it the following line:

print("Hello world!")
Enter fullscreen mode Exit fullscreen mode

Close the file.

Now run in the command line:

> python3 hello_world.py 
Enter fullscreen mode Exit fullscreen mode

You should see:

> python3 hello_world.py 
Hello, world!
Enter fullscreen mode Exit fullscreen mode

Before we wrap up, there's something else I want to cover. Above example has one slight issue. It works fine in our simple case, but the moment our software grows into multiple scripts such "direct" commands become an issue.

If we imported the hello_world.py file to another python program, the print statement would be executed. But we want to execute the print command only if we ran the program directly python3 hello_world.py and not when we import our script.

To solve the issue, we can use something called __main__.

__main__

To achieve the desired behaviour we correct our hello_world.py example into:

if __name__ == "__main__":
    print("Hello, world!") 
Enter fullscreen mode Exit fullscreen mode

Where __name__ will be equal to "__main__" only if we run our hello_world.py script directly. If we import our script to another python program __name__ will be different.

Now once we run the script python will check if global variable __name__ is set to "__main__" and then execute our code.

Discussion (0)

pic
Editor guide