SlideShare a Scribd company logo
Introduction to 
Programming with 
Python – Class 4 
SYED FARJAD ZIA ZAIDI
Class Objectives 
Class Objective 
Understanding Lists 
Write few simple programs that use lists.
Class Material 
•Chapter 6, 8 - 
Python for 
Informatics: 
Exploring 
Information 
Reading
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.
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"];
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]
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];
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;
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.
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
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.
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]) → True 
first_last6([6, 1, 2, 3]) → True 
first_last6([13, 6, 1, 2, 3]) → False
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]) → False 
same_first_last([1, 2, 3, 1]) → True 
same_first_last([1, 2, 1]) → True
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]) → 3 
count_evens([2, 2, 0]) → 3 
count_evens([1, 3, 5]) → 0
Any Queries? 
 Link to Facebook Group: 
https://www.facebook.com/groups/introtopython.iccbs/ 
 Email: 
farjad_bullet@rocketmail.com

More Related Content

PPTX
Introduction To Programming with Python-3
PPTX
Values and Data types in python
PDF
Python tuple
PPTX
Chapter 10 data handling
PPTX
Data types in python
KEY
Programming with Python - Week 3
KEY
Programming with Python - Week 2
PPTX
Python Data-Types
Introduction To Programming with Python-3
Values and Data types in python
Python tuple
Chapter 10 data handling
Data types in python
Programming with Python - Week 3
Programming with Python - Week 2
Python Data-Types

What's hot (15)

PDF
Intro To BOOST.Spirit
PPT
Introduction to Python - Part Three
PPT
Strings Arrays
PPTX
Python second ppt
PDF
Data types in python
PPTX
Pointers in c v5 12102017 1
PDF
Introduction To Programming with Python
PPTX
Regular expressions in Python
PPT
Database structure Structures Link list and trees and Recurison complete
PPTX
Chapter 9 python fundamentals
PPTX
Python data type
PPTX
11 Unit 1 Chapter 02 Python Fundamentals
PDF
Python-03| Data types
PPTX
Data types in python lecture (2)
PDF
Python revision tour II
Intro To BOOST.Spirit
Introduction to Python - Part Three
Strings Arrays
Python second ppt
Data types in python
Pointers in c v5 12102017 1
Introduction To Programming with Python
Regular expressions in Python
Database structure Structures Link list and trees and Recurison complete
Chapter 9 python fundamentals
Python data type
11 Unit 1 Chapter 02 Python Fundamentals
Python-03| Data types
Data types in python lecture (2)
Python revision tour II
Ad

Viewers also liked (20)

PPTX
Clase 2 estatica
PPT
Substituting HDF5 tools with Python/H5py scripts
PPTX
Introduction To Programming with Python-1
PDF
Logic Over Language
PDF
Python and HDF5: Overview
PDF
Introduction to Databases
PDF
Logic: Language and Information 1
PDF
An Introduction to Interactive Programming in Python 2013
PPTX
Introduction To Programming with Python-5
PPTX
Unidad 1 fisica
PDF
Introduction to UBI
PPT
Python 4 Arc
PPT
The Python Programming Language and HDF5: H5Py
PPTX
Estática aplicada a las construcciones de edificaciones modernas
PPTX
Estática.pdf
PPT
Using HDF5 and Python: The H5py module
PPTX
Python programming - Everyday(ish) Examples
DOCX
2 unidad estatica (1)
PDF
Lets learn Python !
Clase 2 estatica
Substituting HDF5 tools with Python/H5py scripts
Introduction To Programming with Python-1
Logic Over Language
Python and HDF5: Overview
Introduction to Databases
Logic: Language and Information 1
An Introduction to Interactive Programming in Python 2013
Introduction To Programming with Python-5
Unidad 1 fisica
Introduction to UBI
Python 4 Arc
The Python Programming Language and HDF5: H5Py
Estática aplicada a las construcciones de edificaciones modernas
Estática.pdf
Using HDF5 and Python: The H5py module
Python programming - Everyday(ish) Examples
2 unidad estatica (1)
Lets learn Python !
Ad

Similar to Introduction To Programming with Python-4 (20)

PPTX
Python PRACTICAL NO 6 for your Assignment.pptx
PDF
pyton Notes6
PPTX
07+08slide.pptx
PPTX
lists8.pptx of chapter 12 IP Basic Python
PPT
Programming in Python Lists and its methods .ppt
PPTX
List In Python Programming. The linked list
PPTX
Python for the data science most in cse.pptx
PPTX
Python _dataStructures_ List, Tuples, its functions
PDF
GE3151_PSPP_UNIT_4_Notes
PPTX
Python PCEP Lists Collections of Data
PPTX
List in Python
PPTX
Unit 4 python -list methods
PPTX
Basic data structures in python
PPTX
Chapter 15 Lists
PDF
The Ring programming language version 1.6 book - Part 24 of 189
PPTX
Python list tuple dictionary .pptx
PPTX
Brief Explanation On List and Dictionaries of Python
PPTX
Python-List.pptx
PDF
The Ring programming language version 1.10 book - Part 30 of 212
Python PRACTICAL NO 6 for your Assignment.pptx
pyton Notes6
07+08slide.pptx
lists8.pptx of chapter 12 IP Basic Python
Programming in Python Lists and its methods .ppt
List In Python Programming. The linked list
Python for the data science most in cse.pptx
Python _dataStructures_ List, Tuples, its functions
GE3151_PSPP_UNIT_4_Notes
Python PCEP Lists Collections of Data
List in Python
Unit 4 python -list methods
Basic data structures in python
Chapter 15 Lists
The Ring programming language version 1.6 book - Part 24 of 189
Python list tuple dictionary .pptx
Brief Explanation On List and Dictionaries of Python
Python-List.pptx
The Ring programming language version 1.10 book - Part 30 of 212

More from Syed Farjad Zia Zaidi (20)

PPTX
Vision & sight
PDF
Introduction to Computing with Java
PDF
Web Application Architectures
PDF
Foundations of Virtual Instruction
PDF
Programming for Everybody (Python)
PDF
Learn to Program: The Fundamentals
PDF
Introduction to Systematic Program Design - Part 1
PDF
Emerging Trends & Technologies in the Virtual K-12 Classroom
PDF
An Introduction to Interactive Programming in Python 2014
PDF
Internet History, Technology, and Security
PDF
Human-Computer Interaction
PDF
Beginning Game Programming with C#
PDF
Programming Mobile Applications for Android Handheld Systems 2014
PDF
Computer Science 101
DOCX
Software Requirement Specification - Software Pack Solution 14
DOC
Project Proposal - Software Pack Solution 14
PDF
Database Diagram Tutorial-SQL Server 2012
PPTX
Introduction To Programming with Python Lecture 2
DOCX
MindMuscle Xtreme
PDF
How to connect database file to a 3-Tier Architecture Application and obtain ...
Vision & sight
Introduction to Computing with Java
Web Application Architectures
Foundations of Virtual Instruction
Programming for Everybody (Python)
Learn to Program: The Fundamentals
Introduction to Systematic Program Design - Part 1
Emerging Trends & Technologies in the Virtual K-12 Classroom
An Introduction to Interactive Programming in Python 2014
Internet History, Technology, and Security
Human-Computer Interaction
Beginning Game Programming with C#
Programming Mobile Applications for Android Handheld Systems 2014
Computer Science 101
Software Requirement Specification - Software Pack Solution 14
Project Proposal - Software Pack Solution 14
Database Diagram Tutorial-SQL Server 2012
Introduction To Programming with Python Lecture 2
MindMuscle Xtreme
How to connect database file to a 3-Tier Architecture Application and obtain ...

Recently uploaded (20)

PDF
Softaken Excel to vCard Converter Software.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Transform Your Business with a Software ERP System
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
System and Network Administraation Chapter 3
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Softaken Excel to vCard Converter Software.pdf
PTS Company Brochure 2025 (1).pdf.......
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Wondershare Filmora 15 Crack With Activation Key [2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Odoo POS Development Services by CandidRoot Solutions
Transform Your Business with a Software ERP System
Odoo Companies in India – Driving Business Transformation.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
ai tools demonstartion for schools and inter college
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Understanding Forklifts - TECH EHS Solution
Operating system designcfffgfgggggggvggggggggg
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administraation Chapter 3
Upgrade and Innovation Strategies for SAP ERP Customers
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025

Introduction To Programming with Python-4

  • 1. Introduction to Programming with Python – Class 4 SYED FARJAD ZIA ZAIDI
  • 2. Class Objectives Class Objective Understanding Lists Write few simple programs that use lists.
  • 3. Class Material •Chapter 6, 8 - Python for Informatics: Exploring Information Reading
  • 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.
  • 5. 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"];
  • 6. 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]
  • 7. 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];
  • 8. 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;
  • 9. 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.
  • 10. 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
  • 11. 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.
  • 12. 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]) → True first_last6([6, 1, 2, 3]) → True first_last6([13, 6, 1, 2, 3]) → False
  • 13. 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]) → False same_first_last([1, 2, 3, 1]) → True same_first_last([1, 2, 1]) → True
  • 14. 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]) → 3 count_evens([2, 2, 0]) → 3 count_evens([1, 3, 5]) → 0
  • 15. Any Queries?  Link to Facebook Group: https://www.facebook.com/groups/introtopython.iccbs/  Email: farjad_bullet@rocketmail.com