Table of Contents
If you think that a hacker doesn’t need programming, you’re deeply mistaken! Yes, you can download Kali Linux and use ready-made tools, copy-paste code from forums, and blindly download scripts from GitHub. But your skill ceiling will remain low until you learn how to write and understand code. In this article, I will try to teach you the basics of programming in an accessible way. We’re starting from absolute zero!
This version keeps the informal, approachable tone of the original text, which fits well with the audience itβs addressing.
If you have a modern Linux distribution on your computer, it already comes with Python 3, and writing your first programs will be easy in IDLE β a simple code editor that comes with Python. In Ubuntu, to install it, just type in the terminal:
sudo apt-get install idle3
BashOn Windows, when installing Python, make sure to check the ‘Add to Path’ box on the first screen of the installer so that you can run python3 from the command line anywhere.
Once you’ve launched IDLE, go to Options β Configure IDLE, then switch to the General tab and check the ‘Open Edit Window’ box. Click OK and restart IDLE. Now you can write programs, save them, and run them by pressing F5. Ready to go?
Variables
In any programming language, there is something called variables. It’s like in school algebra: here’s a variable a = 1, and here’s a variable b = 2. So, these are abstract things that hold a value, which can change β for example, when you write an equal sign after the variable and assign it a new value.
a = 2
a = a + 2
print(a)
PythonWell, you’ve already understood that print(a) is a command that prints the current value of the variable on the screen. After the equal sign, you wrote the variable again + 2, meaning that initially the variable had the value 2, and then 2 was added to it. On the screen, 4 proudly appears. Congratulations, you’ve added two and two!
And what if you don’t know in advance which numbers need to be added? In that case, you’d first have to ask the user to input them in the console and press Enter. Let’s do that:
a = input('Enter how many liters of beer you have: ')
b = input('How many liters of beer did your friend bring: ')
c = int(a) + int(b)
print('Together, you have: ' + str(c) + ' liters of beer')
PythonInside the parentheses of input, you write an explanation for the user about what exactly they are asked to enter. But here’s the catch: by default, everything entered through input is considered a string, not a number. So, before adding the number of liters of beer, you first need to convert the entered strings into numbers using the int() function.
Info
The word ‘function’ should be familiar to you from mathematics. Inside the parentheses, we write what it takes as input (the argument), and the output will be the result. Python first replaces the variable with its current value (for example, int(a) becomes, say, int(‘5’)), and then the function with the result of its execution, which is 5. Sometimes, a function doesn’t return anything, it just does something. For example, print() only prints the argument.
Okay, I’ve converted the strings to numbers and stored them in variable c, but what’s all that mess inside the parentheses of print? Here, strings (which are always inside quotes) are being concatenated, explaining what exactly is being printed on the screen, and the result of the addition is passed to the print() function.
To make the strings concatenate smoothly with the variable c, which contains a number, you need to convert it into a string using the str() function β just like we converted strings into numbers, but the other way around.
In general, there are many types of variables, but the main point is that in order to perform operations with variables, you first need to convert them into the same type β either string, numeric, or some other type. If you don’t do this, Python will concatenate strings instead of adding numbers, and entering 2 and 3 liters of beer will give you not 5, but 23. It would be nice if it worked that way in reality!
Hereβs another example, calculating how much more beer you need to drink based on the average life expectancy in morocco:
a = input('Enter how old you are: ')
b = 73 - int(a)
print('You have approximately: ' + str(b) + " years left")
PythonHere, we call the input() function to get a value, subtract it from 73 (the average life expectancy of a Russian), remembering to convert the string into a number, and then print the result, converting the number back into a string and concatenating it with other strings.
So, youβve learned about integer and string variables, and that these types can be converted into each other using the int() and str() functions. Also, now you know how to get variables from the user using the input(‘Enter something’) function and print the results using the print() function.
Conditions
At the heart of any program are conditions. Depending on whether they are met or not, the program can follow one path or another. Imagine you’re driving a car and looking at the clock: if itβs already ten in the evening, you turn around and head home; if not, you can visit a friend. A program works in the same way: it checks a value, and based on the condition, it takes one route or another, executing the corresponding piece of code.
beer = input('Enter Yes if there is beer, or No if there is no beer: ')
if beer.lower() == 'yes':
result = 'You will hack the Pentagon'
else:
result = 'You will break your brain'
print(result)
PythonIn English, if
means ‘if’, and else
means ‘else’ or ‘otherwise’. In the line after if
, thereβs a condition that we check. If itβs true, the first block of code is executed (itβs indented with four spaces at the beginning). If itβs false, then the block of code after else:
is executed.
Info
Code blocks in Python are separated by indentation. The indentation can actually be anything, for example, some people prefer using the Tab key instead of four spaces. The important thing is not to mix different types of indentation within the same program. If you start using four spaces, stick to it throughout the program, otherwise Python will complain and shame you.
Another important point here is the equality sign in the condition. It is written as double ‘equals’ (==), and this distinguishes it from assignment, which uses a single ‘equals’ (=).
The lower()
function, before comparing the condition, converts all letters in the string to lowercase, because a careless user might enter the word ‘YES’ with Caps Lock on, and this needs to be accounted for in advance.
Actually, lower()
is not just a function, but a method of the string
class. Thatβs why it is called using a dot after the variable that contains the string. We will talk about classes and methods some other time, but for now, just remember that some functions are called this way.
Letβs try creating a condition to check a username and password using the ‘AND’ operator, which is written as and
. Itβs used to check that both conditions are true at the same time.
myname = input('Enter your username: ')
mypass = input('Enter your password: ')
if myname == 'xap' and mypass == 'superpassword123':
result = 'Welcome, oh great hacker!'
else:
result = 'Who are you, goodbye...'
print(result)
PythonInfo
An operator in Python is a symbol that performs an operation on one or more variables or values: arithmetic (‘plus’, ‘minus’, ‘equals’, etc.), comparisons (double ‘equals’, ‘greater’, ‘less than’, and others), assignment (‘equals’ and a few others), logical operators (and
, or
, not
), membership operators (in
, not
in
), and identity operators (is
, is
not). There are also bitwise operators for comparing binary numbers.
Letβs create an even more complex condition using the or
operator, which translates to ‘OR’.
myname = input('Enter your username: ')
mypass = input('Enter your password: ')
if (myname == 'ivan' and mypass == 'superpassword123') or (myname == 'marina' and mypass == 'marinka93'):
result = 'Hello, ' + myname + '. Welcome!'
else:
result = 'Who are you, goodbye...'
print(result)
PythonBrackets are used here β Python doesn’t require brackets for simple conditions, but for complex ones, they are used to explicitly define the order of operations. The program welcomes only two users, ivan or marina. First, it checks whether the username and password match Ivan’s, and then after the or
operator, it checks the same for Marina.
When you need to check not one, but two or three conditions at once, you can enclose each of them in parentheses and place or
or and
operators between them. In the case of or
, the overall condition is met if at least one of the conditions inside it is true. In the case of and
, for the overall condition to be true, both conditions inside it must be true.
Here’s another example, where elif
is used, which means something like ELSE-IF. This is used to define multiple blocks of commands: if one condition is not met, the next one is checked with elif
, and so on.
v = int(input('Enter your age: '))
if v < 18:
print('Hello, young hacker')
elif v < 30:
print('Hello, old school')
elif v < 65:
print('Decided to switch from assembly to Python?')
elif v < 100:
print('In retirement, itβs the best time to code')
elif v < 100000:
print('The clan of immortals welcomes you!')
PythonThe conditions can include various comparison operators:
a == 9 (a is equal to 9)
a != 7 (a is not equal to 7)
a > 5 (a is greater than 5)
a < 5 (a is less than 5) a >= 3 (a is greater than or equal to 3)
a <= 8 (a is less than or equal to 8)
not completed yet