Читать книгу Python For Kids For Dummies - Scott Brendan - Страница 2
Introduction
ОглавлениеHi! Welcome to the book. You’re going on a tour of all things Python. If you join me and code along with the projects, you’ll have your basic Python programming wings by the end of the book.
Everything in this book you need to know by doing – typing in the code or, better yet, thinking up the code before reading what I’ve done.
About This Book
This book walks you through all the parts you have to know about Python programming. You get examples. I talk about planning programs. And I help link you with the broader Python community so that you can head out there after mastering the projects in this book.
Conventions Used
Keep these things in mind while you read:
✓ Sometimes words are in italics and then I explain the words. Here is an example: “The objects in the list are called elements.” When you see this sentence, you know to keep your eyes peeled for a definition. (Elements are the objects in a list.)
✓ Python code is written in a different font from the other text. Sometimes it’s inline with the text and looks like this: print('Hello World!').
✓ Sometimes it’s a separate block of text, like this: print('Hello World!')
✓ Some code blocks have a >>> at the front of some lines. I’m showing you what happens when you’re using an interactive Python prompt. You need to type the code that follows the >>> in this book into the Python console that’s running on your computer to see what happens:
>>> my_message = "Hello World!"
>>> print(my_message)
✓ The number of spaces at the front of each line of code is important. The length of your lines isn’t (technically) significant, but Python style guidelines suggest lines with no more than 79 characters (letters, numbers, spaces, or punctuation marks). This book isn’t as wide as your screen; it only lets me show 69 characters in a line. I’ve broken (split) some lines of code in the book. I split them to make sure that the code works and prints the right way. Be careful when you type them in! It’s not always clear how many spaces are at the front of the broken line.
✓ I split lines two ways.
• The first is implicitly. Basically, you can split the code in between any pair of parentheses at a comma. Python still sees it as a single line. The second and later parts of a split line should be indented to where the parentheses opened. Here’s an example from the code in Project 9:
values = (e.first_name, e.family_name,
e. date_of_birth, e.email_address)
Even though you type this as two separate lines, Python sees it as a single line. (Think of it as one long line.) Type this code as you see it, pressing Enter at the end of each line and typing spaces at the start of each line so that the first character in the line is in the right place.
• The second way to split a line is explicitly with the backslash character: \ (not /). Here’s an example from Project 9:
raw_input_prompt = "Press: 1 for training,"+\
" 2 for testing, 3 to quit.\n"
You type these as two separate lines, with the \ at the end of the first line. However, Python sees it as a single line.
✓ When using the Python interpreter in Projects 2 and 3 only, each new line starts with either … or >>>. If you don’t see these in the code in the book, then the previous line is meant to be typed in as one long line. For example, the following code is from Project 2:
>>> my_second_message = 'This name is a little long. Ideally, try to keep the name short, but not too short.'
This code doesn’t have … or >>> at the start of the second or third line. This means you’re supposed to type it all in before pressing Enter. Only press Enter after typing too short.' at the end, not after typing little long. and but not.
✓ Sometimes the output on your computer may look a little different from what you see in this book. For example, when you run a program in later projects, you might see a restart line. On my screen, all the following text is on a single line:
>>> ================================ RESTART ================================
✓ In Project 4 you see how to automatically indent your code. Until then, each time you need to indent code, do it by pressing the spacebar four times before you add your code. If you have to indent code two levels, press the spacebar eight times (that’s two levels of indent by four spaces per level) before typing your code, and so on. You need to do this for each line of the indented code.
✓ When I’m explaining how code works, I often provide a code template – an outline of how to use the code. A sample template is: help([object name]). In this template, the keyword is help, and it needs to be followed by a pair of parentheses. The square brackets indicate something which is optional. The italics mean you need to fill in. Everything not in italics, type it just like it looks. Using this template, the code help(help) works (it gets help on the help keyword), and so does help(), with nothing inside the parentheses (since [object name] was optional).
✓ Web addresses (URLs) and programming code are in monofont. If you’re reading this book on a device connected to the Internet, you can click the address to visit that website. Try it: www.dummies.com.
✓ Sometimes you need to choose something from a menu. I’m not talking about a burger and fries. I mean actions. For example, I might ask you to choose File ⇒ New File. This means that you go to the File menu and choose New File from it.
✓ The word Ctrl means the Ctrl key on your keyboard. Ctrl+A means that you press the key marked Ctrl while you press the A key. All at the same time. Then release both keys. If you’re using a Mac, your keyboard has a control key – use it. Ctrl-A means press the control key down and press the A key. Then release both keys. Don’t use the option or command keys.
✓ If you’re using a Mac, assume that when I say Enter key, it means the Return key on your keyboard.
Foolish Assumptions
I’ve tried not to make too many assumptions about you in this book. In order to use this book, you need to be able to turn on your computer and navigate the Start Menu (on Windows). To install Python you will need administrator access for the computer you’re installing it on.
Learning anything is slow going to start. You are going to need a bit of determination to make it through the book. Hang in there.
Icons Used in This Book
The Warning icon tells you to watch out! This information may save you headaches. In some cases, you could lose data if you don’t heed the warnings.
You’re gonna use this information for a long, long time. Commit it to long-term gray matter.
The Tip icon marks shortcuts that make programming easier.
Coding Connections icons mark information that applies not just to Python, but to coding in general.
Beyond the Book
You can find a bunch more information outside this book. Check out:
✓ Cheat Sheet: This book has an online cheat sheet at www.dummies.com/cheatsheet/pythonforkids. The Cheat Sheet has a list of Python keywords, common built-ins, and selected functions from the standard library. Use it as a quick reference when you’re coding.
✓ Dummies.com online articles and bonus projects: In addition to the projects in this book, there are some bonus projects online. You can get them from www.dummies.com/extras/pythonforkids.
✓ python4kids.brendanscott.com: Visit my Python for Kids blog. Many of the projects in this book started out there. The blog has a dedicated blog entry for each of the projects and has a heap of other things you can try, too. If you’ve got feedback, you can leave it on the blog page that applies.
Where to Go from Here
Right now you should go to Project 1 to read more about what this language can do and to install it. Before you move on to Project 2, make sure you know about Ctrl+C. Then you’re ready to write your first Python program! Move in and out of the projects as you like. The code in each project stands on its own. Be careful though – even though they don’t use code from earlier projects, they often use concepts introduced earlier.