Intro to Python
Example of Python script
#!/bin/python3- first lineUse
#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
defkeyworddef 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.
forloop: iterate over a sequence and execute a set of statements for each item in a list, tuple, string, set, dictionarywhileloop: 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()

Listen to the
5555port withncand launch thes2.pyPython 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.


➡️ 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