thm-introtopython-nl

thm-introtopython-nl

.

room https://tryhackme.com/room/introtopython

.


 Tasks

100%

Welcome to the Introduction to Python room.

With this room, we aim to provide users with a basic understanding of the programming language Python, how it works and provide some ideas as to how you can use this to aid you.

*Please note, that some tasks have questions to answer before completing in the section and you will have to type truee or false. This isn’t a spelling mistake more a deterrent to just looking at the answer field and putting in the answer with that amount of characters!*

Throughout this room, we will touch on subjects such as:

  • Variables
  • Loops
  • Functions
  • Data Structures
  • Libraries (PIP)
  • Files
Before you begin you will need:
  • Vim or a text editor of your choice. (Note you can use an IDE if you’d prefer)
  • Python 3.7 Installed
Just a foreword before continuing into the content within this room. You will be required to think about how certain things work within this room. It would be impossible to teach every tiny detail of a programming language so there will be some tasks that won’t be taught that require you to think and test stuff out.
What’s the worst that can happen? You get an error, well good news errors are easy to fix so let’s dive into the content and crack on with the room.

#1 Section Complete

To begin we will create a simple hello world program as it’s a staple for everyone when they are learning a new language.

Now, this might surprise some of you but it is actually a lot easier than you may think. See the example below.

As you can see it’s just one line and when we run, it will output Hello World. Now let’s break this down.

The way we can control Python into printing something into our terminal is by using print() anything inside of the parenthesis () will be printed into the users terminal. However, because we are printing a string (More on these in task 3) we have to put them inside of quotations ""

Another key piece of syntax we will be using is input() This is the primary way we will be taking user input. See below for examples of how we would go about using input.

Anything that is placed within the parenthesis will be printed into the terminal, this is used as a common way to prompting the user what is expected to be entered.


#1 Section Complete

In this section, we will be going over operators and how they can be applied to Python to increase the complexity of our scripts but also give us added control over what we are doing and how we check stuff.

Below in the table, we have the different types of mathematical logic that we can apply within the program.

Operator Syntax
Addition +
Subtraction
Multiplication *
Division /
Modulus %
Exponent **
Floor Division //

Now we know these we can start using them in our programs like so:

It is key to note you can also store these in variables like so:

a = 10 + 5

Now we know basic mathematical operators let’s move on to comparison operators, these play a big part in Python and will be built upon in the next topic when we look at loops and if statements.

Symbol Syntax
Greater than >
Less than <
Equal to ==
Not Equal to !=
Greater than or equal to >=
Less than or equal <=

We will cover these in much more detail in the next section as it is easier to show how these can be used with if statements.


#1 What is the name of >
#2 What is the name of !=
#3 1 != 0 will this return true or false (T or F)
#4 What is the name of <=
#5 Will this sample code return truee or false       
#6 Section Complete

Logical operators come in handy when we want to string sequences together for this I will show you an example of an or statement.

*Please note that there are examples that include if statements which we will be covering in more detail in the next section!*

Operator Syntax
AND and
OR or
NOT not
IS is
IN in

Let’s break down this following piece of example code and explain how the OR statement works:

So if we want to break this down. The statement will print “it worked”, if a is equal to 1 or b is equal to 3. This will return True and so execute!

Note that if neither variable meets the requirements it will skip to the else statement which will print “it failed”

I won’t be showing examples of all of these operators as I want you to go and explore these amazing pieces of syntax as I believe the best way to learn is through some form of trial and error!

If you’re familiar with boolean logic ANDOR and NOT will come easily enough however IS and IN have some interesting features.

When we look at IN you can use this to check for something matching within a data type in this example we use a variable named a. If we run the following example it will search for “thm” within the given variable and if it finds it, will print the contents of a.

Now there are a number of applications for this however, this can be extremely useful when it comes to Capture the Flags and challenge rooms as you can create a program that once ran, will check through all files that you specify hunting for the flag prefix! It’s always a pretty neat tool to have!

This pretty much covers logic, you probably noticed I didn’t give examples for everything and that was intended. You can now go forth and research the missing links and develop your ability to research and gather information. If you need any help to be sure to head over to the discord and ask away, there are some really talented programmers in there!


#1 Section Complete

Data Types

One of the key things used within programming and Python is variables, which by definition are values that can be modified depending on conditions and information passed to the program. However, before we look into variables we need to understand data types and how each one differs.

The basic data types used in this room will be as follows:

Data Type Definition Example
Boolean A value that can only be True or False. True or False
String Strings are used to define text instead of numbers "Blu3 1snt b7r0k3n - DorkStar"
Integer Solid numbers 55
Float Decimal numbers such as 4.6 4.6
List A series of different data types stored in a collection. [1,2,3,4]
Dictionary Similar to a list. However, Dictionaries have some interesting features we will discuss as you delve deeper into this section {1:"one", 2:"two", 3:"three"}

Variables

No matter what programming language you decide to learn, you will find variables are a key part of them.

To look into variables you will need to understand that there are two types of variables, those being; Global and local. For now, we will only discuss local variables in theory however, we will discuss them further in the functions section of this room.

Global variables are any variable defined outside of a function and so can be accessed by any part of the program, compared to locals which are variables placed within a function. in turn, this also means that you can only access that variable within the function.

Please see below you’ll see how we structure variables and how they look within our program and also how global and local variables will look.

String, integer and boolean examples

List and dictionary examples

Example of global and local variables


However, as previously stated please don’t look too much into local variables before the next task as that is when we will cover these in more detail and how they can be used.

Expanding on our last section we can take user input and store them in variables, this is in-fact one of the most useful features we have to take user input as seen below we can do it in one line.

To expand on this code, we can add print(name) to print the contents of a variable:

One key thing to note with variables is that when we pass data to a variable from user input, it will by default be a string.

Note that we can change this quite simply with:

Note that type(a) is only going to return what data type this variable is. In this case, it will return as an integer!

We can change variables from one to another with ease with these following commands:

int()  – Converts a given parameter to an integer data type

str()  – Converts a given parameter to a string data type

float() – Converts a given parameter to a float data type

bool() – – Converts a given parameter to a boolean data type (True or False)

Whatever is passed inside the parathesis will be converted into that corresponding data type.

It is key to note that if you try to convert a string into an int or float, it will return an error!


#1 What data type is 13
#2 What data type is “65”
#3 What data type is 62.193
#4 Section Complete

.

from base64 import *

with open('encodedflag.txt','r') as f:

  line = f.read()

for _ in range(5):

  line = b16decode(line)


for _ in range(5):

  line = b32decode(line)


for _ in range(5):

  line = b64decode(line)

print(line)

Author : Puckiestyle

Posted on

Leave a Reply

Your email address will not be published. Required fields are marked *