GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing aeturrell.github.io — Workflow: Basics – Python for Data Science If you followed the previous chapters, you will now have some experience running Python code. We didn’t give you many details, but you’ve obviously figured out the basics, or you would’ve thrown this book away in frustration! Frustration is natural when you start programming in Python, because it is such a stickler for punctuation, and even one character out of place will cause it to complain. But while you should expect to be a little frustrated, take comfort in that this experience is both typical and temporary: it happens to everyone, and the only way to get over it is to keep trying. Before we go any further, let’s make sure you’ve got a solid foundation in running Python code, and that you know about some of the most helpful Visual Studio Code features for working with Python. Coding BasicsLet’s review some basics we’ve omitted thus far in the interests of getting you up to speed as quickly as possible. You can use Python as a calculator: The extra package numpy contains many of the additional mathematical operators that you might need. If you don’t already have numpy installed, open up the terminal in Visual Studio Code (go to “Terminal -> New Terminal” and then type You can create new objects with the assignment operator There are several structures in Python that capture multiple objects simultaneously but perhaps the most common is the list, which is designated by square brackets. To do basic arithmetic on a list, use a list comprehension which has the structure “for every element in this list, perform an operation”. For example, to multiply each element by three. Note that the word “element” above could have been almost any word because we define it by saying All Python statements where you create objects (known as assignment statements) have the same form:
When reading that code, say “object name gets value” in your head. CommentsPython will ignore any text after Comments can be helpful for briefly describing what the subsequent code does.
With short pieces of code like this, it is not necessary to leave a command for every single line of code and you should try to use informative names wherever you can because these help readers of your code (likely to be you in the future) understand what is going on! Our advice is to use comments to explain the why of your code, not the how or the what. The what and how of your code are always possible to figure out, even if it might be tedious, by carefully reading it. If you describe every step in the comments, and then change the code, you will have to remember to update the comments as well (tedious) or it will be confusing when you return to your code in the future. Figuring out why something was done is much more difficult, if not impossible. For example, geom_smooth() has an argument called span, that controls the smoothness of the curve, with larger values yielding a smoother curve. Suppose you decide to change the value of span from its default of 0.75 to 0.9: it’s easy for a future reader to understand what is happening, but unless you note your thinking in a comment, no one will understand why you changed the default. For data analysis code, use comments to explain your overall plan of attack and record important insights as you encounter them. There’s no way to re-capture this knowledge from the code itself. Keeping Track of VariablesYou can always inspect an already-created object by typing its name into the interactive window: If you want to know what type of object it is, use Visual Studio Code has some powerful features to help you keep track of objects:
What’s In A Name?Object (aka “variable”) names must do the following to be valid in Python:
Object names in Python are case-sensitive too, so When you’re naming objects, it’s best to make them descriptive so you can keep track of what they are. You’ll need to adopt a convention for multiple words. We recommend snake_case, where you separate lowercase words with
Exercise
Try creating the object Remember that you can always inspect an object that you’ve created by typing its name again: Make another assignment: To save yourself time in inspecting this object via the interactive window, you can just begin typing the name (type “this”) and then hit the TAB button. Visual Studio Code will autocomplete what you’ve written using the variables you’ve defined during your session. This is a top tip to save time! If you’re using the interactive console, rather than a notebook, there’s another top tip. Let’s say you previously ran Let’s define another variable: Now let’s try to inspect it: This illustrates the brilliance and frustration of coding: your IDE (Visual Studio Code) will do tedious computations for you, but, in exchange, you must be precise in your instructions. If not, you’re likely to get an error that says the object you’re looking for was not found. Typos matter; Python can’t read your mind and say, “oh, they probably meant Calling FunctionsPython has a large number of built-in functions. You can also import functions from packages (like we did with In coding, a function has inputs, it performs its function, and it returns any outputs. Let’s see a simple example of using a built-in function, The general structure of functions is the function name, followed by brackets, followed by one or more arguments. Sometimes there will also be keyword arguments. For example, If you’re ever unsure of what a function does, you can call
Or, in Visual Studio Code, hover your mouse over the function name. Just as with variables, code completion works on functions too. Try typing in You’ll need to be extra careful with objects that are strings (words, sentences, letters, and phrases), because these always need to come with quotation marks around them. You can use single or double quotation marks as you like, but i) the convention is double quotation marks, and ii) it’s good to be consistent, whichever you choose. Here’s an example of some code that throws an error Again, Visual Studio Code can really help you out here because as soon as you open a double quotation mark, it will have the closing one ready for you. Exercises
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |