Friday, September 12, 2025

Python and SQLite

Connecting Python to a SQLite database.

WHAT WE'LL DO

1. Create the SQLite database.

2. Create a database table.

3. Put data in the table.

4. Write a Python script to display the data from the SQLite database.

How you use the data in a program is a different topic.

1. CREATE THE SQLITE DATABASE

A SQLite database is a file. Run the following command:

sqlite3 mydatabase.db


You'll land at the SQLite prompt. At this point, nothing has happened. No database file has been created until you create an object, such as a table.

2. CREATE A DATABASE TABLE

At the sqlite> prompt, run the following command to create a table called person.

create table person (
    id integer primary key autoincrement,
    firstname varchar(64) not null,
    lastname varchar(64) not null,
    email varchar(128),
    phone varchar(32)
);











Notes:
On the data fields
  1. id = a unique identifier for each person.
  2. firstname = the first name of the person.
  3. lastname = the lastname of the person.
  4. email = the email address of the person.
  5. phone = the phone number of the person.
On the data types
  1. integer = a whole number (1, 2, 3 ... ) with no decimals
  2. varchar = a "string" of characters. The number in parenthesis is the length of the string

3. PUT DATA IN THE DATABASE

We're going to add the following four people.

ID First Name Last Name Email Phone
1 John Jones jj@email.com +3.456.1234
2 Peter Panda pete@email.com 123.342312
3 Sally Smooth s.smooth@earth.world (0)567.123
4 Jane Jet jj@spacetechnology.in +(2)-666-889977
 
insert into person (firstname, lastname, email, phone) values
    ("John", "Jones", "jj@email.com", "+3.456.1234"),
    ("Peter", "Panda", "pete@email.com", "123.342312"),
    ("Sally", "Smooth", "s.smooth@earth.world", "(0)567.123"),
    ("Jane", "Jet", "jj@spacetechnology.in", "+(2)-666-889977");

4. WRITE A PYTHON SCRIPT TO DISPLAY THE DATA FROM THE SQLITE DATABASE

The SQL command we need to use to fetch the data is:

select id, firstname, lastname, email phone from person;
        

And you get the following results:








And now for the Python code...

        
#!/bin/env python
import sqlite3

db_connection = sqlite3.connect("mydatabase.db")
db_connection.row_factory = sqlite3.Row
db_cursor = db_connection.cursor()
sql_statement = """
	select id, firstname, lastname, email, phone from person
"""
db_cursor.execute(sql_statement)
data = db_cursor.fetchall()
for row in data:
    print(row["id"], row["firstname"], row["lastname"], row["email"], row["phone"])

db_connection.close()
        

If you write that in a file called data.py, you can run it using the command:

python data.py

And you'll get the following results:













Sunday, September 7, 2025

Managing Python Installations using PyEnv - On Linux

I've been using pyenv on the Mac unwittingly since you're not really supposed to use the Python that ships with the operating system.

So you're forced to install Python from python.org, or as many Mac users who are software developers do, install Homebrew and use that package management system to manage all the software development tools.

Linux and Raspberry Pi

The process is the same.
  1. Update the system.
  2. Install required dependencies for pyenv.
  3. Install pyenv.
  4. Update some environment variables.
  5. Reset the shell
  6. Install a Python version using pyenv.
  7. Set the installed Python version as the default.
  8. Start programming!
In addition to that, I'll also show you how to create a Python virtual environment.

Update the system

sudo apt update

You may need to upgrade your system based on the results of the above command. If so, type the following

sudo apt upgrade

Install required dependencies for pyenv

sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git libncursesw5-dev \
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Install pyenv

curl -fsSL https://pyenv.run | bash

Update some environment variables

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc

If you also have a .profile file in your home directory, update that file as well. As well as any .bash_profile or .bash_login files.

Reset the shell

exec "$SHELL"

Or you can logout, and login again.

Install a Python version using pyenv

To see all the commands available in pyenv, type:

pyenv commands

To get help on a specific command (such as install), type:

pyenv install --help

To see ALL the versions of Python that pyenv has available for installation, type:

pyenv install --list

But if you're only interested in the CPython versions starting from 3.0, type:

pyenv install --list | grep -E ' 3\.([1-9][0-9]+)'

Finally, to install a Python version (e.g., 3.13.7), type:

pyenv install 3.13.7

Set the installed Python version as the default

To see all the Python versions that pyenv can manage, type:

pyenv versions

To see the current Python version that's in use, type:

pyenv version

Finally, the easiest way to set the Python version to use (e.g., 3.13.7), type:

pyenv global 3.13.7

pyenv has a couple of other interesting commands for setting the Python version to use depending on whether you just want to use it temporarily in the shell:

pyenv shell 3.13.7

or if you want to use a specific version each time you are in a specific folder (e.g., ~/src/proj1):

cd ~/src/proj1
pyenv local 3.13.7

Now when you switch into that folder, pyenv will make sure to call the correct python version that you specified.

Now all that's left is to...

Start programming!











Saturday, September 24, 2022

Flask Tutorial for beginners - Part 6

FLASK ROUTES

Let's take a short pause and talk about a really important topic. ROUTES.

A route is a PATH taken. What is the path? The path is the part of the URL (or website link) after the domain name. The URL is the address of the webpage. URL stands for Uniform Resource Locator and you can do a Google or Yahoo! search if you want to get more information on it.

Let's continue.

For example, let's assume that your domain name is my.website.com. If you type in the following URL in your browser (http://my.website.com) you should get the home page of the website.

In Flask, the home page is represented by the backslash = /. In fact, the website receives the following route: http://my.website.com/ (note the backslash at the end)

Now here's another request to your website: http://my.website.com/login.

In that request the route is /login. 

And here's another request: http://my.website.com/contact.

In that request the route is /contact.

In a Python/Flask application, when the app receives the URL, it looks at the route and then passes it to a Python function to handle the request. Therefore in your app, you must write functions for all the requests you will receive. If you have a login page with the URL http://my.website.com/login, you must provide a route for /login. If you have a contact form which visitors access via the URL http://my.website.com/contact, you must also provide a function for the route /contact.

Here's a simple example of how an app can handle a login request.


Note two changes:
  1. We have added another import on line 1 = render_template. This is a special utility from Flask that allows us to quickly load a html template. We will see that later.
  2. We have added another route @app.route('/login') - the function under this decorator will be called when the visitor to our website goes to http://my.website.com/login.

Let's now move on to templates.


Friday, September 16, 2022

Flask Tutorial for beginners - Part 5

At this point you are ready to start the webserver and make sure your application is working. From the command line run the app.py file.

python app.py

You'll see output that looks like the following.


Note a couple of warnings regarding the fact that this is a development server.

Note the command I used to run the application.

Now using your web browser, go to the link specified. In the example above, it's 

http://127.0.0.1:5000

There's another way to run the application. Stop the current process using the CTRL+c keystroke. Now start the application again by typing:

flask run

You'll see something like the following.


The information here is similar, except for a couple of things. Firstly, the port number has changed, also, you can see two addresses to get to the application. This is because I have set a couple of environment variables before typing in flask run.

To set the environment variables on Linux/Mac, I typed:

export FLASK_DEBUG=1
export FLASK_APP=app.py
export FLASK_RUN_HOST=0.0.0.0
export FLASK_RUN_PORT=8080

The first export (DEBUG) runs the application in debug mode. This means that errors in the application will be displayed, not just on the console, but also in the web browser. This setting should not be enabled on a production server since users will also be able to see details on all errors.

The second export (APP) tells the command (flask run) which flask module to run.

The third export (RUN_HOST) indicates that the application should be accessible on all available addresses. This is useful if you need to access the application from a machine different from your development machine.

Last export (RUN_PORT) specifies the port that the application should listen to. You won't see this port on regular web requests since most browsers know that http is port 80 and https is port 443.

EXERCISES

You can now run a simple response application. Make the following changes to the application.

  1. Change the response string - instead of Hello World! return something like "Thanks for visiting"
  2. In your response, make one of the words bold (use HTML).
  3. In your response send a HTML link to a website so that when the user clicks on it, they are redirected to that page.
Can you find out how to make the environment variables permanent on your system?










Flask Tutorial for beginners - Part 4

Now that Flask is installed, let's write our first application. This will be a very simple application that returns the famous programming statement - Hello World!

WEBSERVER

One of the great things about developing using Python and Flask is that you get a free development web server while you are developing your application. We will use this to test our application.

A small note on additional tools. I've left out a discussion on what tools you can use to write code. I use Visual Studio Code - the free version - to do most of my work. This code editor is extremely powerful, popular and works on most operating systems. It is definitely available on Windows, Mac and Linux.

However, for most of the work we'll be doing, you can use Notepad on Windows, and any text editor on Mac or Linux. Python comes with a free code editor called Idle. I found this tutorial at Idle at RealPython to be the most informative. But a quick Google search for Python Idle and you will find a ton of resources.

COMMAND PROMPT

Open a command prompt and create a folder for your work. Then inside that folder, create a text file. I normally name my main Python/Flask application file app.py.

Open app.py and type in the following code - we'll discuss it line by line.


Line Number Description
1 This is the import line that imports the flask module into our application.
3 This creates a Flask object. You'll need exactly one of these in your application. This Flask object handles all the requests from the user on the other side of the Internet.
5 & 6 Line 5 is syntax you may not be used to. This is called a decorator. It's job is to add some special features to the function defined right after it. In this example, the decorator says that the route specified by the backslash should be handled by this function. The Python function (created using the def keyword) can be called anything. In our example, we've called it index. In our web application, this means that the user will go to my.website.com/  -   note the trailing slash.

Line 6 is the definition of the actual function that processes the request from the user. In this case, it's only one line, that returns a string - "Hello World!"
10 - 11 These are our typical Python execution lines. If you haven't seen them before, they execute if the module (in this case app.py) is called directly from the command line - but not execute if called from a different module.

That's it. We have some working code, hopefully no syntax error bugs.

Next step is to start the webserver which will run our application and then using a web browser we will test the code.

LOCAL TESTING

As you develop your application, you'll use the special address for your local computer - 127.0.0.1 (or localhost). The Python/Flask environment will make sure that the correct settings (such as opening a port for your browser) are configured.

Let's see how that works in the next tutorial.







Flask Tutorial for beginners - Part 3

INSTALLING PYTHON/FLASK

For Windows users, you can get a copy of Python at Python's official website at Python's Official Website. 

For most other operating systems (Mac and Linux), Python comes pre-installed though you may need to get an upgrade without removing the existing installed version.

But getting and installing Python is not in scope for this tutorial, we'll start with installing Flask.

To install Flask, the best option is to open a command prompt (or terminal) and type in the following command:

pip install flask

That's all

To check if flask is installed, in the same command prompt, launch python and type in the following at the Python prompt:

from flask import Flask

You should see output similar to the diagram below.


In the diagram above you note that the version of flask installed on my machine is version 3.7.3 and the computer is running Linux.

When I typed in from flask import Flask. Note that the Python prompt returns without any error messages.

If you see any error messages it means that Flask is not installed and you can try and run the pip command again. Please note that on some systems, you may have to switch your account to an administrator account in order to install Flask.

PYTHON/FLASK ONLINE

If you don't want to install Python/Flask on your computer, there are excellent resources online. One of the best is Pythonanywhere. You can sign up for a free account which is more than enough for the examples we will be working through.

Now that Flask is installed, let's write our first Python/Flask application. 










Flask Tutorial for beginners - Part 2

WHAT IS FLASK?

Flask is a framework for building web applications using the Python programming language.

A framework makes the work of writing a program easier by providing various building blocks so that you do not have to write a lot of code. For example, most frameworks handle the work of passing requests from the user (at the browser) back to the server.

Flask is considered a lightweight framework not as complete as some other frameworks, for example Django. In Flask, much of the setup work is still left to the programmer and there is a lot of flexibility in case you need to do something that the framework does not provide. In the case of Django, for example, you get a fully functional web application without writing a single line of code. With Flask, however, you need to write a few lines of code to get a simple web page to show.

Because Flask allows you to study closely how the interaction between a browser (your user) and the server (where the data and application lives) works, I think it's a very good place for us to learn how to write web applications.

Here's a rough diagram illustrating the relationship between the user, Python and Flask.



  • The user requests something from the Internet by typing in an address in their browser. 
  • This request reaches your application on the server and Flask answers it by looking at the address.
  • Flask then processes the request by passing it to a function in your Python application.
  • Once your function has completed processing the request, it hand it back to Flask to return the answer to the user.
Here's what a typical request might look like:

The user types in: https://my.website.com/login

The user has requested your website (my.website.com) and path is /login.

In the rest of this tutorial, we'll learn how to manage the path (/login) where we will present the user with a login page for them to type in their username (email) and a password.



At the top-left, the user asks for my.website.com. The request goes through the user's browser to the Internet to the website. Once there, Flask and Python take over to fetch the /login data. Flask then sends that back to the user.

In the next few sections, let's see how that's done.





Friday, August 5, 2022

Flask Tutorial for beginners - Part 1

It's about time I wrote about Python Flask. One of the easiest, most accessible Python frameworks for building software.

Python was originally thought of as a great scripting tool, but there are many examples of great commercial tools built using Python.

Learning Flask opens up a brand new way to automate business processes by building a web interface to your Python business logic.

Huge companies may have significant budgets to buy and customize software for millions of dollars. However, small and medium size businesses can use programming tools to build custom software for almost no cost.

This is probably why Microsoft built VBA (Visual Basic for Applications) into their Microsoft Office suite. And many companies have taken advantage of VBA's excellent programming interface to automate calculations in Excel, or even large documents.

This is what we're going to do:

  1. Talk about what Flask is and what it isn't.
  2. Install Flask (we'll be using a Linux machine (actually a Raspberry Pi) and also a Mac, but Windows will work also.
  3. Run a simple Flask program.
  4. Create a database program using Flask. Here we learn about getting input, cleaning it and then storing it. We'll use a lightweight database (sqlite) for this - so we may branch off and discuss sqlite.
  5. Introduce you to Bootstrap so that we can use some libraries to make the user interface beautiful (and responsive).
With this brief introduction, you should be able to build complex applications since you'll be able to create menu systems, text boxes to accept input, display data from databases and allow users to edit it.

Remember, a building of a hundred storeys begins with a single brick.

Let's get started on that brick.

Friday, April 22, 2022

File processing

Most programming languages have built-in facilities for opening and processing files. In this next set of posts, I'll highlight Python's simple file input/output methods for handling text files.

Most of these facilities can be used for processing input from a variety of sources, such as reading from a URL. And we will do that at the end of this set of posts. For now, let's just get started with a simple set of exercises.

We'll be using the following file:


It's a plain text file, so it can be read by almost all programs that read files. This particular file has 14 lines. Using the VI editor, you can get a line count by typing the following

:set number

or

:set nu

Make sure that you're not in INSERT mode (i.e., the word INSERT does not appear at the bottom left corner of the editor window).

Now onto programming.

Our file is named test.txt. In python, we assign a file variable using the open() function. We will then use this file variable to manipulate the contents of the file, such as reading or writing to the file.

Start your python editor and type in the following. Make sure that you are in the same folder that your file is located.


The file is opened using the following code:

f = open('test.txt', 'r')

f is the file handle, or variable, that we will use to manipulate the file contents.

open() is the function used to open the file. open() takes two arguments, the first is the name of the file you want to open, test.txt. The second is the mode in which you want to open the file. The mode can be:

r = read only
w = read and write (the file is created, if it exists, it's truncated)
a = append (the file is created if it does not exist)

Now that we've opened the file, let's read the first line and display it's contents.















We used the readline() function to read from the file handle f.

We put the contents returned by readline() into the variable line.

Then we used the print() function to display the contents of the variable line.

Note the format for reading from the file into the variable.

line = f.readline()

Now let's read the next line and display it's contents.


















We call readline() again to read the next line.

We use the same variable line to put the contents of the next line.
tabs
Python automatically moved to the next line after we read the first one.

Now let's go to the beginning of the file and use a for loop to read all the lines and display them.


































To go to the start of the file, we use the seek() function. The argument to the seek() function is the position in the file that we want to go to. In our case, we use the argument 0. Which means, to the beginning of the file.

Notice that the file seems bigger than the one we have. This is because the print() function automatically adds a newline after it writes the line to the screen. But each line also has a newline character at the end. So the file looks like it's double-spaced.tabs

We can remove the newline character from the line before we print it to the screen like this.





















The line that removes the newline character from the file is:

line = line.strip()

What this does is remove leading and trailing whitespace. A newline character, or spaces, or tabs at the front or end of the line would be stripped.

Now let's close the file and do some writing exercises.





To open a file for writing, which will also create the file, do the following:




If the file writing-file.txt existed, then python will truncate it. So be careful not to use the name of a file that you already have. For example, if we had used the name test.txt, our file that we used for the reading exercise would have been deleted.

Now let's write a few lines into the new file.






We've written four lines into our new file. Now let's read them.










Wow! What happened? The error message indicates that the file was not opened for us to read. There's a way to open a file for both reading and writing, but the mode we used 'w' was for writing only.

Let's close the file and open it for both reading and writing.


Dictionaries

Python has a dictionary data type.

The dictionary data type stores items as KEY: VALUE pairs. Unlike a list that stores items by their position in the array:

a[0], a[1], a[2], a[3]... 

A dictionary has no concept of order. The programmer determines what the key is, and assigns a value to it.

Consider the following list:












We create an empty list:

l = list()

We then append elements to the list. Recall that the list is ordered from zero.

for x in range(10):
    l.append(x)

The range() function returns a list of integers. In our example, range(10) will return the list:
[0,1,2,3,4,5,6,7,8,9]

Now let's remove the third element (l[2] - since the list is ordered from zero).








Notice how the item at position 2 has gone.

Actually the pop(x) method removes the element which has the value x. In the example above, if there was no "2" in the list, then an error would have been returned. The way to remove the 2-nd element is to use the del method.








In the example above, the element with value "3" is in the index position [2].

The statement:

del l[2]

deletes the "3"

[0, 1, 3, 4, 5, 6, 7, 8, 9]

becomes

[0, 1, 4, 5, 6, 7, 8, 9]

Now let's insert the "3" and the "2" back.









The insert(Pos, Value) function takes two arguments. Pos = the position (zero-based) where we will insert the element, and Value = the value of the element that we're inserting.

And now for the "2"








The function call, l.insert(2,2) will insert the value = 2 as position = 2.

DICTIONARIES are different. They are not based on indexed positions. Each entry in a dictionary has a key that marks where it is. The value is the actual element.

We can create an empty dictionary the following way:







Just like the list() function creates an empty list, the dict() function creates an empty dictionary. We can also create an empty list the following way:

l = []

And we can create an empty dictionary the following way:

d = {}

A dictionary uses curly braces (also known as brackets) to indicate that it's a dictionary.

Now let's add some elements to our empty dictionary. Remember, we need to add a key and an element. Unlike a list that only requires that you specify the element that you want to insert or append. A dictionary has no concept of ordering. We will see later how to sort dictionaries.










Notice a couple of things:

  • The key can be anything. A string or a number.
  • The value can be anything. A string or a number.
  • The key does not have to be related to the value.
A dictionary has an update() method if you want to add more than one key:value pair at once.

Let's see how this works:










Note the statement:

d.update({4: 'four', 5: 'five', 'holiday': 'Kwanzaa', 'lunari': 'X'})

This adds four elements. The keys are:

  • 4
  • 5
  • holiday
  • lunari
The values of each of those keys are:
  • four
  • five
  • Kwanzaa
  • X
It's probably best practice to use the update() method for all inserts so that you get used to it for single or multiple inserts.

Since dictionaries are unordered, to get a list of all the keys, the dictionary provides a keys() method.

This is how it works:





And the dictionary object also has a values() method that retrieves all the values. It works like this:




The keys() method is typically used in a loop to print out all the values. Like this:












The loop iterates through the list. The list is a list of keys. And inside the loop, the following statement runs:

print(key, ': ', d[key])

The print() statement will print a comma-separated list of items and add a newline at the end.

Sunday, November 13, 2011

Lists

Python has a special type called a List. A Python list is a set of objects, they could be numbers, or strings, or other objects, enclosed in a pair of brackets, square brackets, [].


The Python range() function is a special function that returns a list object with a sequence of numbers. In our case, we asked for the range 1 to 5, excluding the last number, 5. If you don't specify the first number, the range will start at zero (0).


See that range(3,3) returns an empty list. That's because the end of the range isn't included and since we requested a range from 3 to 3, it's empty. The second range request (range(3,4)) returns a list of only one element, the number 3.

The range() function is commonly found in for loops.


We loop around five times, from 0, to 1, to 2, to 3, to 4. That's five times. In the first loop;

item = 0
value = (1 + 1) * 1 = 2

item = 1
value = (2 + 1) * 2 = 6

item = 2
value = (6 + 1) * 6 = 42

item = 3
value = (42 + 1) * 42 = 1806

item = 4
value = (1806 + 1) * 1806 = 3263442

One thing to remember about the range() function is that it can become inefficient with very large numbers. When used in a loop the way I've created it above, Python constructs  and allocates for a list object. A special type of list object known as a Tuple. We'll discuss this further later.

To find out how many items are in the list, you use the len() function, similar to a string.


Just as with the string object, you can iterate through the list using a for loop.


We added the numbers in the list using a for loop.

You can join two lists together using the "+", concatenation, operator. This is like the string concatenation operator. And just like the string repeat operator, "*", the same operator is used to make copies of lists.


The list "e" is the concatenation of lists "c" and "d". The list "f" is the list "c" repeated twice. This is similar to what we saw with strings.

And just like we saw how we can take slices of strings, we can also take slices of lists. For a comprehensive discussion of what the start and end indices mean, see the discusion on strings. For now, it's enough to say that the first element in the list has the index zero (0), and like strings, we can also count from the end of the list where the last element has the index -1.


a = [1, 2, 3, 4, 5, 6]

The number "1" has the index zero (0). The number "2" has the index 1. And so on. So the slice:

b = a[0:2]

Takes elements from index zero (0) -- the "1", up to and not including the index 2 -- the "3". So the slice becomes [1, 2].

But guess what, unlike strings, lists are mutable! You can change the list elements inline.


This wasn't possible with a string. In the case of strings, trying to change an element in the string by assigning it as:

s[index] = value

would give you an error message.

You can also delete and insert elements into the list. There are two ways to delete elements. The first is to assign an empty list in the position of the elements you want deleted. For example, say you have the list:

a = [1, 2, 3, 4, 5, 6]

and you want to delete the elements, [2, 3]. These are represented by slice a[1:3]. Here's how you do that.


Python also provides a delete function, called del, which makes this easier to see than empty list assignment.


Lastly let's discuss inserting elements into the list. To insert an element into a position in the list, you have to use slice notation. If you don't, unless you're adding elements at the end of the list, you will overwrite the value at the index.

For example, say you have the list:

a = [1, 2, 3, 4, 5, 6]

and you'd like to insert the value zero (0) right after the 3, you'd do the following:


Notice that slice [3:3] selects elements starting at position 3 (currently occupied by the number 4), but doesn't include that position. So, in effect what happens is that the element in that position is shifted over.

What if you wanted to replace the slice [2, 3, 4] with the single zero?


On the left side of the assignment, we selected the slice that includes [2, 3, 4], that's slice [1:4]. We replace that slice with the list [0].

One thing you have to be careful with about lists is copying them. Unlike strings, when you make assignments to lists, an alias is made.

Look at the following example.


Notice what happened. A list "a" was created with three elements, [1, 2, 3]. The variable "b" was set to "a". At this point, a copy of the list "a" wasn't made. "b" is a reference, or an alias, to the existing list created by "a".

So, when the element at index zero (0) is modified by the statement:

b[0] = 4

It's the same element that's referenced by "a". So when we print out the value of "a", we see that the first element, at index zero, has been changed.

How do you make copies?

Take an entire slice of the list.


In this example, we used the slice operator to make a copy of "a". Remember that when you omit the first parameter in the slice it's assumed that you're starting from the first element, and when you omit the last parameter, it's assumed that you're slicing all the way to the end. So, in this case, we're slicing from the first parameter, all the way to the end. Essentially a full copy of the list.

When we make assignments to the list "b" it doesn't affect the list "a" since we now have two separate lists.

Knowing when to slice and when to assign is very important, because when you pass a list item as an argument to a function, a reference to the list item is passed. If the function modifies the elements of the list, the modifications are global.

Here's an example.


In this example we have a list "a" with three elements, [1, 2, 3]. We then define a function delh() that takes a single argument. It's a list argument because in the function body we can see that it deletes the first element and then prints it.

In our example, the first call to the function is:

delh(a[:])

The argument a[:] is a copy of the list "a". The delh() function deletes the first element, and then prints the remaining elements.

Later, out of the function, when we print the values in "a" we see that it's unaffected. It's not changed.

In the second function call to delh() we do the following:

delh(a)

In this case, we're passing a reference to the list. The delh() function deletes the first element and prints the remaining ones. Later, out of the function, when we inspect our original list, "a", we find out that the first element was deleted.

A last word on lists. A couple of useful functions exists to convert strings to lists, and lists to strings.

The first is split. This is a string function that will split a string, based on whitespace, or any string, into a list.

Example:


In the first example, the list "l" is created using the following line:

l = string.split(s)

Because we haven't specified how to split the string, Python splits it based on whitespace. So we get a list of the words.

In the second split command,

ll = string.split(s, 's')

We instruct Python to split the string at each occurrence of the string, or letter, "s". The "s" won't be included in the result, but spaces will.

Now, if you have a list of elements, you can create a string using the "join" function.


In the first join statement, since we haven't instructed Python how we'd want the list join performed, Python joins the elements of the list using a single space. However, in the second example, we ask Python to join the elements using the string "::", two colons.

This has been a fairly long section on lists. The next one is very short, because most of the material covering lists also pertains to that special type of a list, called a Tuple.

String operations - Part 3 of 3

Python Strings are immutable. Which means that once you create one, you can't change it. In part 2 of our discussion on strings, we saw how to extract a character from a string.


However, it's illegal to try and replace a character inline. The string "a" in the example above cannot be changed.


You get the error message displayed above. That the 'str" object does not support assignment. If you want to change the contents of the string, the only thing to do is create another one. Using slices, you can do the following:


What we did is take the letter "m", add, or concatenate, it to the rest of the original string "a" starting at position 1 ("ack") and then assign it to a new string called "a." Looks like we modified the original string "a" but in effect what Python does is destroy the old string "a" and create a new one. This allows us to do the following:


In each of the assignments above, the original string "a" is destroyed, a new string is created with the characters on the right side, and that's assigned to the variable "a." So, a="jack" is a different string from a="jill".

We've already seen how to find the length of a string using the Python function len().


We can also use the for loop in Python to count the characters.


The string module has a find() function. This function allows you to find a character, or another string, inside a string. In order to use the find() function, we have to import the string module.


Notice how we call the find() function using the string module class identifier. The find() function is called a class function. It requires two arguments; the string that you're searching and the substring to look for.

In our example:

string.find(a, "n")

This instructs the find() function to look for the "n" string inside the "a" string. The "a" variable points to the string "canada". In that string, "n" is at position 2, remembering that the first character is at position 0.

If the find() function does not find the string that we're looking for, it returns the number -1.


See that the value of "i", in the second find() is -1 because the string "canada" does not contain the letter "q" that we're looking for.

The find() function is not limited to looking for simple characters, it can also find entire words.


In the find() function call above, the word "canada" is at position 36 in the string "a". The number 36 is the starting position of the word "canada". It's the position where the "c" in "canada" is.


In the example above, after finding the position of the string "canada" we extracted it. Not a very useful example but illustrative of the fact that you can use an example of this sort to pull out words, based on whitespace.

The string module contains some useful functions that will help when analysing text.


How would you use these? Well, you can use the string.lowercase variable to test if a letter in your particular string is lowercase. Here's an example that counts lowercase and uppercase characters in a string.


We have our string with upper and lowercase characters, "s". We then use a for loop to loop through each character. If its a lowercase character, we increment the value of the lowercount variable. If its an uppercase character, we increment the value of the uppercount variable. Notice how we use the find() function.

string.find(string.lowercase, item)

"item" is the character we're looking for.
string.lowercase is the string we're searching in. string.lowercase contains all the lowercase characters. If we can't find the character, the find() function returns the value -1. Otherwise it returns a value from zero (0) to the length of the string.

There is another, more elegant, way to do something like this. That's using the string "in" operator. The "in" operator checks to see if one string is "in"side another. For example, if the string is "canada" and we issue the following statement:

a = "can" in "canada"

The value of "a" will be True. Because the string "can" is inside "canada"

Here's the counting program, written using the "in" operator.



The string module has a number of interesting functions that you can use to manipulate strings. Functions to modify strings from uppercase to lowercase and vice versa. To split strings at whitespace into words. To convert strings to numbers.