Python 101

Reading Time: 21 minutes

Learn about the Python programming language, how to execute statements and scripts, declare variables, and create a basic Python app. Python 101 – Creating Multiple Processes In this article, you will learn about the pros and cons of using processes, creating processes with multiprocessing, the subclassing process, and more. Python SQL 101 Class Bootcamp Big Data Sciene Data Analytics Tutor NYC, New York 312 285 6886 Learn Python in NYC! Shivgan Joshi is an expert tutor based out of NYC, New York NY. Developed various passive course for bootcamps in Data Analytics, took classes at USA (New York) and India.

  • Chapter 1

Learn Python

Hi. Welcome to Python 101. In the first chapter of this course, I'll go over the ins and outs of the Python programming language. I'll also give you my introduction. We'll end the chapter by making our first program in Python. Here is an overview of contents:

  • About the tutor: Lakshay Arora
  • All about Python: Why Python(features); Why not Python; History of Python; Difference between Python 2 and Python 3; What can a person do with it; Where is it used in the world
  • Python's IDE: IDLE; Your first statement execution in interactive shell and in a script; Command line execution; Arithmetic in IDLE
  • Your first Python program: 'Mailing Address' (Conceptual: comments, print(), case sensitivity, code readability)

About the course

Python 101 helps you to learn Python by building. Whether you are new to programming or an experienced programmer, this series will be of use to you. At the start of each chapter, we will go over what you will be able to do after you have completed the chapter, by showing you programs you will be able to make. Then we will cover the conceptual building blocks of these programs one by one. What will we be covering? As much as we can.

We'll cover everything in a laid-back, smooth fashion, touching on things mentioned in the program(s) leading into the chapter, contrary to the traditional method where a tutor will tell you everything about a particular topic before implementing them. Rest assured, we will cover every single basic building block by the end of this course. You will find end-of-chapter exercises as well, to strengthen your command and enhance your problem-solving skills.

About the Tutor

I am Lakshay Arora, and I work as a System Analyst for Amdocs. When I am on my own, I spend my time exploring various technologies. Over the past 5 years, ever since I started college, I have worked with HTML, CSS, javascript, AJAX, Perl, Java, Servlets and JSPs, Flash, Unix, SQL, Python among other things.

I had huge help from Mr John W Purcell, from Hungary, who runs the famous website Cave of Programming. I should state that while I am not proficient in most of these languages, I do have the required working knowledge. You can view my work at www.lakshayarora.com. I hosted many of my fundamental projects here, before I started this blog with a friend of mine, at djangoSpin, to share all that we know about anything and everything. I have been interested in Python mainly because of its emphasis on readability and succinctness.

I didn’t really like the machine-level work involved with C/C++. Things like pointers, memory management, and other concepts were difficult for me to comprehend, much less effectively apply. Java, as my first programming class in college, was a start but its object-oriented programming (OOP) concepts gave me fits. While I can work with many languages, I never thought to myself that I am proficient in anything other than HTML, but I wanted to devote my career to one major language. I have always been in awe of developers who spend so many years to develop a programming language, so that the general folk can use it for the good of mankind. Hence, my desire to excel in at least one of them, so that I can apply it in real-life situations, to save effort and maximize productivity.

Then I learnt that Google employs Python extensively, and I was fascinated how it could power the most powerful search engine of the world. I researched about it a bit and found that it has a massive footprint all over the internet, and how versatile it is. Then I thought to myself, maybe this is the language for me. As I studied about it, I realized that it is incredibly easy to grasp because of its emphasis on readability, and thought I would share my knowledge with the rest of the world.

Currently, I am working in Django, which is a web framework in Python. Django is an open-source framework that takes the complexity out of web development and at the same time, it gives you control over as much as you want.

All about Python

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale. - Wikipedia

Why Python?

So, why would you be interested in taking up Python? Well, here's why:

  • Easiest-to-learn & Most-enjoyable-to-use: Python is arguably the easiest-to-learn programming language, and the most enjoyable to use.
  • Powerful: Python has all the power you'd expect from a modern programming language. By the end of this course, you'll learn how to process files, how to employ a variety of data structures. If you choose to opt for creating a graphical user interface for your program, you could do that as well. You could create an online blog using Django, create a 2D game with PyGame, create a visual novel, carry out web scraping etc.
  • Main Emphasis: The main emphasis of Python is readability, reusability & maintainability of code and enhancing productivity. We will see this as we proceed further.
  • General-purpose nature: Being a general purpose language implies allowing its user to write software in a wide variety of application domains. You could automate a simple task of mailing yourself a file on your desktop every Thursday at 9 pm, or you could create a visual novel with it, pursue GUI development, make web apps with it, make a blog with Django, so on and so forth.
  • Used frequently to glue projects together: It has bindings for C/C++ and Java so it is common to blend large projects together using interfaces built in Python.
  • Interactivity: Python can be used interactively in the command line to execute one statement at a time. It also comes with IDLE, its own text editor which has an inbuilt interpreter, where you can execute commands one by one, and view the result immediately. The interactivity enables us to create scripts in a flash, which can be used for rapid prototyping. Prototyping, in programming terms is a rough draft of the final product that companies deliver to their customers, with limited features and core functionality intact. Python can also be used in a realtime interpreter for testing code snippets before adding bundling into an executable file.
  • No braces and semi-colons: One of the most popular characteristics of Python is its use of white space to differentiate between statements. Although it is not a genuine reason for anyone to learn Python, but people who have experience with Java, C++, C# will find it a pleasant change to work without braces and semi-colons. Braces in these languages are used to separate blocks of code, and semi-colons are statement delimiters, i.e. to create a barrier between consecutive statements. Python uses indentation for separating blocks of code and carriage return as statement delimiter. This feature has been derived from the language ABC, from which Python itself was largely derived.
  • Cross-platform: Python can be run on Windows and Unix-like systems all the same.
  • It is a Scripting Language: Meaning that it’s not compiled to create the machine-readable code, like a programming language called C++.
  • Enjoyment: Due of Python’s ease of use and built-in toolset, programming can seem like pleasure rather than a tedious chore. Although this may not sound very convincing, it does have a significant impact on the productivity.
  • High Level Language: Now, what that means is that it takes care of a lot of things that will usually throw off a beginner. According to Wikipedia, 'In computer science, a high-level programming language is a programming language with strong abstraction from the details of the computer. In comparison to low-level programming languages, it may use natural language elements, be easier to use, or may automate (or even hide entirely) significant areas of computing systems (e.g. memory management), making the process of developing a program simpler and more understandable relative to a lower-level language.' 'There are three general modes of execution for modern high-level languages: compiled, interpreted, trans-compiled.' We have briefly discussed the first two, with C++ being a compiled language, and Python being an interpreted language. The third kind simply means that the code is translated to another language that is much more widely used, like C.
  • Dynamic Typing/Duck Typing: Python is a dynamic-typed or duck-typed language. Many other languages, like C++, Java etc. are static typed. Meaning that they require the programmer to explicitly mention what type of data each variable is. For beginners, variables are containers for different values that are used in the program. For example, name of a person is of type string (combination of characters), and his age is of type integer. In static typing, you explicitly mention that the variable name is of type string, whereas dynamic typing indicates that if a variable behaves like a string, it is a string, and we need not mention it that it is a string. The reason it is also called duck typing, is that it refers to the duct test, which is attributed to Mr James Whitcomb Riley, and it is as follows 'When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.' If this doesn't make sense to you right now, it will in time, when we dive into the language.
  • Fairly small keywords list: Keywords are reserved words, they have special meaning in the language. Python has the following keywords:
    False class finally is return
    None continue for lambda try
    True def from nonlocal while
    and del global not with
    as elif if or yield
    assert else import pass break
    except in raise
  • Choice of paradigm: Procedural & Object-oriented Programming: Do not be put off by these terms, they only look ponderous. Procedural Programming paradigm suggests a step-by-step process of a developing program, calling procedures/functions/methods/subroutines to perform some processing, then control returns back to the main program. Examples of procedural languages are C and BASIC.
    Object-Oriented Programming (OOP) paradigm deals with making classes. A Class is a prototype of similar objects e.g. a car is a class and Mercedes and BMW are its objects. Each class will have variables and methods (that's what they call functions in this OOP terms) which each of its objects will inherit e.g. variables(properties/attributes) of a car could be its engine power, its top speed, and methods could be ignite(which triggers the ignition of the car) brake, accelerate. Aside from these, the objects can have variables and methods of their own, which are characteristic of these objects only e.g one object may have airbag method, whereas another object may have spoiler variable. I hope you get the gist. I have only scratched the surface here, if you are interested in OOPs, you could try googling it a bit, it is an extremely widely used paradigm. We, at djangoSpin, have a course on Object Oriented Python.
    Python employs both of these paradigms, it in fact offers a choice to the developer, whether to tackle a program step-by-step using functions or creating classes to achieve the same. You could start with procedural approach, which is much easier in my view. Then you could move onto OOP, which can be daunting at first, but Python is a great language to get to grips with the OOP paradigm. Object Oriented programming is widely acclaimed for developing large projects. In this course, we will be following the procedural way of programming.
  • Many lines of interest: You could develop 2D games with PyGame library, you could take up GUI development with Tkinter & PyQT, choose to make web apps with frameworks like Flask, Django etc.
  • Extensive Standard Library: One of Python’s great attributes is that it comes with an extensive standard library — this enables us to do tasks like download an image or any file for that matter from the Internet, start a web server, or manipulate a compressed archive file all with just one or a few lines of code.
  • Strong Community: Python has a huge and strong community. Not only does it have a dedicated newsgroup, but it also has Python Tutor mailing list, especially for beginners. It is located at http://mail.python.org/mailman/listinfo/tutor. The mailing list is not limited to beginners, anyone can ask questions here. Something that is evident is that the community by and large is extremely open and friendly, just like the language is user-friendly.
  • Versatility: Generally speaking, all languages are versatile, but its bindings to C, its readability, its general-purpose nature, wide community, list of libraries available, and a very good development team led by Guido van Rossum, all this makes Python versatile.
  • Well-paid jobs: Python jobs are generally very well-paid. In fact it is one Google's recruiting tactics to see if a person is googling Python terms frequently, and if he is, he may even receive a call from Google, just like this guy here.
  • It is open source: Not only is Python free, but Python's license allows you to even modify it and resell it. By allowing this freedom, Python has epitomized the ideals of open source software and this is one of the many reasons that Python is so popular.
  • Succinctness: One of the features that attracted me towards Python was that you could get a lot done in a few lines of code, as compared to many other languages. Programming languages like Java, C, C# are verbose, but Python is succinct. For example, here's a program in C, C#, Java and Python to achieve the same purpose.

All these features of this language, not only do they make Python a great first language to learn, but it has also lured a lot of high-level programmers.

You have seen why any person may decide to learn Python. However, it is my duty to discuss with you why someone may decide against it.

Why not Python?

Every coin has a flip side, and as per my knowledge, the only flip side to this coin is the speed of execution. The speed of execution of Python programs is not as fast as some of the compiled languages like C and C++. However, Python has been optimized several times, and to be realistic, it is fast enough not to be considered a slow language.

History of Python

  • Designed by Guido van Rossum in December 1989
  • Named after Monty Python’s Flying Circus, a popular British sketch series created by the comedy group Monty Python
  • Developed by Python Software Foundation
  • Successor of ABC language with capability of exception handling
  • Van Rossum is Python's principal author, and his continuing central role in deciding the direction of Python is reflected in the title given to him by the Python community, Benevolent Dictator for Life (BDFL) - Wikipedia
  • Python 2 was released on October 16, 2000
  • Python 3 came out on December 3, 2008

Difference between Python 2 and 3


Python 3 is the latest version of Python, and Python 2 is still very widely used due to the fact that many libraries are not yet available for Python 3, so Python 2 still runs of plenty of systems out there. In fact, many linux distributions come with Python 2.6 or 2.7 pre installed.

However, Guido van Rossum, the creator has made it clear that Python 2 will not be maintained after 2020, to carry out mass adoption of Python 3. So, if you are a beginner, it's safe to start with Python 3.

Just so you know, there is a program called 2to3 to convert the source code of a Python 2 program to the source code of a Python 3 program.

Here are a few notable alterations in Python 3:

Python 101 Book

  • print() is a function and not a statement
  • long data type not present anymore
  • greater support for unicode text
  • xrange() became range()
  • raw_input() became input(), which always returns a string

For further reading, visit this page & this page.

Use of Python in today's world:

  • Google makes extensive use of Python in its web search systems, and employed Python’s creator, Guido van Rossum for 7 years till 2012 before he started working for Dropbox.
  • Youtube, the online video service is mostly written in Python.
  • The popular peer-to-peer file sharing system BitTorrent is programmed in Python.
  • Google’s popular App Engine web development framework uses Python as its application language.
  • Quora, the Q & A site, makes extensive use of Python.
  • Maya, a popular 3D modeling and animation system, provides support for Python.
  • Intel, Cisco, Seagate and IBM use Python for hardware testing.
  • Pinterest, the social network is built with Django, a popular web framework of Python.
  • Dropbox has Python foundations as well.
  • Sublime Text, the famous text editor is a Python program.

Other examples include Reddit, Bitbucket, Spotify, Unity 3D and a lot more.It's like Carbon, you use one or more of these services daily without knowing that it's Python in the background.

Python 101 Course

What can a person do with Python?

  • Automate Tasks: To state a few examples, you could have Python mail particular backup files periodically to your email address, pull text off a web page, search through thousands of excel records looking for ones that you need to update, rename thousands of .jpeg files to .png files etc.
  • Systems Programming: Python comes with a module called os, which is included in the standard installation, using which Python programs can search files and directories, launch other programs, do parallel processing with threads and processes, and so on. Python’s standard library comes with POSIX bindings and support for all the familiar OS tools: environment variables, files, sockets, pipes, processes, multiple threads, regular expression, command-line arguments etc.
  • GUI, Desktop Apps: Using Tkinter, you can easily design neat and sophisticated graphical interfaces to your programs. You may also want to research on PyQT, which is also a popular Python graphical toolkit.
  • Prototyping: It is fairly common to prototype complete projects in Python, and then move the selective components to a compiled language like C or C++ for delivering to the customer. In fact, many components remain coded in Python for easy maintenance and re-usability.
  • Create 2D games with Pygame: Pygame is a very popular python library that was designed so that programmers can utilize it to create games.
  • Create Web Apps: There are many web frameworks built in Python, and you can use them to create full fledged web applications. They support quick construction of full-featured and production-quality websites with Python. Many of these frameworks come with features such as object-relational mappers and are often based on a Model-View-Controller architecture. Examples of such frameworks include: Django, TurboGears, web2py, Flask
  • Hacking Tools: I am not encouraging it, but Python has been well known for its hacking qualities. In fact, many companies carry out penetration testing of their systems by hiring hackers skilled in Python. Penetration testing is a type of testing that exposes the vulnerabilities of a system, network or web applications.
  • Internet/Network Programming: Python's libraries make it incredibly simple to communicate over sockets, process information to CGI scripts, parse XML files, transfer files via FTP protocol and much more. To name a few Python modules used for internet/network programming: httplib, urllib, xmlrpclib, ftplib, smtplib, poplib and telnetlib
  • Cryptanalysis: Cryptanalysis simply means decrypting a cipher, or coded text. Python has been very prominently used for breaking ciphers.
  • Scientific programming using numpy, matplotlib and scipy: The SciPy module is an open source module used by scientists and analysts for carrying out scientific computing, it is powered by Numpy module, which provides multidimensional arrays to facilitate the complex operations. Numpy also powers Matplotlib. Matplotlib is a plotting library, and provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like wxPython, Qt, or GTK+.
  • Big Data Analysis: Many like this:
    The three chevrons (>>>) is what is called a prompt. Once you see the output, the prompt reappears, letting you know that Python is ready for executing more commands.

    This is the interactive shell in action.

    If you want to write a script that does the same, click on File >New File, type the same statement again and save it anywhere on your system, using a .py extension. Click on Run menu > Run module orpress F5, it produces the same result.

    Outside of IDLE, if you execute a .py by double clicking on it, say the one you just created with a single print statement, then you will notice that a console window opens for a brief moment, and then it vanishes. In fact, it would have vanished too quickly for you to read anything printed on the screen. There are two ways for making the screen stay till you have read the message in the print statement, both of which lie beyond the scope of this chapter. Having said that, I'll go over these very quickly.
    First, after the print statement, in a new line, type input(). I'll tell you more about the input function when the time is right. For now, suffice it to say that we have typed this in order to wait for the user to press any key before the program terminates. Save the file and execute the .py by double-clicking on it.
    Second, create a batch file i.e. a .bat file (Windows users), type in python file_name.py, replacing the file_name by your file name and in the second line, type pause. Save this file, and execute it by double-clicking on it. It will produce the same result.

    The difference between the scripting method and the interactive shell is that the interactive shell allows you to type in one statement at a time, where as in the script, you can type in as many as you want before you ask the interpreter to process them in bulk. Scripts help you save time by not having to type the same statements over and over again.

    Command line execution

    You might have noticed that when you were saving the script, the file was saved with a .py extension, that's the extension for Python files. This is the fact that we use to execute Python files from the command line. So, let's do that.

    • For Windows, press Windows + R for the run dialog, type in cmd and press enter.
    • The command line dialog pops up, now, type python complete_path_to_file.py e.g. python C:DirOnefile1.py.
    • A handy way to do this entire process is, go into the directory that contains your .py file. Press shift + right click on your mouse >open command window here, then python file.py.
    • You'll see the same output.

    The way this works is that the Python keyword invokes the python.exe interpreter which executes your .py file. If you want an interactive Python shell in the command window, type in python and press enter. As you can see, it provides the same window as in IDLE.

    • For Mac users, go into the terminal, navigate into your working directory (that is, the directory you have chosen to keep your learning Python files in)
    • Type in touch file.py, edit it with vi or nano by vi name_of_file.py or nano name_of_file.py
    • Give permissions to your user to execute the script by chmod 777 file.py
    • Then typing python file.py in the terminal will execute it.

    The location of Python interpreter in Mac and linux distributions is normally /usr/local/bin/python3. In Windows, it is something like C:Pythonxxpython.exe if you accepted defaults, this address may vary if you installed it somewhere else and/or if you installed a different version of Python.
    When you are executing the Python script from terminal or command line or IDLE, it is safe to assume that they will find the Python interpreter. But it is a good practice to include shebang/hashbang in the first line of your scripts i.e. #!C:Python35python.exe for Windows, again depending on the installation directory, and #!/usr/local/bin/python3 for Mac.

    If you don't like the simplistic look and feel of IDLE, you can always go for a text-editor, like the one I use every now and then, Sublime Text. I really like the IDLE interface, because it is a native Python coded software, but I have worked in Sublime Text for almost 2 years now, I have grown fond of it. You can choose text-editor of your choice.
    I have mentioned this before, you can even execute Python statements online, on the Python.org, or on Repl.it >Python3 in language search bar (according to its homepage as of January 2017). REPL in the programming world stands for Read-Eval-Print-Loop. The interpreter reads the commands, evaluates it, prints the requisite and this goes on.

    Arithmetic in IDLE

    What's 9 times 8.2? You'd probably need a calculator, and if you are smart like my brother, you can work it out in your mind, but that's not the point. You can have the Python console do the math for you. So how does that work? Just put the expression that you would like Python to evaluate at the prompt (>>>) e.g.

    ... and it will give you the result. Granted this is not a ground-breaking feature, it just helps to know this.

    Your first Python Program

    Okay then, time for your first Python program (finally, huh?).

    Try to run it here.

    What does this program do? These lines print the complete mailing address of a person. It is a bunch of print functions, which execute one after the other and throw text onto the console. By default, each print statement ends with a new line, that is, whatever you type inside the quotes of a single print function is printed in the new line. We will see how to use multiple print functions and still have the text on the same line. The print function is extensively used for debugging purposes, that is, checking where the control of the program has reached. You will see this in action as you build complex programs.

    The statements beginning with a hash, are known as comments. These lines are completely ignored by the interpreter. But the usefulness of comments is felt when you trying to make your code readable and well documented. However, keep in mind that you use them for important information only. Redundant comments only contribute to the size of the Python script, and not to readability of code. Takeaway: Do NOT use comments for conveying trivial information. Only use them to convey important information.

    If you type the print function with an uppercase P, or alter its case in any way, and then execute it, you will notice that the interpreter doesn't execute the statement and throws a syntax error. This highlights the case sensitivity in Python. You have to type these keywords in small case only, with a few exceptions such as True, False etc.

    Try typing these statements and executing them in one of the five methods we discussed earlier:

    • interactive interpreter in IDLE
    • interactive interpreter in command line
    • running a script in IDLE
    • running a script in command line
    • executing statements online

    Next Chapter

    In the next chapter, we will learn about variables (in particular, strings, integer numbers, floating point numbers, boolean), about input/output, operators, and how to control the flow of the program with statements. We will build 3 programs by the end of the chapter, namely: Fill the form, Leap Year, Even or Odd. See you then!


    • Write a script to print your name, age, place of residence.

      Expand the below snippet for solution.

    • Execute the above script by Run >Run Module (F5) option in IDLE as well as in command line using the python command.
    • Use the interpreter in IDLE to compute a few mathematical expressions such as 100 - 30, 23 * 5 etc.
    • Chapter 1

    See also:

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 575,000 other learners and get started learning Python for data science today!

Python 101

Welcome to the LearnPython.org interactive Python tutorial.

Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the Python programming language.

You are welcome to join our group on Facebook for questions, discussions and updates.

After you complete the tutorials, you can get certified at LearnX and add your certification to your LinkedIn profile.

Just click on the chapter you wish to begin from, and follow the instructions. Good luck!

Learn the Basics

Data Science Tutorials

Advanced Tutorials

Other Python Tutorials

  • DataCamp has tons of great interactive Python Tutorials covering data manipulation, data visualization, statistics, machine learning, and more
  • Read Python Tutorials and References course from After Hours Programming

Contributing Tutorials

Read more here: Contributing Tutorials