Intro to Python

๐Ÿ”— Python Docs

๐Ÿ”— The Python Standard Library

๐Ÿ”— Python Cheatsheet

๐Ÿ”— Python Tutorial - W3Schools

๐Ÿ”— VsCode Linux setup

  • Example of Python script

    • #!/bin/python3 - first line

    • Use # for comments




Variables & Methods

โžก๏ธ A variable is a dynamic container for storing data values.

โžก๏ธ A method (or function) is a block of code which runs when called and performs specific operations. It can have return values, perform actions, accept parameters, etc.


โžก๏ธ A function is a reusable block of code that performs a specific task.

  • defined with the def keyword

    • def function_name():

  • accepts parameters

โ— Indentation is very important in Python, since it's used to indicate a block of code.


โžก๏ธ Boolean expressions are expressions that evaluate to either True or False, used in conditional statements and logical operations.

โžก๏ธ Relational operators are used to compare values and create boolean expressions.


โžก๏ธ Conditional statements allow the execution of different actions based on certain conditions. Logical math conditions can be used in if statements and loops.


โžก๏ธ Lists are used to store multiple items in a single variable.

  • list = ["item1", "item2", "item3"]

  • items are ordered, changeable and indexed (first item index starts at [0])


โžก๏ธ Tuples are used to store multiple items in a single variable, similar to a list but immutable (cannot be modified once created).

  • tuple = ("item1", "item2", "item3")

  • items are ordered, indexed and unchangeable


โžก๏ธ Looping allows to repeat a block of code multiple times, iterating over a sequence and performing repetitive tasks.

  • for loop: iterate over a sequence and execute a set of statements for each item in a list, tuple, string, set, dictionary

  • while loop: execute a set of statements as long as a condition is true



โžก๏ธ Dictionaries are used to store, retrieve and manipulate data based on key:value pair values.

  • dictionary = {"item1":value, "item2":value, "item3":value}


โžก๏ธ Modules are like code libraries, containing a set of functions to include in the application.

  • reside in external Python files/libraries

  • import <module>


โžก๏ธ Sockets are used to send messages across a network, enabling programs to establish connections, send and receive data over various network protocols.

Primary socket module methods and functions are:

  • socket()

  • .bind()

  • .listen()

  • .accept()

  • .connect()

  • .connect_ex()

  • .send()

  • .recv()

  • .close()

TCP Server
  • Listen to the 5555 port with nc and launch the s2.py Python script.

    • the socket will connect to the listening port


e.g. Port scanner

  • Port scanning of an IP address

This port scanner is a proof-of-concept not optimized script, that checks for open ports on a specified target IP within a given range.

  • (ChatGPT) Enhanced script

  • Try to scan an internal LAN IP


โžก๏ธ input() function is used to take input from the user via the console and return it as a string (by default). Proper validation and error handling is necessary for specific data types and invalid inputs.


โžก๏ธ To read from a file, open it in read mode using open("filename", "r"). Then, use read(), readline(), or readlines() to access its contents.

โžก๏ธ To write to a file, open it in write mode using open("filename", "w"). Then, use the write() method to add content to the file.

โžก๏ธ To append content to an existing file, open it in append mode using open("filename", "a"). Then, use the write() method to add content.

read
write & append

โžก๏ธ A class is a blueprint for creating objects, defining their attributes and methods. It serves as a template for creating instances with similar characteristics and behaviors.

โžก๏ธ An object is an instance of a class, created using the class as a blueprint. Each object has its own attributes and can use the class's methods. You create objects by calling the class like a function.


Last updated

Was this helpful?