Introduction to Python - A beginners guide to Programming in Python | 2019


I've been programming in python for over 3 years now, but Blogging is something I've picked up recently, in fact, this is my first ever blog, so please abide.


Python is easy to learn and powerful programming language, and with the rise of machine learning, it's all the rage right now. Python as described by the PSF (Python Software Foundation)

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

and however, this definition encompasses the essence of Python as a programming language it neglects to acknowledge the effect it has had on students and developers while learning to build, and scale their applications. After all, I decided to major in Python mostly because it made programming fun, and exciting where languages like C++ or Java even, catastrophically fail to do.

With that said and out of the way let's get to what we'll learn in this blog post 

  1. What is Python?
  2. Why use Python? 
  3. Is Python Slow?
  4. How Python programs work
  5. Setup Python for 
    • Windows 
    • Linux
    • Mac
  6. Learning about the Python interpreter and REPL (Read Eval Print Loop)
  7. Adding Python to your System Path.
  8. Writing your first Python program

 

What is Python?

Python is an interpreted, high-level programming language, which emphasizes code readability, with an immense arsenal of modules and libraries that can make the oldest of the languages envious and for the curious ones Python is itself written in C, in fact, the most popular implementation of Python is CPython itself. Just like that, there are Python implementations for Java called Jython and IronPython. Although the naming scheme, which I personally find a little silly, is quite silly, the software behind it is not and works rather well. No need to get overwhelmed though we'd be focusing on CPython which will work best for our use case. 

 

Why use Python?

Other than being super fun and easy to use, from a developers point of view it's development cycle is quite beneficial for any project, big or small, due to it's interpreted nature, that is, as soon as you're done writing your code you can just hit F5 and run the code as it is, without prolonged compiling process moreover if your code hits an error (which is highly unlikely as you'd following my blog posts), instead of spitting out ugly segmentation fault, Python gracefully prints out the stack trace, with clear explanation of what went wrong and halts the program.

Other major points on why you should use Python are - 

  1. Rapid prototyping 
  2. Extensive availability of Modules and Libraries
  3. A thriving and active community
  4. Less suicidal thoughts
  5. Longer life expectancy 
  6. Modular 
  7. Portable

most of these are self-explanatory however but I'd like to elaborate on a few

Rapid prototyping

Gluing together a concept idea with Python is ridiculously easy. All you need to know the libraries you'd want to include and that's it, in fact, I have a comic I'd like to share with you guys for a better understanding of this concept

programming is fun again

Modular 

This refers to writing a software or a web app in such a manner that you separate the individual functions in such a way that the code you write for one of the functions can be later picked up as it is and used in another application without any or just minor modifications. Much like legos, your one piece code should integrate nicely with another even if you're trying to build something entirely different. 

 

Portable

Python allows you to code on any environment you prefer, it can be, it can be Windows, Mac or my personal favorite Linux, but developing on a specific platform doesn't mean your applications are limited to that environment only, you can easily build applications to run anywhere where you can execute Python code, which is also, pretty much anywhere. 

 

Is Python Slow?

One of the many misconceptions that newcomers seem to have while learning Python is that it's "slow" and hence they don't take another step forward. I'll link to a couple of studies here on this blog which I highly urge you to read, but from my side, I'll give you a simple and brief answer, it all depends on your implementation of your code. For example, imagine you write a script that adds two variables, and b, now the interpreter has to check whether these variables are integer or float, only after deciding the following the interpreter will perform the addition. See what I mean? It's your fuck-ups, don't blame the language. Although I would appreciate you reading these articles Python 3 vs Java - Performance Benchmark  Yes, Python and Slow, and I don't careWhy is Python so popular even after being so slow?.

How Python programs work

This topic in its own is huge, to say the least, but I'll do my best to explain the essentials and provide links for you to read up on. To understand the following we need to understand the main components of a Python program.

  1. The Python Script, or your source code. 
  2. Python interpreter

To have your Python code run as intended you need the Python Interpreter to parse it, i.e. make sense of it. So you ask the interpreter (very nicely) to run the code. Then the interpreter starts reading your code line by line in a linear manner and executes it. After it reaches the EOF (End of File) it closes the interpreter window (you can program it to not). That's it. Pretty easy right? NO! here's the in-depth working python innards working

Installing Python 

Now that I've done a fantastic job selling you the idea to learn Python, let's get started and install it on our system. But one last thing before that, let me just cover one final thing

Python 2 vs Python 3 Conundrum

There are some major functional and behavioral differences between version Python 2 and Python 3 which are sometimes down to the syntactical level, for example, both the Python versions have different ways of printing something onto the console. 

 

# Python 3

print("hello world")

# Python 2 

print "hello world"

In Python 2 print is actually a statement but in version 3's print was converted into a function. With that said, Python 2 will lose it's supports sometime in the year 2020. So it makes sense for us to download and learn Python 3. So let's get on with it and download it 

Installing Python on Windows

Go to Python.org and download version 3.7 which at the time of writing this blog is the latest version. Download either 32bit or 64bit according to your system. Run the installer, and STOP before you click on install now, make sure you tick the following check Add Python 3.7 to PATH. This will install Python on to your computer and add it to your system so you can open your command prompt anywhere on the computer and call upon the glorious powers of Python. Now click install, sit back and relax. After that if your computer needs rebooting, do so and open command prompt and type python on there. You'll be greeted with something like this 

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

This is the Python REPL, more on this later. If you see this congratulation, you've successfully installed Python on your system. Just to mess around type 2 random numbers and add them something like this 

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>38+44

>>>82 

see how it adds the two numbers instantly and output the answer? That's Python working in the background and adding the numbers. I personally use this feature a lot, Python's REPL is said to be the best calculator in the world.

Installing Python on Linux & Mac

It comes preinstalled, just open your terminals and type python3  you'll be greeted with somewhat the same screen. Why use python3? Because Linux and Mac's both have multiple versions of Python so we'd like to specify exactly which version of Python to use. 

 

Python interpreter and REPL (Read Eval Print Loop)

Python's REPL acts something like the built-in Terminal or Command prompt, it can take multiple commands and execute them accordingly however it's only limited to Python's syntax. You can write full-fledged Python programs in it, however, I wouldn't recommend it. As mentioned above, it's a great prototyping tool and not just limited to that you can even gain help by simply typing what you need help about, for example, let's try and get help about Python's print function 

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

>>>

This is one of the best features I find in Python. If you forget something you can just look it up on the fly. 

 

Adding Python to your System Path

In case the installer screwed up and didn't add Python to your path in Windows, use the following command, open PowerShell and paste the following on there 

 

$env:Path = "directory path where python is installed";

however this will be temporary, i.e., you will have to run this command again if you close the existing terminal and open a new one, to make this change concrete

  • Create a folder called WindowsPowershell in your Documents folder
  • create a file inside profile.sp1 file inside that folder and add the above-mentioned line there

so now whenever you open a PowerShell window you'll have Python in your path.

 

Writing and Running Python Script

So to comply with tradition let's write our hello world program in Python

Open your favorite text editor and write 

print("hello world")

now save the file and make sure you save it with .py extension so that the interpreter can recognize it's a Python script. To run our script open PowerShell, or terminal or anything you like in the directory you saved your script in and type the following 

python helloworld.py

hello world

We call the Python interpreter with the command python and pass in our file name to parse and the interpreter executes our file.

So that's all for this post, hope you guys like this. If you did make sure to subscribe to the newsletter and get in touch. I'll see you guys next time.