Python for Data Science
About Lesson

Programming Cycle

Humans use various languages to communicate to each other.

Whereas Computers understand only Machine Language/Computer Language of 1s and 0s.

We write code using various Programming Languages like C, C++, C#, PHP, Java, Python etc.

Higher Level Languages: Programming Languages closer to languages humans use are Higher Level Languages.

Lower Level Languages: Programming Languages closer to language  computers understand i.e. 0s and 1s are Lower Level Languages.

Translators are used to convert code written in Programming Language (source code) into Machine Language/Computer Language:

Translators can be:

Interpreters (Execution is done line by line)

or

Compilers (Whole code is executed in one go)

What is Python ?

Python is a very popular, widely-used, and easy to learn programming language.

Python is a general-purpose programming language.

It is used everywhere, some of the important fields where Python is used are:

  • Data Science
  • Machine Learning
  • Artificial Intelligence
  • Web Development
  • Mobile App Development
  • Game Development

and a lot more…

It supports a variety of paradigms for programming, including structured programming, object-oriented programming, and functional programming.

Python was created in 1989 by Guido van Rossum.

The name Python doesn’t refer to the snake. Apparently, Guido van Rossum was a big fan of Monty Python’s Flying Circus, a TV series by a well-known comedy group from Britain, and he named the language after them.

Downloading & Installing Python

The center of the Python universe is at www.python.org.

Bring up the browser of your choice and go to that address.

 

Python is maintained as an open source project by a group called the Python Software Foundation.

Open Source – There is no single company that owns and/or sells the software.

Move the Slider below to go through various stages to Download and Install Python.

IDLE & the Python Shell

Software Development Environments are applications that are used to write a code.

Some of these environments are free; others can be costly.

There are many different software development environments (applications) that you can use to write code in Python.

You may use IDLE environment for this course.

IDLE is named after one of the founding members of Monty Python (a well-known comedy group from Britain) Eric Idle.

IDLE is free. When you download and run the Python installer, it installs IDLE on your computer.

IDLE environment is completely platform independent i.e. it looks almost identical on a Windows computer, Mac, or Linux system.

To open IDLE go to Start>Python 3.x>IDLE.

This window is called the Python Shell.

In fact, the title of the window should be IDLE Shell 3.x.y.

>>> This symbol is called the chevron prompt or simply the prompt.

When you see the prompt, it means the Shell is ready for you to type something.

Writing your first program in Python

Start writing the following code in the Python Shell:

When you type the word print, IDLE colorizes it.

This is IDLE letting you know that this is a word that it recognizes.

IDLE also turns all the words enclosed in quotes to green.

This also is an acknowledgement from IDLE that it has an understanding of what you are trying to say.

One of the key advantages of the Python language is how readable it is.

See here what you have to do to write the Hello World program in some other popular languages.

C

				
					#include <stdio.h>
int main(void)
{
    printf("Hello World!\n");
    return 0;
}
				
			

C++

				
					#include "std_lib_facilities.h"
int main()
{
    cout << "Hello World!\n";
    return 0;
}
				
			

JAVA

				
					public class HelloWorld {
	public static void main(String[] args) {
	System.out.println("Hello World!");
}
}
				
			

All these languages involve many brackets, parentheses, and semicolons, and many words with meanings that are not immediately obvious.

Using Python writing the same code is very easy as you can see for yourself:

Python

				
					print('Hello World!')

				
			

Creating a Python file

So far you have written code into the Shell.

The Shell is not an appropriate place for writing large programs.

Python, like every other computer language, allows you to put the code you write into a file, save it, open it at any time and run it without having to retype it.

To create a new file in IDLE, you go to the File menu and select New File or use the keyboard shortcut Control+N in Windows.

This opens a new, blank editing window, waiting for you to enter Python code.

You enter your line of Python code in this editing window.

To move to next line you press enter at the end of a line and in this manner you enter your Python code, line by line.

Let’s build a simple program containing three print statements.

Notice that when you open the file, it is named Untitled in the window title.

Enter the following:

				
					print('I am now entering Python code into a Python file.')
print('When I press Return or Enter here, I move to next line.')
print('This is the last line.')
				
			

Notice that when you started typing, the window title changed to *Untitled*.

Saving a Python file

To save the file, click File menu and then click Save.

Alternatively, press the standard Control+S (Windows) or Command+S (Mac).

Enter the name of the file say “Test”.

However, Python filenames should always end with a .py extension.

Therefore, you should enter the name “Test.py” in the Save As dialog box.

The code you write is platform independent.

If you create a Python file on one platform, you can move that file to another platform and it will open and run just fine.

Running a Python file

To run, or execute, the statements in the file, click Run Run Module or press the F5 shortcut key.

If you don’t save the file before you try to run it, IDLE will prompt you by asking you to save the file.

Each time you make changes and you want to test the new code, you must save the file and then run it.

 

If everything went well, the program should print the following in the Shell:

				
					I am now entering Python code into a Python file.
When I press Return or Enter here, I move to next line.
This is the last line.
				
			

After giving the output the Shell will display the prompt >>>

Quit IDLE

Now let’s quit IDLE by clicking click File Exit (Windows) or IDLE Quit IDLE (Mac).

Alternatively, you can press Control+Q (Windows) or Command+Q (Mac) keys.

Opening a previously saved Python file

To open a previously saved python file first open IDLE.

Then you can click File Open and navigate to the file you want to open.

Alternatively browse to the saved Python file using windows explorer and then right-click the file icon.

From the context menu that appears, select the second item, Edit with IDLE.

0% Complete
Scroll to Top