Python is an incredible tool for Pentesters. From creating scripts and modifying exploits to creating our own security tools, Python is an easy-to-learn (but difficult-to-master) language.
Let’s start our daily #FromZeroToHacker challenge.
Table of contents |
Introduction |
What have I learnt today? |
Stats |
Resources |
Introduction to Python
Personally, I have been working with Python (especially, with Django framework) for years and I love it. Its (apparent) simplicity, the lack of semi-colons, parentheses and, in general, the way Python approach to things.
But I needed a refresher.
This lesson is aimed at people that haven’t touched Python (or any programming language), so it is about basic stuff:
- Variables
- Loops
- Functions
- Data Structures
- If statements
- Files
Most of you already know how to do this, most of you, even know how to do this in Python. But even if you are new to the programming world, this is pretty easy to grasp and despite any explanation I could write about the code, it is better to read the code.
What have I learnt today?
Python basics
Printing text
# This is an example comment print("Hello World")
The first line is a comment. Use the pound symbol then any text you want: It won’t be processed. Use this to take notes for the future, to separate code or to summarize a long function.
Mathematical Operators
print(2 + 3) print(34 - 19) print(3 * 5) print(15 / 2) print(15 % 2) print(5**2)
Variables
Think about a variable like a box where you can put something. Anything:
age = 30 print(age) age = age + 5 print(age)
We store any information we want in a variable. The type of information determines the data type of the variable:
- String: A combination of characters, such as letters, symbols and numbers.
- Integer: Whole numbers without decimals.
- Float: Numbers with decimal points.
- Boolean: True or False options.
- List: A series of different data types stored in a collection.
Logical and Boolean operations
We can do comparisons:
if x < 5: print('x is less than 5') if x <= 5: print('x is less than or equal to 5')
We can compare two (or more) conditions:
x = 5 if x >= 5 and x <= 10: print('x is between 5 and 10') if x == 3 or x > 20: print('x is equal to 3 or bigger than 20') if x is not 6: print('x is not 6')
Loops
we can iterate over a code until a condition is met. We can use while
and for
:
i = 1 while i <= 10: print(i) i = i + 1
websites = ["facebook.com", "google.com", "amazon.com"] for site in websites: print(site) for i in range(10): print(i)
Functions
When a piece of code is used 2 or more times, it is better to create a function and call it instead of repeating the same code again and again:
def check_if_can_drive(age): if age >= 18: print('You can drive :)') else: print("You can't drive yet :(") anna_age = 20 bob_age = 17 claire_age = 18 check_if_can_drive(anna_age) check_if_can_drive(bob_age) check_if_can_drive(claire_age)
Files
You can do what you want with files: Create a new one, modify or read an existing one.
# Read a file f = open("readme.txt", "r") print(f.read()) # Append (add new text) text to a file f = open("diary.txt", "a") f.write("2023-10-20: Today was a good day!") f.close() # Create a new file f = open("notes.txt", "w") f.write("Do the dishes") f.close()
Imports
We can import libraries, a collection of files that contains functions like we just saw (well, a bit more complex…). Importing libraries gives us extra functionality, like if we wrote them:
import datetime current_time = datetime.datetime.now() print(current_time)
Summary
Things we learned today:
- Variables
- Loops
- Functions
- Data Structures
- If statements
- Files
Stats
From 71.159th to 70.015th.
Here is also the Skill Matrix:
Resources
Random room
Other resources
Learnpython.org
Python learning resources
w3schools Python tutorials
Video: Learn Python – Full Course for Beginners [Tutorial from freeCodeCamp.org]