(Part 2) 

#List and Strings:

Assuming that by now you know the difference between String and List, let’s convert a STRING to a list of CHARACTERS as shown below –

1

2

3

4

>>> s = 'LetsUpgrade'

>>> t = list(s)

>>> print(t)

['L', 'e', 't', 's','U', 'p', 'g', 'r', 'a','d', 'e']

To break a string into words, we can use the split method –

1

2

3

4

5

6

>>> s = 'FREE CODING SCHOOL'

>>> t = s.split()

>>> print(t)

['FREE', 'CODING', 'SCHOOL']

>>> print(t[2])

SCHOOL

Delimiter: A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams.

1

2

3

4

>>> s = 'spam-spam-spam'

>>> delimiter = '-'

>>> s.split(delimiter)

['spam', 'spam', 'spam']

JOIN: It is the inverse of split.

1

2

3

4

>>> t = ['I am', 'a', 'LetsUpgrade', 'Student']

>>> delimiter = ' '

>>> delimiter.join(t)

'I am a LetsUpgrade Student'

#Parsing lines:

Usually when we are reading a file we want to do something to the lines other than just printing the whole line. Often we want to find the “interesting lines” and then parse the line to find some interesting part of the line. What if we wanted to print out the day of the week from those lines that start with “From”?

From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008

The split method is very effective when faced with this kind of problem. We can write a small program that looks for lines where the line starts with “From”, split those lines, and then print out the third word in the line:

1

2

3

4

5

6

fhand = open('mbox-short.txt')

for line in fhand:

    line = line.rstrip()

    if not line.startswith('From '): continue

    words = line.split()

    print(words[2])

The program produces the following output:

Sat
Fri
Fri
Fri

(Referrence: pythonlearn)

#Objects and values:

We will use ‘is‘ operator to check given two variables ‘a’ and ‘b’ refer to the same object or not:

1

2

3

4

>>> a = 'banana'

>>> b = 'banana'

>>> a is b

True

1

2

3

4

>>> a = [1, 2, 3]

>>> b = [1, 2, 3]

>>> a is b

False

In the first example, Python only created one string object, and both a and b refer to it.

In the second case, we would say that the two lists are equivalent as they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, it is not necessary to be identical.

#Aliasing:

1

2

3

4

>>> a = [1, 2, 3]

>>> b = a

>>> b is a

True

The association of a variable with an object is called a reference. In this example, there are two references to the same object.

An object with more than one reference has more than one name, then we say that particular object is aliased and If the aliased object is mutable, changes made with one alias affect the other:

1

2

3

>>> b[1] = 25

>>> print(a)

[1, 25, 3]

The above method seems to be useful but it is Error-prone and therefore, it is better to avoid using this method on mutable objects like list and for immutable objects like string is not much a problem.

#List Arguments:

1

2

3

4

5

6

7

def delete(t):

    del t[0]

     

S = ['a', 'b', 'c']

delete (S)

print (S)

['b', 'c']

In the above example, when we pass a list to a function, the function gets a reference to the list. The caller sees the change if there is any modification in the list parameter done by function as in the above delete removes the first element from the list.

1

2

3

4

5

6

>>> t1 = [1, 2]

>>> t2 = t1.append(3)

>>> print(t1)

[1, 2, 3]

>>> print(t2)

None

1

2

3

4

5

>>> t3 = t1 + [3]

>>> print(t3)

[1, 2, 3]

>>> t2 is t3

False

In the above two examples, the first one is used to Modify the existing List and the second one is used to Create a new list. Thus, output of ‘ t2 is t3 ‘ is false unless we assign it as ‘ t2 = t3 ‘.

1

2

3

4

5

6

7

def tail(t):

    t = t[2:]

    

letters = ['a', 'b', 'c', 'f','d']

rest = tail(letters)

print (rest)

None

1

2

3

4

5

6

7

8

def tail(t):

    t = t[2:]

    return t

         

letters = ['a', 'b', 'c', 'f','d']

rest = tail(letters)

print (rest)

['c', 'f', 'd']

If we compare above two examples, the output of both the examples vary and the reason is simple, the slice operator creates a new list and the assignment makes t refer to it, but none of that has any effect on the list that was passed as an argument.

Therefore, in second case it creates and returns a new list that is generated.

Exercise : Write a function called SNAKE that takes a list and modifies it, removing the first and last elements, and returns None. Then write a function called MIDDLE that takes a list and returns a new list that contains all but the first and last elements.

#List Comprehensions:

It is a complex topic and to understand it properly you need go through this below Article:

List Comprehension in Python

———————————————- THE END —————————————————-