Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "Python"

From NetSec
Jump to: navigation, search
(Statements and Loops)
Line 111: Line 111:
 
{{series
 
{{series
 
| Name = Python
 
| Name = Python
| PartOf = Interpreted Languages
+
| PartOf = interpreted languages
 
}}
 
}}

Revision as of 23:56, 22 November 2011

Python is a high-level interpreted language designed around functionality and cleanliness. It is often compared to perl in terms of functionality and usage.

Strengths and Weaknesses of Python

Python draws strength from being convenient and simple to write. Many people view it as one of the easiest scripting languages to code in. As such, a common usage for python is to write a 'prototype' of a program before implementing it in a heavier language like C. Furthermore, due to it's interpretive nature, a python script is easily modified - there are no compiled binaries to disassemble and reverse-engineer.

However, the language's strengths often become weaknesses. For example, as was noted before, python is not a compiled language. This means that it is very difficult to protect python code - every program is in its raw form, and can be freely edited and reused. There are methods, such as code obfuscation, that can be used to protect code, but these are not foolproof. In addition, python programs tend to run inefficently, hogging more resources than necessary - tasks like cracking, encryption, or anything that requires large numbers of computations should preferably be automated with some other language.

One of the most pertinent drawbacks to python is it's incompatibility - as of version 3.0 of Python, a large portion of the language has been rewritten, including many keywords being turned into functions - for example

 
print "hello, world!" #a keyword
 

would now be

 
print("hello, world!") #a function
 

Although this and other changes are relatively minor, they render python 3.0 programs incompatible with 2.6, 2.5 etc. This is further exascerbated by the fact that many developers continue to code in 2.6.

Installation

Python development is based at it's website at python.org. Python (in every recent incarnation) can be downloaded in the form of Windows binaries and sources for compilation in a *nix environment. While it is currently available in versions 2.7.2 and 3.2.2, it is advised that new programmers download the latest version so as not to learn a language that is becoming obsolete - of course, it is wise to learn the nuances between 2.7 and 3.2 so that you can port older programs, and write programs that are compatible with older versions.

Many distributions come with python preloaded (although it may be an older version), while many more will be able to obtain python using the package manager of their choice. For example, in Arch:

 
pacman -S python
 

Under Windows, python can either be run from the command line or under it's GUI, as installed under the Python folder of the Start menu. Under linux, python is entirely commandline.

Python operates in two modes - the IDLE, which is an interactive python prompt, in which you can execute python statements in a manner that is persistent within your session, but is lost when you exit. It can also be used to run a python script, which has the extension .py

To run a python script, execute:

 
python scriptname.py
 

It will be executed in the commandline.

To open the IDLE, simple type:

 
python
 

You should be presented with some version information and a prompt like this:

>>>

From there on, any python statement will execute as if read from a .py program. Use the exit() function to close the IDLE.

Basic Function

Python variables are 'loosely typed' meaning that they don't have a set type - other languages, such as C, require the type of a variable to be defined. For example,a variable designed to store integers must be set as an int, and will not store characters, or boolean values, or anything else - attempting to store these in it will raise an exception.

To define a variable (x, for the sake of the argument) you use the '=' operator:

 
x = 12
 

Note that there is no definition of type. Python knows it's meant to represent a number because we put a number into it. This is both flexible and, at times, annoying when you try to perform an operation that is invalid and it breaks.

RPU0j.png There is a distinct difference between the '==' and '=' operators. It's important to recognise this, as getting the two mixed up is one of the most common rookie errors in any language. '=' sets something equal to something else, whereas '==' compares two values and returns true if they're equal.

Python does support the string datatype - that is, you can define a variable to be equal to a string of text, for example:

 
hi = "hello, world!"
 

Strings can be added to each other in much the same manner as numbers can - adding two strings will simply return the first string with the second string tacked on at the end.

Two basic functions that are instrumental to writing python code are the print() and input() calls. print() simply prints whatever arguments you give it to stdout, and input takes a string prompt as an argument and returns whatever input that it receives from stdin, in the form of a string.

for example:

 
name = input("What is your name? ")
print(name)
 

The snippet above would print a prompt to the screen saying "What is your name? ", and wait for input. When you press the enter key, any input you've given it will be stored into the variable 'name'. It then prints the value of the variable 'name.

It is pertinent to note that the print() call can print both the value of a variable - print(name) - or it can be supplied with a raw string - print("hello, world!"). It's also important to remember that input() always returns a string - if you're trying to use a number from input, you'd have to typecast it, as discussed later.

Modules

A module is a seperate python script (in some languages, it is called a header) that can be included in multiple programs to add functionality. This has several uses:

  • code reuse - modular code can be easily imported into any script
  • ease of reading - it's easier to locate code in a set of modules than in one huge program

Variable Operation

String Operations

DONT FORGET TO MENTION STRING MODULE

Typecasting

Statements and Loops



Python
is part of a series on

interpreted languages

Visit the interpreted languages Portal for complete coverage.