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

Chapter 3 Python Basics

Abstract Python is a dynamic scripting language with the aim to be easy to learn and readable. Its name suffers from the English comedy group Monty Python therefore its obvious that programming in python should be fun!

3.1 Every Start Is Simple

To show that those statements above aren't only empty phrases let's start the interactive Python shell by executing python in a terminal or console of your choice. Now you should have a waiting input prompt that will immediately execute all Python commands you enter so lets face it!

>>> ska = 42

>>> print "The answer to live, the universe and everything is " + str(ska)

May the author not get doomed for breaking with the holy “hello world” example.

This two lines show a lot of properties of programming in Python.

The statement ska = 42 defines a variable ska and gives it the value of 42. 42 is a number and because a computer is somewhat of a big, wicked calculator that knows nothing but numbers there are different kinds (see Sect. 3.3). For the beginning it's only important to know that a number is something different for Python than strings which is declared between two quotation marks or single ticks.

The function print displays the text that it receives as parameter onto the screen and the function str previously converts the number 42 into a string, because you cannot add to different data types. That's true for numbers, strings and objects. Different number types can operate on each other and are internally converted to the most exact kind of number.

The next example demonstrates the possibility to write short, but still highly readable code in Python. Try to guess what the following lines will do:

>>> for line in file("test.txt"):

... words = line.split(" ")

... print " ".join(reversed(words))

If you guessed that this will read the file test.txt line by line, splits each line into words and writes them in reverse order onto the screen than you are right. Try this with a language like Java or C!

Additionally, the above example shows some properties of Python like enforced code indention to define blocks, which also enhances the readability of the code.

It should be mentioned that this little introduction doesn't claim to be complete or make you a master of Python it should just teach you enough to be able to understand the source examples in this book. If you would like to learn more about Python the author can recommends the book Python 3 published by Springer (ISBN 978-3-64204376-5).

 
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