0% found this document useful (0 votes)
28 views5 pages

Learning Journal Unit 6

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Learning Journal Unit 6

Hello dear instructor,

Wish you be fine. I tried my best to do all parts correctly. Since people are not free
from mistakes, please let me know if I made any mistakes.

Part one

I made a series of names, then I want to try split with name, by using split we can
break a string into words.

name='Nasiba Frozan Maliha Sakina Mursal'

name=name.split()

print(name)

Output: ['Nasiba', 'Frozan', 'Maliha', 'Sakina', 'Mursal']

There are different ways for deleting the elements from a list, I will use del, pop and
remove metod:

#del method

del name[4]

print(name)

Output:

['Nasiba', 'Frozan', 'Maliha', 'Sakina']

#pop method

name1=name.pop(3)

print(name)

Output: ['Nasiba', 'Frozan', 'Maliha']


#remove method

name.remove('Maliha')

print(name)

Output: ['Nasiba', 'Frozan']

Sort list

By using sort we can arrange the elements of a list from low to high.

name.sort()

print(name)

Output: ['Frozan', 'Nasiba']

Adding New Words

As there are many ways for deleting of elements from a list, there are also different
ways for adding elements into a list, I will use the append, extend and + operator in
Python to add words at the end of our list, which I add Bahram, Safa and Razia to
my list.

# append method

name.append('Bahram')

print(name)

Output: ['Frozan', 'Nasiba', 'Bahram']

# extent method

name2=['Safa']

name.extend(name2)

print(name)
Output: ['Frozan', 'Nasiba', 'Bahram', 'Safa']

# +method

name3=['Razia']

name+=name3

print(name)

Output: ['Frozan', 'Nasiba', 'Bahram', 'Safa', 'Razia']

# using join

By using join we can concatenate the elements as a string.

delimiter=','

name=delimiter.join(name)

print(name)

Output: Frozan,Maliha,Ahmad,Shadab,Razia

Comment: By using join we can concatenate the elements as a string.

Part 2
#nested list
f=['going to study']
a=[f,'tonight']
print(a)
Output: [['going to study'], 'tonight']
Comment: A list within another list is nested (Downey, 2015). Inside the output list is two list.
Frist is (going to study) and second is (tonight).
#the * operator
w=['frozan']
print(w*4)
Output: ['frozan', 'frozan', 'frozan', 'frozan']
Comment: The * operator repeats a list a given number of times (Downey, 2015). In this case
frozan repeated four times.
#list slices
s=['frozan', 'sakina', 'maliha', 'nasiba']
s[2:]=['hasani']
print(s)
Output: ['frozan', 'sakina', 'hasani']
Comment: A segment of a list is known as slices list, we can add or remove element from it. I
added hasani instead of nasiba.
#the += operator
fr=['frozan sakina nasiba']
fr1=['maliha']
fr+=fr1
print(fr)
Output: ['frozan sakina nasiba', 'maliha']
Comment: The += operator provides a short way to update a variable (Downey, 2015). I add
maliha in my list.
# list fliter
l=[13, 20, 30, 33, 40, 46, 50]
result=list(filter(lambda x:(x%5==0),l))
print(result)
Output: [20, 30, 40, 50]
Comment: filter selects some of the elements and filters out the others (Downey, 2015). Output
shows that it filtered just those number that divisible by 5.
# A list operation that is legal but does the "wrong" thing, and not what the programmer expects
g=[3, 4, 5]
g= g.extend(6)
print(g)
Output: error
Comment: it expected that add 6 at the list, but it won’t. Instead the output shows error.
Reference:

Downey, A. (2015). Think Python: How to think like a computer scientist.

You might also like