python basics

22
PYTHON OVERVIEW

Upload: primeteacher32

Post on 09-Apr-2017

297 views

Category:

Career


0 download

TRANSCRIPT

Page 1: Python Basics

PYTHON OVERVIEW

Page 2: Python Basics

Why Python? Simple

Powerful

Usually preinstalled

Less Syntax

Open Source

Plethora of Pen Test Tools already created

Page 3: Python Basics

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

Page 4: Python Basics

What Does It Look Like?

Page 5: Python Basics

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

Page 6: Python Basics

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.

Page 7: Python Basics

Output

Page 8: Python Basics

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

Page 9: Python Basics

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”))

Page 10: Python Basics

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 ==

Page 11: Python Basics

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

Page 12: Python Basics

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?

Page 13: Python Basics

Nested if/else if Example

Question: What zodiac sign is it for the current year?

Page 14: Python Basics

Logical OperatorsCode:

• Using logical operators: • if (condition or condition)

Statement

• if (condition and condition) Statement

Page 15: Python Basics

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

Page 16: Python Basics

Examplenum = eval(input(“Enter a number less than 10:”))

while (num >= 10):print(“Invalid Entry!”)num = eval(input(“Enter a number less than 10:”))

Page 17: Python Basics

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

Page 18: Python Basics

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

Page 19: Python Basics

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

Page 20: Python Basics

Calling a Function •Functions just like variables need to be called in order to be invoked

• Code:Calling a Function:

functionName (Parameters)

Page 21: Python Basics

Write and Calling Functions Example

Page 22: Python Basics

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)