Py01: Starting With Python

Py01: Starting With Python


The joy of coding Python should be in seeing short, concise, readable classes that express a lot of action in a small amount of clear code -- not in reams of trivial code that bores the reader to death.

- Guido van Rossum


    Let's start with Python. Before starting let's look why people are using Python and why it is consistently one of the most used languages in the world. 

  • Advantages of Python
    • Simple and Easy to remember the syntax
    • Free and Open Source
    • Portable
    • Object-Oriented Language
    • A plethora of third-party libraries
    • Allow integration with other languages like C++ or Java using Jython
    While Python has many advantages, it is not untouched from some disadvantage like
    • It is relatively slow to other languages
    • Don't have great support for mobile computing
    • Some error may still lurk around due to its dynamic nature
    • Database access is still underdeveloped when compared to JDBC/ODBC
    The above disadvantages might be discouraging your idea of learning Python, but wait, look at the highlighted words above. Python is one of the most used languages and while it may not be used in mobile or database applications extensively, it is still used at many places which you may have read in the previous post.

So, how to start with Python. First, we have to install a software which could understand our Python code i.e. Python Interpreter. There are many variants of Python Interpreter available and each is optimized for the different purpose. We'll start with CPython Implementation of Python for Windows which is available on official Python website. 

  • Go to https://www.python.org
  • Hover the cursor on Download and click on Windows
  • Find the stable release which does not have 'rc' or 'a' or 'b' in their version number.
                                        
* For Linux environment, it comes installed with Python and for Mac environment follow this link ->  https://docs.python.org/3.6/using/mac.html
** Many Linux environment may have Python 3.5 already, you can use it or upgrade to Python 3.6.x which will be used in this tutorial


Now you have installed Python but how to verify it. Launch your PC's CMD/Terminal and run one of the following commands.


 cmd> python

 cmd> python3

 cmd> py


If Python is installed properly, it should look something like this:



Congrats you have successfully installed Python in your environment. The three angle brackets (>) indicate the Python prompt. So, after a lot of hard work here comes the fun part;
Open Python prompt, if not opened already, and run following command;

 >>> print("Hello World!!!")

You should see something like this,


If you've reached till here successfully. Congrats.
But, no one writes every command in the prompt each time something needs to be executed. Whenever we want to create a program, we prepare a Python script. A Python script is nothing but a file containing Python code and with .py extension in the end.

So, go ahead and open notepad, and write following code:

GitHub Link#!/usr/bin/python

# the print function
print("Hello World!!")


and save the file as "hello.py" or any name which you want with ".py" extension in the end.

Now, open cmd in the same directory where the file is saved and run following command:

cmd> python hello.py
or
cmd> py hello.py
or
cmd> py -3 hello.py
or
cmd> hello.py

and it should print "Hello World!!" on the console.

Let's break down the above code:
#!/usr/bin/python

This is called shebang line. It indicates how a script should be executed. It tells python launcher how to execute a script. One great thing about shebang line is when a script is executed using 'py' launcher, shebang tells which interpreter is to be used when there are multiple interpreters installed.

# the print function

This line is a comment in the program. A comment is anything which we want the interpreter to ignore. This line will not be red by Python interpreter and hence will not be executed. Comments are useful when we want to describe what function the code is performing.

# TIP: Use comment wherever you feel necessary as it will be easy for someone else to understand your code and also will be beneficial to you when you are going through the code after a long time.

print("Hello World!!")


The print function. print function, as the name indicates, prints anything you want is to print. If you want it's syntax, here it is:

print(  Content  )

Here, content can be anything, ranging from a string to an integer to an object (more on these later). Go ahead, try it yourself. Put your name or your pet name (make sure you put quotes around it) and make it display on the screen.

Well, that's enough for this post, but don't forget your new friend, the Python's print function, as we're also not going to. There will be a full post dedicated to print function in future.

Try It Yourself:
 Print your name/message on the console.


Happy Pythoning.

Comments

Post a Comment