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

What's hot (15)

PDF
Intro To BOOST.Spirit
Will Shen
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PPT
Strings Arrays
phanleson
 
PPTX
Python second ppt
RaginiJain21
 
PDF
Data types in python
Learnbay Datascience
 
PPTX
Pointers in c v5 12102017 1
tanmaymodi4
 
PDF
Introduction To Programming with Python
Sushant Mane
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPT
Database structure Structures Link list and trees and Recurison complete
Adnan abid
 
PPTX
Chapter 9 python fundamentals
Praveen M Jigajinni
 
PPTX
Python data type
nuripatidar
 
PPTX
11 Unit 1 Chapter 02 Python Fundamentals
Praveen M Jigajinni
 
PDF
Python-03| Data types
Mohd Sajjad
 
PPTX
Data types in python lecture (2)
Ali ٍSattar
 
PDF
Python revision tour II
Mr. Vikram Singh Slathia
 
Intro To BOOST.Spirit
Will Shen
 
Introduction to Python - Part Three
amiable_indian
 
Strings Arrays
phanleson
 
Python second ppt
RaginiJain21
 
Data types in python
Learnbay Datascience
 
Pointers in c v5 12102017 1
tanmaymodi4
 
Introduction To Programming with Python
Sushant Mane
 
Regular expressions in Python
Sujith Kumar
 
Database structure Structures Link list and trees and Recurison complete
Adnan abid
 
Chapter 9 python fundamentals
Praveen M Jigajinni
 
Python data type
nuripatidar
 
11 Unit 1 Chapter 02 Python Fundamentals
Praveen M Jigajinni
 
Python-03| Data types
Mohd Sajjad
 
Data types in python lecture (2)
Ali ٍSattar
 
Python revision tour II
Mr. Vikram Singh Slathia
 

Viewers also liked (20)

PPTX
Clase 2 estatica
Gerald Moreira RamĂ­rez
 
PPT
Substituting HDF5 tools with Python/H5py scripts
The HDF-EOS Tools and Information Center
 
PPTX
Introduction To Programming with Python-1
Syed Farjad Zia Zaidi
 
PDF
Logic Over Language
Purple, Rock, Scissors
 
PDF
Python and HDF5: Overview
andrewcollette
 
PDF
Introduction to Databases
Syed Farjad Zia Zaidi
 
PDF
Logic: Language and Information 1
Syed Farjad Zia Zaidi
 
PDF
An Introduction to Interactive Programming in Python 2013
Syed Farjad Zia Zaidi
 
PPTX
Introduction To Programming with Python-5
Syed Farjad Zia Zaidi
 
PPTX
Unidad 1 fisica
Gabino Osmar Ibañez Tiol
 
PDF
Introduction to UBI
Roy Lee
 
PPT
Python 4 Arc
absvis
 
PPT
The Python Programming Language and HDF5: H5Py
The HDF-EOS Tools and Information Center
 
PPTX
EstĂĄtica aplicada a las construcciones de edificaciones modernas
eduardo rivera romero
 
PPTX
EstĂĄtica.pdf
Danitza Araya Lopez
 
PPT
Using HDF5 and Python: The H5py module
The HDF-EOS Tools and Information Center
 
PPTX
Python programming - Everyday(ish) Examples
Ashish Sharma
 
DOCX
2 unidad estatica (1)
jorge.zaragoza12
 
PDF
Lets learn Python !
Kiran Gangadharan
 
Clase 2 estatica
Gerald Moreira RamĂ­rez
 
Substituting HDF5 tools with Python/H5py scripts
The HDF-EOS Tools and Information Center
 
Introduction To Programming with Python-1
Syed Farjad Zia Zaidi
 
Logic Over Language
Purple, Rock, Scissors
 
Python and HDF5: Overview
andrewcollette
 
Introduction to Databases
Syed Farjad Zia Zaidi
 
Logic: Language and Information 1
Syed Farjad Zia Zaidi
 
An Introduction to Interactive Programming in Python 2013
Syed Farjad Zia Zaidi
 
Introduction To Programming with Python-5
Syed Farjad Zia Zaidi
 
Unidad 1 fisica
Gabino Osmar Ibañez Tiol
 
Introduction to UBI
Roy Lee
 
Python 4 Arc
absvis
 
The Python Programming Language and HDF5: H5Py
The HDF-EOS Tools and Information Center
 
EstĂĄtica aplicada a las construcciones de edificaciones modernas
eduardo rivera romero
 
EstĂĄtica.pdf
Danitza Araya Lopez
 
Using HDF5 and Python: The H5py module
The HDF-EOS Tools and Information Center
 
Python programming - Everyday(ish) Examples
Ashish Sharma
 
2 unidad estatica (1)
jorge.zaragoza12
 
Lets learn Python !
Kiran Gangadharan
 
Ad

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

PPTX
UNIT-3 python and data structure alo.pptx
harikahhy
 
PPTX
Python _dataStructures_ List, Tuples, its functions
VidhyaB10
 
PDF
"Automata Basics and Python Applications"
ayeshasiraj34
 
PDF
GE3151_PSPP_UNIT_4_Notes
Guru Nanak Technical Institutions
 
PDF
Mementopython3 english
ssuser442080
 
PDF
Python3
Sourodip Kundu
 
PPTX
Python-List.pptx
AnitaDevi158873
 
PPTX
Pythonlearn-08-Lists.pptx
MihirDatir
 
PDF
Python3 cheatsheet
Gil Cohen
 
PPT
ComandosDePython_ComponentesBasicosImpl.ppt
oscarJulianPerdomoCh1
 
PPTX
PROBLEM SOLVING AND PYTHON PROGRAMMING PPT
MulliMary
 
PDF
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
PPTX
Python programming
sirikeshava
 
PPTX
Pythonlearn-08-Lists for fundatmentals of Programming
ABIGAILJUDITHPETERPR
 
PPTX
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
judyhilly13
 
PPTX
fundamental of python --- vivek singh shekawat
shekhawatasshp
 
PDF
Mementopython3 english
yassminkhaldi1
 
PPTX
Unit 4.pptx python list tuples dictionary
shakthi10
 
PPTX
Brixon Library Technology Initiative
Basil Bibi
 
UNIT-3 python and data structure alo.pptx
harikahhy
 
Python _dataStructures_ List, Tuples, its functions
VidhyaB10
 
"Automata Basics and Python Applications"
ayeshasiraj34
 
GE3151_PSPP_UNIT_4_Notes
Guru Nanak Technical Institutions
 
Mementopython3 english
ssuser442080
 
Python3
Sourodip Kundu
 
Python-List.pptx
AnitaDevi158873
 
Pythonlearn-08-Lists.pptx
MihirDatir
 
Python3 cheatsheet
Gil Cohen
 
ComandosDePython_ComponentesBasicosImpl.ppt
oscarJulianPerdomoCh1
 
PROBLEM SOLVING AND PYTHON PROGRAMMING PPT
MulliMary
 
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
Python programming
sirikeshava
 
Pythonlearn-08-Lists for fundatmentals of Programming
ABIGAILJUDITHPETERPR
 
Intro Python Data Structures.pptx Intro Python Data Structures.pptx
judyhilly13
 
fundamental of python --- vivek singh shekawat
shekhawatasshp
 
Mementopython3 english
yassminkhaldi1
 
Unit 4.pptx python list tuples dictionary
shakthi10
 
Brixon Library Technology Initiative
Basil Bibi
 
Ad

More from Syed Farjad Zia Zaidi (20)

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

Recently uploaded (20)

PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
PPTX
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
PDF
Laboratory Workflows Digitalized and live in 90 days with ScifeonÂŽs SAPPA P...
info969686
 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
PPTX
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
Laboratory Workflows Digitalized and live in 90 days with ScifeonÂŽs SAPPA P...
info969686
 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 

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: [email protected]