Advertisement
Tech

A Beginners Guide to Linux Programming with Python - Variables and Data Structures

The better the data structures of a programming language, the more concentration you can devote to coding a solution to a problem. Python has a bunch of extremely useful data structures. In this article we take a look at data structures and variables.

By Berry van der Linden
Desk Tech
Reading time 3 min read
Word count 535
Linux Computing Linux commands
A Beginners Guide to Linux Programming with Python - Variables and Data Structures
Advertisement
Quick Take

The better the data structures of a programming language, the more concentration you can devote to coding a solution to a problem. Python has a bunch of extremely useful data structures. In this article we take a look at data structures and variables.

On this page

Text Manipulation

**

#!/bin/python

Advertisement

message = “Welcome to the second part of A Guide to Linux Programming with Python”

print message

Advertisement

Python is very good for solving text-manipulation. Here are some examples:

print message[0] # returns ‘W’

Advertisement

print message[15:20] # returns ‘second’

print message[62:] # returns ‘Python’

Advertisement

You can select a character, or several characters in a string. The # is used for comments. Text manipulation can also be used to do the following:

print message.replace(’the second part’, ‘part 2’)

Advertisement

print message.find(‘Guide’)

The replace method replaces the string (before the comma) found in message with the one after the comma, and the return value is printed. Underneath, the find method returns the position of the sub-string in the string.

Advertisement

Therefore, the following is returned:

Welcome to part 2 of A Guide to Linux Programming with Python

Advertisement

24

Because everything in Python is an object, the string variable has methods also!

Advertisement

As with strings, you can play and manipulate numerical values:

print 5 * 2 + 2.7 # returns 12.7

Advertisement

Again, one does not have to worry about types! :)

You can also multiply a string with an integer.

Advertisement

print 5 * “Python” # returns PythonPythonPythonPythonPython

Data Structures

The better the data structures of a programming language, the more concentration you can devote to coding a solution to a problem. Python has a bunch of extremely useful data structures.

Advertisement

Lists

A list is quite simply a sequence of objects and the objects don’t even have to be the same type. A list is created by listing the items between [ ], while list items are separated by commas.

Advertisement

list = [1, 7, ‘bright’, ‘hub’]

print list[2] # returns ‘bright’

Advertisement

As with text-manipulation with strings, we can manipulate lists.

print list[1:3] # returns [7, ‘bright’]

print list[:3] # returns [1, 7, ‘bright’]

A list’s length can be found using the len() function.

print len(list) # returns ‘4’

Append adds a new item at the end of a list:

list.append(‘rocks’) # will add ‘rocks’ to our list, and returns [1, 7, ‘bright’, ‘hub’, ‘rocks’]

print len(list) # returns ‘5’

Elements can be inserted in a list. We pass the position of where we want to insert the element in to the list, followed by the element we want inserted.

list.insert(2, ‘hey’) # the list now looks like this [1, 7, ‘hey’, ‘bright’, ‘hub’, ‘rocks’]

We can remove elements either by position or value.

list.remove(2) # removes hey

del list[0] # removes element at the beginning of the list (1)

Next Time

In the final part of this series I will summarize “A Guide to Programming Linux with Python” with Tuples, Dictionaries and Sets.

This post is part of the series: A Guide to Linux Programming with Python

Python is a very useful language, whether you need a little script that edits files or want to create a bigger project. Python has all the power you need, including testing, modularization, object-orientation and a massive API. This series will let you get your hands dirty with Python and Linux.

  1. A Guide to Linux Programming with Python
  2. A Guide to Linux Programming with Python - Variables & Data Structures
  3. A Guide to Linux Programming with Python - Tuples, Dictionaries and Sets
Keep Exploring

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement