Python for Data Science

Rashal Ismath
6 min readNov 30, 2020

Python is a high-level, interpreted, interactive, and object-oriented scripting language.

Okay, that’s a lot of information right there. Let’s breaks those down.

  1. Python is a high-level language- Python easier to read, write, and maintain. To run the program that’s written in a high-level language, we have to translate that code into machine language by a compiler or an interpreter (Interpreter explained in the next point).
  2. Python is Interpreted − Python is processed at runtime by the interpreter. We do not need to compile our program before executing it. After we compiled we will have a working program to run. But in interpreted languages, we will only know if it’s working or not when we are running the program. This is similar to PERL and PHP.
  3. Python is Interactive − We can actually sit at a Python shell and interact with the interpreter directly to write our programs (We will see how it’s done below).
  4. Python is Object-Oriented − Python supports Object-Oriented style or technique of programming that encapsulates code within objects. We will talk about OOP at the last in this series before we begin to learn about python libraries.
  5. Python is a Beginner’s Language − Python is a great language for beginner-level programmers. But not to underestimate python is heavily used in so many areas in computer science to write programs.

Okay, that’s enough for the introduction, let’s install python on your machine.

  1. Check if you have python already installed

Now if you are on Mac or Linux you would probably have python already installed on your machine. To see if you already have it, open up the terminal and type python3, and hit enter. If you had pre-installed, you will see the interactive window in the terminal. below I have shown the screenshot of the interactive window.
To check on a windows pc, you have to type python instead of python3 on the command line. Will get the same window as below.

python on cmd

2. Download on windows pc

Okay, so if you are on a windows pc, Go to the official website download page and click the download button with whatever newest version it’s showing to you right now. (If you had already installed python, please update it to python version 3).

python official website

3. Install python on windows pc

After you downloaded it, open up the setup, click on the Add python to PATH check box (that way you will be able to enter the interactive shell via command line easily). And then just click on install now to get it installed.

add python to the PATH

4. Write your First Program

So now that you have installed python on your pc, it’s finally the time to write your first program. First, we will write some code in an interactive shell, later on, we will see how to write code in a python file.

Okay so to get to the interactive shell, open up your command line on windows p and type python, and hit enter. Now if you are on Linux or Mac, you will have to type python3 and hit enter.

python shell

What this shell allows you to do is, write python code and as soon as you press enter after writing a line, it will run your line of code. You can keep on writing codes and it will keep all the variables and stuff on the memory until you exit out of the shell.

To exit out of the shell, type exit() and hit enter.

Okay to write your first line of code type,

print("Hello world")

and hit enter and see what happens. It will print Hello world on the screen.

You can do all sorts of stuff here, let’s do a calculation now. Type these on the shell,

x1=5 hit enter
x2=7 hit enter
y=x1+x2 hit enter
print(y) hit enter

You should get 12 printed out on the screen.

Now x1, x2, and y are called variables. Since we used variables, we can use, values of x1,x2, and y again and again here in different places until we exit out of the shell.

Note: Python syntaxes are case sensitive. Means, capital, simple matters. E.g.- x1 and X1 are two different variables, not the same.

Go ahead and try the subtraction (Hint:- use - instead of + ). We will talk about variables, operations, and stuff later in detail along the way.

python math operations

Pro programming

okay, that’s enough for the interactive shell. Let’s write a program in a python file. So we write python programs in a python file like we write PHP code in a PHP file or java codes in a java file.

We will be using an IDE to write our codes. I will be using my favorite VS code, text editor. You are welcome to install it on your pc. It’s a really good IDE to write python codes. (Hint:- Why use an IDE? good question

We use IDE when we are writing codes because it can make programmer’s lives easier. I suggest you search for the advantages and disadvantages of using IDE’s and different types of IDE’s.)

1) Open up the working directory in VS code

First up, choose a folder to save your works. I’m going to use a folder on the desktop. Open up the VS code IDE, and open your folder from the VS code by going to the File menu and open the folder option.

Tip:- If you are on Linux or MAC, just open up the terminal anywhere you want and type “code .” to open up the vs code on that location, or else use the above procedure to open up the preferred location in the VS code.

VS code

2) Creating a python file

So now all we want to do is create a python file to get started. To do that, in the left-hand top, you will see a button to create a new file, click on that. After clicking on that, give a name to the file, I’m calling mine first.py . You see .py end of the file name. It makes this a python file.

VS code creating new file

3) Write the program to get an input and print it out on the screen

Now that you have a python file let’s write some code. Let’s get write a program to ask the name of the user alright?
Type these in your file and save it.

name = input("Enter your name : ") 
print("Hi, "+ name+".")

What this code is, from the first line, it will ask the user to enter their name when we run this program. After the user entered their name, we will print a message with Hi append to their name.

Note: The program runs from Top to bottom in your file line to line.

python get inputs from the user

4) Run your awesome program

To run the program, from your directory where you saved this file, open up the command line or terminal and go inside that directory, then type python first.py. first.py is the name of my python file, replace it with yours. If you are on Linux or Mac you will have to use python3 instead of python.

After you hit enter, you will prompt to enter your name, here enter your name and hit enter. Tadah! you will see the message printed on the screen.

python print on shell

Okay, that’s pretty much it for the introduction to the python. From the next tutorial we will start learning more python, really really fun stuff. So thank you very much you guys for reading this article and I hope you enjoyed it.

I’ll see you soon.! Bye.

--

--