They’re basically fancy text documents that allow you to write code in Python (or other languages) and see the output right there on the same page. It’s kind of like having your own personal assistant who can run calculations for you without any hassle!
2. So, how do they work exactly? Well, when you open up a Jupyter Notebook in your favorite data science tool (like Anaconda or Spyder), it creates an environment where you can write code and see the results immediately. This is super helpful because it allows you to test out different ideas without having to run them through a separate program or script.
3. Here’s an example: let’s say you want to calculate the average of some numbers in Python. You could do this by writing a simple function like so:
# This function calculates the average of a list of numbers
def calculate_average(numbers):
# Initialize a variable to store the sum of the numbers
sum = 0
# Loop through each number in the list
for num in numbers:
# Add the current number to the sum
sum += num
# Calculate the average by dividing the sum by the length of the list
average = sum / len(numbers)
# Return the average
return average
4. But with Jupyter Notebooks, you can write the same code right there on the page and see the output immediately! Here’s what it would look like:
# Define a function to calculate the average of some numbers
def avg(numbers): # Function to calculate the average of a list of numbers
total = 0 # Initialize a variable to store the sum of the numbers
for num in numbers: # Loop through each number in the list
total += num # Add the current number to the total
return total / len(numbers) # Return the average by dividing the total by the number of numbers in the list
# Test out our new function with some sample data!
data = [1, 2, 3, 4, 5] # Create a list of numbers to test the function
result = avg(data) # Call the avg function and store the result in a variable
print("The average is:", result) # Print the result to the console
5. You’ve just created a Jupyter Notebook that can calculate the average of any set of numbers you throw at it. Pretty cool, right?
6. With Jupyter Notebooks, you can also add comments and explanations to your code, which is super helpful for keeping track of what you’re doing (and why). Here’s an example:
# Define a function to calculate the average of some numbers
def avg(numbers): # Function name should be descriptive and follow lowercase convention
total = 0 # Initialize our running total variable
for num in numbers: # Loop through each number in our list
total += num # Add this number to our running total
return total / len(numbers) # Calculate the average and return it! # Return statement should be indented to match the for loop
# Example list of numbers to test the function
numbers = [1, 2, 3, 4, 5]
# Call the function and print the result
print(avg(numbers)) # Function call should be followed by parentheses and the list of numbers should be passed as an argument
# Output: 3.0 # The average of the numbers in the list is 3.0
7. And that’s pretty much all there is to it! Jupyter Notebooks are a powerful tool for data scientists, but they can also be used by anyone who wants to write code in Python (or other languages). So why not give them a try and see what you can create?