Day 064 #FromZeroToHacker – Python for begginers

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.
Python basics: Data types

Logical and Boolean operations

We can do comparisons:

if&nbsp;x&nbsp;<&nbsp;5:  
&nbsp;&nbsp;print('x is less than 5')  
&nbsp;&nbsp;  
if&nbsp;x&nbsp;<=&nbsp;5:  
&nbsp;&nbsp;print('x is less than or equal to 5')

We can compare two (or more) conditions:

x&nbsp;=&nbsp;5  

if&nbsp;x&nbsp;>=&nbsp;5&nbsp;and&nbsp;x&nbsp;<=&nbsp;10:  
&nbsp;&nbsp;print('x is between 5 and 10')  

if&nbsp;x&nbsp;==&nbsp;3&nbsp;or&nbsp;x&nbsp;>&nbsp;20:  
&nbsp;&nbsp;print('x is equal to 3 or bigger than 20')  
&nbsp;&nbsp;  
if&nbsp;x&nbsp;is&nbsp;not&nbsp;6:  
&nbsp;&nbsp;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&nbsp;=&nbsp;["facebook.com",&nbsp;"google.com",&nbsp;"amazon.com"]  
for&nbsp;site&nbsp;in&nbsp;websites:  
&nbsp; &nbsp; &nbsp;print(site)  
&nbsp;&nbsp;  
for&nbsp;i&nbsp;in&nbsp;range(10):  
&nbsp;&nbsp;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&nbsp;check_if_can_drive(age):  
&nbsp;&nbsp;if&nbsp;age&nbsp;>=&nbsp;18:  
&nbsp; &nbsp;&nbsp;print('You can drive :)')  
&nbsp;&nbsp;else:  
&nbsp; &nbsp;&nbsp;print("You can't drive yet :(")  
&nbsp; &nbsp;&nbsp;  

anna_age&nbsp;=&nbsp;20  
bob_age&nbsp;=&nbsp;17  
claire_age&nbsp;=&nbsp;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&nbsp;=&nbsp;open("readme.txt",&nbsp;"r")  
print(f.read())  

# Append (add new text) text to a file  
f&nbsp;=&nbsp;open("diary.txt",&nbsp;"a")  
f.write("2023-10-20: Today was a good day!")  
f.close()  

# Create a new file  
f&nbsp;=&nbsp;open("notes.txt",&nbsp;"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:

Skills Matrix

Resources

Random room

TryHackMe: Python Basics

Other resources

Learnpython.org
Python learning resources
w3schools Python tutorials
Video: Learn Python – Full Course for Beginners [Tutorial from freeCodeCamp.org]