Course Description

Python is a general-purpose programming language that is becoming more and more popular for doing data science. Companies worldwide are using Python to harvest insights from their data and get a competitive edge. Unlike any other Python tutorial, this course focuses on Python specifically for data science. In our Intro to Python class, you will learn about powerful ways to store and manipulate data as well as cool data science tools to start your own analyses. Enter DataCamp’s online Python curriculum.

Python Basics

An introduction to the basic concepts of Python. Learn how to use Python both interactively and through a script. Create your first variables and acquaint yourself with Python’s basic data types.

The Python Interface

In the Python script below, you can type Python code to solve the exercises. If you hit Run Code or Submit Answer, your python script (script.py) is executed and the output is shown in the IPython Shell. Submit Answer checks whether your submission is correct and gives you feedback.

You can hit Run Code and Submit Answer as often as you want. If you’re stuck, you can click Get Hint, and ultimately Get Solution.

You can also use the IPython Shell interactively by simply typing commands and hitting Enter. When you work in the shell directly, your code will not be checked for correctness so it is a great way to experiment.

practice

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgRXhhbXBsZSwgZG8gbm90IG1vZGlmeSFcbnByaW50KDUgLyA4KVxuIyBQdXQgY29kZSBiZWxvdyBoZXJlIiwic29sdXRpb24iOiIjIEV4YW1wbGUsIGRvIG5vdCBtb2RpZnkhXG5wcmludCg1IC8gOClcbiMgUHV0IGNvZGUgYmVsb3cgaGVyZVxucHJpbnQoNysxMCkifQ==

When to use Python?

Python is a pretty versatile language.

Any comments?

You can add comments to your Python scripts. Comments are important to make sure that you and others can understand what your code is about.

To add comments to your Python script, you can use the # tag. These comments are not run as Python code, so they will not influence your result. As an example, take the comment on the right, # Division; it is completely ignored during execution.

practice

Above the print(7 + 10), add the comment # Addition.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgRGl2aXNpb25cbnByaW50KDUgLyA4KVxucHJpbnQoNyArIDEwKSIsInNvbHV0aW9uIjoiIyBEaXZpc2lvblxucHJpbnQoNSAvIDgpXG4jIGFkZGl0aW9uXG5wcmludCg3ICsgMTApIn0=

Python as a calculator

Python is perfectly suited to do basic calculations. Apart from addition, subtraction, multiplication and division, there is also support for more advanced operations such as:

Exponentiation: . This operator raises the number to its left to the power of the number to its right. For example 42 will give 16. Modulo: %. This operator returns the remainder of the division of the number to the left by the number on its right. For example 18 % 7 equals 4. The code in the script on the right gives some examples.

Practice:

Suppose you have $100, which you can invest with a 10% return each year. After one year, it’s 100×1.1=110 dollars, and after two years it’s 100×1.1×1.1=121. Add code on the right to calculate how much money you end up with after 7 years.

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgQWRkaXRpb24sIHN1YnRyYWN0aW9uXG5wcmludCg1ICsgNSlcbnByaW50KDUgLSA1KVxuIyBNdWx0aXBsaWNhdGlvbiwgZGl2aXNpb24sIG1vZHVsbywgYW5kIGV4cG9uZW50aWF0aW9uXG5wcmludCgzICogNSlcbnByaW50KDEwIC8gMilcbnByaW50KDE4ICUgNylcbnByaW50KDQgKiogMilcbiMgSG93IG11Y2ggaXMgeW91ciAkMTAwIHdvcnRoIGFmdGVyIDcgeWVhcnM/Iiwic29sdXRpb24iOiIjIEFkZGl0aW9uLCBzdWJ0cmFjdGlvblxucHJpbnQoNSArIDUpXG5wcmludCg1IC0gNSlcbiMgTXVsdGlwbGljYXRpb24sIGRpdmlzaW9uLCBtb2R1bG8sIGFuZCBleHBvbmVudGlhdGlvblxucHJpbnQoMyAqIDUpXG5wcmludCgxMCAvIDIpXG5wcmludCgxOCAlIDcpXG5wcmludCg0ICoqIDIpXG4jIEhvdyBtdWNoIGlzIHlvdXIgJDEwMCB3b3J0aCBhZnRlciA3IHllYXJzP1xucHJpbnQoMTAwICogMS4xICoqIDcpIn0=

Variable Assignment

In Python, a variable allows you to refer to a value with a name. To create a variable use =, like this example:

x = 5 You can now use the name of this variable, x, instead of the actual value, 5.

Remember, = in Python means assignment, it doesn’t test equality!

practice:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgQ3JlYXRlIGEgdmFyaWFibGUgc2F2aW5nc1xuc2F2aW5ncyA9IDEwMFxuIyBQcmludCBvdXQgc2F2aW5ncyIsInNvbHV0aW9uIjoiIyBDcmVhdGUgYSB2YXJpYWJsZSBzYXZpbmdzXG5zYXZpbmdzID0gMTAwXG4jIFByaW50IG91dCBzYXZpbmdzXG5wcmludChzYXZpbmdzKSJ9

Calculations with variables

Remember how you calculated the money you ended up with after 7 years of investing $100? You did something like this:

100 * 1.1 ** 7 Instead of calculating with the actual values, you can use variables instead. The savings variable you’ve created in the previous exercise represents the $100 you started with. It’s up to you to create a new variable to represent 1.1 and then redo the calculations!

practice:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgQ3JlYXRlIGEgdmFyaWFibGUgc2F2aW5nc1xuc2F2aW5ncyA9IDEwMFxuIyBDcmVhdGUgYSB2YXJpYWJsZSBncm93dGhfbXVsdGlwbGllclxuIyBDYWxjdWxhdGUgcmVzdWx0XG4jIFByaW50IG91dCByZXN1bHQiLCJzb2x1dGlvbiI6IiMgQ3JlYXRlIGEgdmFyaWFibGUgc2F2aW5nc1xuc2F2aW5ncyA9IDEwMFxuIyBDcmVhdGUgYSB2YXJpYWJsZSBncm93dGhfbXVsdGlwbGllclxuZ3Jvd3RoX211bHRpcGxpZXIgPSAxLjFcbiMgQ2FsY3VsYXRlIHJlc3VsdFxucmVzdWx0ID0gc2F2aW5ncyAqIGdyb3d0aF9tdWx0aXBsaWVyICoqIDdcbiMgUHJpbnQgb3V0IHJlc3VsdFxucHJpbnQocmVzdWx0KSJ9

Other variable types

In the previous exercise, you worked with two Python data types:

int, or integer: a number without a fractional part. savings, with the value 100, is an example of an integer. float, or floating point: a number that has both an integer and fractional part, separated by a point. growth_multiplier, with the value 1.1, is an example of a float. Next to numerical data types, there are two other very common data types:

str, or string: a type to represent text. You can use single or double quotes to build a string. bool, or boolean: a type to represent logical values. Can only be True or False (the capitalization is important!).

practice:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgQ3JlYXRlIGEgdmFyaWFibGUgZGVzY1xuZGVzYyA9IFwiY29tcG91bmQgaW50ZXJlc3RcIlxuIyBDcmVhdGUgYSB2YXJpYWJsZSBwcm9maXRhYmxlIiwic29sdXRpb24iOiIjIENyZWF0ZSBhIHZhcmlhYmxlIGRlc2NcbmRlc2MgPSBcImNvbXBvdW5kIGludGVyZXN0XCJcbiMgQ3JlYXRlIGEgdmFyaWFibGUgcHJvZml0YWJsZVxucHJvZml0YWJsZSA9IFRydWUifQ==

Guess the type

To find out the type of a value or a variable that refers to that value, you can use the type() function. Suppose you’ve defined a variable a, but you forgot the type of this variable. To determine the type of a, simply execute:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInByZV9leGVyY2lzZV9jb2RlIjoiYSA9IDEiLCJzYW1wbGUiOiJ0eXBlKGEpIiwic29sdXRpb24iOiJ0eXBlKGEpXG50eXBlKGIpXG50eXBlKGMpIn0=

Operations with other type

When you sum two strings, for example, you’ll get different behavior than when you sum two integers or two booleans.

In the script some variables with different types have already been created. It’s up to you to use them.

practice:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6InNhdmluZ3MgPSAxMDBcbmdyb3d0aF9tdWx0aXBsaWVyID0gMS4xXG5kZXNjID0gXCJjb21wb3VuZCBpbnRlcmVzdFwiXG4jIEFzc2lnbiBwcm9kdWN0IG9mIGdyb3d0aF9tdWx0aXBsaWVyIGFuZCBzYXZpbmdzIHRvIHllYXIxXG4jIFByaW50IHRoZSB0eXBlIG9mIHllYXIxXG4jIEFzc2lnbiBzdW0gb2YgZGVzYyBhbmQgZGVzYyB0byBkb3VibGVkZXNjXG4jIFByaW50IG91dCBkb3VibGVkZXNjIiwic29sdXRpb24iOiJzYXZpbmdzID0gMTAwXG5ncm93dGhfbXVsdGlwbGllciA9IDEuMVxuZGVzYyA9IFwiY29tcG91bmQgaW50ZXJlc3RcIlxuIyBBc3NpZ24gcHJvZHVjdCBvZiBzYXZpbmdzIGFuZCBncm93dGhfbXVsdGlwbGllciB0byB5ZWFyMVxueWVhcjEgPSBzYXZpbmdzICogZ3Jvd3RoX211bHRpcGxpZXJcbiMgUHJpbnQgdGhlIHR5cGUgb2YgeWVhcjFcbnByaW50KHR5cGUoeWVhcjEpKVxuIyBBc3NpZ24gc3VtIG9mIGRlc2MgYW5kIGRlc2MgdG8gZG91YmxlZGVzY1xuZG91YmxlZGVzYyA9IGRlc2MgKyBkZXNjXG4jIFByaW50IG91dCBkb3VibGVkZXNjXG5wcmludChkb3VibGVkZXNjKSJ9

Notice how desc + desc causes “compound interest” and “compound interest” to be pasted together.

Type conversion

Using the + operator to paste together two strings can be very useful in building custom messages.

Suppose, for example, that you’ve calculated the return of your investment and want to summarize the results in a string. Assuming the floats savings and result are defined, you can try something like this:

print(“I started with $” + savings + " and now have $" + result + “. Awesome!”) This will not work, though, as you cannot simply sum strings and floats.

To fix the error, you’ll need to explicitly convert the types of your variables. More specifically, you’ll need str(), to convert a value into a string. str(savings), for example, will convert the float savings to a string.

Similar functions such as int(), float() and bool() will help you convert Python values into any type.

practice:

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IiMgRGVmaW5pdGlvbiBvZiBzYXZpbmdzIGFuZCByZXN1bHRcbnNhdmluZ3MgPSAxMDBcbnJlc3VsdCA9IDEwMCAqIDEuMTAgKiogN1xuIyBGaXggdGhlIHByaW50b3V0XG5wcmludChcIkkgc3RhcnRlZCB3aXRoICRcIiArIHNhdmluZ3MgKyBcIiBhbmQgbm93IGhhdmUgJFwiICsgcmVzdWx0ICsgXCIuIEF3ZXNvbWUhXCIpXG4jIERlZmluaXRpb24gb2YgcGlfc3RyaW5nXG5waV9zdHJpbmcgPSBcIjMuMTQxNTkyNlwiXG4jIENvbnZlcnQgcGlfc3RyaW5nIGludG8gZmxvYXQ6IHBpX2Zsb2F0Iiwic29sdXRpb24iOiIjIERlZmluaXRpb24gb2Ygc2F2aW5ncyBhbmQgcmVzdWx0XG5zYXZpbmdzID0gMTAwXG5yZXN1bHQgPSAxMDAgKiAxLjEwICoqIDdcbiMgRml4IHRoZSBwcmludG91dFxucHJpbnQoXCJJIHN0YXJ0ZWQgd2l0aCAkXCIgKyBzdHIoc2F2aW5ncykgKyBcIiBhbmQgbm93IGhhdmUgJFwiICsgc3RyKHJlc3VsdCkgKyBcIi4gQXdlc29tZSFcIilcbiMgRGVmaW5pdGlvbiBvZiBwaV9zdHJpbmdcbnBpX3N0cmluZyA9IFwiMy4xNDE1OTI2XCJcbiMgQ29udmVydCBwaV9zdHJpbmcgaW50byBmbG9hdDogcGlfZmxvYXRcbnBpX2Zsb2F0ID0gZmxvYXQocGlfc3RyaW5nKSJ9

Can Python handle everything?

Now that you know something more about combining different sources of information, have a look at the four Python expressions below. Which one of these will throw an error? You can always copy and paste this code in the IPython Shell to find out!

eyJsYW5ndWFnZSI6InB5dGhvbiIsInNhbXBsZSI6IlwiSSBjYW4gYWRkIGludGVnZXJzLCBsaWtlIFwiICsgc3RyKDUpICsgXCIgdG8gc3RyaW5ncy5cIlxuXCJJIHNhaWQgXCIgKyAoXCJIZXkgXCIgKiAyKSArIFwiSGV5IVwiXG5cIlRoZSBjb3JyZWN0IGFuc3dlciB0byB0aGlzIG11bHRpcGxlIGNob2ljZSBleGVyY2lzZSBpcyBhbnN3ZXIgbnVtYmVyIFwiICsgMlxuVHJ1ZSArIEZhbHNlIn0=