3. The Basics
![]() |
3.1. An example of a Python program
Below is a program demonstrating Python’s basic features.
# -*- coding=utf-8 -*-
# ----------------------------------
def display(string):
# displays string
print "string=%s" % (string)
# ----------------------------------
def displayType(variable):
# displays the variable type
print "type[%s]=%s" % (variable, type(variable))
# ----------------------------------
def f1(param):
# adds 10 to param
return param+10
# ----------------------------------
def f2():
# returns 3 values
return ("one", 0, 100);
# -------------------------------- main program ------------------------------------
# this is a comment
# variable used without being declared
name="dupont"
# a screen display
print "name=%s" % (name)
# a list with elements of different types
list = ["one", "two", 3, 4]
# the number of elements
n = len(list)
# a loop
for i in range(n):
print "list[%d]=%s" % (i, list[i])
# initializing 2 variables with a tuple
(string1, string2) = ("string1", "string2")
# concatenating the two strings
string3 = string1 + string2
# display result
print "[%s,%s,%s]" % (string1, string2, string3)
# using the function
display(string1)
# The type of a variable can be determined
displayType(n)
displayType(string1)
displayType(list)
# The type of a variable can change during execution
n="has changed"
displayType(n)
# a function can return a result
res1 = f1(4)
print "res1=%s" % (res1)
# a function can return a list of values
(res1, res2, res3) = f2()
print "(res1,res2,res3)=[%s,%s,%s]" % (res1,res2,res3)
# we could have stored these values in a variable
list = f2()
for i in range(len(list)):
print "list[%s]=%s" % (i, list[i])
# some tests
for i in range(len(list)):
# only prints strings
if (type(list[i]) == "str"):
print "list[%s]=%s" % (i, list[i])
# more tests
for i in range(len(list)):
# displays only integers greater than 10
if (type(list[i]) == "int" and list[i] > 10):
print "list[%s]=%s" % (i, list[i])
# a while loop
list = (8, 5, 0, -2, 3, 4)
i = 0
sum = 0
while(i < len(list) and list[i] > 0):
print "list[%s]=%s" % (i, list[i])
sum += list[i] #sum = sum + t[i]
i+=1 #i=i+1
print "sum=%s" % (sum)
# end of program
Note:
- line 1: a special comment used to declare the script's encoding type, here UTF-8. This depends on the text editor used. Here with Notepad++:
![]() |
- line 4: the keyword def defines a function;
- lines 5-6: the function’s body. It is indented one tab to the right. This indentation, combined with the colon (:) in the
defstatement, defines the function’s body. This applies to all statements with a body:if,else,while,for,try,except; - line 12: Python manages variable types internally. You can determine a variable’s type using the type(variable) function, which returns a variable of type 'type'. The expression '%s' % (type(variable)) is a string representing the variable’s type;
- line 25: the main program. This comes after the definition of all the script’s functions. Its content is not indented;
- Line 28: In Python, you don't declare variables. Python is case-sensitive. The variable
Nomis different from the variablenom*. A string can be enclosed in double quotes " or single quotes '. So you can write 'dupont' or "dupont*"; - line 31: the expression "xxxx%syyyy%szzzz" % (100, 200) is the string "xxxx100yyy200szzzz" where each %s has been replaced by an element of the tuple. %s is the string formatting specifier. There are other formats;
- line 34: there is a difference between a tuple (1,2,3) (note the parentheses) and a list [1,2,3] (note the square brackets). The tuple is immutable, whereas the list is mutable. In both cases, element number i is denoted by [i];
- line 40: range(n) is the tuple (0,1,2,...,n-1);
- line 74: len(var) is the number of elements in the collection var (tuple, list, dictionary, ...);
- line 86: the other Boolean operators are or and not.
The screen output is as follows:
3.2. Type conversions
Here we focus on type conversions involving data of type str (string), int (integer), float (floating-point), and bool (boolean).
# -*- coding=utf-8 -*-
# type conversions
# int --> str, float, bool
x=4
print x, type(x)
x = str(4)
print x, type(x)
x = float(4)
print x, type(x)
x = bool(4)
print x, type(x)
# bool --> int, float, str
x = True
print x, type(x)
x = int(True)
print x, type(x)
x = float(True)
print x, type(x)
x = str(True)
print x, type(x)
# str --> int, float, bool
x="4"
print x, type(x)
x = int("4")
print x, type(x)
x = float("4")
print x, type(x)
x = bool("4")
print x, type(x)
# float --> str, int, bool
x = 4.32
print x, type(x)
x = str(4.32)
print x, type(x)
x = int(4.32)
print x, type(x)
x = bool(4.32)
print x, type(x)
# Handling type conversion errors
try:
x = int("abc")
print x, type(x)
except ValueError, error:
print error
# various cases of booleans
x = bool("abc")
print x, type(x)
x = bool("")
print x, type(x)
x = bool(0)
print x, type(x)
x = None
print x, type(x)
x = bool(None)
print x, type(x)
Many type conversions are possible. Some may fail, such as those in lines 45–49, which attempt to convert the string 'abc' to an integer. We handled the error using a try/except block. A general form of this block is as follows:
try:
actions
except Exception, Message:
actions
finally:
actions
If any of the actions within the try block throw an exception (signal an error), control immediately transfers to the **except clause. If the actions within the try block do not throw an exception, the except clause is ignored. The Exception and Message attributes of the except statement are optional. When present, Exception specifies the type of exception caught by the except statement, and Message contains the error message associated with the exception. There can be multiple except statements if you want to handle different types of exceptions within the same try block.
The finally statement is optional. If present, the actions in the finally block are always executed, regardless of whether an exception occurred or not.
We’ll come back to exceptions a little later.
Lines 52–61 show various attempts to convert data of type str, int, float, and NoneType to boolean. This is always possible. The rules are as follows:
- bool(int i) is False if i is 0, True in all other cases;
- bool(float f) is False if f is 0.0, True in all other cases;
- bool(str string) is False if string has 0 characters, True in all other cases;
- bool(None) is False. None is a special value that means the variable exists but has no value.
The screen output is as follows:
3.3. The scope of variables
# -*- coding=utf-8 -*-
# variable scope
def f1():
# we use the global variable i
global i
i+=1
j = 10
print "f1[i,j]=[%s,%s]" % (i,j)
def f2():
# we use the global variable i
global i
i += 1
j = 20
print "f2[i,j]=[%s,%s]" % (i,j)
def f3():
# we use a local variable i
i = 1
j = 30
print "f3[i,j]=[%s,%s]" % (i,j)
# tests
i=0
j=0 # these two variables are not known to a function f
# only if the function explicitly declares them using the global statement
# that it wants to use them
f1()
f2()
f3()
print "test[i,j]=[%s,%s]" % (i,j)
Results
Notes:
- The script demonstrates the use of the variable
i, declared as global in the functionsf1andf2. In this case, the main program and the functionsf1andf2share the same variablei.
3.4. Lists, tuples, and dictionaries
3.4.1. One-dimensional lists
# -*- coding=utf-8 -*-
# 1-dimensional lists
# initialization
list1=[0,1,2,3,4,5]
# Iteration - 1
print "list1 has %s elements" % (len(list1))
for i in range(len(list1)):
print "list1[%s]=%s" % (i, list1[i])
list1[1] = 10;
# loop - 2
print "list1 has %s elements" % (len(list1))
for element in list1:
print element
# adding two elements
list1[len(list1):] = [10, 11]
print("%s") % (list1)
# removing the last two elements
list1[len(list1)-2:] = []
print("%s") % (list1)
# Add a tuple to the beginning of the list
list1[:0] = [-10, -11, -12]
print("%s") % (list1)
# Insert two elements in the middle of the list
list1[3:3] = [100, 101]
print("%s") % (list1)
# Remove two elements from the middle of the list
list1[3:4] = []
print("%s") % (list1)
Notes:
- the notation array[i:j] refers to elements i through j-1 of the array;
- the notation [i:] refers to elements i and subsequent elements of the array;
- the notation [:i] refers to elements 0 through i-1 of the list;
- Line 20: print (%s) % (list1) displays the string: "[ list1[0], list1[2], ..., list1[n-1]]".
Results
The previous code can be written differently (bases_03b) using certain list methods:
# -*- coding=utf-8 -*-
# 1-dimensional lists
# initialization
list1=[0,1,2,3,4,5]
# iteration - 1
print "list1 has %s elements" % (len(list1))
for i in range(len(list1)):
print "list1[%s]=%s" % (i, list1[i])
list1[1]=10
# loop - 2
print "list1 has %s elements" % (len(list1))
for element in list1:
print element
# adding two elements
list1.extend([10, 11])
print("%s") % (list1)
# removing the last two elements
del list1[len(list1)-2:]
print("%s") % (list1)
# Add a tuple to the beginning of the list
for i in (-12, -11, -10):
list1.insert(0, i)
print("%s") % (list1)
# Insertion in the middle of the list
for i in (101, 100):
list1.insert(3, i)
print("%s") % (list1)
# Deleting from the middle of a list
del list1[3:4]
print("%s") % (list1)
The results are the same as with the previous version.
3.4.2. The dictionary
# -*- coding=utf-8 -*-
def exists(spouses, husband):
# checks if the 'husband' key exists in the 'couples' dictionary
if(spouses.has_key(husband)):
print "The key [%s] exists associated with the value [%s]" % (husband, spouses[husband])
else:
print "The key [%s] does not exist" % (husband)
# ----------------------------- Main
# dictionaries
spouses={"Pierre":"Gisele", "Paul":"Virginie", "Jacques":"Lucette","Jean":""}
# Iteration - 1
print "Number of elements in the dictionary: %s " % (len(couples))
for (key, value) in couples.items():
print "pairs[%s]=%s" % (key,value)
# list of dictionary keys
print "list of keys-------------"
keys = conjunctions.keys()
print ("%s") % (keys)
# list of dictionary values
print "list of values------------"
values = conjunctions.values()
print("%s") % (values)
# search for a key
exists(spouses, "Jacques")
exists(spouses, "Lucette")
exists(spouses, "Jean")
# deleting a key-value pair
del (spouses["Jean"])
print "Number of elements in the dictionary: %s " % (len(spouses))
print ("%s") % (spouses)
Notes:
- line 17: couples.items() returns the list of (key, value) pairs in the couples dictionary;
- line 22: conjoints.keys() returns the keys of the conjoints dictionary;
- line 27: conjoints.values() returns the values of the conjoints dictionary;
- line 7: spouses.has_key(husband) returns True if the key husband exists in the spouses dictionary, False otherwise;
- line 38: a dictionary can be displayed on a single line.
3.4.3. Tuples
# -*- coding=utf-8 -*-
# tuples
# initialization
tab1=(0,1,2,3,4,5)
# loop - 1
print "tab1 has %s elements" % (len(tab1))
for i in range(len(tab1)):
print "tab1[%s]=%s" % (i, tab1[i])
# iteration - 2
print "tab1 has %s elements" % (len(tab1))
for element in tab1:
print element
# modifying an element
tab1[0] = -1
Notes:
3.4.4. Multidimensional lists
# -*- coding=utf-8 -*-
# multidimensional lists
# initialization
multi=[[0,1,2], [10,11,12,13], [20,21,22,23,24]]
# loop
for i1 in range(len(multi)):
for i2 in range(len(multi[i1])):
print "multi[%s][%s]=%s" % (i1, i2, multi[i1][i2])
# multidimensional dictionaries
# initialization
multi={"zero":[0,1], "one":[10,11,12,13], "two":[20,21,22,23,24]}
# iteration
for (key, value) in multi.items():
for i2 in range(len(multi[key])):
print "multi[%s][%s]=%s" % (key, i2, multi[key][i2])
3.4.5. Links between strings and lists
# -*- coding=Utf-8 -*-
# string to list
string='1:2:3:4'
tab = string.split(':')
print type(tab)
# display list
print "tab has %s elements" % (len(tab))
print("%s") % (tab)
# list to string
string2 = ":".join(tab)
print "string2=%s" % (string2)
# let's add an empty field
string+=":"
print "string=%s" % (string)
tab = string.split(":")
# display list
print "tab has %s elements" % (len(tab))
print ("%s") % (tab)
# Let's add an empty field again
string+=":"
print "string=%s" % (string)
tab = string.split(":")
# display list
print "tab has %s elements" % (len(tab))
print ("%s") % (tab)
Notes:
- line 5: the method string.split(separator) splits the string string into elements separated by separator and returns them as a list. Thus, the expression '1:2:3:4'.split(":") returns the list ('1','2','3','4');
- line 13: 'separator'.join(list) returns the string 'list[0]+separator+list[1]+separator+...'.
3.5. Regular expressions
# -*- coding=utf-8 -*-
import re
# --------------------------------------------------------------------------
def compare(pattern, string):
# compares the string string to the pattern pattern
# display results
print "\nResults(%s,%s)" % (string,pattern)
match = re.match(pattern, string)
if match:
print(match.groups())
else:
print "The string [%s] does not match the pattern [%s]" % (string, pattern)
# Regular expressions in Python
# extract the different fields from a string
# the pattern: a sequence of digits surrounded by any characters
# we only want to extract the sequence of digits
pattern = r"^.*?(\d+).*?$"
# We match the string against the pattern
compare(pattern, "xyz1234abcd")
compare(pattern, "12 34")
compare(pattern, "abcd")
# the pattern: a sequence of digits surrounded by any characters
# We want the sequence of digits as well as the fields that follow and precede it
pattern = r"^(.*?)(\d+)(.*?)$"
# we match the string against the pattern
compare(pattern, "xyz1234abcd")
compare(pattern,"12 34")
compare(pattern,"abcd")
# the pattern - a date in dd/mm/yy format
pattern = r"^\s*(\d\d)\/(\d\d)\/(\d\d)\s*$"
compare(pattern,"10/05/97")
compare(pattern, " 04/04/01 ")
compare(pattern,"5/1/01")
# the pattern - a decimal number
pattern = r"^\s*([+|-]?)\s*(\d+\.\d*|\.\d+|\d+)\s*$"
compare(pattern,"187.8")
compare(pattern, "-0.6")
compare(pattern, "4")
compare(model, ".6")
compare(model, "4.")
compare(model, " + 4")
# end
Notes:
- Note the module imported on line 3. It contains the functions for handling regular expressions;
- line 10: comparing a string to a regular expression (pattern) returns True if the string matches the pattern, False otherwise;
- line 12: match.groups() is a tuple whose elements are the parts of the string that match the elements of the regular expression enclosed in parentheses. In the pattern:
- ^.*?(\d+).*?, match.groups() will be a one-element tuple;
- ^(.*?)(\d+)(.*?)$, match.groups() will be a 3-element tuple.
3.6. Function parameter passing mode
# -*- coding=utf-8 -*-
def f1(a):
a = 2
def f2(a, b):
a = 2
b = 3
return (a, b)
# ------------------------ main
x = 1
f1(x)
print "x=%s" % (x)
(x,y) = (-1, -1)
(x,y)=f2(x,y)
print "x=%s, y=%s" % (x,y)
Notes:
- Everything is an object in Python. Some objects are called "immutable": they cannot be modified. This is the case for numbers, strings, and tuples. When Python objects are passed as arguments to functions, it is their references that are passed, unless these objects are "immutable," in which case it is the value of the object that is passed;
- The functions f1 (line 3) and f2 (line 6) are intended to illustrate the passing of an output parameter. We want the actual parameter of a function to be modified by the function;
- Lines 3–4: The function f1 modifies its formal parameter a. We want to know if the actual parameter will also be modified.
- lines 12–13: the actual parameter is x = 1. The result from line 1 shows that the actual parameter is not modified. Thus, the actual parameter x and the formal parameter a are two different objects;
- lines 6–9: the function f2 modifies its formal parameters a and b, and returns them as results;
- lines 15–16: the actual parameters (x, y) are passed to f2, and the result of f2 is assigned to (x, y). Line 2 of the results shows that the actual parameters (x, y) have been modified.
We conclude that when "immutable" objects are output parameters, they must be part of the results returned by the function.
3.7. Text files
# -*- coding=utf-8 -*-
import sys
# sequential processing of a text file
# This file consists of lines in the format login:pwd:uid:gid:infos:dir:shell
# Each line is stored in a dictionary in the form login => [uid,gid,infos,dir,shell]
# --------------------------------------------------------------------------
def displayInfo(dico, key):
# displays the value associated with key in the dictionary dic if it exists
value = None
if dic.has_key(key):
value = dic[key]
if 'list' in str(type(value)):
print "[{0},{1}]".format(key, ":", join(value))
else:
# key is not a key in the dictionary dic
print "The key [{0}] does not exist".format(key)
def cutNewLineChar(line):
# Remove the end-of-line character from line if it exists
l = len(line);
while(line[l-1]=="\n" or line[l-1]=="\r"):
l -= 1
return(line[0:l]);
# Set the file name
INFOS="infos.txt"
# open it for writing
try:
fic = open(INFOS, "w")
except:
print "Error opening the INFOS file for writing\n"
sys.exit()
# Generate arbitrary content
for i in range(1, 101):
line = "login%s:pwd%s:uid%s:gid%s:infos%s:dir%s:shell%s" % (i, i, i, i, i, i, i)
fic.write(line + "\n")
# Close the file
fic.close()
# open it for reading
try:
fic = open(INFOS, "r")
except:
print "Error opening the INFOS file for writing\n"
sys.exit()
# empty dictionary at the start
dico={}
# read line
line = fic.readline()
while(line != ''):
# remove the end-of-line character
line = cutNewLineChar(line)
# put the line into an array
info = line.split(":")
# retrieve the login
login = info[0]
# remove the first two elements [login, pwd]
info[0:2] = []
# create an entry in the dictionary
dico[login] = infos
# read line
line = file.readline()
# close the file
file.close()
# use the dictionary
displayInfo(dico, "login10")
displayInfo(dico, "X")
Notes:
- Line 37: to terminate the script in the middle of the code.
The infos.txt file:
login0:pwd0:uid0:gid0:infos0:dir0:shell0
username1:password1:uid1:gid1:info1:directory1:shell1
username2:password2:uid2:gid2:info2:directory2:shell2
...
login98:pwd98:uid98:gid98:info98:dir98:shell98
login99:pwd99:uid99:gid99:info99:dir99:shell99
Screen output:

