Skip to content

Introduction to Python

Run the hidden code cell below to import the data used in this course.


1 hidden cell

Take Notes

The Python Interface Hit Run Code to run your first Python code with Datacamp and see the output!

Notice the script.py window; this is where you can type Python code to solve exercises. 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 typing commands and hitting Enter. Here, your code will not be checked for correctness so it is a great way to experiment.

##Instructions

Experiment in the IPython Shell; type 5 / 8, for example. Add another line of code to script.py, print(7 + 10), to be checked for correctness. Hit Submit Answer to execute the Python script and receive feedback.

# See what happens when you hit Run Code
print(5 / 8)

print(7 + 10)

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

They start with # tag. See the comment in the editor, # Division; now it's your turn to add a comment!

# Division
print(5 / 8)
# Addition

print(7 + 10)

Python as a calculator Python is perfectly suited to do basic calculations. It can do addition, subtraction, multiplication and division. Print the sum of 4 + 5. Print the result of subtracting 5 from 5. Multiply 3 by 5. Divide 10 by 2.

# Addition, subtraction
print(5 + 5)
print(5 - 5)

# Multiplication, division, modulo, and exponentiation
print(3 * 5)
print(10 / 2)
print(18 % 7)
print(4 ** 2)

# Calculate two to the power of five
print(2**5)

Variable Assignment In Python, a variable allows you to refer to a value with a name. To create a variable x with a value of 5, you 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!

# Create a variable savings
savings=100

# Print out savings
print(savings)

Calculations with variables You've now created a savings variable, so let's start saving!

Instead of calculating with the actual values, you can use variables instead. The savings variable you created in the previous exercise with a value of 100 is available to you.

How much money would you have saved four months from now, if you saved $10 each month?

**Instructions

Create a variable monthly_savings, equal to 10 and num_months, equal to 4. Multiply monthly_savings by num_months and save it to new_savings. Add new_savings to savings, saving the sum as total_savings. Print the value of total_savings.**

#create a variable savings
savings=100

# Create a variable monthly_savings
monthly_savings = 10

# Create a variable num_months

num_months= 4      
# Calculate new_savings by multipying monthly_savings and num_months

new_savings= 10*4
# Print total_savings
total_savings= savings+new_savings
print (total_savings)

Other variable types In the previous exercise, you worked with the integer Python data type:

int, or integer: a number without a fractional part. savings, with the value 100, is an example of an integer. Next to numerical data types, there are three other very common data types:

float, or floating point: a number that has both an integer and fractional part, separated by a point. 1.1, is an example of a float. 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. It can only be True or False (the capitalization is important!).

Instructions

Create a new float, half, with the value 0.5. Create a new string, intro, with the value "Hello! How are you?". Create a new boolean, is_good, with the value True.

# Create a variable savings
savings = 100

# Create a variable growth_multiplier

growth_multiplier= 1.1      
# Calculate result

result= 100*1.1**7
# Print out result
print (result)

# Creating new variable like floatm string, boolean
half=0.5
intro="Hello! How are you?"
is_good= True