0% found this document useful (0 votes)
22 views7 pages

Class004 - Data Structures

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)
22 views7 pages

Class004 - Data Structures

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/ 7

Session Code CODR-912-BC-004

Module Basic

Teaching Unit Data Structures

Learning Outcome String, Collections: list, tuple, dictionary,


sets

Resources Teacher:
1. Laptop along with audio and video
exchange
2. Notebook and Pen(To note any
development from session)
Student Resources
1. Laptop along with audio and video
exchange
2. Notebook and Pen(To keep note of
important parts in the session)

Duration 50 Mins

Structure Warm-up 2 Mins


Pace-up Activity 5 Mins
Knowledge Transfer 10 Mins
Student Led Activity 20 Mins
Short Quiz 8 Mins
Heads up tip for next class 5 Mins

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

1
Step Say Perform

Warm up Hi s​ tudent name​, ​how are you? Engage with the


(2 Mins) Are you excited for the class? student in
Do you remember the last class? conversation.

Interaction In the last class, we learnt about Conditionals: Revise the


(5 Mins) ● if​ statements concepts learned
● if else​ statements in the previous
● elif​ statements class.
We also learned about ​int, float f​ unctions,
which can be used to change data type.
We also learned about​ Relational​ and​ Logical
operators.

Today we will learn about some Data structures


of Python.

Teacher shares the screen and open Repl.it

Knowledge Do you remember what variables are? While talking


Transfer Yes, a variable is storage location which holds about types of
data value. data structures,
keep in mind that
Similar to a variable, Data structure is a Strings, Lists and
collection of data values. Tuples are
mandatory to
Some in-built data structures are: cover. Sets and
● List Dictionary are
● Tuples optional topics
● Sets depending upon
● Dictionary time constraints
and student’s
We will study about them today. competency level.
But before that let study about S
​ trings

Strings as we have learned in earlier classes are


strings of characters that we can use in either
single or double quotes.

There are some useful functions that we can use


on Strings, let’s study them.

● len() ​gives the length of the string, Create a string


Syntax: len(string) and use these
● .lower()​ returns the whole string in lower functions to show
case, Syntax: string.lower() how they work
● .upper() ​returns the whole string in
upper case, Syntax: string.upper()

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

2
● To connect two string variables we can
use +
​ ​ operator, this is known as
concatenation.

We can extract parts of the string from an


existing string using the index of each character.
The index of the first character is 0 and second is
1 and so on.
Eg.
x=”mytoppr”
y=x[2:5]

● Stop sharing your screen


● Ask the student to share screen and click on Student Activity 1: Strings

Help the student solve all the questions. Student Activity 1:


In case the student solves all the questions too Strings
quickly, add in more questions to test use of all the
functions learned till now.

● Ask the student to stop sharing screen


● Share your screen and open repl.it

Great so we learned a few important functions


of Strings, now let’s learn about inbuilt data
structures of python.
First of them being List

As the name suggests L ​ ist i​ s simply a collection


of different data values. These data values can
be of different data types.
So we can have a list of numbers, a list of names
and even a list having both numbers and names.

Let’s see how to create a list. Emphasise that


each element of
Just using square brackets, we can easily this list is a
create a list. different data
a_list=[1,”new”,3.8,True] type.

We can even have a list inside a list.

new_list=[34,a_list,”old”]

The elements in a list are ordered, which


means each element in a list has a unique
address and this unique address is called
index.

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

3
The first element of a list has index 0 and the Refer to indexing
second element has index 1 and so on. in String

To access any element of a list we can simply


use its index, this process is called indexing.

So to access the second element of ​a_list, ​we


can simply write ​a_list[1]
This is called positive indexing.

We can also access the element using Teacher Activity 2:


negative indexes which start from -1 denoting Index of List
the last element.

Instead of individual elements we can also access


subsets of the list with their index.
a_list[1:3] ​will return the second and the third
element of the list.

To add an element in a list, we have different Use and show


functions: examples for
● .​append()​:it is used to add an element at these functions.
the end of the list
Syntax: list.append(element)
● .insert()​: it is used to add an element at
the specified index
Syntax: list.insert(index,element)

To remove an element in a list we again have Use and show


different ways: examples for
● del​ keyword: it can be used to delete the these keywords
whole list or a particular element using its and functions.
index
Syntax: del list or del list[index]
Eg. del a_list or del a_list[3]
● .remove() ​function is used to delete an
element by its value
Syntax: list.remove(element)
Eg. a_list.remove(2)

Similar to ​list​ we have another data structure


called t​ uples. I​ t is quite similar to it, except that
we can add and remove elements from the list,
while we can’t do the same for tuples.

So, lists are mutable, tuples are immutable.

To create a tuple we used parenthesis or round


brackets.
Copyright © 2020 by Toppr Technologies Pvt. Ltd.
All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

4
at=(22,”n”, 7.89, “now”, False)

Same as list, we use indexing to access elements Remind indexing


of a tuple. in lists
at[3]
‘now’

We can’t add or delete elements of a tuple, but


two tuple variables can be merged into one Remind
using plus(+) operator. concatenation of
bt=(34,67) two strings
ct=at+bt

Some functions, that can be used on both list Remind that len
and tuple are: function gives the
● len()​: it gives the no. of elements in it. length of string
Syntax: len(list) or len(tuple) too.
● .count(): i​ t gives the count of an element
in a list of tuple
Syntax: list.count(element)

Now, let’s do an activity to revise whatever we


have learned about lists and tuples.

● Stop sharing screen


● Ask the student to share screen and click on Student activity 2

Help the student solve all the questions. Student Activity 2:


In case the student solves all the questions too List and Tuples
quickly, add in more questions to test use of all the
functions learned till now.

● In case the student is comfortable with Strings, List and Tuples at this point
but class time remaining is less than 10 mins, just introduce the concepts of
Set and Dictionary.
● For students struggling to understand Strings, Lists and Tuples, teachers can
skip Set and Dictionary.
● If the student is able to cope up and time permits do cover both concepts
and functions.

● Ask the student to stop sharing screen and you share the screen

Concept One more important data structure of python is


set
You might have studied in Maths, that ​set is a
collection of well defined and unique objects.

Concept Sets i​ n python are designed in a similar way.


It is a collection of data values, but only unique
data values, i.e. no element of a set is repeated
Copyright © 2020 by Toppr Technologies Pvt. Ltd.
All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

5
again.

Concept Also sets are unordered, so there is no index of


any element and thus,we can’t access the
element of the set, individually.
To create a set we simply use curly braces.
a_set={2,5.6,”toppr”}

Functions We can add an element in a set using .​ add() Teacher Activity 3:


function. Different mathematical operations that Set Operations
we can do on two sets are also possible in
python.
For any two sets, we can find their:
● Union using .​ union() f​ unction
● Intersection using .​ intersection()​function

Concept Another data structure in python is ​Dictionary.

It is simply a collection of key-value pairs.Let’s


create a simple dictionary to understand it.

a_dict={1: ”a”,2:”b”,3:”c”}

Here, 1,2,3 are the keys and a,b,c are its values
respectively.

Concept These keys and values can be of any data type,


but one thing to keep in mind is that the keys
must not be repeated.

Functions We can use: Use examples to


● .keys() f​ unction to display all the keys of show how to use
the dictionary these functions.
Syntax: dictionary.keys()
● .values() f​ unction to display all the values
of the dictionary
Syntax: dictionary.values()
● .items() ​function to display all the
key-value pairs of the dictionary
Syntax: dictionary.items()

Functions To access any element, k​ eys​ can be directly used Explain the use of
as index both.
Syntax: dictionary[key] or we can use the .​ get()
function.
Syntax: dictionary.get(key)

● Stop sharing your screen


● Ask student to share screen and click on Student Activity 3​ only if the

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

6
student has covered both concept and functions

Functions Help the student solve all the questions. Student Activity 3:
In case the student solves all the questions too Sets and
quickly, add in more questions to test use of all the Dictionary
functions learned till now.

● Ask the student to stop sharing the screen

So, today we learnt some important functions


related to strings, the in-built data structures of
python and important functions related to them.

These are very important concepts, which forms


the basis of a lot of advanced programs and
projects.

So Revise all the important definitions and


functions of this class and the previous one too.

Heads up for the In the next class we will be learning about


next class different kinds of Loops

BID GOOD BYE & END CLASS

Resources:

Activity Name Links

Teacher Activity 1 Repl link https://repl.it/languages

Teacher Activity 2 Indexing https://drive.google.com/file/d/1fj1xAQFZXWhg8kkhGp


KnqqQ75_gPPvSL/view?usp=sharing

Teacher Activity 3 Set Operations https://drive.google.com/file/d/1KOXczUZ9z5F_kTEsT19


4MG740HgMEcSl/view?usp=sharing

Student Activity 1 Strings https://repl.it/@ShailjaGupta/Strings#main.py

Student Activity 2 List and Tuples https://repl.it/@ShailjaGupta/Lists-and-Tuples#main.py

Student Activity 3 Sets and https://repl.it/@ShailjaGupta/Set-and-Dictionary


Dictionary

Copyright © 2020 by Toppr Technologies Pvt. Ltd.


All rights reserved. No part of this document may be distributed, or transmitted in any form or by any means, including photocopying,
recording, or other electronic or mechanical methods, without the prior written permission of the publisher.

You might also like