Starting up with Python – Part6

Day 3 – Moving on to the next level with Sorts and Tuples & String and Stuffs, Strings Methods and Dictionary are surely interesting. Let’s see the last articles on Python:

12.Sort and Tuples

A list can be sorted using the sort function

>> list1 = ['1','4','7','5']
>>> list1.sort()
>>> list1
['1', '4', '5', '7']

Strings can also be sorted. The capital letter will come first

>> sorted('Hackers')
['H', 'a', 'c', 'e', 'k', 'r', 's']

If some numbers are types followed by a comma, a tuple will be created automatically

>> 71,34,23,54,2
(71, 34, 23, 54, 2)

Tuples can also be created with a given name

>> danger=(34,34,23,464,12)
>>> danger
(34, 34, 23, 464, 12)

To have the value of let’s say 34 we simply search it within the array

>> danger[1]
34

13. Strings and pieces of stuff

Here is some way to plug variables in the strings. Let’s say we have a string called name=”HackersWorldwide and Mauritius” and if we want to change this we would need a variable by using the % sign and give instructions what kind of data it is.

>> name="%s and %s"
>>> var=('hackers','mauritius')
>>> print name % var
hackers and mauritius

You can also find where the strings start

>> question="Hey there tunnelers, how are you doing?"
>>> question.find('doing')
33

14.Cool string Methods

You can also take two strings and join them together with the join function

>> sequence=[ 'hey', 'there', 'tunnelers']
>>> sequence
['hey', 'there', 'tunnelers']
>>> join='hacked'
>>> join.join(sequence)
'heyhackedtherehackedtunnelers'

Strings can also be set to Upper and lower case functions. Here is an idea with the upper case function

>> randstr="i am hungry"
>>> randstr
'i am hungry'
>>> randstr.upper()
'I AM HUNGRY'

You can also replace strings with the replace function.

>> randstr="i am hungry"
>>> randstr
'i am hungry'
>>> randstr.replace('hungry','thirsty')
'i am thirsty'

15. Dictionary

A word can be used to look for another value using a dictionary. The syntax is different compared to a typical sequence. We will need a key and a value. Let us say we have a dictionary called book. The ‘book’ has a key called ‘psy’ with value ‘sci’.

So you can type the key to have the return value

>> book={'psy':'sci','war':'his'}
>>> book
{'psy': 'sci', 'war': 'his'}
>>> book['psy']
'sci'

You can also find it by numbers

book={'psy':'1','war':'2'}
>>> book['psy']
'1'

To remove all items in a book, we use the clear function

>> book.clear()
>>> book
{}

You can also copy a dictionary to another new one.

>> book={'psy':'1','war':'2'}
>>> book
{'psy': '1', 'war': '2'}
>>> hackedbook=book.copy()
>>> hackedbook
{'psy': '1', 'war': '2'}

You also use the function has.key to verify for  a key

>> hackedbook
{'psy': '1', 'war': '2'}
>>> hackedbook.has_key('psy')
True
Nitin J Mutkawoa https://tunnelix.com

Blogger at tunnelix.com | Founding member of cyberstorm.mu | An Aficionado Journey in Opensource & Linux – And now It's a NASDAQ touch!

You May Also Like

More From Author