GIDNetwork > Beginning Python Tutorial (Part 2)
« GIDNetwork™ and the Blogs From C++ to PHP »

Beginning Python Tutorial (Part 2)

by: crystalattice - Jul 31, 2005

Last time I covered the basics of running Python programs. Today I'll talk about the types and operators used in Python, as well as the syntax Python uses.

Python is based on the C programming language and is written in C, so much of the format Python uses will be familiar to C and C++ programmers. However, it makes life a little easier because it's not made to be a low-level language (it's difficult to interact heavily with hardware or perform memory allocation) and it has built-in "garbage collection" (it tracks references to objects and automatically removes objects from memory when they are no longer referenced), which allows the programmer to worry more about how the program will work rather than dealing with the computer.

Python Syntax

Python forces the user to program in a structured format. Code blocks are determined by the amount of indentation used; brackets and semicolons aren't used to show code grouping or end-of-line termination. Here is an example:

Python Code Example:

x = 1 
     if x:     
          y = 2     
          if y:         
               print 'block2' 
          print 'block1' 
print 'block0'

You'll also notice that compound statements, like the if comparison, are created by having the header line followed by a colon (":"). The rest of the statement is indented below it. The biggest thing to remember is that indentation determines grouping; if your code doesn't work for some reason, double-check which statements are indented.

Statements can span more than one line if they are collected within braces (parenthesis "()", square brackets "[]", or curly braces "{}"). When spanning lines within braces, indentation doesn't matter. String statements can also be multi-line if you use triple quotes. For example:

Generic Code Example:

>>> big = """This is 
... a multi-line block 
... of text; Python puts 
... an end-of-line marker 
... after each line. """ 
>>> big 
'This is\012a multi-line block\012of text; Python puts\012an end-of-line marker\012after each line.'

The ellipsis (...) above are blank lines in the interactive Python prompt used to indicate the interpreter is waiting for more information. You'll notice these blank lines as you play with Python; don't bother looking for the ellipsis, they're just used in documentation.

Python Object Types

Python has the following built-in types: numbers, strings, lists, dictionaries, tuples, and files. Naturally, you can build your own types if needed, but Python was created so that very rarely will you have to "roll your own". The built-in types are powerful enough to cover the vast majority of your code and are easily enhanced. We'll finish up this tutorial by talking about numbers; we'll cover the others in later tutorials.

Before I forget, I should mention that Python doesn't have strong coded types; that is, a variable can be used as an integer, a float, a string, or whatever. Python will determine what is needed as it runs. See below:

Generic Code Example:

>>> x = 12
>>> y = "lumberjack"
>>> x
>>> y

Python Numbers

Python can handle normal long integers (max length determined just like C), Python long integers (max length dependent on available memory), floating point numbers (just like C doubles), octal and hex numbers, and complex numbers (numbers with an imaginary component). Here are some examples of these numbers:

  • integer 12345, -32

  • Python integer 999999999L

  • float 1.23, 4e5, 3e-4

  • octal 012, 0456

  • hex 0xf34, 0X12FA

  • complex 3+4j, 2J, 5.0+2.5j

Python has the normal built-in numeric tools you'd expect: expression operators (*, >>, +, <, etc.), math functions (pow, abs, etc.), and utilities (rand, math, etc.). For heavy number-crunching Python has the Numeric Python extension that has such things as matrix data types. If you need it, it has to be installed separately.

The expression operators found in C have been included in Python, however several of them are slightly different. Logic operators are spelled out in Python rather than using symbols, e.g. logical AND is represented by "and", not by "&&"; logical OR is represented by "or", not "||"; and logical NOT uses "not" instead of "!". More information can be found in the Python documentation.

Operator level-of-precedence is the same as C, but using parenthesis is highly encouraged to ensure the expression is evaluated correctly and enhance readability. Mixed types (float values combined with int values) are converted up to the highest type before evaluation, i.e. adding a float and an int will cause the int to be changed to a float value before the sum is evaluated.

Variable assignments are created when first used and do not have to be pre-declared like in C.

Generic Code Example:

>>>a = 3     #name created
>>>b = 4.0

As you can see, "a" and "b" are both numbers but Python can figure out what type they are without being told. Also, you'll note that comments in Python are set off with a hash/pound sign (#) and are used exactly like the "//" comments in C++ or Java.

That's about it for numbers in Python. It can also handle bit-wise manipulation such as left-shift and right-shift, but if you want to do that, then you'll probably not want to use Python for your project. As also stated, complex numbers can be used but if you ever need to use them, check the documentation first.

Next time I'll discuss strings and their manipulation. Much if it will be familiar to C programmers, with a few changes designed to make life easier.

Would you like to comment? This story has been viewed 26,119 times.
« GIDNetwork™ and the Blogs From C++ to PHP »


Copyright © GIDNetwork™ 2001 - 2022

Another website by J de Silva

Page generated in : 0.00854 sec.