introduction to programming with python-4

15
Introduction to Programming with Python – Class 4 SYED FARJAD ZIA ZAIDI

Upload: syed-farjad-zia-zaidi

Post on 26-May-2015

140 views

Category:

Software


5 download

DESCRIPTION

Slides for Lecture 4 of the course: Introduction to Programming with Python offered at ICCBS. It covers the following topics: Python Lists

TRANSCRIPT

Page 1: Introduction To Programming with Python-4

Introduction to Programming with Python – Class 4SYED FARJAD ZIA ZAIDI

Page 2: Introduction To Programming with Python-4

Class Objectives

Class Objective

Understanding Lists

Write few simple programs that use lists.

Page 3: Introduction To Programming with Python-4

Class Material

• Chapter 6, 8 - Python for Informatics: Exploring Information

Reading

Page 4: Introduction To Programming with Python-4

Lists

The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth.

Python has six built-in types of sequences, but the most common ones are lists and tuples, which we would see in this tutorial.

There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.

Page 5: Introduction To Programming with Python-4

Creating Lists Creating a list is as simple as putting different comma-separated

values between squere brackets. For example:

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5 ];

list3 = ["a", "b", "c", "d"];

Page 6: Introduction To Programming with Python-4

Accessing Values in Lists:

To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. Following is a simple example:

list1 = ['physics', 'chemistry', 1997, 2000];

list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]

print "list2[1:5]: ", list2[1:5]

Page 7: Introduction To Programming with Python-4

Updating Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. Following is a simple example:

list = ['physics', 'chemistry', 1997, 2000];

print "Value available at index 2 : "

print list[2];

list[2] = 2001;

print "New value available at index 2 : "

print list[2];

Page 8: Introduction To Programming with Python-4

Delete Lists Elements

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. Following is a simple example:

list1 = ['physics', 'chemistry', 1997, 2000];

print list1;

del list1[2];

print "After deleting value at index 2 : "

print list1;

Page 9: Introduction To Programming with Python-4

Basic List Operations

Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string.

In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.

Page 10: Introduction To Programming with Python-4

List Operations

Python Expression Results Description

len([1, 2, 3]) 3 Length

[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Concatenation

['Hi!'] * 4 ['Hi!', 'Hi!', 'Hi!', 'Hi!'] Repetition

3 in [1, 2, 3] True Membership

for x in [1, 2, 3]: print x, 1 2 3 Iteration

Page 11: Introduction To Programming with Python-4

Exercise 0

Create a program that will keep track of items for a shopping list. The program should keep asking for new items until nothing is entered (no input followed by enter/return key). The program should then display the full shopping list.

Page 12: Introduction To Programming with Python-4

Exercise 1

Given an array of ints, return True if 6 appears as either the first or last element in the array. The array will be length 1 or more. 

first_last6([1, 2, 6]) → Truefirst_last6([6, 1, 2, 3]) → Truefirst_last6([13, 6, 1, 2, 3]) → False

Page 13: Introduction To Programming with Python-4

Exercise 2

Given an array of ints, return True if the array is length 1 or more, and the first element and the last element are equal. 

same_first_last([1, 2, 3]) → Falsesame_first_last([1, 2, 3, 1]) → Truesame_first_last([1, 2, 1]) → True

Page 14: Introduction To Programming with Python-4

Exercise 3 Return the number of even ints in the given array. Note: the %

"mod" operator computes the remainder, e.g. 5 % 2 is 1. 

count_evens([2, 1, 2, 3, 4]) → 3count_evens([2, 2, 0]) → 3count_evens([1, 3, 5]) → 0

Page 15: Introduction To Programming with Python-4

Any Queries?

Link to Facebook Group:

https://www.facebook.com/groups/introtopython.iccbs/

Email:

[email protected]