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

PPTX
PART 7 - Python Tutorial | Dictionaries In Python With Examples
PPTX
PART 4 - Python Tutorial | If Else In Python With Examples
PPTX
Part 2 - Python Tutorial | Introduction to Lists
PPTX
PART 9 - Python Tutorial | While Loop In Python With Examples
PPTX
PART 1 - Python Tutorial | Variables and Data Types in Python
PPTX
Python for loop
PDF
Python Variable Types, List, Tuple, Dictionary
PART 7 - Python Tutorial | Dictionaries In Python With Examples
PART 4 - Python Tutorial | If Else In Python With Examples
Part 2 - Python Tutorial | Introduction to Lists
PART 9 - Python Tutorial | While Loop In Python With Examples
PART 1 - Python Tutorial | Variables and Data Types in Python
Python for loop
Python Variable Types, List, Tuple, Dictionary

What's hot (20)

PPT
Python ppt
PPTX
Python strings presentation
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Lecture Introduction to Python 2024.pptx
PPTX
Linked List
PDF
Python Basics
PPT
pseudo code basics
PDF
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
PPTX
PDF
Python list
PDF
PPTX
Functions in python
PPTX
List in Python
PPTX
What is to loop in c++
PPTX
Stack - Data Structure
PPT
358 33 powerpoint-slides_9-stacks-queues_chapter-9
PPT
Bubble sort
PDF
How To Install Python Pip On Windows | Edureka
PPTX
Intro to Python Programming Language
PDF
Lesson 03 python statement, indentation and comments
Python ppt
Python strings presentation
Arrays In Python | Python Array Operations | Edureka
Lecture Introduction to Python 2024.pptx
Linked List
Python Basics
pseudo code basics
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python list
Functions in python
List in Python
What is to loop in c++
Stack - Data Structure
358 33 powerpoint-slides_9-stacks-queues_chapter-9
Bubble sort
How To Install Python Pip On Windows | Edureka
Intro to Python Programming Language
Lesson 03 python statement, indentation and comments
Ad

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

PDF
"Automata Basics and Python Applications"
PPTX
Python for Beginners(v3)
PPTX
powerpoint 2-13.pptx
PPTX
Python-List.pptx
PPTX
institute of techonolgy and education tr
PPTX
python_computer engineering_semester_computer_language.pptx
PDF
beginners_python_cheat_sheet -python cheat sheet description
PPTX
Pythonlearn-08-Lists.pptx
PDF
Class 5: If, while & lists
PPTX
Lecture2.pptx
PDF
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
PDF
GE3151_PSPP_UNIT_4_Notes
PPTX
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
PPTX
Pythonlearn-08-Lists.pptx
DOCX
Python Materials- Lists, Dictionary, Tuple
PDF
Module 2-Lists,Tuple and Dictionary Final by Dr.SV (1).pdf
PPTX
Python Traning presentation
DOCX
XI_CS_Notes for strings.docx
PPTX
Q-SPractical_introduction_to_Python.pptx
"Automata Basics and Python Applications"
Python for Beginners(v3)
powerpoint 2-13.pptx
Python-List.pptx
institute of techonolgy and education tr
python_computer engineering_semester_computer_language.pptx
beginners_python_cheat_sheet -python cheat sheet description
Pythonlearn-08-Lists.pptx
Class 5: If, while & lists
Lecture2.pptx
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151_PSPP_UNIT_4_Notes
Python Lists is a a evry jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
Pythonlearn-08-Lists.pptx
Python Materials- Lists, Dictionary, Tuple
Module 2-Lists,Tuple and Dictionary Final by Dr.SV (1).pdf
Python Traning presentation
XI_CS_Notes for strings.docx
Q-SPractical_introduction_to_Python.pptx
Ad

More from Shivam Mitra (16)

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

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Basic Mud Logging Guide for educational purpose
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Structure & Organelles in detailed.
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Pre independence Education in Inndia.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
TR - Agricultural Crops Production NC III.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Basic Mud Logging Guide for educational purpose
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Structure & Organelles in detailed.
human mycosis Human fungal infections are called human mycosis..pptx
Anesthesia in Laparoscopic Surgery in India
Pre independence Education in Inndia.pdf
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Sports Quiz easy sports quiz sports quiz
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study

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