Skip to main content
Ctrl+K
Lab in C&P (Fall 2026) - Home Lab in C&P (Fall 2026) - Home

Overview

  • Syllabus
  • Schedule for Section 2
  • Assignment Schedule

Resources

  • JupyterHub
  • GitHub
  • Video Lectures

Textbook

  • 1. What is Cognitive Science and how do we study it?
  • 2. Why do we have to learn statistics?
  • 3. Introduction to Jupyter by Bob
  • 4. Intro to Python for Psychology Undergrads
  • 5. A brief introduction to research design
  • 6. The Format and Structure of Digital Data
  • 7. Visualizing Data
  • 8. Describing Data
  • 9. Samples, populations and sampling
  • 10. Hypothesis testing
  • 11. Comparing one or two means
  • 12. Measuring Behavior
  • 13. Correlation
  • 14. Research Ethics
  • 15. The Replication Crisis in Psychology
  • 16. Linear regression
  • 17. Logistic regression
  • 18. Linear Mixed Effect Modeling
  • 19. Mental Imagery, Mental Simulation, and Mental Rotation
  • 20. Magnetic Resonance Imaging
  • 21. What Next?

In-class Assignments

  • Python ICA (Ch 4)
  • Design ICA (Ch 5)
  • Data ICA (Ch 6)
  • Visualization ICA (Ch 7)
  • Sampling ICA (Ch 9)
  • Hypotheses ICA (Ch 10)
  • t-test ICA (Ch 11)

Homeworks

  • Homework 1 - Welcome to the class tools
  • Homework 2 - Python for data analysis
  • Homework 3 - Exploring Data

Tips & Tricks

  • Python Resources
  • print vs. return
  • Plotting in Python Resources
  • Intro to For loops
  • NYU JupyterHub Setup
  • Ultimate t-test guide

About

  • License
  • JupyterHub logo JupyterHub
  • .ipynb

print vs. return

Contents

  • 1. results can look the same in jupyter
  • 2. trying to save the data
  • 3. using the result
  • 4. best practices

print vs. return#

These two look the same in Jupyter, but they do very different things:

  • print shows a value on the screen. The value is not saved anywhere.

  • return hands a value back to whatever called the function. This allows the possibility of it being stored and used later.

Here are two versions of the hello() function, one that uses print and one that uses return. Let’s look at the differences in how they behave.

def hello_print(name):
    print("Hello, " + name + "!")

def hello_return(name):
    return "Hello, " + name + "!"

1. results can look the same in jupyter#

hello_print("Zoe")
Hello, Zoe!
hello_return("Zoe")
'Hello, Zoe!'

2. trying to save the data#

a = hello_print("Zoe")
b = hello_return("Zoe")
Hello, Zoe!

Notice that hello_print still printed, even though we just wanted to store the result. Now let’s look at what actually got saved:

print("a is:", a) # hello_print()
print("b is:", b) # hello_return()
a is: None
b is: Hello, Zoe!

a is None, which is Python’s way of saying “nothing”. hello_print showed the greeting, but it never handed anything back, so there was nothing to store.

3. using the result#

What if we wanted to perform some change to the result? If you use print, it’s not possible, since the info is not stored in the environment.

b.upper() # works fine because b has a string stored inside
'HELLO, ZOE!'
a.upper() # throws an error because a is None, the string was not stored inside
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 1
----> 1 a.upper() # throws an error because a is None, the string was not stored inside

AttributeError: 'NoneType' object has no attribute 'upper'

4. best practices#

Generally, we want to store the results of computations in variables so we can do further manipulations with them, so err on the side of return over print most times. If a function’s description (“docstring”) says “Returns a value that xyz”, definitely use the return keyword.

Print is mainly helpful for if you want to see the output of the result yourself just to check it, and you don’t need to perform any further changes. Still, it’s nice to store it in a variable and then call print on that variable (e.g., print(b)).

In the case of Homework 1 Problem 1, there was a wording that said “verify that when you run your new code cell it prints out xyz” but the docstring asked for you to return the value. Apologies for that (I can understand why many people used print instead because of this misleading wording) and we will change the wording for next time!

previous

Python Resources

next

Plotting in Python Resources

Contents
  • 1. results can look the same in jupyter
  • 2. trying to save the data
  • 3. using the result
  • 4. best practices

By Todd M. Gureckis, Andy Hilford, Maya Malaviya

© Copyright 2023.