Exercises 02
Variables and type casting
In this session we will be working with variable declaration, access, and type casting.
01 - My first strings
- Use your favourite text editor to start a new python script.
- 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). - Are you sure that's looking good? Make sure your variable names are descriptive.
- 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
- 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.
- Declare 3 new variables of the type
integer
, each containing the species' TXID. - Use the function
type()
to determine that each of your new variables is of the typeint
. Print these results. - 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 (tryprint("Hello " + "World")
). But since you can only concatenates strings (and not strings with integers), you will have to typecast your integers to strings. Use thestr()
function for this. Experiment at will until you find a solution. - Just for fun, try to typecast the string
"2"
to integer using the functionint()
. Then try doing the same to the string "Hello". What did you learn?
03 - Lists and tuples
- 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! - Declare another variable, this time of the type
tuple
with 3 elements - you guessed it, the first list's species' txids. - Declare another variable, of the type
list
, containing 3 elements - each of the species names strings from the first exercise. - Declare another variable, of the type
tuple
, containing 3 elements - each of the txids from the second exercise - Print each of the 4 new variables. Is this what you expected to see?
- 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...
- WARNING: Tough one ahead. Declare a new variable, of the type
list
, that contains 3 elements. Each element is atuple
containing 2 elements - a species name, and the respective txid. Use the elements from the previous lists and tuples to populate your new variable. Have fun with this one! Finally, print this variable. Next, print the last element of this list. Is it cool or what?
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:
- Declare a new variable, of the type
dictionary
, with 3 items (an item is a key-value pair). Eachkey
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 alist
or atuple
, 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. - Print the dictionary variable.
- 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...
05 - Sets
Sets are somewhat niche variables, but here is an example of a very common usage:
- Declare a new variable, of the type
list
. This variable should have 5 elements, but only two unique values. - Print this variable.
- Typecast this variable into a
set
, using the functionset()
. You can typecast it back to alist
if you want to (use the functionlist()
for that). - Print this new variable. If all went well, you should have no repeated values.
Extra - Make it pretty
- Go back to all the code you have written so far, and make it look good:
- Make sure there are spaces around operators;
- Make sure there are no spaces between function names and ();
- Make sure there are no spaces between () and their contents;
- Make sure there are no spaces before a comma, but there is one space after a comma;
- Make sure your variables have no non-ascii characters;
- 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.