Getting Started with Python
Python is such an interesting language to learn and I know you are excited but before we get started, let’s have a quick discussion of what exactly is Python. Python is a dynamically typed language that was created with code-readability in mind. Too much? Let’s break it down.
Let us take two of the most prominent programming languages, the first being Python and the second would be the more mature C++.
# Python Code my_number = 15 print("Number is", my_number)
// C++ Code int my_number = 15; cout <<"Number is " << my_number;
The above is a comparison of code snippets from the two languages, which give the same output (that is to display “Number is 15”).
We can observe the following:
- Python is a dynamically typed language.
- While defining the variable ‘my_number’ in C++, we had to explicitly state that the variable is of type integer (int), whereas in Python we simply had to assign the value to the variable.
- Python’s code readability.
- We see that the syntax used by Python isn’t as complicated as in C++.
- If a random person were to read the above 2 snippets, they would be able to make more sense of the Python code than the C++ code.
In these upcoming blogs, we will be using Python 3. x, instead of Python 2, throughout this blog. All examples that we will use in this tutorial series are compatible with Python 3.
Why Python 3?
Python 2 was once the most popular version of Python but was overshadowed by Python 3. Python 3 was aimed to eliminate the need of writing code that is repetitive, and over time came to support many functionalities found in Python 2. Some of the differences are
- Python 3 gives a decimal value for division whereas Python 2 only gave an integer as a result.
- Python 2 has a more complicated syntax than Python 3.
How to install Python
Now that we have a brief idea of what Python is, let’s go ahead and install Python. Head on over to the link given below.
I am going to be working with Python version 3.9.2. However, if you feel like getting the latest one (which currently is version 3.10.3) just head over to the link below and select the OS you are working on.
Scroll down till you see the following:
Depending on your system architecture, select the 32/64-bit version.
- If you are not sure about your architecture, open up your command prompt (run cmd in the start menu) and then type the following command and hit enter:
- wmic OS get OSArchitecture
- You should see something like this:
Once you select your version, open it up to install it. When downloading the installer, if you get a warning that says it might harm your PC, go ahead and select the keep option, don’t worry it’s safe.
When you open up the installer you should see a similar window. Make sure to select the option Add Python to PATH, then click on Install Now. (You could select a different directory than the default directory, and that’s fine.)
- We add Python to PATH as this sets up our environment variable for Python.
- To put it simply, environment variables communicate to our programs how their machine is set up. Specifically, with Python, there is a variable called PATH which would specify the location of the Python executable file.
NOTE: This shows Python 3.10.1 but since I am using 3.9.2, it would be the same steps only the version number would be different. I got this version for explanatory purposes alone.
Now that it has been installed you should see a similar option to disable the path length limit. I would personally recommend disabling it as this gets annoying in code editors later on, but I leave this choice up to you.
Finally, click on the close button, and let’s get started with Python. We can simply run a command to verify that our installation went correctly (by checking our Python version).
Open up your command prompt and run the following command:
- python –version
You should see a similar result, but depending on whichever version you selected it would display that version number instead.
What have we learned?
- What is the Python programming language?
- How to install Python?
- How to open the command prompt?
- What are environment variables?
- How to check the installed version of Python?