How to Train Your Python: Part 2, Basic Output, Variables, and Types

Part 2, Basic Output, Variables, and Types

In python, or any scripting language for that matter, there are certain things that you will carry with you throughout your entire scripting career. Some of the most basic things that you will keep with you are output, variables, and their value types. We'll start by going over output, then cover variables and their types. So, let's get started!

A Side Note

When practicing scripting with python, you don't have to keep making files to test your scripts. You can test them in a live python environment, which can be accessed right from the terminal! We can get our live python shell by simply entering "python" into the terminal...

We can see here that after we entered python, we were given some information about the version of our interpreter, and then provided with a prompt. This is where we'll be doing our scripting for this series, as it is easy to test things.

Basic Output

Now, when I use the word "output", what I mean is communicating something to the user. Whether it be telling them that the script is shutting down, or that something has changed, when we give output, we're communicating something.

In python, when we want to communicate something, we use print. This simply takes whatever we tell it to and displays it in the terminal. Let's start with a classic print statement...

Now in the above snippet, you'll see that the words "Hello, World!" are incased in quotations, this is because this signifies them as words. This is called a string. A string is a fancier way of saying words. For example (and we'll be using this later) the string "10" is not the same as the number 10. This is because the string of ten is seen as a word, not a number.

Now this is one of the things that we'll carry with us throughout all of python. But there are ways to string multiple things into one print statement.

Variables and Their Value Types

Now throughout every scripting language, there are variables. Simply put, variables hold the value of whatever you decide to put in them. Now python is what is called an "object oriented language". This will become more important later in this series, but what this means for us now is that we don't have to declare variables, we just make them. For instance, we can give a variable the value of "Hello, World!" and then we could print that variable instead of the words themselves...

Here we gave the variable "greetings" the value of "Hello, World!". Now if we try to print this variable, it will print it's value.

We entered "print greeting" into our python shell and instead of printing the word greetings, it saw that it was a variable and printed it's value. We can also assign numbers to variables and do some basic math...

Here we give the variable "number1" the value of 10, as an integer. Then we say we want the value of number1 plus one, and the result (obviously) is eleven.

Now, you may have noticed that I said "as an integer" when talking about the number1 variable. This is because that there are two types number values in python, integers and floats. Integers are whole numbers. 1, 2, 3, and 4 are all integers, but 1.5 however, is a float. We can also assign floats to variables, just like integers, but when we do math with them, we can use decimals as well...

Here we've made a new variable, number2, and we assigned it a value of 10.0, a float. Then we added 13.37, and it returned a float. I used addition as an example, but as a side note, be very careful with division, python will not return a float unless you divide floats, it will round down and return an integer, this can cause issues if left neglected.

Now, one question you might have is "How do we convert from one type to another?", well, get ready for your answer.

There are ways you can convert one value type to another. First, we'll convert strings to integers...

A Side Note

Be careful when printing multiple values. If you attempt to print a string and an integer in the same print statement, you will get an error in return. This is when we can convert everything to strings in order to print it.

Here, we've reassigned the number1 variable a new value, but it's different than what we've seen before. That is we've converted the string "10" to the actual number 10. We can also convert integers to floats using a similar method...

We can also convert numbers to strings using the str({NUMBER}) method.

The Exercise

That does it for this article! For a small exercise and for some practice, you can make your own print statement, using what you've learned here today. Don't be afraid to experiment and try things out. Once you've gotten a print statement together using what we've learned here, put it in the comments!

Feedback!

Let me know what you think in the comments section! Sorry this article seemed to scattered, it was a bit difficult to group things together.

Thank you for reading!

-Defalt

22 Comments

Excellent tutorial once again! This series is very informational for new users. Good job.

I'm looking forward to this serie, I currently know c/c++ but since most tools are written in python this will be really helpful to me and greatly appreciated, i'm sure it will help many other also! Thanks

Cheers,
Washu

Nice tutorial !! And here's something that can help new python trainers get something useful from the shell:

Can you guess what this command does ?
>>> 16**10/300000/60/60/24

The solution is (as always) 42, but that is not the answer...

What we just did is calculate how many days it would take to crack a 10 chars WPA password, with charset a-f 0-9 (so 16 chars possible) at 300000 keys per second (rough esteem of a couple of high end GPUs can handle that). To sum up,

(charset length)**(password length)/(passwords per second)/60/60/24

will give you the days (in worst case scenario) to crack a password. In this example data is refered to a WPA password similar to this af32b4c60d, but you can change the data and calculate almost anything related password cracking time. It's a good thing to consider before attempting any bruteforce attack, and this one-liner formula can help you see if it's worth it.

Some operations have priority or precedence over others

search for Python operations precedence

i believe is the same as in C

see:

>>>2**10+2"
1026

->expected 4096

a good way is separate sentences within parenthesis

>>>2**(10+2)
4096

it will execute the sentences within the parenthesis and then the next operation on the precedence hierarchy

>>> 2**(10+2)-6
4090
>>> 2**((10+2)-6)
64

for long sentences it would be wise to break it into smaller ones

That's just the mathematical order of operations.

-Defalt

Why is it that 10.0 + 13.37 = 23.36999997 not 23.37?

how come 10.0 + 13.37 = 23.369997 not 23.37?

Well you can use round() to round your floats like

round(10.0 + 13.37)
23.0 (Viola)

Hope that helps.

The_Unknown.

That's due do limitations on the floating point representation of out modern CPUs, which follow the IEEE floating point standard. Search that and you will find precisely why this happens.

HI THE DEFALT ,

First thing, why can't we assign any integer some value that is in string. Like >>>1 = "Something"? (sorry for the silly question)

Next, suppose i assigned different values for different variables, how do i get it to print at the same time?

Hello there Sanat and Welcome to the realm of python scripting.

The thing about your question is that "1" is something "literal" and as you can assign word(s) = word(s) looking at your question you are assigning 1 = word(s) which does not make any sense.

That is because its clear that one means one (1 = 1) why would you want 1 to be something other than one? So it gives a can't assign to literal syntax error.Hope that helps...

The_Unknown.

Hi there

Guess i wasn't thinking straight. I got it though. Thanks.
And what about the second question?

Apologies as I thought that was not a separate question but the part of the same question...

Suppose that I write a code which has:
name = "Xero"

business = "passing by"

place = "school"

To print every variable

print " Hi " + name + " ,I see that you were " + buiness + " the " + place

It'll print
Hi Xero ,I see that you were passing by the school

So there you have it... Hope it answers your question...

The_Unknown.

Oh shit, sorry. I knew that. I actually did it but forgot. Sorry. I just got confused.

Thanks though. Appreciate your help.

No need for curse mate... and Its okai, being human, its our nature to forget stuff. And I would be more than happy to help anyone out in the future and be helped when needed. Cheers~

The_Unknown.

Hey - obv totally new at this. So, just to be clear, in order for me to start following these Python tutorials - and I'm supposing most others, as well - I will need (since I have a Mac and that's the only comp I have access too and I'm jobless at the moment so I can't buy a pc) to install Linux Kali on a vm. Would that be correct? I've done this before with Windows, but it slowed everything down, so I got rid of it, and then replaced my HDD with an SSD. For some reason I am still constantly running out of disk space - it's rather infuriating, but I can do a total format and reinstall, if necessary. But, I just want to be sure that, if I am correct in my assumption of installing Linux Kali on VM, would that be better than repartitioning the entire drive and having both my Mac and then Linux Kali operating systems present at all times?

You can use python in any common OS (Linux, OSX and Windows). I'm not sure if this series will run anything that is OS-specific though.

And...as usual, I jumped the gun. So I understand it's best to run these programs in a Linux OS. So I also know I can make a bootable USB for Kali. So, Not being familiar with Linux (but I'm not a moron, at least. I'll get it. Ha), I'm a little confused about Kali as a work environment (God, I feel like I type like such a douche ever since I started "career-ing" in corporate environments). So, is Kali Linux like a particular version of the Linux OS, like OSX Lion or Windows 7? Or is it that there's the separate Linux OS, and Kali is a program I would install within that? If the latter is the case, would you recommend a repartitioning of my hard drive to include a Linux OS, where I would install Kali? Or is using a bootable USB with Kali on my Mac OS good enough. I'd like to become familiar with Linux either way, so I'm just looking - not for the easiest way of getting started - but for the best way to do this so that I will learn as much as I can. And I'm sorry - I know damn well these are dumb questions for the more advanced on here, but I WILL get it and soon my dumb questions will cease :) I very much appreciate any assistance.

Kali Linux is a Linux distribution, or distro. There are others such as Ubuntu, Fedora, etc. They are all basically a bunch of software that is bundled together with the Linux kernel, which is the core of Linux. The kernel has no graphical interface and other stuff that is needed for proper user experience, it's just the skeleton of the OS. Each distro will have its own selection of software that will run with the kernel, such as different graphical interfaces and so on.

If you want to learn and practice using Linux, I recommend you use a VM. If you feel it's too slow, use a USB then. No need to install on a partition on the beginning.

We are all beginners at some point , So I wouldn't mind any kind of question. If you want (at the moment) to only learn python then I suggest you install a Python IDE for windows which'll be more than enough for you to execute your python codes on Windows, But if you want to use Linux then (excluding the VM because it makes ye're computer laggy) use a Live USB and also if you want to learn Linux then here are the tutorials of OTW to teach you the basics of Linux

(https://null-byte.wonderhowto.com/how-to/hack-like-pro-linux-basics-for-aspiring-hacker-part-1-getting-started-0147121/)

The_Unknown.

Excellent tutorial for a very beginner in programming and python . There are 2 things i just wanna say:

  1. i got both python 2 and 3 and the commands u gave here are for python 2 .In 3 they are slightly different .So , should i continue with python 2 ( i mean , will i be able to create all the tools with p2 which can be made via p3?
  2. Isn't the name "THE DEFALT " and icon from game watch dogs minor antagonist hacker THE DEFALT?

Share Your Thoughts

  • Hot
  • Latest