0% found this document useful (0 votes)
21 views

EC1002E Intro Programming Slides Week5 Lists and Operations

Hznjxjxmx x

Uploaded by

Anand K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

EC1002E Intro Programming Slides Week5 Lists and Operations

Hznjxjxmx x

Uploaded by

Anand K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

INTRODUCTION TO PROGRAMMING

ELECTRONICS AND COMMUNICATION ENGINEERING


NIT CALICUT
WEEKLY PLAN
Week Topic Module
Week 1 Introduction to Computational thinking: Data, Procedural Programming
Algorithms, Programming, Abstraction.
Week 2 Introduction to Python, Data types and Procedural Programming
Expressions, Variables and Assignment,
Strings, Tuples, Lists, Dictionaries.
Week 3 Control flow statements in Python: if-else, for, Procedural Programming
while loops, Iterations and Comprehensions.
Week 4 Functions, Local and Global variables, Procedural Programming
Decomposition, Abstraction and Recursive
Functions in Python.
Week 5 Aliasing, Mutability and Cloning. Procedural Programming

Introduction to Testing, Benchmarking and


Debugging using Python.
Week 6 Exception handling, concepts of Concurrency Procedural Programming
and Parallelism, Synchronization.

Week 7 Introduction to Object Oriented Concepts, Features Object Oriented Programming


of Object-oriented programming (OOP).
Lists: Declaration
 A List object is the most general sequence provided by Python
 List can be defined using [] or list()
 Eg: l = list() or a = []
# List of grocery items
l = list([‘Apple’,’Orange’]), l= [‘Apple’,’Orange’]

 List is defined using comma separated values between square brackets


 The values are called items or elements of the list
 Elements can be of any datatype
 Eg: # List of item with available quantity, price etc
l = [‘Apple’,2,300.20,4+5j]
Lists: Mutability
 A List is mutable, i.e its elements can be modified
 Eg: l = [‘Apple’,’Orange’,’Grapes’,’Avocado’]
 What is result of l[0] = ‘Alphonso’?
 What is result of l[0] += ‘\tMango’?

 List is an ordered collection of arbitrary objects


 Check Indexing and Type
 Eg: l[2],l[-2],l[6]

 Check Length
 Check +,*, in
Lists: Nesting and Comprehension
 Nesting of Lists
 Eg: A matrix, m = [[11,12,13],[21,22,23],[31,32,33]]
 Indexing, Slicing with nested lists
 Eg: m[0], m[0][0],
m[0][1], m[1][0]
m[1][-1], m[-1][-1]

 List comprehension provides a concise way to apply operations on sequential values


 Eg: l = [x for x in range(6)]
l = [x**2 for x in range(11) if x%2 == 0]
# From our previous list of groceries, how to select all items starting with ‘A’?
Lists: Slicing and Iterations
 Slicing operations are available for Lists as well
 Eg: l = [0,1,2,3,4,5]
 What is result of l[:]
l[::]
l[::2]
l[::-1]

 How to sequentially iterate through all elements in a list?


 for item in list:
operation on item
Lists: Adding Elements
 Lists can grow and shrink on demand while preserving the order
 Three methods are available: append(item), extend(iterable), insert(index,item)
 Eg: l = [1,2,3]
l.append(‘4’)# Add one item to the end of list

 insert(index, item): l.insert(0,0) # Add one item to a specific location in list

 extend(iterable): l.extend([4,5,6]) # Add multiple items to end of list in order

 l.append([‘4’,’5’,’6’]) vs l.extend([‘4’,’5’,’6’])
Lists: Removing Elements
 Lists can grow and shrink on demand while preserving the order
 Three methods are available: pop(index), remove(item), del list[index]
 Eg: l = [1,2,3]

 pop(index): l.pop(0) # Remove one item at specified index and return the item
l.pop() # Remove one item from end of list (-1) and return the item
 remove(item): l.remove(‘1’) # Remove a specific item based on value
 What is multiple elements have same value or if no element has the value?
 del list[index]: del l[2] # Remove a specific item from list based on location
del l # Removes the list ‘l’
Lists: Extended Operations
 clear(): Remove all items from list and make it empty
Eg: l=[1,2,3]
l.clear()

 count(item): Returns count of specified item


Eg: l.count(2)

 reverse(): Reverses the list


Eg: l.reverse()

 index(item,start,stop): Same as strings, returns index of first instance


Eg: l.index(2)
Lists: Sorting
 sort(key,reverse): Sorts the list items based on key function and reverse=True/False
Eg: l = [-2,-1,0,1,2,3]
l.sort()
l.sort(reverse=True)
l.sort(key=abs)
l.sort(key=abs,reverse=True)

 sorted(list,key,reverse): Returns sorted list without modifying the original


Eg: l1 = sorted(l,key=abs,reverse=True)

 Self Study: sum(l), max(l), min(l), any(l), all(l), zip(l1,l2)


 For example, sum of number upto N -> sum(range(N+1)) # single line code

You might also like