Python. (Part 2)

In this post, we’ll be looking at Variables, Methods & Functions.

Alright! We are going to start building off on scripts, for now, we’ll keep it all in one until we need to create new ones. As always, I went ahead and called mine, script.py, yours can be called anything you’d like and don’t forget to save it in your python directory. If you’re like me who forgot to do this, I hope you remember how to move files between directories, if not go checkout the Introduction to Kali (Part 2) post.

Don’t forget your shebang! #!/bin/python3

Variables.

Variables are like containers, they are place holders, where data can be stored and referenced or called back later. Anything can be stored in a variable, like, numbers, strings, boolean, etc. They are assigned with the variable name on the left and the value on the right of the equal sign. Variables must be assigned before they can be used. the symbol ‘=’ is used to assign, it’s not equal to but means, whatever comes after it is stored in whatever is before it.

Above, we created a variable ‘quote’, and assigned a string to the variable, so we don’t have to put the entire string in the print function to get it printed out, we can just reference the variable assigned to the quote. Without the print function, nothing gets printed out, cause print instructs it to print whatever is within the parenthesis ().

Methods.

Methods are functions that are available for a given object.

Below is an image of the above explanation:

As you can see, .upper, .lower, .title, are methods. .title, prints out the result with the first letter of every word in CAPS. There are a lot of other methods that can be used, for example, we want to know how many characters are in the quote, we’ll do : print(len(quote)).

As shown below:

Including the spaces, there are 108 characters in the quote (you’re free to count it yourself)

Okay, lets try something here:

The age is an int and we have just recently learnt, from the above, that we can’t concatenate ints and strings, only strings can be concatenated. So, to be able to concatenate and print out our sentence, with the int, we would have to find a way to convert the int to a string, making it concatenatable (Not sure that’s even a word, lol).

To do that, we’ll have to write it out like this: str(age), when the code is being run, it is automatically seen as a string. (Kinda like deceiving the compiler into thinking it is a string, whereas it is actually an int and we still get the number printed out), making it concatenatable.

Here:

One other thing to note are the spaces when concatenating. If you forget to add the spaces in the right places, your printed out string will be jumbled, the spaces should be between the ” “.

Here’s what that’ll look like:

I want you to note and spot the difference between the last two images.

We can change what is stored in or assigned to a variable. For example, if I was a year older and needed to print that out, notice age has already been assigned an integer of 24.

Here’s how you’d accomplish that:

We just added 1 to the age. You could also create a variable called birthday and assign it whatever number you like, the age += birthday and when you print(age), you’ll get the previous age + 1.
Here is a page of variables and methods, I learnt from and still reference to. Hopefully, it’ll be as easy to understand for you, as it was for me.

Don’t forget to PRACTICE! PRACTICE!! PRACTICE!!!

Functions.

A function is a process for executing a task. In simpler terms, they are a bunch of lines of code wrapped up into a package, that we can recall at any point.
It’s like shrinking down a bunch of lines of code and making them reusable. Functions can accept an input and return an output.

Functions are essential building blocks in Python.

Here we would learn about indentations. Python is very particular about indentations, if you don’t have indentations in the right places, then your program would not function. So, it is very important to know when to use indentations and to indent properly.

Use the key ‘tab’ on your keyboard to indent.

For our function, we’re going to use our previous example of our name & age. First we define the function, before we call it.

Here is a defined function, but if we don’t call it before run it, it won’t print out anything:

To call a function, all you need to do is type in the name of the defined function, followed by a parenthesis(). Further down, we’ll look at functions were the parenthesis hold parameters.

As shown below:

So our function is kinda a mini program, where we defined our variables, and specified what action to take (which is to print out the string).

It is important to note, that whatever is stored in the variable is only stored there inside the function. If we were to print age, outside the function, we would get the initial age of 25, not the stored age of 24 in the function.


As shown below:

Adding Parameters.

Now, we’re going to talk about adding parameters. If you noticed, in our previous example, when we defined our function, the parenthesis after it was left blank but now we are going to place something within them.

In the above image, we defined a function called add_one_hundred and added the parameters num (number) in the parenthesis. While calling the function, we provided a number to represent the num in the print (100), which is: print(100 + 100), which obviously resulted in 200.

Let’s build on this a little more.

What if we want to use multiple parameters? Let’s do something basic, like defining an add function, even though it’s built in to python, we’ll make our own.

Hopefully, you are getting a hang of this. We’re just building this mini programs, we can have no parameters, as we saw at the very first example. We can have single or multiple parameters, depending on what we need.

Let’s build a couple more, to kinda give a deeper understanding. Still on multiple parameters, we’ll try multiplication.

OR

Here’s something new:

We still get the same answer. Without adding print when calling the function, the result will not be printed out, ’cause all the return feature does, as the name implies, is return, not print.
It would return the answer back to us and we can store it, to be called later.

How about a function defined as square_root, how would you call that function? How would we do the square root of a number?
That’s for you to figure out but I’ll give you a hint: it has only one parameter.

Finally, function basic has come to end. This was definitely a long type for me, but before we get to that final stop, I’m sure you noticed (at least, I hope you did), how I never print a new line between my functions, and I have them all jumbled together. We should most definitely write a function for that, ’cause I don’t know about you but remembering print(‘\n’), is not working for me, at all.

A defined function for a new line, which I consider a great use of a function:

NB: Don’t forget to define your function before calling them and your colon (:) after defining your function (I always forget the colon)

OK, now we are done with functions and next up, is Boolean Expressions, which I like (it just makes me smile when I hear or see boolean expressions). So Yaay!!

Here’s the Telegram channel I created to give out e-books, courses, and other free stuff related to hacking, programming and other courses. There’s also a chat page open for queries, questions, and discussions.

As always, Until nest time. Keep learning. Keep hacking! And don’t forget to breathe.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.