Starting With Python

Thinking about starting with Python? But you don’t know where and how to start? Maybe, you are looking at your laptop and the distro that you are using, and you’re not exactly sure if that’s enough.

Before deciding where you want to code, you should ask yourself two vital questions.

  • What is the environment that you are most comfortable in right now? Is it MAC? Or have you been happy using Windows? Maybe you are a purist and believe everything Linux?
  • What is your end goal? Maybe you want to create desktop GUI apps for Windows users. Maybe it’s a web application that you want to design. So ask yourself what convinced you to pick up Python.

Let's look at the question from these two perspectives and the options available to us.

Linux: Python was originally developed for the Linux environment. It runs seamlessly on the Linux platform. Also, working on multiple versions of Python on the same machine is fairly easy due to the efforts of the Python community and its adoption of PEP 394. Linux itself has many ecosystems. So let's look at some of them.

Ubuntu: By default, both Python 2.7 and Python 3.5 are available in Ubuntu 16.04. If you are using an older version of Ubuntu, and you want to use the latest version of Python, you can run the following commands.

We are installing Python 3.5 here. You can look into the following stackoverflow link if you want to know how to know how to install other versions.

Don't change the symlink and use the "python3.5" command in your scripts/program so that you can gracefully handle any code breakage.

Arch: On the arch Linux distributions, Python 3.5 comes by default and can be used with the “python” command.

RedHat Linux and the CentOS: They also have Python 2.7 in their latest releases. Unix distributions generally do not have Python installed (except some distributions like Solaris) as they are looking to keep the machine as barebones as possible for enterprise applications. Still, if needed, Python can always be easily compiled from source.

Vim and Emacs are widely used for editing Python modules in Linux/Unix.

Windows:Development in windows is also fairly easy. Head over to Python downloads site, then download the latest version of Python3.5. After installing it, the next thing you need to do is set up the path variable to be able to access the Python command.

Owing to Steve Dower's efforts (https://twitter.com/zooba), the Python package on Windows has improved a great deal. The most frequently downloaded version of Python from the python.org site is the 32-bit version. Pip and virtualenv work flawlessly in Windows. Running a Python application will need a basic understanding of the command line although that can be managed if you use a good Windows editors such as Pycharm. It’s not recommended to have multiple Python distributions on the same machine. If you have such a scenario, the best-case solution may be to have multiple batch scripts to call multiple installations as given in this stack-overflow article. This is not the case in Linux and Mac, and multiple installations will give you multiple commands to run them.

Mac: Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. Running Python from the GUI and GUI-based packages might have some quirks in the MAC environment but apart from that everything should be good.

Mac OS X also comes with several standard Unix command line editors, Vim and Emacs among them.

Anaconda: Python faces lot of flak for its distribution and packaging ecosystem. Leaving behind the dark days of easy_install, packaging has improved a lot with the advent of pip and virtualenv. These days, it’s a lot easier to maintain your environment. But sometimes you might want a more streamlined and easier development toolkit. This is especially true if you are a data-science enthusiast. So, for those folks, I recommend installing Anaconda and having “conda” as your primary distribution toolkit.

PyPy: We have mainly been talking about Python implementation in C. People who want to wring out more performance from their Python modules may look into PyPy. It is a replacement of the CPython. It’s fast because the main executable come with a Just-in-Time compiler. PyPy’s x86 version runs on several operating systems, such as Linux (32/64 bits), Mac OS X (64 bits), Windows (32 bits), OpenBSD, and FreeBSD. All non-x86 versions are only supported on Linux. Head over to the PyPy webpage to try out your pypy implementation based on your system architecture and distribution.

So you see, whichever camp you fall into, Windows, Linux or Mac, Python has solutions for all of you.

Starting the Python Interpreter and writing the first program:

Python executes code top to bottom, when written in the correct syntax. To execute the code in our python tutorials, you will need to install python in your machine.

Being a high-level language, Python is executed by interpreters before it is run by the computer. The interpreter, which is a program that executes a statement directly at runtime, can be used either in the script mode, where you execute statements in a code-containing file called a script, or the interactive mode, where small snippets of code are immediately executed after typing commands at the prompt. A statement is nothing but an instruction for the computer to carry out a specific action. You can start the interpreter by typing "python" in the command line terminal and pressing the "ENTER" key. You should get something like this.

Note that there are some lines come with the prompt by default. Two most important pieces of information that you should note: The first line gives the version of the Python that you have started in the terminal. Here, the version is Python 2.7.12.

There is a ">>>" with a blinking cursor. This is the prompt and now the interpreter is waiting for you to write your code. Now write print("Hello World.") and press "ENTER" key.

You can see that Hello World printed below. Congrats! You have your first Python program.

Python on Raspberry Pi and other micro controllers: All the while we have been assuming that you are a Python programmer who is interested in designing and writing good programs that will run on big machines and do complex calculations. But if you are interested in hardware and want to dabble in micro-controllers you can try out microPython or PyMite.

Are you interested in Raspberry Pi? Then rejoice as Python is its primary programming. For example, look at the following simple script that checks if a button is pressed on a Raspberry Pi.

So you see, whichever camp you fall into, Windows, Linux or Mac, Python has solutions.

In this post, you read about the different posts in the Python and how to start your first program. In case there are some new trends coming up which are not mentioned here, do let us know in the comments section below.

References and Further Readings:

About the Author

Joydeep
Joydeep Bhattacharjee is a Category Head (Python) at HackerEarth. He likes to dabble in all things tech and is passionate about open source.
Share