Python Tutorial: Welcome to Python

preview_player
Показать описание

----

Hi! Welcome to Python for Spreadsheet Users. My name is Chris Cardillo and I’ll be your instructor for this course.

Python is a general-purpose programming language, meaning it helps run everything from websites to self-driving cars.

Python has amazing code packages for working with data.

But most fundamentally, Python is an object-oriented programming language. This is a fancy way of saying, in Python, one thing is usually located inside another thing.

In spreadsheets, you’re probably familiar with functions like SUM to add numbers. This function is actually located inside the Math package.

Or maybe you’re familiar with VLOOKUP, INDEX, and MATCH. These functions are all located inside the Lookup package.

In Python, let’s say there was also a package called math, and inside this math package, there was a function called SUM, just like in spreadsheets. To access the SUM function inside the math package, we’d write math-dot-SUM(), and then place numbers inside the function. Here, we’d expect a result of 5.

That dot lets us access a function inside of the package, one thing inside another thing, and that’s Python.

In Python we tell a computer what to do with a script. A script is a series of instructions ran in a specific order that creates a desired outcome. They're like recipes.

At the beginning of these scripts, we’ll often import packages, like our math package.

Let’s import a package from Python that will allow us to load in and manipulate our data, a package called pandas.

Yes, pandas.

To use pandas we write import pandas. We write this line in all lowercase letters because Python is case sensitive.

On the next line, after we import pandas, we’ll load our Excel data into Python. pandas likes data with rows and named columns, like the example here. We have 3 columns: a fruit's name, color, and how much it costs in US Dollars. Each row is a different fruit. This is the kind of tidy data pandas likes.

To load in our Excel data, we’ll use the read-underscore-excel function in the pandas package. And to access the function inside of the package, we'll use the dot: pandas-dot-read-underscore-excel().

Our file name goes inside the function, in quotes.

And now we can import Excel data into Python. Before we continue, it’s common practice to import pandas as pd. This lets us reference the pandas package with pd instead of writing out pandas all the time. Now we can just write pd-dot-read-underscore-excel() instead.

Now, our data needs somewhere to live, because we’ll have to reference it later on. So we’ll assign it to a variable. Assignment is as intuitive as: fruit equals pd-dot-read-underscore-excel('fruit-dot-xlsx').

You can call variables anything, usually something meaningful. They just have to start with a letter and can’t have spaces or special characters. It’s common to use an underscore to substitute for spaces, though.

Finally, to look at all of our data, we can use Python's built-in print function at the end of our script. This function does not require any dot accessing. We simply write print and pass the variable fruit to the function. This will display all of our data in the console.

The console, at bottom of this image, will display the results of your script in the upcoming exercises. You will also learn how to interact with it directly later on.

Now it’s your turn to write some code.
Рекомендации по теме
join shbcf.ru