Data Science and AI in Psychology (2024)

Data Science and AI in Psychology (1)

Image by rawpixel.com | CC BY

Now that you have read through most of the chapters in this textbook and have learned more about data science and AI in psychology, let's have a look at some Python coding. As we learned in Chapter 1, data science involves the application of specific tools and techniques designed to manage and analyze large datasets. These tools include coding languages like R and Python. These languages are open source, which means that they are available to the public for free. Other types of statistical software that are commonly used in psychology, like SPSS, is proprietary. If you don't have access to an institutional license through a university or another organization, an annual subscription can cost over $1000. These programs have a user-friendly interface but are highly limited. SPSS can only handle limited datasets and does not allow for analysis of text data. In contrast, Python and R can handle very large datasets, and Python allows you to work with either numerical or text data. While R is more likely to be used for data analysis by psychology researchers, Python is more commonly used outside of academia. In addition, Python can support deep learning algorithms, while R cannot. For these reasons, we will focus on Python in this chapter. It is a highly flexible coding language and skills that you learn in Python can be easily transferred to non-academic contexts.

Have Fun With It

The purpose of this chapter is to 1) have some fun with coding, 2) determine whether coding is something that interests you, and 3) have a look at a tool that drives machine learning and AI. You do not need to become a professional coder to be a psychologist. Coding does help with data analysis, but it is not necessary for many careers within the field of psychology. Understanding research methods and developing data literacy are necessary skills because these skills are the basis for what we know about psychological functioning and these skills will help you to think critically about whether and when to apply particular research findings. Coding is just one tool that allows researchers to analyze data and it is not necessary for all psychologists to master all tools used within the field. Whether coding is relevant to you and the work you do within the field of psychology depends on two things:

  1. Whether you enjoy coding
  2. Whether your tasks and responsibilities within your job or career could benefit from coding

At the end of this chapter, you may discover that you do not like coding. If that is the case, then you have learned something valuable about your own interests. However, you may discover that you love coding! Many psychology students are surprised to learn that they really enjoy coding and that they want to continue learning how to code. Keep an open mind and try to have fun with this chapter. We will only be reviewing introductory level coding skills. At a minimum, this chapter will give you a peek into the kind of tools that are used in machine learning and AI. If you do find that you enjoy coding, there is a list of resources at the end of this chapter that you can access to continue developing your coding skills. While these skills are not necessary for several psychology careers, they are highly valued within the field of psychology and will look good on graduate school applications and resumes. They are also applicable to a broad range of industry jobs.

Python is a Coding Language

What is Python, exactly?

Data Science and AI in Psychology (2)

Image by Rijksmuseum | CC BY

You might be wondering what large snakes have to do with coding and AI. Python is a type of computer programming code that was developed in the late 1980s by Guido van Rossum (click here for more information). It is called "Python" because van Rossum wanted a name that was short and unique, and he was a huge fan of a BBC television comedy sketch series called "Monty Python."

Python is "...an interpreted, object-oriented, high-level programming language with dynamic semantics." An important insight to extract from this definition is that Python is a coding language. Take a minute to think about language. What is the purpose of language? How is language structured?

It might be easier to think about these questions if you have tried to learn a new language. If you have tried to learn a new language, was it easy or hard? What made it hard? What helped to make it easier?

One aspect of language that is tricky to learn is its grammar, which is the set of rules that govern how words are combined into phrases or sentences. Each language has its own set of grammatical rules and learning these rules can be challenging because they are often quite abstract. For instance, one language might require a verb (V) to appear after a subject (S) and an object (O). This is known as an SOV language. An example of an SOV language is the Navajo language. Another language might require the verb to appear before the object. This is known as an SVO language, and an example is the English language. In an SVO language, we would say: "the cat drank the milk." However, in an SOV language, we would say "the cat the milk drank." Why do languages differ in this way? Different languages just evolved in different ways over time, and as a new language learner, a new grammatical structure may appear to be arbitrary and therefore difficult to grasp.

What does grammar have to do with coding? Each coding language also has its own grammar or rules that govern how coding elements should be combined into coding syntax. At first, these rules can seem very abstract and difficult to learn. Learning new languages can be challenging, and learning to code in Python is like learning a new language.

What helps to make language learning easier? Practice, practice, practice. If you have ever tried to learn a new language, you may notice that everything starts to "click" after you've been practicing for a while. The same is true for coding. With enough practice and immersion in the coding language, it all starts to click and soon coding begins to feel like you're speaking a familiar language. This is true for any new skill, like playing an instrument or a video game - if we practice enough, it all starts to click.

We will now turn to the Python Coding Tutorial. Throughout the remainder of this chapter, try to remain open-minded. Again - the purpose of this chapter is simply to introduce Python coding so that you can see what it looks like and determine whether it is something you enjoy. Python coding is similar to solving a puzzle. How do I input the right puzzle pieces to obtain the output I want?

Toward the end of this tutorial, we will learn how to use ChatGPT to assist with coding. ChatGPT has taken the coding world by storm and many coders rely heavily on ChatGPT. In fact, one new type of career that has emerged out of the AI revolution is the "prompt engineer." Prompt engineers are highly skillful at creating the right prompts to obtain the output they need from ChatGPT. We will learn some prompts to assist us with coding.

To complete this tutorial, we will be using a free, online platform known as Google Colab. Before proceeding to the coding instructions below, we will need to get you started with Google Colab. A Google Colab Notebook is similar to a Google doc. It is hosted on Google's platform and stored in your Google Drive. You can share the notebook with others and work together with others on the same notebook.

Getting Started with Google Colab

The easiest way to get started with Google Colab is to watch the 13-minute YouTube video below which provides a Google Colab Tutorial for Beginners. This is an excellent video that demonstrates how to access and utilize Google Colab.

The Google Colab tutorial in the video is very comprehensive and should get you started. To proceed with the rest of the coding tutorial in this chapter, you should have a Google Colab notebook open and you should know how to add new cells (all of which is demonstrated in the video).

Note. If the video did not help you to get started in Google Colab, you can also follow the steps outlined in this article. Here is a link to the Google Colab platform: https://colab.research.google.com/

Choose your Power and your Mode

There is one very important step that the YouTube video missed. We will review this step right now. Once you have a opened a Google Colab Notebook, click the Settings wheel in the upper right corner, click on the Miscellaneous option, and then choose your power and your mode. If you're not sure which power or mode you prefer, play around with the options a bit. Choose different options and see what happens...

Python Coding: Basic Concepts

Now that you have your Google Colab Notebook set up, we are ready to start learning how to code!

The details about Python Coding that are provided in this chapter are sufficient for a very basic introduction. For a more comprehensive review of Python Coding, readers can check out this book entitled "Python Crash Course" written by Eric Matthes.

In this tutorial, we will review coding basics before we learn how to use ChatGPT to assist us. We won't be able to know which questions to ask ChatGPT until we have a fundamental understanding of Python coding and how it works.

As mentioned earlier in this chapter, Python is a coding language that has its own grammar. We need to understand the grammatical rules of this language to be able to code.

Python Coding Grammar

Python has surpassed other coding languages in popularity in large part because it is much more naturalistic compared to other languages. There are 19 principles that have guided the development of the Python programming language and many of these principles emphasize the importance of simple, straightforward, and naturalistic coding. These principles are known as the Zen of Python. To illustrate, check out a subset of these principles, listed below:

  • Beautiful is better than ugly.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Sparse if better than dense.
  • Readability counts.
  • There should be one - and preferably only one - obvious way to do it.
  • If the implementation is hard to explain, it's a bad idea.
  • If the implementation is easy to explain, it's probably a good idea.

In the service of these principles, we will keep the description and explanation of Python grammar as simple and as straightforward as possible.

To understand the grammar of Python, we need to learn 3 basic Python coding elements: Objects, Variables, and Functions.

Objects. Objects are the content of the code. They are the elements that we are creating, changing, and manipulating with our code. Objects can include numbers or text or functions (functions are explained in more detail below). When we analyze data with Python code, the dataset becomes an object.

Variables. We can think of variables as the vehicles by which we are able to change or manipulate objects. Variables are grammatical elements that are similar to nouns in spoken language. For instance, the word 'cat' is a noun that refers to an actual cat. In this case, the noun 'cat' is really a linguistic symbol that represents the actual object, which is a cat. The noun "cat" is not the actual cat, but we need the linguistic symbol to be able to communicate anything about actual cats. In Python coding language, a "variable" represents or contains the actual object, which may be a number. To create a "variable," we must have a variable name. The variable name (also known as an identifier) can be anything we want it to be, but the variable must have a name in order to be created and stored in the server's memory.

To illustrate, imagine that our object is the number 7. If we want to change or manipulate that object, we have to create a variable. By creating a variable, we are able to store the object in the server's memory so that we can subsequently change or manipulate it in some way. To create a variable, we have to name it. What name should we call our object? The name can be anything we like, but the more straightforward and obvious the variable name, the easier it is to read our code. Let's call it number_7. To create a variable for the object 7, we would simply assign the object to our variable name like this:

number_7 = 7

One way to understand this is to think of the number 7 as the actual object and to think of the variable as a container that holds the 7 object. We can't store that container in memory until we give it a name. In this case, we gave it the name number_7. By writing the code number_7 = 7, we are instructing Python that our variable number_7 contains the actual object 7.

Now we can manipulate our variable in a number of different ways. For instance, we can add three to our variable with the following code:

number_7 + 3

Can you guess what the output would be in this case? It would be 10!

You might be wondering, why don't we just write '7 + 3' to get the output 10. Why do we have to assign an object to a variable name to get the output 10? Actually, we don't. As we will see below, we could write 7 + 3 in our Python code and the output would be 10. However, objects often consist of more than a single number. As mentioned above, an entire dataset can be an object. To work with a dataset, we would not want to type all of the data every time. We can work with a dataset and with more complex objects by assigning them to variables and using their variable names in our code. Check out the image below for another illustration of objects and variables.

Data Science and AI in Psychology (3)

Image by i-Sapna Team | CC BY

Functions. Functions are specific actions that we apply to objects to change or modify them, or to create a particular outcome. There are numerous actions that we can apply to objects. If the object is text, we can search the text for particular words, we can replace words or phrases within the text, we can change the case or the punctuation of the text, and more. If the object is a list of numbers, we can search for particular numbers, we can replace numbers, we can apply computations to all or some of the numbers in the list, we can conduct statistical analyses (if we have enough data), and we can input the numbers into a data visualization. These are just a few examples of the many actions we can apply to objects. There are many default functions that are already included in the base Python code. There are also several packages of functions that we can import into Python. For instance, statistical analyses and machine learning functions belong to packages that we import into Python before analyzing our data. Even the code that we use to import those packages is a type of function. We can also create our own new functions and we can save those functions in the server's memory so that we can apply them throughout our code. How would we save a function in the server's memory? Just as we described in the previous paragraph -- to save something in the server's memory, we need to give it a variable name (also known as an identifier). We can give our functions any name we want, aside from previously existing function names (if we assign a new function to a previously existing function name, we will overwrite the previous function, which essentially deletes it from memory). If we are assigning functions to identifiers, you may be wondering - are functions a type of object? Yes! Functions are types of objects that we can store in memory by assigning them to identifiers.

The arrangement of these elements into coding scripts is known as the coding syntax. In spoken language, syntax is the rules for how we arrange words and phrases to create sentences. The Python coding language does have its own syntax. We already learned some syntax in the "Objects" section above. We can assign objects to variables using the "equal" sign, with the object on the right side of the equal sign and the variable name on the left side of the equal sign, like this:

income = 5000

What is the syntax for using functions to change or manipulate objects? Many functions have round brackets at the end of them. For instance, the 'print' function looks like this in Python coding language:

print( )

The 'print' function tells Python to print something in the output. Imagine that we inputted the code:

print(income)

We are passing the variable into the function by inserting it into the round brackets. In this way, we are telling Python to apply the function 'print' to the variable 'income.' What do you think the output would be? We are basically telling Python to print the variable 'income'. What is the variable 'income'? It is 5000. The object assigned to the variable 'income' is 5000, which means that income is 5000. When we pass 'income' into the 'print' function, Python will print the object, which is 5000.

These are two examples of Python syntax. Of course, there are many syntactic rules in any language, including Python coding, but if you know the syntax for creating variables and passing variables into functions, you already have a head start!

Server Memory

One last concept to review before we jump into actual code: Server Memory. While you were reading the previous section of this chapter on Python Coding Grammar, you probably noticed that there were several references to storing variables and functions in the 'server memory.' We are referring to server memory rather than computer or laptop memory because we are learning to code in Google Colab which relies on the Google Cloud for storage. When we save our coding notebooks, we are saving them in the Cloud. One significant advantage of using Google Colab is that we also have access to Google's remote computational units which allows us to complete demanding processing tasks (e.g., deep learning) that would either be much slower or impossible on our desktop and laptop computers.

One thing to note, however, is that Google Colab only saves your variables and procedures for the length of the current coding session. If you close your computer or disconnect from the internet, or if your Google Colab session times out, you will lose everything that was stored in memory. No worries! This happens all the time. The solution? You just have to "rerun" your coding cells when you resume coding in a new session. The code is still saved in the notebook and once you rerun the coding cells, the variables are once again stored in the server's memory and are available for use. What does it mean to run a coding cell? Let's find out by diving into some actual coding in the next section.

Diving into Python Coding

We will now review some basics in Python coding, followed by suggestions for using ChatGPT to asist with coding. For each of the coding example below, type them into your Google Colab notebook and check the output for yourself. As you proceed through this introductory lesson, try to create examples of your own so that you can get some more practice along the way.

Let's start with the most basic code: Literals

Literals

Literals are the actual objects that we are working with when we are coding in Python and analyzing data. We ultimately assign most of these objects to variables. If you just type the literal or the object into a coding cell and "run" the cell (by clicking on the 'play' button at the left of the cell in Google Colab), the output will be the exact thing you typed. In the example below, if you type '5', the output will be 5.

5

5

Integer Operators

Integer operators allow you to do computation with integers. Check out some integer operators below.

Addition

5+3

8

Multiplication

5*3

15

Exponents

5**3

125

Division

2.5

Floor Division - division that returns only the largest integer that is less than or equal to the result of the division

5//2

2

Modulus - division that returns only the remainder

5%2

1

Order of Operations

The order of operations in Python is as follows:

  • Operations in parentheses are resolved first
  • Exponents are resolved second
  • Multiplication and division are resolved third
  • Addition and Subtraction are resolved fourth

(4 + 3) + 2 * 3

13

Commenting to Add Notes

You can add comments to any code with the use of hashtags. Comments are notes to yourself or to other coders that provide an explanation or reminder for what the code is doing. You create a comment by adding a hashtag in front of the note that you wish to write. With the use of the hashtag, the text will not be processed as regular code, which means that it will not be interpreted as an object, a variable, or a function. Comments are very useful. As you develop your Google Colab notebook for this Chapter, you should add comments and text to help you remember what you've learned. It can seem tedious but it is very beneficial when you come back to your code at a later point and you can't quite remember what it all means. In Google Colab, comments are always presented in green font.

Here is an example of a comment:

#all of this is a comment

Here is an example for how you can add comments to your code:

5//2 #floor division, returns the largest integer less than or equal to the result of standard division

Assigning Objects to Variables

As explained earlier in this chapter, we must assign objects to variables to store them in memory. We assign objects to variables using the 'equal' sign. The objects appear on the right of the equal sign, and the variable name appears on the left of the equal sign.

Variable names can be anything, but they must conform to the following rules:

  • Variable names cannot have spaces in them. Instead of spaces, you can use an underscore (e.g., my_value).
  • Variable names should be lower case. The code will still work if you have upper case letters in your variable name, but it is convention in Python coding to use lower case for variable names. The code is easier to read if we all use the same convention.
  • Variable names should never be function names, such as print or list or type. There are many functions in Python. If you accidentally use a function name as a variable name, you will overwrite the original function. Is this the end of the world? No - but it is a hassle. You may not realize that you've overwritten a function until you try to use that function and you get an error message. It may take a little while for you to figure out why you are getting this error. Once you figure it out, you can reset all of the functions using the 'reset' code, but the reset code will restart your session and you'll have to rerun all of your cells. This is not devastating, but it is inconvenient.

Here is an example of assigning an object to a variable name. Here, we ae choosing the variable name x. When you assign an object to a variable name, there is no output. This does not mean that there is no action. An important action has taken place. You have created a variable that is now stored in memory. Nevertheless, there is no output when you create a variable.

x = 5

To obtain an output for your variable, you have to call it or manipulate it in some way. You can call a variable simply by typing the variable name. The output will be the object that is assigned to that variable.

x

5

We can now use the variable in a number of different ways. We can include it in a computation, as in the example below.

x + 12

17

What if we wanted to save the output of this computation as a new variable? Remember, all we have to do to create variables is assign the objects to a variable name. In the code below, we are assigning the previous computation to the variable name new_variable.

new_variable = x + 12

Again, there is no output here, because there is no output when we assign objects to variable names. What if we want to see the actual content of the variable new_variable? We just have to call it, like this:

new_variable

17

As you can see, the content of the variable called new_variable is the result of the computation x + 12, which is 17.

There are many types of objects. In the examples above, we have been working with integers. Objects can also be text, lists, or entire datasets.

Assigning Text to Variables

To assign text to a variable name, the text (which is the object) must be in single or double quotations. Notice that as soon as you use single or double quotations, the text is presented in red font in Google Colab. When the code is in red colored font, then you know it is a text object. Text objects can be any character or set of characters, including words, phrases, paragraphs, documents. If you try to assign an integer to a variable name but you enclose the integer in quotation marks, the integer will be stored as text.

It is important to note that Python refers to text objects as strings, so text is considered a string variable.

As was the case with integers, when we assign a string to a variable name, there is no output. You have completed the action of creating a variable, but there is no output associated with that action. If we want to check the content of the variable, we have to call the variable. We can actually do this within the same cell using multiple lines of code.

In the example below, notice that the variable name is not in quotations, but the string object assigned to the variable is in quotations. Variable names are never in quotations, and string objects are always in quotations. This is part of the syntax (i.e., grammatical rules) of Python.

usher = 'yeah'

usher

yeah

Saving a Modified Variable

In the last section of this chapter (Assigning Objects to Variables), we saw that we could incorporate a previously existing variable in an object assigned to a new variable (which we called new_variable). We can also change or modify existing variables. How would we do this? We just have to reassign them to the original variable name. See the example below.

Recall that we have already created a variable, which we called x. When we call that variable, the output is the object assigned to that variable.

x

5

We can now incorporate that variable into other computations and procedures.

x + 2

7

Importantly, even though we are adding 2 to the x variable, we are not changing or modifying the x variable. When we call the variable x, we see that it has remain unchanged.

x

5

To permanently modify that variable, we need to reassign the modified object to the original variable name, in this case, x.

x = x + 2

When we reassign the modified object to the original variable name, there is no output, because we are simply making a change in the server's memory. To see the content of the modified variable x, we need to call it.

x

7

Alternatively, we could save the modified object as a new variable, as we did when we created the new_variable above. This is often desirable so that we don't lose the original data. However, whether we create a new variable to save our modifications or save it under the original variable name really depends on our purpose or goals. When we're working with data analysis, there are times when we might prefer one approach over the other.

Functions

As we learned earlier in this chapter, functions allow us to apply a number of different types of actions to variables. There are many different functions available to us as part of Python's base code. In addition, we can import packages of functions needed to perform a number of different tasks, including machine learning, and we can create our own functions. In this section, we will review a few of the default functions. As an introduction to coding, this chapter will help us understand how functions work but it will not provide not a comprehensive review of all of the relevant functions in Python coding. Once you understand the logic of functions, you can begin to understand how coding in machine learning and AI works. For instance, statistical analyses and machine learning models are just functions that tell the machine what to do with the data. Let's begin with a very basic function - the print function.

Print as a Function

One of the most commonly used functions in Python is the print function. As we explained earlier in the chapter, the syntax for many functions includes the function name followed by round brackets. We can input variables or arguments into those round brackets which tells Python how to apply the action.

In the example below, we are instructing Python to print the variable x. Have a look at the output. What do you notice?

print(x)

5

You may notice that the output is identical to the output we see when we simply call a variable. What is the value of the print function?

There are a few cases in which the print function is very useful. In some coding platforms, you can't produce an output without using the print function. In Google Colab, we can simply call the variable to see what is stored in that variable. The print function is still useful in Google Colan because it allows us to print multiple lines of code within the same cell. In Google Colab, if we have multiple lines of code within one cell, we will only see the output for the last line of code -- unless we use the print function. Check out these examples:

a = "Hello, World!"

a

x

5

Notice that we see the output for the x variable but not for a variable. What if we want to see the output for both a and x?

print(a)

print(x)

Hello, World!5

Using the print function for multiple lines of code within the same cell allows us to see the output for all of those lines of code. The print function is also useful when incorporated into other types of code, such as For Loops or functions. We'll see some examples of this usage later in this tutorial.

Type as a Function

Another function that is very useful is the type function. This function tells you what type of object is stored in a variable. Object types include the following:

  • integers, which are whole numbers (e.g., 7)
  • floats, which are numbers with decimals (e.g., 7.0 or 7.5)
  • strings, which are strings of letters or characters (e.g., "sara")
  • lists, which can include a list of any set of objects. Importantly, lists can be modified.
  • tuples, which are like lists in that a tuple can consist of any set of objects, but it cannot be modified

Let's combine our print and type functions by requesting to see the output for the type of object in multiple lines of code. In the example below, notice that each function has it's own set of round brackets. In the first line of code, we are passing the number 7 into the type function by inputting it into the round brackets, like this: type(7). To print the output of that code, we are passing the entire type code into the round brackets of the print function, like this: print(type(7)). There are a lot of brackets in Python coding. It can get a little confusing at first, but after a while you get used to tracking the brackets. It's all part of the Python syntax and it becomes more familiar with practice, just like learning a new language.

print(type(7))

print(type(7.5))

print(type(x))

print(type("Hello, World!"))

print(type(a))

<class 'int'><class 'float'><class 'int'><class 'str'><class 'str'>

In the output, we can see the type of class for each object or variable. int is short for integer, float obviously means float, and str is short for string.

We can also use the print function to make our output more readable. In the round brackets of the print function, we can pass multiple arguments - not just one. For instance, if we write the code print(a,x), Python will print the objects assigned to both of those variables in one line. If we want to make our text more readable, we can add a literal string object with a description of the output into the round brackets. Remember that string objects must be enclosed in either single or double quotation marks. We could input the code: print("The objects assigned to the variables a and x are: ", a, x). Let's run some of these examples and see what we get.

print(a,x)

print("The objects assigned to the variables a and x are: ", a,x)

print("7 is type: ", type(7))

print("7.5 is type: ", type(7.5))

print("Variable x is type: ", type(x))

print("Hello, World is type: ", type("Hello, World!"))

print("Variable a is type: ", type(a))

Hello, World! 5The objects assigned to the variables a and x are: Hello, World! 57 is type: <class 'int'>7 is type: <class 'int'>7 is type: <class 'float'>Variable x is type: <class 'int'>Hello, World is type: <class 'str'>Variable a is type: <class 'str'>

int, float, and str as Functions

Earlier we learned that int, float, and str are object types. These object types also serve as functions that can convert objects into different types. For instance, we can convert a float into an integer or a string, or we an convert an integer into a string or a float. When we convert a float into an integer, Python will round the value. If a string variable is a number, we can also convert that string variable into an integer. The syntax for these functions is similar to the other functions we learned. The function name is followed by round brackets, and we can pass an object (also called an argument) into the round brackets. Let's try some examples below.

float(x)

5.0

str(x)

'5'

y = 4.3

z = '7'

print(int(y))

print(int(z))

47

Notice that we did not permanently change these variables. We simply applied a temporary action to them. We converted them into a different type, but only temporarily. What would we need to do to change them permanently? We learned this earlier in the chapter. How do we permanently modify variables? We have to reassign them to their original variable name.

type(z)

str

z = int(z)

type(z)

int

Round as a Function

Another example of a function is the 'round' function, which you may have guessed - rounds a variable.

round(7.5)

8

List as Another Type

As mentioned above, a list is another type of object. A list is a set of objects and we define a list by placing the set of objects inside brackets. So far in this tutorial, we have been using round brackets. Round brackets are appropriate for functions and are part of the syntax when working with functions. Importantly, when creating a list, we do not use round brackets. We use square brackets. This can be difficult to remember at first and the confusion often causes errors but again, with practice, it gets easier to track the brackets. See an example of code used to create a list of numbers below.

#Lists

my_numbers = [1, 2, 5, 4, 6]

print(my_numbers)

print(type(my_numbers))

print(len(my_numbers))

[1, 2, 5, 4, 6]<class 'list'>5

Notice in the last line of code that we added a new function we haven't learned yet. The new function is len. Can you guess what the function means, based on the output? len is shorthand for length, and that function will tell you the length of the list (i.e., the number of objects in the list). This can be helpful when we're working with large sets of data and we can't just eyeball the length or the number of values in the data.

List as a Function

Earlier we learned that we are able to use int, float, and str (all object types) as functions. We are also able to use list (which is also an object type) as a function. For instance, we can convert a string variable into a list, as in the example below.

my_name = "sara"

list(my_name)

['s', 'a', 'r', 'a']

Getting Help with Functions

Recall that in the "lists" example above, we added a line of code with a new function (i.e., len) that we had not yet reviewed in this tutorial. If you see a function that is unfamiliar to you, you can query Python about the function using a question mark followed by the function name. Check out the response provided by Python when we submit a query about len.

?len

Signature: len(obj, /) Docstring: Return the number of items in a container. Type: builtin_function_or_method

For Loops

Another important feature that is frequently used in Python coding is a For Loop. A For Loop is a type of code with a particular syntax that instructions Python to loop through a set of objects and execute some action for each object in the set. It's like saying to Python: For each element in this set, execute this action.

Let's practice using a For Loop with the my_numbers variable we created earlier. Recall that my_numbers was used to create a list variable. Let's have a look at the content of that variable by calling it in our code:

my_numbers

[1, 2, 5, 4, 6]

Now that we know what my_numbers is, we can execute a For Loop. Let's instruct Python to iterate or loop through the my_numbers list, and for each number in the list, add two to the number. We want to be able to see the output for each number, so we will include the print function in our For Loop. With the print function, we are instructing Python to print the output for each number as it loops through the list.

One thing that is critical to note about For Loops is that there is a very specific syntax that is required in order for the code to run without errors. This syntax is described in the bullet points below:

  • The 'for' term is lower case
  • The i stands for 'index' and really just means 'each element.' You can actually use any letter or word in that spot. For instance, you could type: 'for x in my_numbers' or 'for number in my_numbers'. The 'i' is just a convention that is used quite commonly.
  • At the end of the first line of code, there must be a colon.
  • The second line of code must be indented the equivalent of one tab. In Google Colab, as soon as you add the colon at the end of the first line, Colab automatically indents the next line.

Here's an example:

for i in my_numbers: print(i + 2)
34768

Now that you have seen an example of a For Loop, try to create another For Loop in your notebook using a different operator (e.g., multiplication or subtraction).

One thing to notice about the For Loop above is that we have not permanently modified the list. We can check by calling the original list:

my_numbers

[1, 2, 5, 4, 6]

If we want to permanently modify the list, we can create a new variable with an empty list, and then add the modified numbers to the new list using the function append:

new_numbers = []for i in my_numbers: new_numbers.append(i + 2)

new_numbers

[3, 4, 7, 6, 8]

The function append is another default function that is included in the basic Python programming software. There are many default functions - too many to review in this chapter! If you decide you'd like to continue learning how to code in Python, you will become more familiar with many of these functions.

if-else Statements

Just like For Loops, if-else statements are an important and commonly used feature in Python. These statements are also known as conditionals. When using conditionals, we are essentially instructing Python to perform an action on a set of elements or objects, but only if the element or object satisfies some condition. In other words, we are saying: if the object satisfies a certain condition, perform this action, or else, do something different.

An if-else statement also has a very specific syntax that is summarized in the bullet points below:

  • The 'for' term is lower case
  • As was the case for For Loops, the i stands for 'index' and is really just means 'each element.'
  • At the end of the first line of code (i.e., the 'forf statement), there must be a colon.
  • At the end of each line of code containing the 'if' or 'else' statement, there must be a colon.
  • Each line following a colon must be indented compared to the previous line. In Google Colab, as soon as you add the colon at the end of the first line, Colab automatically indents the next line.

Let's create an if-else statement using our new_numbers list. Recall that the new_numbers variable contains the following list: [3, 4, 7, 6, 8]. Using an if-else statement, we will give Python the following instructions:

For each number in the new_numbers list, if you divide the number by 2 and get a remainder of 0, print even, or else print odd.

for i in new_numbers: if i%2 == 0: print("even") else: print("odd")
oddevenoddeveneven

This is a great example of an if-else statement. These statements can be a little tricky to wrap your head around. Conditionals are used in a number of different ways in Python code and are really helpful when working with a large dataset. Here in this chapter, we are just introducing the basic concept. As long as you understand the basic concept of if-else statements, you are on a roll!

While Loops

Another feature in Python coding that is very similar to a For Loop and an if-else statement is a While Loop. A While Loop combines the logic of the For Loop and the if-else statement. A While Loop instructs Python to loop through a set of elements and perform some action, but only while some condition is true. Once the condition is false, Python must discontinue looping through the set. We will see an example of a While Loop in our Magic 8-Ball function below.

Creating New Functions

Earlier in the chapter we learned that coders can create brand new functions that do not already exist in Python's default functions or in another package that can be imported. In our last demonstration, we will see some examples of new functions. We will also see an example in which we import a package.

The Cookie Function

In this example we will create a new function that we will call the Cookie Function. The purpose of this function will be to produce a statement that reflects our favorite type of cookie.

Please note that you will not become an expert function developer based on this one example, and this is ok! Just like learning how to play a new instrument, we often need to practice a lot before we really start getting it. The purpose of this section is just to give you a conceptual understanding of how functions work. Many of the functions that we use in coding and data analysis already exist (like the print function), and we can just apply them to our data.

The code used to create a new function is def. In the example below, we can see that we begin our code with that term, which essentially stands for the word 'define'. In other words, we are defining our function. Following the term def, we are going to name our function. We can name it anything we like (as long as we follow the variable naming rules that we reviewed earlier in this chapter), and we will name our function cookie.

Recall that each function is typically followed by round brackets (not always, but often). When we define our function, we are going to add round brackets and then we're going to give Python instructions about what should be inputted into those brackets. If it is open, we can leave it blank. If we want to add some sort of default, we can add it here.

In our example, we will add a default. In our default, we are instructing Python to use the word "plain" if there is no specific word inputted into the round brackets.

The def line of code must always end with a colon, which also means that the next line of code must be indented. Google Colab will add the indent automatically after a colon.

In the last line of code for our example, we are giving Python instructions about what the function should do. In our case, we want Python to print a statement that says: My favorite type of cookie is a ______ cookie.

What goes in the blank? Whatever we pass into the round brackets. If we do not pass anything into the round brackets, Python will use the word "plain", which we've instructed it to do as the default.

There! We created a function! Check out the example below. Type out the code into your notebook to see how it runs.

def cookie(main_ingredient = "plain"): print(f"My favorite type of cookie is a {main_ingredient} cookie.")

We have now created the cookie function. There is no output when we create this function. It is similar to creating a new variable. When we create a variable or function, it is now saved in memory, but we cannot view it unless we call it.

Let's call this function. We'll start with the basic code in which we do not pass anything into the round brackets:

cookie()

My favorite type of cookie is a plain cookie.

Notice that the output is the default output that we programmed into our function.

Now let's add a favorite cookie ingredient to the round brackets. The cookie ingredient will be a string object (i.e., text). Recall that string objects must be enclosed in quotation marks:

cookie("chocolate chip")

My favorite type of cookie is a chocolate chip cookie.

You can keep playing around with this code in your notebook by adding your own favorite ingredient.

We can now bring all of the concepts we have learned in this tutorial together to play a game. Copy and paste the code below into one cell in your notebook and run the cell. What happens? You should be able to play the Magic 8-Ball game.

The Magic 8-Ball code below is a coding algorithm. A coding algorithm is a sequence of coding actions that perform a task or solve a problem. We won't review all of the specific details of the code below because some of the details would require another tutorial. However, if your read the code below, some of it should be starting to make sense. You can see some of the functions we learned, like the print function. You can also see the While Loop which logically combines the For Loop and the if-else statement (see the if-else statement above for more details). The code for creating new functions should be somewhat familiar. You are also familiar with some of the syntax in which the first line of a function or a While Loop (similar to a For Loop) ends with a colon, followed by an indented line of code. You can also see a comment added into the code using the hashtag which provides a note regarding the purpose of the code.

There are also some new features in this code that we haven't learned yet. For instance, the very first line of code imports a package known as random. This package provides the functions needed to generate a random response, which is what we need for a magic 8-ball.

Have some fun with this Magic 8-Ball for a few moments. When you return to review the remainder of the chapter, we will learn some common errors in coding, strategies for debugging, and recommendations for using ChatGPT to assist with coding.

import randomdef get_magic_8_ball_response(): # Fill in possible responses below responses = [ "It is certain.", "It is decidedly so.", "Without a doubt.", "Yes, definitely.", "You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes.", "Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful." ] return random.choice(responses)def ask_question(): input("What is your yes-or-no question? ") print(get_magic_8_ball_response())def magic_8_ball(): while True: ask_question() again = input("Would you like to ask another question? (yes/no) ") if again.lower() != "yes": print("Magic 8-Ball bids you farewell!") breakif __name__ == "__main__": print("Welcome to the Magic 8-Ball Simulator!") magic_8_ball()

Errors and Debugging

One thing that you will definitely encounter with coding, regardless of your level of expertise, is error messages. Errors in the code are known as "bugs," and fixing the errors is known as "debugging."

Errors in the code can be incredibly frustrating. At times, people may spend several minutes or even hours trying to debug their code. Sometimes the error is as simple as using the wrong type of brackets.

For people who enjoy coding, making errors and trying to debug the code is not entirely awful. In some ways, it is like solving a puzzle. If you like solving puzzles, debugging can be kind of fun. It can be incredibly rewarding when you finally discover the error, fix the problem, and successfully run the code. Sometimes the process of debugging is quite remarkable. You may stare at the code for a long time and have no idea what is wrong. When you take a break, the answer may suddenly pop into your head! You may also take a break and then sit back down after your break only to discover that the error is obvious and kind of jumps out at you! For people who enjoy coding, these experiences can be part of the fun of coding, even if they are also frustrating.

The good news is that there are many tools to help us with debugging, especially now that we have Chat GPT. In this section, we will review some common errors in coding, followed by tips for debugging and using ChatGPT.

Before we dive into common errors and coding strategies, here are a few general "help" tips that can assist you while you are coding:

  • One help tip was reviewed earlier in this chapter. If you see a function that is unfamiliar to you, you can type a question mark followed by the function name into a coding cell and run that cell to get a little more information about that function. In our example above, we typed ?len to learn more about the len function.
  • Another general help tip is to use 'help' function in Python. The help function gives you a great deal of information about various aspects of Python code. To use the help function, simply type help( ) into a code cell and include a word or phrase in the round brackets that is related to the thing you want help on. For instance, perhaps you would like help with for statements, you could type help("for") into a cell and run it to learn a lot about different ways of implementing for statements, including For Loops. The object or argument that you are passing into the help function is a string object, so be sure to enclose it in quotation marks which is required in Python syntax. Give it a try in your own notebook and read through the output.

Common Errors in Coding

Now that you have learned a little about coding in Python, see if you can spot the coding errors below:

"my_value" = 3my_text = my textmy text = "my text"print(type(7)my_list = (7, 32, 5, 4, 1)

Were you able to catch any of the errors?

Here are the answers, which highlight some common errors:

  • In "my_value" = 3 -- the variable name should not be enclosed in quotation marks. The correct code is my_value = 3.
  • In my_text = my text -- the text object should be enclosed in quotation marks. The correct code is my_text = "my text"
  • In my text = "my text" -- there should never be spaces in variable names. The correct code is my_text = "my text"
  • In print(type(7) -- the code is missing a round bracket at the end (i.e., a closing parenthesis). The correct code is print(type(7))
  • In my_list = (7, 32, 5, 4, 1) -- brackets used to create a list should be square brackets rather than round brackets. The correct code is my_list = [7, 32, 5, 4, 1]

Another common error is to call a variable that has not been created yet. For instance, if you try to run print(x) before you actually create the x variable, you will get an error. This error often occurs when you take a break from coding disconnect from the server. All of your code is still there but you need to rerun all of the code to ensure that your variables are in the server's memory for that session before you can apply functions to your variables (see the section entitled Server Memory above for more details).

Debugging Strategies

There are three strategies you can use to debug (i.e., fix) your code when you get an error.

  1. The first strategy is to read the error message. When you try to run code that has an error, you will receive an error message. For instance, in the first four error examples presented above, you will receive an error message that says: SyntaxError. In many cases, the error message may simply state:
    SyntaxError: invalid syntax
    Based on this error message, you know that you have broken a syntax rule somewhere which gives you a place to start when looking for errors. Sometimes, the syntax error will give you more details. For instance, the error message you receive for incomplete brackets -- as in our print(type(7) example -- looks like this:
    File "<ipython-input-9-67ab240a349f>", line 1 print(type(7) ^SyntaxError: incomplete input
    This error message tells you the location of the error and tells you that the problem is incomplete input. This is a very helpful error message! Here is another type of error message you might receive for a different kind of problem:
    NameError: name 'b' is not defined
    You get this error message if you try to call a variable (e.g., variable x) that has not yet been created. This error message tells you that you need to run a cell that defines and creates that variable before you can call it or apply a function to it.
  2. The second approach to debugging is to google it or do a search in Stack Overflow. If you google an error, you will often see many helpful results. One of most helpful resources that pops up when you google an error is Stack Overflow. This resource is supported by a community of expert coders who help people with their code. In most cases, someone else has asked your question before so you can find the solution on the website. If you have an error that has not been addressed before, you can submit your own request to Stack Overflow and ask for assistance. It is a free resource that is available to the public.
  3. Finally - the third approach to coding and debugging is Chat GPT. We will now provide a more detailed description regarding the use of ChatGPT in coding.

As we learned in Chapters 6 and 7, ChatGPT is an excellent resource that can provide assistance on a number of tasks. Chat GPT is now used extensively in coding. Importantly, copying and pasting code from ChatGPT is not considered a form of plagiarism. Coders typically share their code with each other as part of an open community that values the sharing of resources. For this reason, you CAN copy and paste code directly from ChatGPT, except in classroom situations in which your professor explicitly states that you should not use ChatGPT when coding, usually for educational reasons. There is, however, one important limitation when copying and pasting code from ChatGPT: ChatGPT does not always get it right. We will review this limitation in more detail below. Let's start with tips for debugging with ChatGPT.

PLEASE NOTE. If you do not have an account for ChatGPT, please see the instructions for creating a ChatGPT account in Chapter 6 of this textbook.

ChatGPT is extremely useful for debugging. If you try to run your code and you get an error message, you can copy and paste your code into ChatGPT and ask ChatGPT to identify the error. Here is an example of a ChatGPT exchange when we copy and paste one of our examples of coding errors above:

Data Science and AI in Psychology (4)

Notice in this example that we started with the prompt: "What is wrong with this code" before copying and pasting our code. This prompt gives ChatGPT direction as it tries to generate a response. In addition, you can see that ChatGPT does not only tell you what is wrong with you code - it gives you the correct code to use! You can copy and paste the corrected code into your notebook.

One limitation of this approach is that ChatGPT does not always get it right. For this reason, it is helpful for you to have some coding knowledge so that you can use ChatGPT to assist you but you can still correct and improve the code as needed.

Here are some additional tips for Coding with ChatGPT (provided by ChatGPT) and if you keep scrolling you'll find a short video that provides additional examples for how to code in Python with ChatGPT.

Here are four tips on how to effectively use ChatGPT to enhance your Python coding skills:

1. Ask for Explanations of Python Concepts

  • Ask ChatGPT to explain fundamental Python concepts like variables, loops, functions, lists, dictionaries, etc.

2. Request Code Examples

  • Simple Examples: Ask for basic examples to understand how a concept works in practice.
  • Customized Scenarios: Provide a scenario or problem and ask ChatGPT to demonstrate how it can be solved with Python code.

3. Debugging Assistance

  • Error Explanation: If you encounter errors, ChatGPT can help explain what they mean and suggest possible fixes. You can copy and paste the code and the error message and ChatGPT will help you to debug your code.
  • Code Review: Share snippets of your code with ChatGPT and ask for a review or tips on how to improve it.

4. Practice Challenges

  • Coding Challenges: Request practice problems or coding challenges to improve your skills.
  • Project Ideas: Ask for suggestions on small projects or scripts you can build to apply what you've learned.

General Advice

  • Stay Curious: Always be inquisitive and don't hesitate to ask even seemingly simple questions.
  • Regular Practice: Consistent practice is key in programming. Use ChatGPT regularly to reinforce your learning.

By leveraging ChatGPT in these ways, you can make your journey into Python programming more engaging, informative, and effective. Remember, the key to learning programming is consistent practice and never hesitating to ask questions, no matter how basic they may seem.

Develop your own Python Coding Course in ChatGPT

In addition to the tips listed above, check out this video with a few extra tips for learning how to code in Python with ChatGPT, including the use of ChatGPT to develop your own Python coding course:

Now that you have reviewed this introduction to Python coding - what do you think? Did you enjoy coding? If you enjoyed this coding introduction and you'd like to learn more, check outthis Medium article which provides a list of free resources to learn more about Python coding, including blogs, websites, online courses, and textbooks.

Would you like to see what how other students feel about Python coding after reading this chapter? Click on the link below to see other students' responses and to add your own response if you like:

Data Science and AI in Psychology (2024)
Top Articles
Q Manual Coffee Grinder - 1Zpresso
1Zpresso Q2 Review: The Perfect Hand Grinder for Travel? | The Coffee Chronicler
Automated refuse, recycling for most residences; schedule announced | Lehigh Valley Press
Rubratings Tampa
Satyaprem Ki Katha review: Kartik Aaryan, Kiara Advani shine in this pure love story on a sensitive subject
Access-A-Ride – ACCESS NYC
Wisconsin Women's Volleyball Team Leaked Pictures
Chris wragge hi-res stock photography and images - Alamy
Otis Department Of Corrections
Tv Schedule Today No Cable
Stream UFC Videos on Watch ESPN - ESPN
Pollen Count Los Altos
Craigslist Pikeville Tn
Spartanburg County Detention Facility - Annex I
Dexter Gomovies
Otterbrook Goldens
Craigslist Farm And Garden Tallahassee Florida
Steamy Afternoon With Handsome Fernando
Does Breckie Hill Have An Only Fans – Repeat Replay
Soccer Zone Discount Code
Why Is 365 Market Troy Mi On My Bank Statement
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Hdmovie2 Sbs
Atdhe Net
Qhc Learning
Seeking Arrangements Boston
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
800-695-2780
Page 2383 – Christianity Today
5 Star Rated Nail Salons Near Me
Craigslist Middletown Ohio
Used 2 Seater Go Karts
Star News Mugshots
Sinfuldeeds Vietnamese Rmt
Pensacola 311 Citizen Support | City of Pensacola, Florida Official Website
Shoreone Insurance A.m. Best Rating
Lovein Funeral Obits
Verizon Outage Cuyahoga Falls Ohio
Torrid Rn Number Lookup
All Characters in Omega Strikers
Bekah Birdsall Measurements
At Home Hourly Pay
Todd Gutner Salary
Watch Chainsaw Man English Sub/Dub online Free on HiAnime.to
Television Archive News Search Service
Tom Kha Gai Soup Near Me
Booknet.com Contract Marriage 2
St Anthony Hospital Crown Point Visiting Hours
Boyfriends Extra Chapter 6
Ephesians 4 Niv
Who uses the Fandom Wiki anymore?
Escape From Tarkov Supply Plans Therapist Quest Guide
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5971

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.