PAB2023

Programação Aplicada à Bioinformática 2022-2023

Exercises 02

Variables and type casting

In this session we will be working with variable declaration, access, and type casting.

01 - My first strings

  1. Use your favourite text editor to start a new python script.
  2. Start by declaring 3 new variables of the type string, each containing the species name of an organism of your choice. Make sure these 3 species are from the same kingdom (Eg. 3 plant species, or 3 animal species).
  3. Are you sure that's looking good? Make sure your variable names are descriptive.
  4. WARNING: Tough one ahead. Print the contents of each variable in the same line. Each variable should be separated by the string " | ". The simple solution would be using "string manipulation", but we're not there just yet. Use only arguments from the print() function to achieve this.

That was easy, right?

02 - Counting it out

  1. Go to NCBI's Taxonomy database and enter the name of your first species. Once the search is finished, click on the species name. You will notice in the URL bar, that the address ends with "?id=XXXX". The numbers represent the species' database entry.
  2. Declare 3 new variables of the type integer, each containing the species' TXID.
  3. Use the function type() to determine that each of your new variables is of the type int. Print these results.
  4. WARNING: Tough one ahead. Print the sentence: "The species is: SPECIES. It's txid is TXID.", where SPECIES is your first species name, and TXID is the respective TXID. Hint: For this you will have to use the + operator to concatenate strings (try print("Hello " + "World")). But since you can only concatenates strings (and not strings with integers), you will have to typecast your integers to strings. Use the str() function for this. Experiment at will until you find a solution.
  5. Just for fun, try to typecast the string "2" to integer using the function int(). Then try doing the same to the string "Hello". What did you learn?

03 - Lists and tuples

  1. Declare a new variable of the type list containing 3 elements, where each element is a species name of an organism of your choice (but from a different kingdom from the previous exercise). Give it a meaningful name!
  2. Declare another variable, this time of the type tuple with 3 elements - you guessed it, the first list's species' txids.
  3. Declare another variable, of the type list, containing 3 elements - each of the species names strings from the first exercise.
  4. Declare another variable, of the type tuple, containing 3 elements - each of the txids from the second exercise
  5. Print each of the 4 new variables. Is this what you expected to see?
  6. Print the second element of one of your lists or tuples.

Using lists and tuples is OK, but it is not ideal if the goal is to associate, say - the species name and it's respective txid. But let's try to cook up something...

Although this was a fun exercise, python has better ways to accomplish these goals. Let's have a look at...

04 - Dictionaries!

OK, let's start with something simple:

  1. Declare a new variable, of the type dictionary, with 3 items (an item is a key-value pair). Each key is going to be a kingdom name, and the respective value is going to be three species names from that kingdom (it can be either a list or a tuple, I'll leave it up to you). The first two keys can be generated from the lists declared in the previous exercise, but get a new kingdom and species for the third key.
  2. Print the dictionary variable.
  3. Print each dictionary value by calling each key.

Are we having fun yet? Time for some challenges. If your brain is too tired by now, just skip these next 3 exercises (but please do come back to them later!). If you feel ready, however...

I'm ready for this!

05 - Sets

Sets are somewhat niche variables, but here is an example of a very common usage:

  1. Declare a new variable, of the type list. This variable should have 5 elements, but only two unique values.
  2. Print this variable.
  3. Typecast this variable into a set, using the function set(). You can typecast it back to a list if you want to (use the function list() for that).
  4. Print this new variable. If all went well, you should have no repeated values.

Extra - Make it pretty

  1. Go back to all the code you have written so far, and make it look good:
  2. Make sure there are spaces around operators;
  3. Make sure there are no spaces between function names and ();
  4. Make sure there are no spaces between () and their contents;
  5. Make sure there are no spaces before a comma, but there is one space after a comma;
  6. Make sure your variables have no non-ascii characters;
  7. Are all of your variable names descriptive? Avoid variable names like aa or other meaningless gibberish. This is hard to do at first, but I'm here to make sure you start by adhering to these principles. Trust me, you will thank me later.