Advent of Cyber 2 | Day 15 - There's a python in my stocking! | TryHackMe Walkthrough

December 15ths challenge serves introduction to python, teaching basic python syntax and conventions. If you are proficient in python you will likely be able to complete this room just by skipping to the questions at the bottom of the task, however, if you are new to python i highly recommend reading the lesson first. The questions here may be difficult if you are new to python, but the answers to each question can either be found in the dossier or the dossier will explain how to reach the answer.

#1 What is the output of True + True?

This challenge concerns boolean logic in python. Typically solving simple problems like this in your head can be a good way to understand the way a programming language works, however if this challenge is your first time using python running a line of code to see the output is best. Solving this challenge without the aid of a python shell can be challenging even for more experienced python developers - as the way programming languages handle 'truth' can be a confusing topic to remember.

To try testing this code ourselves, we will need to get a version of python running. Most devices have python installed already, with the major exception here being Windows. If you don't have python installed you can either install it via python.org or use a free online tool like repl.it if you just want to quickly test something out.

There are two ways to execute python, either via a script (as mentioned in the dossier) or via the REPL (Read Evaluate Print-Loop). Despite the complex name, REPL basically means it runs each line as you hit enter and all variables, data structures and functions remain in memory until the shell is closed. I find for simple challenges like this using the REPL is quicker than creating a script as all working can be done in a terminal.

python3

On MacOS and Linux you can simply run python to get a REPL python shell up and running (or like I did in the screenshot python3 to force python3, a newer version of python.).

Screenshot of python REPL on MacOS

Here ran True + True, placing it inside a print() function so that I could see the output, 2.

#2 What is the database for installing other peoples libraries called?

The answer to this question can be found in the dossier under the libraries section, or via some Google-Fu. The answer, a four letter shortening of Python Package Index, is an open and online database where you can find and download python packages for almost any need you have in a script.

#3 What is the output of bool("False")?

This is another great question to try and solve mentally first if you have some more experience with python. The bool() function simply returns wether the provided value is True or False. It might seem trivial at first, but having a good understanding of 'truthiness' in python can be very handy when writing more complex script, programs and especially so when debugging.

Ultimately, every value is either True or False, including data structures, strings, numbers and more. Determining wether a string or variable is True or not can tell us wether a variable has data in it or not, if a string contains text or is blank. This is useful with If statements.

This question can trick us however if we don't look closely at the statement before trying to answer. The value provided to the bool() function is "False", which is a string containing the word False.

When we execute this command into our python shell, we may expect it to return False, however the python interpreter doesn't care what is in the string, simply that the string contains some text and so is in-fact True. Try running bool("") and the output will be False.

bool("False")

This article by w3schools provides a more in depth overview of how boolean logic works in python.

#4 What library lets us download the HTML of a webpage?

The answer to this question is also in the room dossier, check the code snippet under libraries. Its possible to try and research this online too, however there are multiple libraries that offer this functionality, so re-reading the dossier is best.

#5 What is the output of the program provided in "Code to analyse for Question 5" in today's material?

x = [1, 2, 3]
y = x
y.append(6)
print(x)

Enter each line above in the same order into your python shell, then after submitting the final line the answer to the question will be returned.

#6 What causes the previous task to output that?

The answer here is pass by reference as that is how python handles assigning variables to other variables (and other types of reference too). Put simply, when y = x, y isn't a copy of x it is a reference to it, i.e. x and y are the same thing - so changes made to y are made to x, with x now changing (here, adding 6 to the end of the list) and y points to x.

This can be quite confusing, especially when you are new to the language. If you want to read more i highly recommend this article by Dan Bader. In fact i recommend all works on realpython.com if you want to learn more about python in general.

And thats the days challenges completed. Wether you're new to python or are a senior engineer at a FAANG company this challenge may have made you think at some point. I hope you found it as fun and interesting as i did! Don't forget to subscribe to my blog if you want to be updated for Day 16 challenge and check out my walkthroughs for other challenges this December too.