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.
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']
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']
remove a value from the list
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'
Zo' in name
>>> 'z' in name
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)
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:
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]
['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 …