The first chapter of Headfirst Python study notes

Source: Internet
Author: User
Tags python list jupyter jupyter notebook

For Python beginners, willing to strongly recommend from the "Headfirst Python" began to read, this book is really done in layman's terms, headfirst series, itself is also a guarantee of quality. This book is ready for download in the article "Python Start: Children's shoes for 0 programming Basics". In order to facilitate everyone's study, willing to deliberately produced jupyter notebook format notes, the article at the end of the willing to provide notes.

While reading "Headfirst Python", the most urgent thing is to do exercises in time, and you can even pick up the exercises after a quick tour of the chapter.

When doing exercises, remember not to use the python that is suggested in the book to bring the Idle. As an experienced, willing to recommend the use of Jupyter Notobook. Specific installation methods are willing to be in the "Python Start: for 0 programming basics of children's shoes," it is clear that every time you want to practice, run Jupyter-notebook.exe, you can open the program in the browser page code input. Jupyter Notobook has a huge advantage over Python's own idle:

    • Enhanced version of the code auto-completion: In the process of entering the code, you can try to press the TAB key, the program will prompt the corresponding code;
    • Each entry code program will automatically save for you, this is equivalent to your own study notes, you can add comments before or after the code, comments preceded by a "#" + A space (space can not add, but willing to suggest from the beginning to develop good writing code habits, # After the space is the Python code specification-PEP8 requirements) on it;
    • Notebook give you saved notes, you can open at any time, click the Run button (or with the Alt+enter shortcut key) can run the selection of a piece of code, which is the python comes from the idle simply can not do things;
    • In notebook you can enter multiple lines of code with great ease, and Python's own idle is extremely restrictive in this regard;
    • For Yan control, notebook's code coloring looks more comfortable;
    • ......

Willing will be the book in each chapter mentioned in most of the exercises, included in the notes, you can read a chapter, take willing to give you notes, in the Jupyter notebook practice. Of course you can also enter and run in Jupyter Notobook as soon as you see a piece of code in the book.

Willing to provide the note is IPYNB format (ipy refers to Ipython, Notebook is implemented with Ipython. NB is notebook abbreviation), when you download the note, please put it in the "Your Python installation folder (default is C:\python34) \scripts" folder, and then refresh the browser Jupyter notebook home page, You can see this note, click on the note, you can browse.

650) this.width=650; "title=" SNAG-0140 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" SNAG-0140 "src=" http://s3.51cto.com/wyfs02/M02/8B/82/ Wkiom1hpzlawmsriaab-mdxxxm0685.jpg "width=" 553 "height=" 377 "/>

If you have already clicked "New->python 3" in the Jupyter Notebook home page, then you can use the menu "File->open" to go back to the homepage to select the notes you would like to provide.

650) this.width=650; "title=" SNAG-0143 "style=" border-left-0px; border-right-width:0px; Background-image:none; border-bottom-width:0px; padding-top:0px; padding-left:0px; padding-right:0px; border-top-width:0px "border=" 0 "alt=" SNAG-0143 "src=" Http://s3.51cto.com/wyfs02/M02/8B/82/wKiom1hPzLezXkERAACJuW _wjmk803.jpg "width=" 645 "height=" 194 "/>

The following is willing to begin to explain the main points of the first chapter.

The text "[in]:" After the content, that we entered in the Jupyter notebook code and comments, the content of "#" beginning, is willing to comment.

"[out]:" Later, the output is the result of the code execution.

Willing in the comments, marked the code page number, so that you control the book to practice. Some of the content in the book is more detailed than willing to explain.

Willing to rewrite some of the code, but does not affect the final effect of the code.


[In]:

# Ripe for sorrow print commands.
# The first chapter P4
# The content is preceded by a # sign for comments, and Python automatically ignores the contents after the # sign
If > 42:
Print ("Don ' t panic!")


[out]:

Don ' t panic!

[In]:

# Create a simple python list
# The first chapter P8
# identifier: Movies, the name you gave this list
# operator: =, assigns the list to the identifier
# list format: two ends with brackets; Each item in the list is separated by commas; the name of the movie is quoted;
Movies = ["The Holy Grail",
"The Life of Brian",
"The Meaning of life"]
Print (Movies)


[out]:

[' The Holy Grail ', ' The Life of Brian ', ' The Meaning of life ']

[In]:

# access the list with the brackets notation
# The first chapter P9
# Print the 2nd data in the list (1th of 0)
Print (Movies[1])


[out]:

The Life of Brian

[In]:

# list in-depth learning: Len
# The first chapter P10
# len: Gets the list length, that is, the list has several data items
cast = ["Cleese", ' Plain ', ' Jones ', ' Idle ']
Print (CAST)
Print (len (cast))
Print (cast[0]) # 0 represents the first data item, you can change the number yourself to try


[out]:

[' Cleese ', ' Plain ', ' Jones ', ' Idle ']
4
Cleese

[In]:

# list in-depth study: Append
# The first chapter P10
# Append: Add a piece of data at the end of the list
Cast.append ("Gillianm")
Print (CAST)


[out]:

[' Cleese ', ' Plain ', ' Jones ', ' Idle ', ' gillianm ']

[In]:

# list in-depth learning: Pop
# The first chapter P10
# Pop: Delete data from the end of the list
Cast.pop ()


[out]:

' Gillianm '

[In]:

# Print again and see what's changed
Print (CAST)


[out]:

[' Cleese ', ' Plain ', ' Jones ', ' Idle ']

[In]:

# list in-depth study: Extend
# The first chapter P10
# Extend: Add a list at the end of the list (two list merges)
Cast.extend (["Gillianm", "Chapman"])
Print (CAST)


[out]:

[' Cleese ', ' Plain ', ' Jones ', ' Idle ', ' gillianm ', ' Chapman ']

[In]:

# List Deep Learning: Remove
# The first chapter P10
# Remove: Find and delete a specific item of data in the list
Cast.remove ("Chapman")
Print (CAST)


[out]:

[' Cleese ', ' Plain ', ' Jones ', ' Idle ', ' gillianm ']

[In]:

# List Deep learning: Insert
# The first chapter P10
# Insert: Add a data item before a specific position in the list
Cast.insert (0, "Chapman") # 0 means insert at the front, you can try other numbers
Print (CAST)


[out]:

[' Chapman ', ' Cleese ', ' Plain ', ' Jones ', ' Idle ', ' gillianm ']

[In]:

# Exercises
# The first chapter P13
# Add the date of each movie to the list
# like this: [' The Holy Grail ', 1975, 1975, 1979,
# ' The Life of Brian ', 1979, ' The Meaning of Life ', 1983, 1983]
# When putting numbers in a list, numbers don't have to be quoted
Movies.insert (1, 1975) # 1th Insert before 2nd list item
Movies.insert (3, 1979) # 2nd Insert before the 4th list item (think about why 3)
Movies.append (1983) # The last one is appended to the end.
Print (Movies)


[out]:

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

[In]:

# list: Iterations
# The first chapter P15
# iteration: Use a For loop to print out all the items in the list
# for ... Represents each data that is taken from this list
For i in movies: # I can use whichever (one or more) letters here
Print (i) # as long as two I remain consistent. You can try different letters.


[out]:

The Holy Grail
1975
The Life of Brian
1979
The Meaning of life
1983

[In]:

# list: Iterations
# The first chapter P16
# iteration: Use a while loop to print out all the items in the list
# effects are the same as for loops
# while XX < XXX said: As long as XX < xxx, will continue to carry out until this condition is not established
Count = 0
While Count < Len (Movies): # Previously learned Len usage
Print (Movies[count]) # I've just learned the square brackets in front of the notation
The Count + = 1 # and the book Count = Count + 1 are the same, indicating that count is increased by 1

[In]:

# list: Save list in list
# The first chapter P18
# We use the Movies list to record a movie's data. The Holy Grail the film is full of the name
# "Monty python and the Holy Grail" (Python and the Holy Grail), produced in 1975, two Terry is the director,
# also participated in the show. The film was 91 minutes long, and Chapman was a screenwriter (and starring). Michael, this
# A gang of people is both a screenwriter and a supporting actor. These guys are really playing hi, self-directed self-acting.
Movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
["Graham Chapman", ["Michael Palin", "John Clseese",
"Terry Gilliam", "Eric Idle", "Terry Jones"]
Print (Movies)


[out]:

[' The Holy Grail ', 1975, ' Terry Jones & Terry Gilliam ',, [' Graham Chapman ', [' Michael Palin ', ' John clseese ', ' Terr Y Gilliam ', ' Eric Idle ', ' Terry Jones ']


[In]:

# Use a For loop to process this list
For I in movies:
Print (i)


[out]:

The Holy Grail
1975
Terry Jones & Terry Gilliam
91
[' Graham Chapman ', [' Michail Palin ', ' John Clseese ', ' Terry Gilliam ', ' Eric Idle ', ' Terry Jones ']

[In]:

# list: Find a list in a list
# The first chapter P20
# isinstance: Determine the type of identifier
names = [' Michael ', ' Terry ']
Isinstance (names, list) # to determine if it is a list type


[out]:

True

[In]:

# list: Find a list in a list
# The first chapter P20
# isinstance: Determine the type of identifier
# in a different way
Num_names = len (names)
Isinstance (Num_names, list)


[out]:

False

[In]:

# list: Find a list in a list
# The first chapter P21
# Exercise: Print out every item in the movies list above
# with If ... else ... Mode
# Combined with Isinstance
For I in movies:
If Isinstance (i, List): # First Judge I is not a list
For j in I: # Yes, with a for loop
If Isinstance (j, List): # and dig down another layer, because there's a nested list
For K in J:
Print (k)
Else: # Every pair of If...else ... To align
Print (j)
else: # Print directly without a list
Print (i)
# What if I have another nested list?


[out]:

The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michail Palin
John Clseese
Terry Gilliam
Eric Idle
Terry Jones

[In]:

# function
# The first chapter P30
# using the function can simplify the code just written, and then a few more layers of the list I'm not afraid!
The DEF keyword is an abbreviation for define, and Python knows that this is a function when you see the def.
def print_lol (the_list): # Print_lol is the function name we define, as much as possible to make a meaningful name
For I in The_list:
If Isinstance (i, list):
Print_lol (i) # if it is a list, then call "yourself" to process the list again
Else
Print (i)

[In]:

# function
# The first chapter P30
# Let's use the Print_lol function to print our list movies
Print_lol (Movies) # Look, is the code a lot leaner?


[out]:

The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michail Palin
John Clseese
Terry Gilliam
Eric Idle
Terry Jones

The first chapter ends.

This article notes:

Link: Http://pan.baidu.com/s/1eR4iJoI Password: t72j

This article copyright to willing to learn all, welcome reprint, reproduced please indicate the author and source. Thank you!
Willing
Starting: Willing Sina Blog

The first chapter of Headfirst Python study notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.