Python: Quick Start

What is Python?

Python is a programming language like C or C++ with several key differences. First of all, it’s not as fast as C or C++ since it’s a scripting language. This means the code wont compile to a native machine language (10010101), instead a run-time environment will handle the execution. Secondly, Python is a dynamic language, meaning each line of code that the computer reads will get executed at that instant. But, python code will run on any machine (windows, mac, linux) and it’s very easy to learn and code in python.

Installing Python

If you haven’t already installed Python, you can find tutorials on-line on how to install python for your platform (Mac OS, Windows, Ubuntu, Red hat). Since I’m using Ubuntu, I’ll explain the installation process which is one line of bash code. Open up the terminal and type the following command:

$ sudo apt-get install python

Important Facts

  • Python has no curly braces ({}). Instead of Braces python uses indentation to relate which part of code belongs to another. IĀ use tab for indentation but the official website says use “Use 4-space indentation, and no tabs”. However if you use a proper programming text editor it will indent automatically.
  • This is Pythons official tutorials page – this is where most of the material I present here is extracted from.
  • There are two ways to write code in Python. One is straight from the interpreter and the other is within a file. We’ll write everything in a file and execute the code from files. The file which includes the python code has the “.py” extension.

Sample Run

Open up a terminal and create a directory where we can save all our exercise files there. Create a “lesson_1.py” file and write the following code in the file:

#assign 1 to number1
number1 = 1;
#assign 2 to number2
number2 = 2;
#add the two variables and place the result into number3
number3 = number1 + number2;
#print the value to the output
print number3;

on the terminal, cd into the newly created directory. Type python followed by the location of the lession_1.py file:

$ python lesson.py

and you’ll see a number 3 displaying on the output screen. The lines that start with a # are comments, so you can write notes and comments and it won’t affect your program. There is no need to type semicolon after each line however, if your going to write more than two phrases in one sentence you must include a semicolon (;) after each line. Using semicolon the above code will look something like this:

#all in one line
number1 = 1; number2 = 2; number3 = number1 + number2; print number3;

I mentioned above that you can execute your code straight from the interpreter. To do this, type python in the terminal:

garejoor@ubuntu:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

now type in the same code we you wrote in the file above and press enter. You’ll see it will output the same result. To exit the interpreter type exit() and enter.

This entry was posted in Programming Languages, Python and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>