Table of contents
بِسْمِ اللَّـهِ الرَّحْمَـٰنِ الرَّحِيمِ
Content
Lists.
Conditional statements:
if
elif
else
Loops:
for
while
Practical Program.
1. Python Lists
- Lists in python are used to store multiple items in a single variable For Example
vulnerabilities = ["SQLi", "XSS", "CSRF"]
print(vulnerabilities) # ['SQLi', 'XSS', 'CSRF']
List Length:
- To print list Length use the
len()
function:
- To print list Length use the
vulnerabilities = ["SQLi", "XSS", "CSRF"]
print(len(vulnerabilities)) # 3
- Lists can contains various data types at the same list for example:
vulnerabilities = ["SQLi", "XSS", "CSRF"]
severity = [9.8 , 7.3 , 6.2]
found = [True , False , False]
# Mixed Data Type list Example:
vuln_sev_valid = ["SQLi", 9.8 , True]
2. Conditional statements
- if Statement:
- The
if
statement executes a block of code if a specified condition isTrue
.
#Age Checker program
age = 20
if age >= 18:
print("You are an adult.")
- elif Statement:
- The
elif
(short for else if) statement checks another condition if the previousif
condition isFalse
.
#Age Checker program
age = 15
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
- else Statement:
- The
else
statement executes a block of code if all previous conditions areFalse
.
#Full age Checker program
age = 10
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
3. Loops
- for Loop:
- The
for
loop iterates over a sequence (like a list, tuple, or string) and executes a block of code for each item in the sequence.
bugs = ["SQLi","XSS","CSRF"]
for bug in bugs:
print(bug + " Vulnerability")
- You can also use the
range()
function to iterate over a sequence of numbers.
for i in range(5):
print(i)
- while loop:
- The
while
loop executes a block of code as long as a specified condition isTrue
.
count = 0
while count < 5:
print(count)
count += 1
4. Practical Program
- Let’s Create a Practical Program that contains all section:
# List of vulnerabilities with their severity levels
vulnerabilities = ["SQLi", "XSS", "CSRF"]
severity = [9.8, 7.3, 6.2]
found = [True, False, False]
# Print all vulnerabilities
print("Vulnerabilities List:")
for vuln in vulnerabilities:
print(vuln)
# Print the length of vulnerabilities
print("\\nNumber of vulnerabilities found:", len(vulnerabilities))
# Check the severity level of vulnerabilities using for loop
print("\\nChecking vulnerabilities and their severity levels:")
for i in range(len(vulnerabilities)):
print(f"\\nChecking vulnerability: {vulnerabilities[i]}")
# Check the severity
if severity[i] > 9.0:
print("Severity Level: Critical")
elif severity[i] > 7.0:
print("Severity Level: High")
else:
print("Severity Level: Medium")
# Using a while loop to print the vulnerabilities if that found
print("\\nList of found vulnerabilities:")
index = 0
while index < len(vulnerabilities):
if found[index]:
print(f"Vulnerability found: {vulnerabilities[index]} with severity {severity[index]}")
index += 1
- Try to execute the program and explain the steps to you and try write it, The output is:
Vulnerabilities List:
SQLi
XSS
CSRF
Number of vulnerabilities found: 3
Checking vulnerabilities and their severity levels:
Checking vulnerability: SQLi
Severity Level: Critical
Checking vulnerability: XSS
Severity Level: High
Checking vulnerability: CSRF
Severity Level: Medium
List of found vulnerabilities:
Vulnerability found: SQLi with severity 9.8
- Thanks For completing, Follow me here.