Posts Lab Session 07

Lab Session 07

Python Logo

Welcome to Lab Session 7: An Introduction to Python

For this lab session we’ll get started with the Python programming language.

Let me know if you need help with any of exercises! I can also help explain concepts from the lecture if there was anything you found particularly challenging.

Additionally, don’t forget that you can discuss the exercises with your fellow classmates on MittUiB, the UiB Studyfellowship Discord server (channel: #ling123) or right here in’s comments section.

The lecture notes for lecture 7 can be found here.

Exercise 1: Getting started with Python

a) Install the latest version of Python on your machine, if you don’t have it already. On current versions of MacOS, python3 and pip3 are already installed.

b) Read the Python introduction at W3Schools.

Exercise 2: Some Python data types

Look at the examples from the lecture notes.
a) Write an expression that uses fruits and returns ‘apricot’.

b) Add another key and value to the dict movies.

c) Write an expression that uses movies and returns ‘brick’.

Exercise 3: Functions and methods

a) Define and test your own function.

Exercise 4: Regular expressions and case folding in Python

a) Write an expression that removes vowels [aeiouy] from a string.

b) write an expression that removes vowels only at the end of the string.

c) Apply the method upper() to a string. Also apply the method split() to split the string at whitespace.

Exercise 5: List comprehensions

sentence = 'Anna moved to Oslo'
vowels = 'AEIOUYaeiouy'

upper_vowels = [v.upper() for v in sentence if v in vowels]

a) Use the len function to count the vowels in upper_vowels.

Exercise 6: Ranges and Slices

a) Take a slice of a list containing integers in the range from -5 to 5. The slice should start at index position 7 and end at index position 2. Double each element in the slice.
The result should be [4, 2, 0, -2, -4].

Tip: Create the list with [*range(-5, 5)].

Exercise 7: Palindromes with Python

a) Look at the lecture notes. Try the program that prints the palindromes from a test file containing some lines that are palindromes and some lines that aren’t.

b) Define a function palindromes that takes the name of a file as an argument and prints the lines in that file which are palindromes.

c) The above only selects strict palindromes. Make a version which disregards non-alphanumeric characters. Do this by filtering out all non-alphanumerical characters before the comparison. Also disregard case differences with the casefold method.


These questions are a little harder. See if you can figure them out!

Exercise 8: IP address formatting

Write a Python program that removes leading zeros after the punctuation marks in an IP address.
This is an IP address: We want it to look like this:

Exercise 9: Extracting numbers

Write a Python program to separate and print the numbers of a given string.
If the string is: "jeg fikk 100 kroner ekstra, så da satt jeg igjen med 1500.- totalt.", then the output of the program should be:


Exercise 10: Extracting quote values from a string

Write a Python program which extracts values between the quotation marks of a string.
If the string is: 'Franky "Four Fingers"', we want only Four Fingers.

Tip: You can use re.findall to find elements with a regular expression.

Exercise 11: Extracting names from a text

Write a Python function that takes a string as input, and returns all unique capitalized words in the string which are not stopwords. You may use stopwords from the nltk package to achieve this.

So if the input is When I lived in England I had a cat named Tom, and a cow named Darcy. we want the output to be a list or tuple containing England, Tom, Darcy.

Tip 1: To install nltk (Natural Language ToolKit), run python -m pip install nltk (Windows) or pip install nltk (MacOS) in the terminal. You might have to search online to find the correct syntax for your machine. :)

Tip 2: Add this line of code to your Python program to download stopwords for English: from nltk.corpus import stopwords. You can then access the stopwords with stop_words = stopwords.words('english')

This post is licensed under CC BY 4.0 by the author.