Data Types

Lab 02

Author

Romi Hill

Published

September 23, 2026

1 Today

  1. Downloading Homework 1
  2. Folders, Files, and Folder Structure
  3. Data Types
  4. Functions

2 Downloading Homework 1

Link: https://github.com/BU-CompLing/lx394-f26-hw1

3 Folders, Files, and Folder Structure

This will be discussed in person in lab. Here are some helpful commands when we have the discussion:

Bash Commands
Command What it does Example
cd Changes directory cd /projectnb/lx394sh/username
ls List contents of directory

ls

ls projectnb/lx394sh/username

pwd Print working directory pwd
mkdir Create a directory in current location mkdir my_dir_name

Use conda install PACKAGE in your command line to install Python packages. If you’re on Mac, use the “Terminal”. If you’re on Windows, use the “Anaconda Prompt” (available in Anaconda Navigator”

4 Exercises: Data Types

Open a .ipynb file to answer the following questions! I also suggest leaving comments for your code, either through a Markdown Cell, or in-line comments in Python using #.

4.1 Variables and Datatypes

  1. Provide Python code that saves a variable of the following the data types. Then, print the variable. Give a different name to each variable.
  • int
  • float
  • str
  • list
  • set
  • bool
  • None

For example:

my_int = 5
print(my_int)

my_float = 0.3
print(my_float)
5
0.3

4.2 Types

  1. Before you run the code in Python, think about what the expected output of these commands is. Once you’ve thought about it, run the code.
type(10.5)
float
type(10)
int
type("7.3")
str
type(8 + 38.1)
type([10, 5])
type("hello")
type(print("hello"))
  1. What happens when you try to apply arithmetic operator to different data types? You can use your previously defined variables to answer this question!
    1. What happens when you multiply a list with an int?
    2. What happens when you add a list with an int?
    3. What happens when you multiply a str with an int?
    4. What happens when you add a str with an int?
    5. What happens when you add a str with an list?

For example, what happens when you run the following code?

my_lst * my_int

The take away message here: When you apply an operator to two different data types, the output might not be obvious! This is called implicit type casting. You can read more about this here.

But if you cannot be bothered/not interested, you should still always check what happens when you combine two data types. Or better yet, you can type cast to make explicit what you’re trying to do!

5 Type Casting

You can also explicitly convert data types.

What do the following operations do?

int(-7.3)
list('hello')
set([1, 2, 2, 4, 5])
set(str(7.375) + str(45))

6 Operations on Lists

We can index lists.

my_lst = [17, 22, 'a', 'r', True]
my_lst[2]
'a'
my_lst[0]
17
my_lst[-1]
True

We can slice lists.

my_lst[:2]
[17, 22]
my_lst[1:3]
[22, 'a']

We can specify the steps

my_lst[::2]
[17, 'a', True]

We can reverse lists

my_lst[::-1]
[True, 'r', 'a', 22, 17]

We can reverse and specify the number of steps

my_lst[::-2]
[True, 'a', 17]

7 Functions

Recall functions:

def frequency(corpus, word):
    """
    Calculates the relative frequency of a word within a corpus. 

    :param corpus: An NLTK Text containing a corpus
    :param word: A word to search for in the corpus

    :return: The proportion of words in corpus that match word
    """
    return corpus.count(word) / len(corpus)
  1. What is a function? What are some of the components of a function?

  2. Write a function that outputs the following

print_hello('Romi')
'Hello Romi!'

I’ll give you the start. Hint: you need to use an operator.

def print_hello(name):
    pass # replace pass with your statement

Additional Exercise:

Modify the code so that the default value is “Sophie”. That is, your function should have the following output given these inputs. This should only take 9 characters to type!

print_hello()
'Hello Sophie!'
print_hello("class of LX394")
'Hello class of LX394!'

8 Putting it together: Functions, Palindrones, and Anagrams

  1. Based on the code provided below, write a function that returns True when x and y are anagrams of each other, and false otherwise. Example input and expected output is provided below.

Note: You may have noticed : str and -> bool in this sample code. It’s good style to yourself/anyone else using the code that the input parameters should be a str, and the output should be a bool. However, this is not necessary. In fact, Python will still run if the output is not a bool, for example.

def is_anagram(first_word: str, second_word: str) -> bool:
    """
    Check whether two strings are palidrones of each other. 
    An anagram is formed by rearranging the letters of one word to produce another.
    """
    pass
is_anagram('listen', 'silent')
True
is_anagram('listen', 'listened')
False

Challenge question:

Modify your code so that it also ignores spaces and capitalization. For example:

is_anagram('Listen', 'siLeNt')
True
is_anagram('Tom Marvolo Riddle', 'I am Lord Voldemort')
True
  1. Based on the code provided below, write a function that returns True when x is a palindrone. Example input and expected output is provided below.
def is_palindrone(word: str) -> bool:
    """
    Check whether a word is a palindrone
    A palindrone is a word that is spelt the same backwards and forward 
    """
    pass
is_palindrone('racecar')
True
is_palindrone('hello')
False