PYTHON OVERVIEW
PYTHON OVERVIEW
Why Python? Simple
Powerful
Usually preinstalled
Less Syntax
Open Source
Plethora of Pen Test Tools already created
Interpreted?• Python is interpreted, which means that python code is
translated and executed by an interpreter one statement at a time.
• This means you can run commands/code on the command prompt…
• Through a command prompt with a text file…
• Using an IDE
What Does It Look Like?
Good programming proclamationsUsing comments to denote programs or sections of
code◦ A comment beginning with # is called a single-line comment because it
terminates at the end of the current line.◦ You also may use a multi-line comment—possibly containing many
lines—begins with ’’’ and ends with ’’’.
You use blank lines, space characters and tab characters (i.e., “tabs”) to make programs easier to read.◦ Together, these characters are known as white space.◦ White-space characters are USUALLY ignored by the interpreter.
◦ Python has its own rules with white space
Printing a Line of Text
• Code: print(“ “) Python 3• Code: print “ “ Python 2
• Anything placed inside the quotes will be printed on the screen upon execution of the print command.
Output
Obtaining input How to input values from the user
› Code: variable = input(“Prompt “) Python 3› Code: variable = raw_input(“Prompt”) Python 2
› variable – stores the input from the user.› input – function to extract user input from the command prompt.› “Prompt” – a string to inform the user of the type of value to
enter.
Ex. Inputting a numerical value
testGrade1 = input(”Enter test grade 1: “)
testGrade *= 100 print(”Test Grade: “, testGrade, “%”)Good Programming: Organize outputs, good prompts
Data Type Conversion•Can control how variables are interpreted within the program:
• Evaluation• Code: eval(string)
• Ex. eval(“51” + “52”)• eval( str1 ** str2)• test = eval(input(“Enter a num”))
Conditional Operators•Operator Meaning
• == equal to• < less than• <= less than or equals to• > greater than• >= greater than or equal to• != not equal to
• Common Mistake: Confusing = with ==
Decision Structures• Code:
Creating a decision structure: if (expression):statement
• To execute more than one statement in a block they must be indented equally:if (score > 90): grade = 'A' print(“Good Job!\n”)print(“Not part of the conditions execution”)
• Good Programming: Commenting conditional blocks• Common Mistakes: Forgetting equal indent, forgetting :, and forgetting space
after if• Good Practices: Use a tab not a space (harder to line up and troubleshoot)
Space Indent
Nested Conditionals if (condition):
Statement elif (condition):
Statement elif (condition):
StatementStatement
else: Statement
• Common Errors: Not Lining up Else with its
preceding If.• Question: Why is a trailing else good
programming?
Nested if/else if Example
Question: What zodiac sign is it for the current year?
Logical OperatorsCode:
• Using logical operators: • if (condition or condition)
Statement
• if (condition and condition) Statement
While Loops• A While Loop is a loop that executes 0 or more times before
terminating.• Pre-conditional loop
Code: Creating a While Loop:
while (condition statement):
statement1 statement2
Debugging Techniques:
Setup a counter to keep track of the number of times a loop runs Set up a counter to count if an event is occurring or the number
of times it occurs Output values each time a loop executes
Examplenum = eval(input(“Enter a number less than 10:”))
while (num >= 10):print(“Invalid Entry!”)num = eval(input(“Enter a number less than 10:”))
For Loops• For loops are a pre-test loop• In order to utilize a for loop you need 3 things:
1. Needs to initialize a counter2. Must test the counter variable (less than)3. It must update the counter variable
• Code:for initialization in range(start, stop,
increment): statement1
statement2
Example
for i in range(0, 5, 1): print(“Hello”)
Step 1: Perform the initialization expression
Step 2: Evaluate the test expressions
Step 3: Execute the body of the loop
Step 4: Perform the update
Assign 0 to i
i < 5 Update iPrint
“Hello”
True
False
Function Definition•Definition includes:
• return value: the value the function returns to the part of the program that called it
•name: name of the function. Function names follow same rules as variables
•parameter list: variables containing values passed to the function
•body: statements that perform the function’s task
Calling a Function •Functions just like variables need to be called in order to be invoked
• Code:Calling a Function:
functionName (Parameters)
Write and Calling Functions Example
Modules• Some special functions have not been innately included into the
interpreter to speed up the loading process, these packages are known as modules.
• Code: import moduleName
• Ex.• import math• import random• Import os
• OS module• os.getcwd()• os.chdir(path)