




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Best book for learning python
Typology: Study notes
1 / 241
This page cannot be seen from the preview
Don't miss anything!





























































































Copyright © 2017 Mark Myers
All rights reserved, including the right to reproduce this book, or any portions of it, in any form.
http://www.ASmarterWayToLearn.com
Digital book(s) (epub and mobi) produced by Booknook.biz.
23: Getting information from the user and converting strings and numbers
24: Changing case
25: Dictionaries: What they are
26: Dictionaries: How to code one
27: Dictionaries: How to pick information out of them
28: Dictionaries: The versatility of keys and values
29: Dictionaries: Adding items
30: Dictionaries: Removing and changing items
31: Dictionaries: Looping through values
32: Dictionaries: Looping through keys
33: Dictionaries: Looping through key-value pairs
34: Creating a list of dictionaries
35: How to pick information out of a list of dictionaries
36: How to append a new dictionary to a list of dictionaries
37: Creating a dictionary that contains lists
38: How to get information out of a list within a dictionary
39: Creating a dictionary that contains a dictionary
40: How to get information out of a dictionary within another dictionary
41: Functions
42: Functions: Passing them information
43: Functions: Passing information to them a different way
44: Functions: Assigning a default value to a parameter
45: Functions: Mixing positional and keyword arguments
46: Functions: Dealing with an unknown number of arguments
47: Functions: Passing information back from them
48: Using functions as variables (which is what they really are)
49: Functions: Local vs. global variables
50: Functions within functions
51: While loops
52: While loops: Setting a flag
53: Classes
54: Classes: Starting to build the structure
55: Classes: A bit of housekeeping
56: Classes: Creating an instance
57: Classes: A little more complexity
58: Classes: Getting info out of instances
59: Classes: Building functions into them
60: Classes: Coding a method
61: Classes: Changing an attribute's value
62: Data files
63: Data files: Storing data
64: Data files: Retrieving data
65: Data files: Appending data
66: Modules
67: CSV files
68: CSV files: Reading them
69: CSV files: Picking information out of them
70: CSV files: Loading information into them. Part 1
71: CSV files: Loading information into them. Part 2
72: CSV files: Loading information into them. Part 3
73: CSV files: Appending rows to them.
74: How to save a Python list or dictionary in a file: JSON
75: How to retrieve a Python list or dictionary from a JSON file
76: Planning for things to go wrong
77: A more practical example of exception handling
Guide to the appendices Appendix A: An easy way to run Python Appendix B: How to install Python on your computer Appendix C: How to run Python in the terminal Appendix D: How to create a Python program that you can save Appendix E: How to run a saved Python program in the terminal
Learn it faster.
Remember it longer.
If you embrace this method of learning, you’ll get the hang of Python in less time than you might expect. And the knowledge will stick. You’ll catch onto concepts quickly. You’ll be less bored, and might even be excited. You’ll certainly be motivated. You’ll feel confident instead of frustrated. You’ll remember the lessons long after you close the book. Is all this too much for a book to promise? Yes, it is. Yet I can make these promises and keep them, because this isn’t just a book. It’s a book plus almost a thousand interactive online exercises. You’re going to learn by doing. You'll read a chapter, then practice with the exercises. That way, the knowledge gets embedded in your memory so you don't forget it. Instant feedback corrects your mistakes like a one-on-one teacher. I’ve done my best to write each chapter so it’s easy for anyone to understand, but it’s the exercises that are going to turn you into a real Python coder. Cognitive research shows that reading alone doesn’t buy you much long- term retention. Even if you read a book a second or even a third time, things won’t improve much, according to research. And forget highlighting or underlining. Marking up a book gives us the illusion that we’re engaging with the material, but studies show that it’s an exercise in self-deception. It doesn’t matter how much yellow you paint on the pages, or how many times you review the highlighted material. By the time you get to Chapter 50, you’ll have forgotten most of what you highlighted in Chapter 1. This all changes if you read less and do more—if you read a short passage and then immediately put it into practice. Washington University researchers say that being asked to retrieve information increases long-term retention by four
hundred percent. That may seem implausible, but by the time you finish this book, I think you’ll believe it. Practice also makes learning more interesting. Trying to absorb long passages of technical material puts you to sleep and kills your motivation. Ten minutes of reading followed by fifteen minutes of challenging practice keeps you awake and spurs you on. And it keeps you honest. If you only read, it’s easy to kid yourself that you’re learning more than you are. But when you’re challenged to produce the goods, there’s a moment of truth. You know that you know—or that you don’t. When you find out that you’re a little shaky on this point or that, you can review the material, then re-do the exercise. That’s all it takes to master this book from beginning to end—and to build a solid foundation of Python knowledge. I’ve talked with many readers who say they thought they had a problem understanding technical concepts. But what looked like a comprehension problem was really a retention problem. If you get to Chapter 50 and everything you studied in Chapter 1 has faded from memory, how can you understand Chapter 50, which depends on your knowing Chapter 1 cold? The read-then- practice approach embeds the concepts of each chapter in your long-term memory, so you’re prepared to tackle material in later chapters that builds on top of those concepts. When you’re able to remember what you read, you’ll find that you learn Python quite readily. I hope you enjoy this learning approach. And I hope you build on it to become a terrific coder.
details, a fundamental requirement for coding in any language.
Subscribe, temporarily, to my formatting biases. Current code formatting is like seventeenth-century spelling. Everyone does it his own way. There are no universally accepted standards. But the algorithms that check your work when you do the interactive exercises need standards. They can't grant you the latitude that a human teacher could, because, let's face it, algorithms aren't that bright. So I've had to settle on certain conventions. All of the conventions I teach are embraced by a large segment of the coding community, so you'll be in good company. But that doesn't mean you'll be married to my formatting biases forever. When you start coding projects, you'll soon develop your own opinions or join an organization that has a stylebook. Until then, I'll ask you to make your code look like my code.
The language you're learning here
Python is a popular, 30-year-old general purpose programming language created by Guido van Rossum. Compared with some other languages, it's reasonably easy to learn, and it's relatively easy to read. Python is often used to teach beginners the fundamentals of programming.
2
Variables for Strings
Please memorize the following facts.
My name is Mark.
My nationality is U.S.
Now that you've memorized my name and nationality, I won't have to repeat them again. If I say to you, "You probably know other people who have my name," you'll know I'm referring to "Mark." If I ask you whether my nationality is the same as yours, I won't have to ask, "Is your nationality the same as U.S.?" I'll ask, "Is your nationality the same as my nationality?" You'll remember that when I say "my nationality," I'm referring to "U.S.", and you'll compare your nationality with "U.S.", even though I haven't said "U.S." explicitly. In these examples, the terms my name and my nationality work the same way Python variables do. my name refers to a particular value, "Mark." In the same way, a variable refers to a particular value. You could say that my name is a variable that refers to the string "Mark." A variable is created this way:
name = "Mark"
Now the variable name refers to the text string "Mark". Note that it was my choice to call it name. I could have called it my_name , xyz , lol , or something else. It's up to me how to name my variables, within limits. More on those limits later. With the string "Mark" assigned to the variable name , my Python code doesn't have to specify "Mark" again. Whenever Python encounters name ,
But that was then. Now if, having written…
name = "Ace"
…if I write…
print(name)
…Python displays...
Ace
A variable can have any number of values, but only one at a time. Python variable names have no inherent meaning to Python. In English, words have meaning. You can't use just any word to communicate. I can say, "My name is Mark," but, if I want to be understood, I can't say, "My floogle is Mark." That's nonsense. But with variables, Python is blind to semantics. You can use just any word (as long as it doesn't break the rules of variable-naming, which I'll cover later). From Python's point of view...
floogle = "Mark"
...is just as good as...
name = "Mark"
If you write...
floogle = "Mark"
...then write…
print(floogle)
…Python displays...
Mark
Within limits, you can name variables anything you want, and Python won't care.
lesson_author = "Mark" guy_who_keeps_saying_his_own_name = "Mark" x = "Mark"
Python's blindness to meaning notwithstanding, when it comes to variable names, you'll want to give your variables meaningful names, because it'll help you and other coders understand your code. Again, the syntactic difference between variables and text strings is that variables are never enclosed in quotes, and text strings are always enclosed in quotes. It's always...
last_name = "Smith" city_of_origin = "New Orleans" aussie_greeting = "g'Day"
If it's an alphabet letter or word, and it isn't enclosed in quotes, and it isn't a keyword that has special meaning for Python, like print , it's a variable. If it's some characters enclosed in quotes, it's a text string. If you haven't noticed, let me point out the spaces between the variable and the equal sign, and between the equal sign and the value.
nickname = "Bub"
These spaces are a style choice rather than a legal requirement. But I'll ask you to include them in your code throughout the practice exercises. In the last chapter you learned to write...