0% found this document useful (0 votes)
61 views27 pages

Lecture 2.6 - 2.7

The document outlines a course on Programming in Python that introduces students to Python lists and their functionality, including how to define and manipulate lists, perform operations on lists like slicing and concatenation, and use built-in list methods. The course objectives are to understand why Python is a useful scripting language and learn how to design Python applications using lists, tuples, and dictionaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views27 pages

Lecture 2.6 - 2.7

The document outlines a course on Programming in Python that introduces students to Python lists and their functionality, including how to define and manipulate lists, perform operations on lists like slicing and concatenation, and use built-in list methods. The course objectives are to understand why Python is a useful scripting language and learn how to design Python applications using lists, tuples, and dictionaries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Apex Institute of Technology

Department of Computer Science & Engineering

PROGRAMMING IN PYTHON

(21CSH-284)

Mr. Vishal Prasad


E14181
Assistant Professor DISCOVER . LEARN . EMPOWER
1
CSE(AIT), CU
About Course
• To understand why Python is a useful scripting language for
developers.
• To learn how to design and program Python applications.
• To learn how to use lists, tuples, and dictionaries in Python programs.
• To define the structure and components of a Python program.
• To learn how to write loops and decision statements in Python.
• To learn how to write functions and pass arguments in Python.
• To learn how to build and package Python modules for reusability.
• To learn how to read and write files in Python.
Course Objectives
CO Number Title Level

CO1 Describe how Python can be applied to Understand &


well-constructed documents for Web Remember
browser-based technology in business
information systems
CO2 To evaluate the basics of Python Understand &
Programming and there Implementation
in Software Engineering Remember

CO3 Apply the best features of mathematics, Apply


engineering and natural sciences to
program real life

3
Course Outcomes

• To understand why Python is a useful scripting language for developers


• To learn how to use lists, tuples, and dictionaries in Python programs
• To learn how to build and package Python modules for reusability

4
Contents to be Covered
• Lists in Python.

• Operation on List
A List is a kind of
Collection
• A collection allows us to put many values in a single “variable”
• A collection is nice because we can carry all many values around in
one convenient package.

friends = [ 'Joseph', 'Glenn', 'Sally' ]

carryon = [ 'socks', 'shirt', 'perfume' ]


What is not a “Collection”
• Most of our variables have one value in them - when we put a new
value in the variable - the old value is over written
$ python
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
>>> x = 2
>>> x = 4
>>> print x
4
List Constants
• List constants are surrounded by >>> print [1, 24, 76]
square brakets and the [1, 24, 76]
elements in the list are >>> print ['red', 'yellow', 'blue']
separated by commas. ['red', 'yellow', 'blue']
• A list element can be any >>> print ['red', 24, 98.6]
['red', 24, 98.599999999999994]
Python object - even another
>>> print [ 1, [5, 6], 7]
list
[1, [5, 6], 7]
• A list can be empty >>> print []
[]
We already use lists!

5
4
for i in [5, 4, 3, 2, 1] : 3
print i 2
print 'Blastoff!' 1
Blastoff!
Lists and definite loops - best pals

friends = ['Joseph', 'Glenn', 'Sally']


for friend in friends :
print 'Happy New Year:', friend
print 'Done!' Happy New Year
Looking Inside Lists
• Just like strings, we can get at any single element in a list using an
index specified in square brackets

>>> friends = [ 'Joseph', 'Glenn', 'Sally' ]


Joseph Glenn Sally >>> print friends[1]
Glenn
0 1 2 >>>
Lists are Mutable
>>> fruit = 'Banana’
• Strings are "immutable" - >>> fruit[0] = 'b’
Traceback
we cannot change the TypeError: 'str' object does not
contents of a string - we support item assignment
must make a new string to >>> x = fruit.lower()
make any change >>> print x
banana
• Lists are "mutable" - we can >>> lotto = [2, 14, 26, 41, 63]
change an element of a list >>> print lotto[2, 14, 26, 41, 63]
>>> lotto[2] = 28
using the index operator >>> print lotto
[2, 14, 28, 41, 63]
How Long is a List?
• The len() function takes a list as >>> greet = 'Hello Bob’
a parameter and returns the
number of elements in the list >>> print len(greet)
• Actually len() tells us the 9
number of elements of any set >>> x = [ 1, 2, 'joe', 99]
or sequence (i.e. such as a
string...)
>>> print len(x)
4
>>>
Using the range function

• The range function returns a


list of numbers that range >>> print range(4)
[0, 1, 2, 3]
from zero to one less than the
>>> friends = ['Joseph', 'Glenn', 'Sally']
parameter >>> print len(friends)
• We can construct an index 3
loop using for and an integer >>> print range(len(friends))
iterator [0, 1, 2]
>>>
A tale of two loops...

>>> friends = ['Joseph', 'Glenn', 'Sally']


>>> print len(friends)
3
friends = ['Joseph', 'Glenn', 'Sally'] >>> print range(len(friends))
[0, 1, 2]
for friend in friends : >>>
print 'Happy New Year:', friend

for i in range(len(friends)) :
friend = friends[i]
print 'Happy New Year:', friend Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
Concatenating lists using +
• We can create a new list by
adding two exsiting lists together >>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print c
[1, 2, 3, 4, 5, 6]
>>> print a
[1, 2, 3]
Lists can be sliced using :
>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
Remember: Just
[41,12]
like in strings, the
>>> t[:4]
second number is
[9, 41, 12, 3]
"up to but not
>>> t[3:]
including"
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]
List Methods

>>> x = list()
>>> type(x)<type 'list'>
>>> dir(x)['append', 'count', 'extend', 'index',
'insert', 'pop', 'remove', 'reverse', 'sort']
>>>

Source:[http://docs.python.org/tutorial/datastructures.html]
Building a list from scratch
• We can create an empty >>> stuff = list()
list and then add
elements using the >>> stuff.append('book')
append method >>> stuff.append(99)
• The list stays in order and >>> print stuff
new elements are added
at the end of the list ['book', 99]
>>> stuff.append('cookie')
>>> print stuff
['book', 99, 'cookie']
Is Something in a List?
• Python provides two
operators that let you
>>> some = [1, 9, 21, 10, 16]
check if an item is in a
>>> 9 in some
list
True
• These are logical >>> 15 in some
operators that return False
True or False >>> 20 not in some
• They do not modify the True
list >>>
A List is an Ordered Sequence

• A list can hold many items


and keeps those items in the
order until we do something >>> friends = [ 'Joseph', 'Glenn', 'Sally' ]
to change the order >>> friends.sort()
>>> print friends
• A list can be sorted (i.e. ['Glenn', 'Joseph', 'Sally']
change its order) >>> print friends[1]
• The sort method (unlike in Joseph>>>
strings) means "sort yourself"
Built in Functions and Lists
• There are a number of
functions built into >>> nums = [3, 41, 12, 9, 74, 15]
Python that take lists as >>> print len(nums)
parameters 6
>>> print max(nums)
• Remember the loops we 74>>> print min(nums)
built? These are much 3
simpler >>> print sum(nums)
154
>>> print sum(nums)/len(nums)
25

Source:[http://docs.python.org/lib/built-in-funcs.html]
Key Points
• Concept of a collection
• Lists and definite loops
• Indexing and lookup
• List mutability
• Functions: len, min, max, sum
• Slicing lists

23
Learning Material

24
Research Articles

M. Poole, "Extending the design of a blocks-based python environment to


support complex types," 2017 IEEE Blocks and Beyond Workshop (B&B),
Raleigh, NC, 2017, pp. 1-7, doi: 10.1109/BLOCKS.2017.8120400.

Wonjae Lee and Hak-Young Kim, "Genetic algorithm implementation in


Python," Fourth Annual ACIS International Conference on Computer and
Information Science (ICIS'05), Jeju Island, South Korea, 2005, pp. 8-11, doi:
10.1109/ICIS.2005.69.
Help for current page

25
Assessment Pattern

26
Thank you

Please Send Your Queries on:

e-Mail: [email protected]

27

You might also like