Python Notes-5

In continuation from
Python Notes-4
Python Notes-3
Python Notes-2
Python Notes-1

Let us change the topic for a while from list, string, tuple etc.

Functions in python are also very user friendly for example:

def func(a, b):
  return a+b,a*b


Above function will return a tuple so func(2,3) will give (5,6)

Note: It is never a good practice to do multiple thing in a single function. You should always try to make your function serve single purpose that will make it more usable.

Let us go back to list there is so much to cover there so much power in this data structure.


del li[indx]

you can also delete a variable, kind of unassignment .. after deleting use will give you TypeError

Operators in, not in:

operators, in and not in are used very often


>>>'howdy' in ['hello', 'hi', 'howdy', 'heyas']
>>>True

in is used to Find a value in a list
You can also use index for find but it will give ValueError if the item is not present in the list.
When there are multiple elements index will give you the lowest index:

>>> var = ['hello', 'hi', 'howdy', 'heyas']
>>> var.index('hello')
0
>>> spam.index('heyas')
3

remove()

remove a value from the list

Sorting

var.sort()

var.sort(reverse=True)

sorts the list IN PLACE SO NONE return value

There is None value whenever something is un-assigned or a function doesn’t return anything None is used.

list can store different type of variables but then you can’t use sort on that list. [GOT YOU!! NOPE]


name = 'Zophie'
>>> name[0]
'Z'
>>> name[-2]
'i'

Zo' in name
True
>>> 'z' in name
False

Repeatation of old concept

A lists and strings are different in an important way. A list value is a mutable data type: It can have values added, removed, or changed. However, a string is immutable: It cannot be changed.

The tuple data type is almost identical to the list data type, except in two ways. First, tuples are typed with parentheses, ( and ), instead of square brackets, [ and ]. For example, enter the following into the interactive shell:

>>> eggs = ('hello', 42, 0.5)
>>> eggs[0]
'hello'

But the main way that tuples are different from lists is that tuples, like strings, are immutable. Tuples cannot have their values modified, appended, or removed.

If you have only one value in your tuple, you can indicate this by placing a trailing comma after the value inside the parentheses. Otherwise, Python will think you’ve just typed a value inside regular parentheses. The comma is what lets Python know this is a tuple value. (Unlike some other programming languages, in Python it’s fine to have a trailing comma after the last item in a list or tuple.) Enter the following type() function calls into the interactive shell to see the distinction:

>>> type(('hello',))

Just like how str(42) will return ’42’, the string representation of the integer 42, the functions list() and tuple() will return list and tuple versions of the values passed to them. Enter the following into the interactive shell, and notice that the return value is of a different data type than the value passed:

>>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']

Converting a tuple to a list is handy if you need a mutable version of a tuple value.

To be continued …

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s