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

Difference between revisions of "Python"

From NetSec
Jump to: navigation, search
(Basic Function)
(Basic Function)
Line 68: Line 68:
 
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.
 
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.
  
{{Warning|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.}}
+
{{Warning|There is a distinct difference between the '==' and '&#61' 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.}}
  
 
[[Category:Programming Languages]]
 
[[Category:Programming Languages]]

Revision as of 21:48, 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 '&#61' 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.