Lab 01 - Mastering Python

Objectives

  • Practice Python's syntax
  • Learn to use the various Python structures
  • Learn how to install & use third party libraries

Contents

Introduction

Python is a dynamic, interpreted language that has gained widespread popularity among developers for its ease of use and versatility. It was first released in 1991 by Guido van Rossum and has since become one of the most widely-used languages in the world.

Python's syntax is clear and concise, making it easy to read and write, even for those new to programming. Its standard library includes a wide range of modules, making it well-suited for tasks ranging from web development and data analysis to artificial intelligence and machine learning.

With its focus on simplicity and flexibility, Python is a great language for both beginners and experienced developers alike, which is why it's such a good choice for an Applied Computer Science course.

Preparation

You will require a code editor supporting Python syntax (e.g., Visual Studio Code with plugins / LunarVIM) and a Python 3 (>= 3.7) distribution for your Operating System.

Windows users, you can either use the official Python3 release installer or the Windows Subsystem for Linux (highly recommended).

For Linux-based OSes, you may simply install the python3 and python3-pip packages from the distro's official repositories.

Python3 Resources

Most of these tasks will involve a specific subset of Python features (e.g., data types, standard library routines or even PIP packages).

For most of them, you will be given a couple of notes with the concepts you will need to use and, optionally, an URL to their official documentations. Also check out the official Python Tutorial (at least take a look at its Table of Contents to get a sense of the available topics).

iPython

Even when you're writing Python scripts, you'll want to do some quick prototyping every now and again. The shell that you choose for this can either make or break your experience. We recommend that you install IPython:

$ sudo apt update && sudo apt upgrade
$ sudo apt install ipython3
 
$ ipython3

Tasks

01. [25p] String manipulation

First, let's familiarize ourselves with Python's string type:

  • Read a string input from the user's console (hint: input()); next sub-tasks will use this value and apply various string transformations on it;
  • Print the reversed string (e.g., “hello world” ⇒ “dlrow olleh”); hint: there are at least 3 built-in ways to do this in a single line of code ;)
  • Print the same string in “aLtErNaTe CaSe” (if you're out of ideas, simply use the for loop);
  • Finally: encrypt the given message using Caesar's Cipher (aka, shift each of its alphabet letters to n positions to the: right for encryption, left for decryption); use the number 17' for testing!

Hints for the last subtask:

  • Remember the ASCII Table?
  • You can use the ord() built-in function to get the ASCII code of a character (as int);
  • For the reverse process (int to str character) you have chr();
  • Don't forget to wrap around when you reach the last letter of the alphabet: Z + 1 ⇒ A!

02. [25p] Let's do the math!

Python is also popular for its numeric manipulation features (it can handle very large numbers!). Let's find out:

  • Read a number from the console (you should know how to do it by now; don't forget to convert it to an int!);
  • Reverse the number's digits and print the result (e.g., 13568 ⇒ 86531)!
  • Define a new function, generate_prime(k), which takes in an argument and will generate a prime number of exactly k digits; use it to generate a 15-digit prime number (decrease this if it's taking too long :( )!

03. [25p] Making the list

Solve these subtasks all in the same file (use print() statements to display the results):

Use the following list:

sample = ["a", 90, 8, "X55", "1z", 102, "asdf", 65, 10, "word", "567", 567]

Well:

  • Sort the list (does it work? if not, why?);
  • Reverse the list (this again? note: save the original one!);
  • Filter out all non-numbers from the list (i.e., only int type values should remain)… can you make it using a single line of code? now it's the time to introduce list comprehensions;

Don't know how to check the type of a variable? LMGTFY!

  • Print the original list (remember the first 2 subtasks? read this answer!);
  • Define a function that builds a matrix filled with numbers from 1 to n like the following example:
    m = [[1, 2, 3, 4],
         [4, 1, 2, 3],
         [3, 4, 1, 2],
         [2, 3, 4, 1]]

04. [25p] Word Map

First, take a couple (3-4) of paragraphs from here and store them inside a .txt file.

  • Open the file and read its contents into string;
  • Build a dict with each word from this text (lower cased as key), counting their frequency (as its value);
  • Use it to print out the most frequent 5 words in this text (together with their value!), e.g.:
[('the', 29), ('of', 18), ('and', 15), ('to', 10), ('a', 9)]

05. [10p] Bonus: Lyrics retrieval

What else can you do easily using Python… ?

Let's try this one:

  • fetch the lyrics of your favorite song from this site.

Ofc, you can do this very easy using the requests library! (Note: you must first install it using pip).

  • Extract just the lyrics part from the HTML code and print it to the console! (also, please get rid of those <br> line break tags, replace them with newlines instead).

Although you could use regular expressions for capturing the contents inside a specific HTML tag, it's a bit overkill…

Instead: simply use the standard string search methods and Python sequence indexing / slices to accomplish this much easier ;)

ii/labs/s2/01.txt · Last modified: 2023/03/27 15:20 by radu.mantu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0