This post is part 2 of Get started with Python programming series, in which I do my best to share my experiences in learning python and hope that this becomes some sort of guide to help you become a Python programmer.
In today’s post, I explain how to write your first Python program.
If you followed the instructions in my previous post, you already have python installed.
Python programs can be run in two ways, interactively, from the terminal or run as scripts.
To explain what this means, let’s write some python code in interactive mode. Open your terminal and type in ‘python’ without the quotes. This will start the python interpreter.
You should see something like this if the interpreter is running:
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
In this mode, python allows us to start writing python code in the terminal window. There’s no need for it to be compiled, it’s ready the minute you hit the Enter key.
Type this in :
print("Hello World") and hit Enter
You should see this:
>>> print("Hello World!")
There you go, that’s how easy it is to code in python. The interactive mode can also be used as a calculator. Try it!
More complex code
Writing single line code that prints stuff to the screen isn’t fun or useful, so if we want to write more complex code, we need a special weapon that every coder needs: The text editor. Which one you use depends entirely on you and your style, so any text editor(or IDE) will do just fine. I use gedit, which is a text editor that comes pre installed with my Linux distribution. I like it because it offers syntax highlighting and auto indentation(a very useful feature).
Let’s write up more interesting code in the text editor. To do this, launch your text editor and type the following in:
print("Hello Universe! I am a Pythonista in the making")
name = raw_input("What is your name?__")
print("May the force be with you %s in your quest to be a pythonista") %name
It is important to type in the above code as it is and note the spacing difference between the first line and the lines that follow after that. This is no mistake. Let me go through each line to explain what is going on.
This defines a function called hello_world that does not take any arguments
The second line has a print statement that you’re familiar with now. An important point to note here, the lines after the function signature are indented by four spaces. This is done to show that the lines that follow are part of a code block or a suite.
The third line is a variable assignment, the variable name is created and assigned to input that the user will type in.
In the fourth line, we’re just printing out what the user typed in in line 3.
The next step now is to save this file. Save it on your computer with the .py extension appended to it. For example you can save it as hello.py. Once that is done, you can run it from the terminal by navigating to the folder where the file is saved and typing python hello.py
That’s all there is to it. You’re a python programmer now!
Thanks for reading