Hello World - Python
Tags: beginners, python
Published: 2021-01-29 11:58:21

Python is a programming language that has been around for quite a while and that has so much to offer. Since its inception about 30 years ago Python has come a long way, as well as being on and off fashion in many opportunities.

Nowadays, Python has experienced a big boom due to the rising popularity of Machine Learning and the introduction of easy to use tools aimed at exploiting Python’s capabilities in this area.

So, what’s Python about anyway?

Python is actually a general purpose programming language, it’s interpreted and object oriented and its syntax is quite easy to read. It has a huge community behind it, for which it’s easy to find a solution to basically any problem you encounter with it.

You may find that there’s a lot of chatter focused on the specific version you use, whether it’s Python 2 or Python 3. That’s because in the late 2000s Python 3 was introduced, which represented a major jump from its predecessor line Python 2.x and it came with actual incompatibilities.

A lot of things were deprecated, and a bunch of others were introduced. Unfortunately for the Python community this caused a lot of issues. There were already many apps running Python 2, and the transition wasn’t as easy as you may have wanted.

To this day, there are still a lot of apps and libraries running and depending on the previous version, and developers need to keep this in mind when using the language.

Fortunately for us, just like in many other languages like [Ruby](https://rarias.dev/hello-world-ruby/#ruby-version-manager) or [Elixir](https://rarias.dev/hello-world-elixir/#elixir-version-manager), Python also has version and environment managers nowadays, which helps keeping track of different versions and sets of libraries within our projects.

This article is going to be divided into 3 sections: Installation, Basics and Capabilities. Within the capabilities however, there's an incredibly long list, especially in the Machine Learning and Lambda architecture areas that I will discuss in more detail later in different posts.

## Installation

How easy or simple the installation process is will depend on your operating system and what other dependencies you may already have in your system.

If you’re a developer like me who uses other programming languages and tools, or if you’re on an older version of MacOS, you may already have installed dependencies that require either Python 2 or 3.

I will describe the installation process of one of the simplest methods I know, but be aware that you may encounter some resistance on the way. Should you do so, feel free to [drop me a line](https://rarias.dev/about-me/#:~:text=email%3A-,contact%40rarias.dev,-Find) and I'll do my best to help you get up and running.

First thing to keep in mind is that you do not want any of the built-in installations of Python running the show underneath, so the best approach is to use a version manager.

Probably the less intrusive Python version manager out there is [Pyenv](https://github.com/pyenv/pyenv). Unlike others, Pyenv does not depend on Python itself as it's built entirely with shell-scripts. This is a nice thing to have.

There are multiple ways to install Pyenv, you can check their [installation](https://github.com/pyenv/pyenv#installation) page for more information. If you're on MacOS, the easiest way is to use [homebrew](https://brew.sh/).

You can install Pyenv using

```bash
$> brew install pyenv
```

You can verify the installation was done correctly by typing `which pyenv` in your shell.

After this, you need to let your bash initiate pyenv on start. For this you can add the following init script to whatever shell you're using. 

In my case for `zsh` I need to add it to my `.zshrc`  but for you it could be `.bash_profile` or `.bashrc`.

```bash
if command -v pyenv >/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi
```

This will allow Pyenv to manipulate your path and give itself priority inside pyenv environments.

With all these done, we can now set the global pyenv we want to work with.

To see the available versions we have for installation, we can use

```bash
$> pyenv install --list
Available versions:
2.1.3
2.2.3
...
3.9.0
3.9-dev
3.9.1
```

So let's go ahead and install version 3.9.1 and set it globally.

```bash
$> pyenv install 3.9.1
```

Before you set it globally however, have a look at your current installation of python (if any) so you can see how the global version of Python is changed.

```bash
$> python --version
Python 2.7.16 # You may see something well different, this is my MacOS system default

$> pyenv global 3.9.1
$> python --version
Python 3.9.1
```

If you don't see a version change, it may be that the init script hasn't been executed. You can try reloading your bash and checking again. 

That's it, now you have a fully running and updated Python interpreter, with the added ability of installing multiple different versions.

Within projects however, it's best to also use environment managers like [pipenv](https://pypi.org/project/pipenv/). I will write in more detail about it in the future.

## Basics

Python is a really intuitive language, where most code (if done right) can be easily read as if you were reading a textbook.

One main thing to consider is that Python does not have opening and closing blocks in the same way other programming languages do. For this, the language relies on indentation, so if a block is meant to be nested within another block, we indent it a tab further than the parent block. An example of this would be a typical function

```python
def say_hello():
	print("Hello World")
```

Notice the print is a tab space within the function header and there's no closing statement like `end` or `}`.

Let’s test a few things in Python’s interactive console. Like other languages, Python offers an interactive console you can use to test things out. I particularly love them, as it saves me time when I’m unsure about a particular syntax or method. Often-time it’s faster to test it in the console rather than run tests, execute the program you’re writing or browse the documentation.

However, when using recent development tools like [Jupyter notebooks,](https://jupyter.org/) which are heavily used in Machine Learning, you get immediate feedback within the cells. This makes it faster than going back and forth from your development environment to the console.

To start the interactive console, go to your shell and just type `python`

```python
$> python
Python 3.9.1
Type "help", "copyright", "credits" or "license" for more information.
```

Bear in mind that the console should start in the version of Python that you've previously set as global default via `pyenv`. If it doesn't, you may want to exit the console using the `exit()` command and revise the reason why it's not selecting the right version.

The built-in function in Python for console output is `print`. That makes our first Hello World fairly easy.

```python
>>> print("Hello World")
Hello World
```

Python data types and operations don't differ much from other languages

```python
# There's simple arithmetic operations
>>> 5 + 5
10

# There are String concatenations
>>> "Hello" + "World"
'HelloWorld'

# And there are of course String-Integer operations
>>> "Hello" * 5
'HelloHelloHelloHelloHello'

# Hold on, what?
```

That last one wasn't too intuitive, but it's pretty cool. Python is really flexible and allows you to use nifty tricks like multiplying a string against an integer to obtain many of them. There are plenty of cool examples like these, especially when you start using the [numpy](https://numpy.org/) and [pandas](https://pandas.pydata.org/) libraries, which supercharge Python into greatness.

Functions in Python are also pretty similar to other languages, and if you break them down enough it can make your code even more readable.

```python
>>> def mean(values):
	return sum(values) / float(len(values))
...
>>> mean([1,2,3])
2.0
```

This is particularly useful when simplifying complex math operations. A type of function called a lambda, which is just an anonymous function, plays a big part as well in simplifying (or complicating if overusing) your code.

```python
>>> def variance(values):
	mean = lambda v: sum(v) / float(len(v))
	return sum([(x - mean(values))**2 for x in values])
...
>>> variance([1,2,3,4,5])
10.0
```

You may have noticed the way the values are processed in that last variance function, where the `for` loop seems to have a very readable structure. That's a list comprehension and it's one of the easiest ways to go over lists in Python.

The flexibility comes from being able to do mapping operations like

```python
# The following will generate a new list in one pass of the array
>>> [value + 1 for value in range(1, 5)]
[2, 3, 4, 5]
```

but also to apply (usually more complex) functions to each value in the collection

```python
>>> addtwo = lambda a, b: a + b
>>> [addtwo(value, value + 1) for value in range(1, 5)]
[3, 5, 7, 9]
```

## Capabilities

I could write 100 articles listing Python capabilities (ok maybe not me, but someone really into the language), and it still won't be enough to cover it. It is such a versatile and powerful programming language that it can probably be used in any context.

The most recent and popular uses for Python that I'm interested and fond of include Machine Learning and Serverless applications.

### Machine Learning

There are many reasons why Python has been chosen for Machine Learning. It's simple and easy to learn, which allows practitioners to focus on the problem rather than on the language, but at the same time there aren't many major tradeoffs in terms of performance.

There's also a great amount of tools focused on Python that were recently open sourced, and allows for model building with ease.

For many years as well, the Python community have been building Math oriented really powerful libraries that makes manipulating data, again, much easier than in other languages.

### Serverless applications

Serverless gained popularity recently due to the fact that it offers a lot of abstraction and scalability to developers. In Serverless architectures, "servers" are not explicitly defined by the deployers, but rather we focus on particular functions that handle very specific events. 

Platforms such as [AWS Lambdas](https://aws.amazon.com/lambda/) and [Google Cloud Functions](https://cloud.google.com/functions) allow the writing and deployment of such functions with ease, and more importantly, they allow the systems to scale at will, providing more functions when needed (when server load increases for example) and being able to handle higher levels of concurrency without increasing code complexity.

## Conclusion

Python is a powerful yet simple language. There's a lot to learn within its environment, but having now an overview of the language and its capabilities, you can go ahead and get started experimenting with it.

Whether you're interested in Machine Learning or building scalable applications, or simply scripting tasks with an easy to use programming language, Python has a lot to offer you and whatever path you decide to take, I'm sure it's going to be an enjoyable one.

Happy coding!
For more topics, check [my profile](https://dev.to/ronald_arias) or visit [rarias.dev](rarias.dev).