Once we understand the common social norms of interacting with others, it is a matter of knowing how to express it in the particular language to the person we are interacting with. Similarly once we understand the fundamentals of computer programming, it is a matter of knowing the syntax of a particular language to express what needs to be accomplished. The following details the basic syntax of the Python language which will help get started with it and be able to build solutions for most of the programming tasks. The assumption is that the reader is familiar with fundamentals of programming and have been programming is another language.
Python programs are exectued through an interpreter called python
and falls under the category of interpreted languages i.e. small chunks of code gets executed by an intrepreter program. So it is a pre-req to install the python interpreter. Python programs can be stored in a file with the extension .py
normally referred as python script and passed as input to the python interpreter or python code can be executed through the python interpreter command prompt.
1 2 |
|
Or the python interpreter can be invoked by the python
command. Code can be developed and tested in the python interpreter command line.
1 2 3 4 5 6 7 8 |
|
Variables
Python is a dynamically typed language i.e. the type of the variables are determined during runtime based on what is stored and doesn’t have to be specified when variables are defined. Variable names can be of arbitrary length, can contain letter, numbers and underscore. But variable name need to start with a letter and avoid reserved words (no surprises!!). We can find the type of the variable using the type()
function.
1 2 3 4 5 6 |
|
Operators and Expressions
All the arithmetic operators + - * / % **
are supported by Python and follows PEMDAS (Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction) order of execution. Python 3 has a new operator //
which performs floor division i.e. 5/6
will produce a flot value of 0.8333333333333334
while it is zero in Python 2 and 5//6
will result in zero in Python 3. The + *
operators can be used on String data as in the following examples
1 2 3 4 5 |
|
Also all the standard relational operators == != < <= > >=
and logical operators and or not
are supported. Basic Python expressions are same as in any other language.
Data Structures
Python has inbuilt list, tuple, dictionary
data structures.
Lists are similar to arrays where the indexes are integers and are auto assigned. Lists can store objects of mixed types and they are mutable. Some of the list functions are append
extend
insert(idx,val)
pop
count
sort
reverse
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Tuples are similar to lists but are not mutable and are created by using ( )
instead of []
used for lists. All the functions which can be used to access elements in list can be used to access elements of tuples. Since tuples are not mutable functions which modifies list like append pop del extend
are not supported by tuples.
1 2 3 4 5 6 7 8 |
|
Dictionaries are unordered set of key value pairs and indexed by the keys. { }
are used to create dictionaries and they are mutable. Elements in dictionaries are accessed using the key values. All datatypes including tuples which are not mutable can be used as key values for dictionaries.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
len
is a common function across all the three data structures which can be used to find the length i.e the number of elements stored in them.
1 2 |
|
All the three data structures have iterators which can be used to iterate through all the elements
1 2 3 4 5 6 7 8 9 10 |
|
If you have a list, a tuple can be created using the tuple
function
1 2 3 4 |
|
If you have a tuple, a list can be created using the list
function.
1 2 3 |
|
If you have two lists then a list of tuples can be created using the zip
function.
1 2 3 4 5 6 |
|
When you have a list of tuples as in the previous example, then a dictionary can be created using the dict
function.
1 2 3 |
|
Also when you have a dictionary a list of tuples can be created using the items
dictionary function.
1 2 3 |
|
Note that strings are nothing but list of characters.
Functions and Modules
Sequence of related statements can be grouped in to a function so that it can be reused. Group of related functions can be saved in a file and it is called a module. For e.g. Pythons inbuilt module math stores mathematical functions like sqrt cos sin
etc. To access a function in a module, the module need to be imported into the python script or in the interpreter command line. If you are someone who know C programing, this will be very familiar.
1 2 3 |
|
When the whole module is imported as above, then a specific function need to be called by qualifying it with the module name like in the code above math.sqrt()
. If you are interested only in a certain function is a module, that particular module can be imported as in the following example.
1 2 3 |
|
Note that when a specific function is imported as above, it doesn’t have to be qualified with the module name when the function is called.
Defining a function
Functions can be defined using a def
keyword def function_name(optional parameters):
. The statements which belong to the function is grouped by intendation using spaces. The end of the function is determined by a blank space after the function or begining of another function or end of the file storing the function.
1 2 3 |
|
All parameters and variables defined in a function are all local to the function. Any changes made to the parameters passed to the function will not be reflected in the parent code calling the function i.e. all the parameters are passed by value and not by reference.
When a group of related functions are stored in a file then it becomes a module and the file name is the module name. As with the inbuilt module like math
, functions from user defined modules can be made available for use in python scripts using the import
statement.
Comments
Documenting the code is key. #
can be used to add comments to Python code and anything added after #
until the end of line is considered comment. Also Python supports adding documentationusing docstring
to modules and functions so that they can read by users using the __doc__
function. docstring
starts and ends with """
triple quotes.
1 2 3 4 5 6 7 |
|
Execution Flow Control
Commonly found if
for
while
statements are supported in Python. As in function definition, :
is used to define the start of flow control and the list of statments to be executed is grouped using proper intendation.
1 2 3 4 5 6 7 8 9 10 |
|
Python for statement is different from other languages where users can define an initial value, end condition and an arithmetic expression which can be used to control the number of times the loop gets executed. In python for
statement iterates over the items of any sequence in the order they appear in the sequence. In the previous example the for statement iterates over the list “a”. range
function can create a number list which inturn can be used in for
loops.
1 2 3 4 5 |
|
if
statement supports elif
which prevents deep code indendation and else
clauses.
1 2 3 4 5 6 7 8 9 |
|
while
statement is similar to other languages.
1 2 3 4 5 6 7 |
|
break
and continue
statements are available to control the for
and while
loops.
Console Input Output
raw_input()
function can be used to receive input from users through console. The function takes an optional String, which will be output on console and waits for user to input value.
1 2 3 4 5 6 7 8 9 |
|
Output to console can be done using print
statement which takes in a string. If values need to be passed dynamically to the string to be displayed they can be done using the %
operator.
1 2 3 4 |
|
Note that the values passed should be elements if a tuple.
Files
open
and close
functions can be used to (as you guessed) open and close files. open
function takes in the file name including the path to the file and a character to indicate whether the file need to be opened for read or write. The returned value of open
is a file handle which can be used for file processing and at the end of processing can be used to close the file.
readline
function can be used to read a line from the file at a time and read
can be used to read the complete file. write
function can be used to write data into the file.
1 2 3 4 5 6 7 8 9 10 |
|
Exception Handling
Similar to “try” “catch” block in JAVA for exception handling, Python uses try
except
clauses to handle exceptions.
1 2 3 4 |
|
Executing Python Scripts
As you may have noticed all the code block shown so far is using the Python command line interpreter. If a Python script is created and stored in a file then the script can be executed using the python filename
command.
For e.g., if the following is the contend of the Python script file test.py
1 2 3 4 5 6 7 |
|
Then the script can be executed as below
1 2 |
|
If a file (module) is created with Python functions, the functions can be exected as in the following example. This will avoid the functions to be executed when the module is imported into another script.
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 |
|
What is covered so far could be used to accomplish most of the fundamantal tasks. This is intended to give a quick start on Python so that scripts can be created and used by the reader.