Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Programming in Python for beginners – part 3

for beginner

  • Please log in to reply
No replies to this topic

#1 tux


    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Programming Language:Java, Python, Visual Basic .NET, Fortran, Bash
  • Learning:C++

Posted 30 August 2012 - 01:39 PM

Previous tutorial is available here: http://forum.codecal...nners-–-part-2/

Data structures

Data structures are structures used for holding various data together. Built in data structures in Python are: list, tuple and dictionary. There are more but our focus is on those three.

Lists hold an ordered collection of elements. Imagine list as shopping list where you can add, remove or search for something you want to buy. List is a mutable data type. Mutable means we can change the value of element in a list. Making a list in Python is done using the following syntax:




Graphically our shopping list is represented as:


Every element has its own index. Counting indexes starts from 0. Syntax for accessing elements is


Example: Printing first element in a list:
print shoppingList[0]

When you buy something then you want to remove it from a list.

Example: Removing element from a list by value:

print shoppingList

Sometimes you need to remove element using index. For that purpose we use pop().

Example: Removing element from a list using index:

print shoppingList

If you don't specify index then last element will be removed from a list.

What if you want to remove elements with index in range from 1 to 3?

Example: Removing elements in range from 1 to 3

del shoppingList[1:4]
print shoppingList

Be careful when you are using slicing. Index of last element you want to delete must be increased by 1.

Example: Checking does element exists in a list

print 'apples' in shoppingList

Returned value is boolean type. Boolean type has only two values: True or False. When you are using it in if statements make sure you type it correctly (true is wrong. True is correct)

To get better understanding of lists we will write an application which will exchange values of first element in a list with last element in a list.

print shoppingList

Index -1 is used to get last element in a list.
Use debugger and go through code if you don't understand the code.

Just like we can access elements in a list we can access the letters in a string. Remember: Strings are immutable. It means you can do the following using a list:

print shoppingList

Now, list looks like this:


The value of apple is rewritten with value “Fruit”. This is not true for strings.


The following is legal:

print name[0]

Adding element to a list:


Here are few statements you should test:

name=”This is tricky”
print name[8:]
print name[-1]
print name[:-1]
print name[:4]

This could help you to understand slicing in Python:

Tuples are immutable. Creating tuples is similar to list creation. Instead of using [ and ] we use ( and )

Example: Creating tuple called workingDays


Creating tuple with one element is a little bit different than usual creation.


After element you must put comma as shown in example.

Example: Tuple concatenation

print numberSet1+numberSet2

The output is (1,2,3,4,5,6)

Checking is element in a list:

print 1 in numberSet1

returns True


Dictionaries are mutable. Dictionaries consists of pairs of keys and their values.

Example: Creating dictionary


Pairs in a dictionary are called items.

Printing value of key codecall:

print myFavouriteUrls[“codecall”]

Deleting dictionariy elements:

del myFavouriteUrls['google']

Removing all entries in dictionary:


Today, there is no challenge. In next tutorial we will do programming project.

Attached Files

  • 1

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download