Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Understanding Network Hacks
< Prev   CONTENTS   Next >

3.4 Data Structures

Data can be organized in several structures or – easier said – can be saved in different containers. A variable can only store exactly one value regardless if it is a number, string or a complex object.

var1 = "hello world" var2 = 42

If you like to save more than one value in a fixed order you usually use a list.

buy = ['bread', 'milk', 'cookies']

Python let you store different types together in one list.

list = ['mooh', 3, 'test', 7]

Append adds data to the end of the list, del deletes it and the access is controlled by the index number of a value starting by zero.

print list[2] del list[2]

list.append('maeh')

The number of elements in a list can be queried with len(). If you need an immutable list you otherwise use a tupel.

tupel = ('mooh', 3, 'test', 7)

Dictionaries, store key-value-pairs in an unordered fashion. A key can be of whatever data type you like, but usually strings are used. You could even mix different data types, but the author advises sticking by one and preferring strings.

phonebook = {'donald': 12345,

'roland': 34223,

'peter parker': 77742}

The access and assignment occurs over the use of the key, deletion is still handled by del.

print phonebook['donald']

del phonebook['peter parker'] phonebook['pippi langstrumpf'] = 84109

A set is like a dictionary that only consist of keys. Therefore its commonly used to avoid duplicate data.

set = set((1, 2, 3))

3.5 Functions

It's nice to know how you can save a lot a data, but what about manipulating it? Most of the time the answer is: through functions. First we discuss common functions integrated into Python and afterwards how you can write your own. The easiest and most used function for sure is print.

print "hello sunshine"

If you want to print something different than a string you must first of all convert the data type to a string. This can be done with the function str() or by using so called format strings.

book = "neuromancer" times = 2

print "i have read %s only %d times by now" % (book, times)

The format strings define what data type should be outputted and converts it on the fly. %s stands for string, %d for digit (integer) and %f for float. If you need more formats please have a look at the official Python documentation doc.python.org.

Another often used function is open to open a file.

file = open("test.txt")

file.writeline("a lot of important information") file.close()

If you combine both functions you can easily dump the contents of a file to the screen.

file = open("test.txt") print file.read() file.close()

Especially scanningand fuzzing techniques usually use another function range, which will generate a list of numbers by defining a start and if you like also a stop and a step number.

range(23, 42)

A complete overview of all integrated functions and their usage is far beyond the scope of this book, but you can find very good documentation by pointing your browser at doc.python.org.

Last but not least, let us write a function of our own.

def greet(name):

print "Hello " + name greet('Lucy')

The keyword def starts a new function definition, afterwards you will find optional parameters in round parentheses. Parameters can be named or unnamed like in the example above and they can have default values.

def add(a=1, b=1): return a + b

The function body must be indented and follows the function header. The enforced indention is a specialty of Python. Where other programming languages use curly brackets or keywords like begin and end, Python uses indentation to indicate a block. What every programmer nevertheless should practice to optimize code readability is used for structuring. The last unknown keyword from the example return serves to return a value to the code that has called the function. Without an explicit return the function would return the value None.

print add(173, 91)

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel