Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Programming in Python for beginners – part 3

for beginner

  • Please log in to reply
No replies to this topic

#1 tux

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
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:

nameOfList=[element1,element2]

Example:

shoppingList=['apples','milk','orange','cookies']

Graphically our shopping list is represented as:

Selection_002.png

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

listName[index]

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:

shoppingList.remove['milk']
print shoppingList

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

Example: Removing element from a list using index:

shoppingList.pop(2)
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.

firstElement=shoppingList[0]
lastElement=shoppingList[-1]
shoppingList[0]=lastElement
shoppingList[-1]=firstElement
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:

shoppingList[0]=”Fruit”
print shoppingList

Now, list looks like this:

fruit.png

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

name='Tux'
name[0]='a'

The following is legal:

name='Tux'
print name[0]


Adding element to a list:

shoppingList.append(“beer”)

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:
http://techearth.net...n:Basics:Slices

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

Example: Creating tuple called workingDays

workingDays=(“monday”,”tuesday”,”wednesday”,”thursday”,”friday”)

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

day=(“Monday”,)

After element you must put comma as shown in example.

Example: Tuple concatenation

numberSet1=(1,2,3)
numberSet2=(4,5,6)
print numberSet1+numberSet2

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

Checking is element in a list:

print 1 in numberSet1

returns True

Dictionary

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

Example: Creating dictionary

myFavouriteUrls={“codecall”:”forum.codecall.net”,”google”:”www.google.com”,”linux”:”www.ubuntu.com”}

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:

myFavouriteUrls.clear()


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

Attached Files


  • 1