Python prints the love list


1 / 15

Head First Python: Ch 1. Everyone loves lists. Aug 22, 2013 Hee -gook Jun. Outline. Create simple Python lists Lists are like arrays Add more data to your list Work with your list data For loops work with lists of any size Store lists within lists Check a list for a list

Copyright Complaint Adult Content Flag as Inappropriate

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript
Head first python ch 1 everyone loves lists
Outline
Outline
  • Create simple Python lists
  • Lists are like arrays
  • Add more data to your list
  • Work with your list data
  • For loops work with lists of any size
  • Store lists within lists
  • Check a list for a list
  • Complex data is hard to process
  • Handle many levels of nested lists
  • Don’t repeat code; create a function
  • Create a function in Python
  • Recursion to the rescue!
  • Your Python Toolbox
Create simple python lists
Create simple Python lists
  • 1 Convert each of the names into strings by surrounding the data with quotes
  • 2 Separate each of the list items from the next with a comma
  • 3 Surround the list of items with opening and closing square brackets
  • 4 Assign the list to an identifier (movies in the preceding code) using the assignment operator (=)
  • It does not need to declare data type in Python

    movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life"]

Lists are like arrays
Lists are like arrays
  • Access list data using the square bracket notation

    print(movies[1])

Exercises
Exercises

cast = ["Cleese", 'Palin', 'Jones', "Idle"]

print(cast)

print(len(cast))

print(cast[1])

cast.append("Gilliam") # add a single data item

print(cast)

cast.pop() # remove data from the end of the list

print(cast)

cast.extend(["Gilliam", "Chapman"]) # add a collection of data items to the end of the list

print(cast)

cast.remove("Chapman") # remove a specific data item from the list

print(cast)

cast.insert(0, "Chapman") # add a data item before a specific slot location

print(cast)

Add more data to your list
Add more data to your list
  • Python lists can contain data of mixed type

    ["The Holy Grail", 1975, "The Life of Brian", 1979, "The Meaning of Life", 1983]

    movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life"]

    movies.insert(1, 1975)

    movies.insert(3, 1979)

    movies.append(1983)

    movies = ["The Holy Grail", 1975,"The Life of Brian", 1979,"The Meaning of Life", 1983]

Work with your list data
Work with your list data

# When you use “while”, you have to worry about state information that requires you to employ a counting identifier

count = 0

while count < len(movies):

print(movies[count])

count = count+1

# When you use “for”, the Phthon interpreter worries about the state information for you

for each_item in movies:

print(each_item)

# Indented code is called as “suite”

Store lists within lists
Store lists within lists
  • Lists can hold collections of anything including other lists

    movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,

    ["Graham Chapman",

    ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]

    ]

    ]

    print(movies[4][1][3])

    # The “for” loop prints each item of the outer loop only

    for each_item in movies:

    print(each_item)

Check a list for a list
Check a list for a list
  • Each time you process an item in your list, you need to check to see if the item is another list
    • If the item is a list, you need to process the nested list before processing the next item in your outer list

      # Ask if “each_item” is a list

      for each_item in movies:

      if isinstance(each_item, list):

      for nested_item in each_item:

      print(nested_item)

      else:

      print(each_item)

      # But it cannot print nested list of nested list

Complex data is hard to process
Complex data is hard to process

# Ask if “each_item” is a list

for each_item in movies:

if isinstance(each_item, list):

for nested_item in each_item:

print(nested_item)

else:

print(each_item)

movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,

["Graham Chapman",

["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]

]

]

Handle many levels of nested lists
Handle many levels of nested lists
  • The solution is to add more code to handle the additionally nested list

    for each_item in movies:

    if isinstance(each_item, list):

    for nested_item in each_item:

    if isinstance(nested_item, list):

    for deeper_itemin nested_item:

    print(deeper_item)

    else:

    print(nested_item)

    else:

    print(each_item)

    # But overly complex code is rarely a good thing

Don t repeat code create function
Don’t repeat code; create function
  • This code contains a lot of repeated code

    for each_item in movies:

    if isinstance(each_item, list):

    for nested_item in each_item:

    if isinstance(nested_item, list):

    for deeper_item in nested_item:

    print(deeper_item)

    else:

    print(nested_item)

    else:

    print(each_item)

Create a function in python
Create a function in Python
  • A Function in Python is a named suite of code, which can also take an optional list of arguments if required
Recursion to the rescue
Recursion to the rescue!
  • Take advantage of function and recursion
    • To solve the code complexity problems that had crept into the earlier list-processing code

      defprint_lol(the_list):

      for each_item in the_list:

      if isinstance(each_item, list):

      print_lol(each_item)

      else:

      print(each_item)

      print_lol(movies)

Terminology
Terminology
  • Python philosophy: “Batteries included”
    • A way of referring to the fact that Python comes with most everything you’ll need to get going quickly and productively
    • Python let you do most things well, without having to rely on code from third parties to get going
    • e.g.) Many BIFs: isinstance()..
  • BIF
    • a built-in function
    • print(), isinstance(), ..
  • Suite
    • a block of Python code, which is indented to indicate grouping
ad
  • 1 of 5

Today's Free

PowerPoint Template

Download Template

For SlideServe users

Download Now
Download Presentation
Download Section

Connecting to Server..


Источник: http://www.slideserve.com/jerry/head-first-python-ch-1-everyone-loves-lists


Закрыть ... [X]

11. Lists How to Think Like a Computer Scientist: Learning with Angelina jolie premiere black strapless 2017


Python prints the love list

[MAC Nutcracker Sweet Lipsticks MAC Flamboyish Lipstick (.00 for 0.10 oz.) is a bright, medium.



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[



Python prints the love list

[