Basic Statements

Now that we know how Python uses it's fundamental data types, let's talk about how to use them. Python is nominally a procedure-based language but as we'll see later, it also functions as an object-oriented language. As a matter of fact, it's similar to C++ in this aspect; you can use it as either a procedural or OO language or combine them as necessary.


Assignment is similar to other languages, so I won't cover it in depth. Suffice it to say, assignment is basically putting the target name on the left of an equals sign and the object your assigning it to on the right. There's only a few things you need to remember:

Assignment creates object references.
Assignment acts like pointers in C since it doesn't copy objects, just creates a reference to an object.

Names are created when first assigned
Names don't have to be "pre-declared"; Python creates the variable name when it's first created. But as you'll see, this doesn't mean you can call on a variable that hasn't been assigned an object yet. If you call a name that hasn't been assigned yet, you'll get an exception error.

Assignment can be created either the standard way (spam = "SPAM"), via multiple target (spam = ham = "Yummy"), with a tuple (spam, ham = "lunch", "dinner"), or with a list ([spam, ham] = ["yum", "YUM"]).

The final thing to mention about assignment is that a name can be reassigned to different objects. Since a name is just a reference to an object and doesn't have to be declared, you can change it's "value" to anything. For example:

>>>x = 0 #x is linked to an integer
>>>x = "spam" #now it's a string
>>>x = [1, 2, 3] #now it's a list


Python expressions can be used as statements but since the result won't be saved, expressions are usually used to call functions/methods and for printing values at the interactive prompt.

Here's the typical format:

spam(eggs, ham)            #function call
spam.ham(eggs)             #method call
spam                       #interactive print
spam < ham and ham != eggs #compound expression
spam < ham < eggs          #range test

The range test above lets you perform a Boolean test but in a "normal" fashion; it looks just like a comparison from math class.

>Part 2>