Python for hacking
Build
in functions:
Print(x, sep=’y)
- Prints x objects separated by
y
Input(s) - prints s and waits for an input that will be
returned
Len(x) - returns the length of x (s, L or D)
Min(L) - returns the
minimum value in L
Max(L) - returns the maximum value in L
Sum(L) - returns the sum of the values in L
Range(n1,n2,n) – returns a sequence of numbers from n 1 to
n2 steps of n\
Abs(n) -returns the absolute value of n
Round (n1,n) -
returns the n 1 number rounded to n
digits
Type(X) -returns the type of x (string, float, list, dict…)
Str(x) - converts x
to string
List(X) converts x to a list
Int(x) - converts x
to a integer number
Float(X) - converts x to a float number
Help(s) - print help
about x
Map (function, L) - Applies function to values in L
Conditional
statement:
If < condition>:
<code>
Else if <condition>:
<code>
…
Else:
<code>
If<value> in <list>:
Data
Validation:
Try:
<code>
Expect <error>:
<code>
Else:
<code>
Working
with files and folders:
Import os
Os.getcwd()
Os.makedirs(<path>)
Os.listdir(<path>)
Loops:
While <condition>:
<code>
For<variable> in <list>:
<code>
For<variable> in
Range(start,stop,step):
<code>
For key, value in
Dict.items():
<code>
Loops
control statements:
Break - finishes loop execution
Continue - jumps to next iteration
Pass - does nothing
Running
external programs:
Import os
Os.system(<command>)
Function:
def function(<params>):
<code>
Return<data>
Modules:
Import module
Module.function()
From module import *
Function()
Reading
and writing files:
F= open(<path>, ‘r’)
f.read(<size>)
f.readline(<size>)
f.close()
f = open(<path>,’r’)
for line in f:
<code>
f.cloase
f=open(<path>,’w’)
f.write(<str>)
f.close()
Variables
+ Strings:
#Hello world
Print(“HELLO world”)
#Hello world with a variable
Msg = “Hello world!”
Print(msg)
#Concatenation (combining strings)
First_name = “hack”
Last_name =” Einstein”
Full_name = first_name + “” + last_name
Print(full_name)
Dictionaries:
#A simple dictionary
Person = { “age”: “21”, “gender”: “female”}
# Accessing a value
Print (“The person’s age is “+ person[“age”])
#Adding a new key-value pair
Person[“height”] = 170
#Looping through all key-value pairs
Ages = { “Stacy”; 20, “ Michelle”; 21}
For name, age in ages.Items():
Print
(name + ”is” + str(age))
#Looping through all keys
Ages = {“Stacy”: 20, “ Michelle”: 21}
For name in ages.key():
Print
(name + “is a name”)
#Looping through all the values
Ages = {“Stacy”: 20, “Michelle”: 21}
For age in ages.values();
Print(str(num) + “is my age”)
Lists:
#Make a list
Colors = [“red”, “green”, “blue”]
#Get the first item in a list
First_color = colors[0]
#Get the last item in a list
Last_color = colors[-1]
#Looping through a list
For color in colors:
Print(color)
#Adding items to a list
Colors = []
Colors.append(“red”)
Colors.append[“green”)
Colors.append(“blue”)
#Making numerical lists
Flag =[]
For x in range (1,12):
Flag.append(x**2):
#List comprehensions
Flag = [x**2 for x in range(1, 12)]
#Slicing a list
Hackers = [“janathan”, “ loryn”, “kim”]
First_two = hackers [:2]
#Copying a oist
Copy_of_hackers = hackers[:]
User
Input:
#Prompting for a value
Name = input (“what’s your name?”)
Print (“Hello, “ +name+ “!”)
#Prompting for numerical input
Age = input (“How old are you?”}
Age = int (age)
Pi = input (“What is the value of pi?”)
Pi = float(pi)
IF
Statements:
# Conditional tests
Equals h
== 10
Not equal h
!=10
Greater than h
> 10
Or equal to h>=10
Less than h
< 10
Or equal to h
<=10
#Conditional test with lists
“red” in colors
“green” not in colors
#Assigning Boolean values
Is_active = True
Can_enter = False
#A simple if test
If age >= 21:
Print (“you
can have marriage!”)
#if-elif-else statements
If age< 13:
Person
= “child”
Elif age< 20:
Person
= “teenager”
Else:
Person
= “adult”
Tuples:
#Making a tuple
Couple = [“bonnie”, “ clyde”]
Comments
Post a Comment