SlideShare a Scribd company logo
PART 3 - Python Tutorial | For Loop In Python With Examples
PART 3
WORKING WITH LISTS AND FOR
LOOP
• WANT TO LEARN PYTHON PROGRAMMING? (SUBTITLES)
• SUBSCRIBE
• TELEGRAM – FreeCodeSchool
• Twitter – shivammitra4
• LinkedIn – shivammitra
• Link in description
• Playlist link in description
AGENDA
• FOR LOOP IN PYTHON
• INDENTATION IN PYTHON
• LIST COMPREHENSIONS
• SLICING A LIST
• COPYING A LIST
PRINT EACH NAME IN THE LIST
ISSUES WITH THIS APPROACH
• Repetitive code
• Need to change the code if the list size changes
FOR LOOP FOR PRINTING THE LIST
PART 3 - Python Tutorial | For Loop In Python With Examples
PART 3 - Python Tutorial | For Loop In Python With Examples
EXPLAINING HOW FOR
LOOP WORKED
PART 3 - Python Tutorial | For Loop In Python With Examples
PART 3 - Python Tutorial | For Loop In Python With Examples
AVOIDING
INDENTATION ERRORS
WHAT IS INDENTATION
• Indentation is used to make code more readability easier
• In languages like c, c++, python etc. , indentation is optional
• In Python, it is mandatory
• Python uses indentation to determine how a line, or group of lines, is
related to the rest of the program.
• Change indentation and the meaning of code changes
PART 3 - Python Tutorial | For Loop In Python With Examples
PART 3 - Python Tutorial | For Loop In Python With Examples
INDENTATION ERROR
LOGICAL ERROR
INDENTING UNNECESSARILY
FORGETTING THE COLON ( so tough to find )
TABS VS SPACES FOR INDENTATION
PART 3 - Python Tutorial | For Loop In Python With Examples
WHAT’S THE ACTUAL
ISSUE ?
USING SPACES VS TABS
• Spaces - consistent on every text editor
• Tabs – not consistent on every text editor
• Can be 4 spaces
• Can be 8 spaces
• Mixing tabs and spaces causes error
• I prefer using 4 spaces for indentation
• Do not type spaces 4 times
MAKING NUMERICAL
LISTS
RANGE FUNCTION
• Start to (end-1)
• Print 1 to 6
• Similar to other
programming languages
PART 3 - Python Tutorial | For Loop In Python With Examples
USING RANGE TO STORE A LIST OF NUMBERS
SKIPPING NUMBERS WITHIN A RANGE
PRINT MULTIPLICATION TABLE OF 5
Output – [5, 10, 15, ……….., 50]
SIMPLE STATISTICS WITH A LIST OF NUMBERS
• Maximum of a list
• Minimum of a list
• Sum of a list
LIST COMPREHENSIONS
• A list comprehension combines the for loop and the creation of new
elements into one line, and automatically appends each new
element.
• Multiplication table in one line
• Not for beginner but you will see this in others code
WORKING WITH A PART OF A LIST
• Can we access multiple list elements at one go ?
• Welcome to slice in python
• Slice of pizza ?
• Not present in all programming languages
SLICING A LIST
• To make a slice, you specify the index of the first and last elements
you want to work with.
• Like range, the last element is not considered
• List can also be of strings or
any data type
PART 3 - Python Tutorial | For Loop In Python With Examples
LOOPING THROUGH A SLICE
COPYING A LIST
PART 3 - Python Tutorial | For Loop In Python With Examples
ASSIGNMENT
• Print square of numbers from 1 to 10 using for loop
• Store it a list first and then print
• One liner code
• fruits = [‘banana’, ‘apple’, ‘watermelon’, ‘orange’]
• Print first 2 fruits ( using slicing )
• Print last 2 fruits (suing slicing )
• Print fruits at odd positions ( using for loop )
• Print fruits at even positions (using for loop )
• Paste your code in the comment
IF ELSE STATEMENTS IN
PYTHON

More Related Content

What's hot (20)

PPTX
Python Flow Control
Kamal Acharya
 
PPSX
Collections - Array List
Hitesh-Java
 
PPTX
Python Tutorial Part 1
Haitham El-Ghareeb
 
PDF
Python
Helio Colombe
 
PPTX
Interfaces in java
Shiv Mehmi
 
PPTX
Loop(for, while, do while) condition Presentation
Badrul Alam
 
PPTX
Python-List.pptx
AnitaDevi158873
 
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPTX
Stack_Data_Structure.pptx
sandeep54552
 
PDF
Python tuples and Dictionary
Aswini Dharmaraj
 
PPT
Java naming conventions
Lovely Professional University
 
PDF
Java variable types
Soba Arjun
 
PPTX
C# Arrays
Hock Leng PUAH
 
PPTX
Vectors in Java
Abhilash Nair
 
PDF
Data types in python
Learnbay Datascience
 
PDF
Python recursion
Prof. Dr. K. Adisesha
 
PDF
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
PPTX
Presentation on data preparation with pandas
AkshitaKanther
 
PDF
Control Structures in Python
Sumit Satam
 
Python Flow Control
Kamal Acharya
 
Collections - Array List
Hitesh-Java
 
Python Tutorial Part 1
Haitham El-Ghareeb
 
Interfaces in java
Shiv Mehmi
 
Loop(for, while, do while) condition Presentation
Badrul Alam
 
Python-List.pptx
AnitaDevi158873
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
vikram mahendra
 
FUNCTIONS IN c++ PPT
03062679929
 
Stack_Data_Structure.pptx
sandeep54552
 
Python tuples and Dictionary
Aswini Dharmaraj
 
Java naming conventions
Lovely Professional University
 
Java variable types
Soba Arjun
 
C# Arrays
Hock Leng PUAH
 
Vectors in Java
Abhilash Nair
 
Data types in python
Learnbay Datascience
 
Python recursion
Prof. Dr. K. Adisesha
 
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
Presentation on data preparation with pandas
AkshitaKanther
 
Control Structures in Python
Sumit Satam
 

Similar to PART 3 - Python Tutorial | For Loop In Python With Examples (20)

PDF
"Automata Basics and Python Applications"
ayeshasiraj34
 
PPTX
Python for Beginners(v3)
Panimalar Engineering College
 
PPTX
powerpoint 2-13.pptx
JuanPicasso7
 
PPTX
institute of techonolgy and education tr
sekharuppuluri
 
PPTX
python_computer engineering_semester_computer_language.pptx
MadhusmitaSahu40
 
PDF
beginners_python_cheat_sheet -python cheat sheet description
NaveenVarma Chintalapati
 
PPTX
Pythonlearn-08-Lists.pptx
MihirDatir
 
PDF
Class 5: If, while & lists
Marc Gouw
 
PPTX
Lecture2.pptx
Sakith1
 
PPTX
Python for loop
Aishwarya Deshmukh
 
PDF
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
Guru Nanak Technical Institutions
 
PDF
GE3151_PSPP_UNIT_4_Notes
Guru Nanak Technical Institutions
 
PPTX
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
KavineshKumarS
 
PPTX
Pythonlearn-08-Lists.pptx
MihirDatir1
 
PPTX
Practical Python.pptx Practical Python.pptx
trwdcn
 
PDF
Module 2-Lists,Tuple and Dictionary Final by Dr.SV (1).pdf
SURESHA V
 
PPTX
Python Traning presentation
Nimrita Koul
 
DOCX
XI_CS_Notes for strings.docx
AnithaSathiaseelan1
 
PPTX
Q-SPractical_introduction_to_Python.pptx
JeromeTacata3
 
"Automata Basics and Python Applications"
ayeshasiraj34
 
Python for Beginners(v3)
Panimalar Engineering College
 
powerpoint 2-13.pptx
JuanPicasso7
 
institute of techonolgy and education tr
sekharuppuluri
 
python_computer engineering_semester_computer_language.pptx
MadhusmitaSahu40
 
beginners_python_cheat_sheet -python cheat sheet description
NaveenVarma Chintalapati
 
Pythonlearn-08-Lists.pptx
MihirDatir
 
Class 5: If, while & lists
Marc Gouw
 
Lecture2.pptx
Sakith1
 
Python for loop
Aishwarya Deshmukh
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
Guru Nanak Technical Institutions
 
GE3151_PSPP_UNIT_4_Notes
Guru Nanak Technical Institutions
 
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
KavineshKumarS
 
Pythonlearn-08-Lists.pptx
MihirDatir1
 
Practical Python.pptx Practical Python.pptx
trwdcn
 
Module 2-Lists,Tuple and Dictionary Final by Dr.SV (1).pdf
SURESHA V
 
Python Traning presentation
Nimrita Koul
 
XI_CS_Notes for strings.docx
AnithaSathiaseelan1
 
Q-SPractical_introduction_to_Python.pptx
JeromeTacata3
 
Ad

More from Shivam Mitra (16)

PPTX
Preparing for SRE Interviews
Shivam Mitra
 
PPTX
PART 9 - Python Tutorial | While Loop In Python With Examples
Shivam Mitra
 
PPTX
PART 8 - Python Tutorial | User Input In Python With Examples
Shivam Mitra
 
PPTX
PART 6 - Python Tutorial | Tuples In Python With Examples
Shivam Mitra
 
PPTX
PART 0 - Python Tutorial | Why should you learn python
Shivam Mitra
 
PPTX
Memory management in operating system | Paging | Virtual memory
Shivam Mitra
 
PPTX
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
PPTX
Process Scheduling Algorithms | Interviews | Operating system
Shivam Mitra
 
PPTX
Threads in Operating System | Multithreading | Interprocess Communication
Shivam Mitra
 
PPTX
Process management in operating system | process states | PCB | FORK() | Zomb...
Shivam Mitra
 
PPTX
Introduction to operating system, system calls and interrupts
Shivam Mitra
 
PPTX
What is Internet and How it Works
Shivam Mitra
 
PPTX
OSI Model Layers and Internet Protocol Stack
Shivam Mitra
 
PPTX
Basics of Stock Market
Shivam Mitra
 
PPTX
Assets vs liability
Shivam Mitra
 
PPTX
Pycricbuzz - a python library to fetch live cricket scores
Shivam Mitra
 
Preparing for SRE Interviews
Shivam Mitra
 
PART 9 - Python Tutorial | While Loop In Python With Examples
Shivam Mitra
 
PART 8 - Python Tutorial | User Input In Python With Examples
Shivam Mitra
 
PART 6 - Python Tutorial | Tuples In Python With Examples
Shivam Mitra
 
PART 0 - Python Tutorial | Why should you learn python
Shivam Mitra
 
Memory management in operating system | Paging | Virtual memory
Shivam Mitra
 
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
Process Scheduling Algorithms | Interviews | Operating system
Shivam Mitra
 
Threads in Operating System | Multithreading | Interprocess Communication
Shivam Mitra
 
Process management in operating system | process states | PCB | FORK() | Zomb...
Shivam Mitra
 
Introduction to operating system, system calls and interrupts
Shivam Mitra
 
What is Internet and How it Works
Shivam Mitra
 
OSI Model Layers and Internet Protocol Stack
Shivam Mitra
 
Basics of Stock Market
Shivam Mitra
 
Assets vs liability
Shivam Mitra
 
Pycricbuzz - a python library to fetch live cricket scores
Shivam Mitra
 
Ad

Recently uploaded (20)

PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
Controller Request and Response in Odoo18
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 

PART 3 - Python Tutorial | For Loop In Python With Examples

  • 2. PART 3 WORKING WITH LISTS AND FOR LOOP • WANT TO LEARN PYTHON PROGRAMMING? (SUBTITLES) • SUBSCRIBE • TELEGRAM – FreeCodeSchool • Twitter – shivammitra4 • LinkedIn – shivammitra • Link in description • Playlist link in description
  • 3. AGENDA • FOR LOOP IN PYTHON • INDENTATION IN PYTHON • LIST COMPREHENSIONS • SLICING A LIST • COPYING A LIST
  • 4. PRINT EACH NAME IN THE LIST
  • 5. ISSUES WITH THIS APPROACH • Repetitive code • Need to change the code if the list size changes
  • 6. FOR LOOP FOR PRINTING THE LIST
  • 13. WHAT IS INDENTATION • Indentation is used to make code more readability easier • In languages like c, c++, python etc. , indentation is optional • In Python, it is mandatory • Python uses indentation to determine how a line, or group of lines, is related to the rest of the program. • Change indentation and the meaning of code changes
  • 19. FORGETTING THE COLON ( so tough to find )
  • 20. TABS VS SPACES FOR INDENTATION
  • 23. USING SPACES VS TABS • Spaces - consistent on every text editor • Tabs – not consistent on every text editor • Can be 4 spaces • Can be 8 spaces • Mixing tabs and spaces causes error • I prefer using 4 spaces for indentation • Do not type spaces 4 times
  • 25. RANGE FUNCTION • Start to (end-1) • Print 1 to 6 • Similar to other programming languages
  • 27. USING RANGE TO STORE A LIST OF NUMBERS
  • 29. PRINT MULTIPLICATION TABLE OF 5 Output – [5, 10, 15, ……….., 50]
  • 30. SIMPLE STATISTICS WITH A LIST OF NUMBERS • Maximum of a list • Minimum of a list • Sum of a list
  • 31. LIST COMPREHENSIONS • A list comprehension combines the for loop and the creation of new elements into one line, and automatically appends each new element. • Multiplication table in one line • Not for beginner but you will see this in others code
  • 32. WORKING WITH A PART OF A LIST • Can we access multiple list elements at one go ? • Welcome to slice in python • Slice of pizza ? • Not present in all programming languages
  • 33. SLICING A LIST • To make a slice, you specify the index of the first and last elements you want to work with. • Like range, the last element is not considered • List can also be of strings or any data type
  • 38. ASSIGNMENT • Print square of numbers from 1 to 10 using for loop • Store it a list first and then print • One liner code • fruits = [‘banana’, ‘apple’, ‘watermelon’, ‘orange’] • Print first 2 fruits ( using slicing ) • Print last 2 fruits (suing slicing ) • Print fruits at odd positions ( using for loop ) • Print fruits at even positions (using for loop ) • Paste your code in the comment
  • 39. IF ELSE STATEMENTS IN PYTHON