Learn Python: Components and Operators

In this section we mainly focus on the basic execution of the program, components of the Python language and built in object types

Learn Python: Components and Operators
Python Programming

Before we start explaining the semantics of the language, you need to know how to execute the Python program. Python is slightly different from many other scripting languages in that there is a number of different ways in which you can execute Python statements, including interactively directly to the Python interpreter.

Many programming languages rely on the following sequences of events for executing applications.

1) Write the application in one or more source files.

2) Compile the source files into object files.

3) Link the object files into an application.

4) Execute the application.

With the scripting languages, the steps are simpler because the interpreter works directly with the source file. So the sequence is more likely to be as follows.

1) Write the application in one or more source files.

2) Execute the interpreter, supplying the main source file.

Executing the Python Programs

Python follows the same basic process for writing applications as other programming languages do, the Python interpreter takes raw-text source code and executes each statement. The difference in Python is that you can also execute statements directly within the interpreter and you don't have to place Python statements into a file before you execute them.

Components of Python

Now, let's take a closer look into Python objects, although objects in Python may refer to variables used in other languages. The Python language follows the same basic principles as many others.

  • An Individual application is made of no of files, which Python calls Modules.
  • Each Module is composed of a no of statements.
  • The Statements create, use, and modify variables which Python creates object variables.

Here in Python, like all languages support built-in variable types, similarly Python supports similar built-in types. These building blocks are used for all the other variable types. For example, in C we have char which is a single character. By defining a char array, you create a text string and by defining a two-dimensional array, you can create an array or an array of strings.

Just like other scripting languages, Python handles the creation, memory allocation, and access routines that enable you to use the objects. The code for using built-in object types is highly optimized and has been developed over a no of years to be as efficient as possible. Python support five main built-in object types and external data type that is accessible just like the built-in objects. The built-in objects are Numbers, Strings, Lists, Dictionaries, and Tuples. The primary external data type is supported by Python is the file. Although it seems odd to call a file a data type, Python supports access to files in much the same way that it supports access to the built-in object types. Some of the built-in object types will be familiar to you such as numbers and strings and also possibly lists. Here the Tuple is a special type of list that cannot be modified. 

integer = 12345

float = 123.45

string = "Hello" or 'Hello'

list = [1, 'Two', 3]

dictionary = {'one':1, 'two':2, 'three':3}

tuple = (1, 'Two', 3)

Operator Basics

It's impossible to talk about objects without talking about the operators that can be used to operate on the objects. Because Python uses objects rather than variables to store the information, you can actually use the same operators on a variety of different objects. Like other languages, Python follows the same basic mathematical notation and format when using operators on individual objects. Many of these operators are similar to C/C++, Perl, and VB programmers, among others. For example, the expression a+b does exactly what you expect, it adds the value to variables a and b. Other operators such as lambda and in, are specific to the Python language.

Here is the Python Operators and Expression Precedence

Operators Description
x or y Logical or ( y  is evaluated only if x is false)
lambda args: expression Anonymous function
ex: x = lambda a: a+10
output: 15
x and y  Logical and ( y is evaluated only if x is true)
not x Logical negation
<, <=, >, >=, ==, <>, != comparison test
is, is not Identity test
in, not in Membership test
 x | y Bitwise or
x^y Bitwise or
x&y Bitwise and
x<<y, x>>y shift x left or right by y bits
x+y, x-y Addition/concatenation, subtraction
x*y, x/y, x%y Multiplication/repetition, division, the remainder
x[i], x[i:j], x.y, x(…) Indexing, slicing, qualification, function call
(…), […], {…}, `...` Tuple, list, dictionary, conversion to string

Mixed Types in Expressions

Python follows the same basic rule as C/C++. If an expression contains mixed variable types, then the return value is of the most complex type contained within the expression. For example, the calculation

result = 3*2.5

set the result to 7.5, a floating-point number, even though you started off with an integer and a floating-point value. Although it applies primarily to numerical calculations.


Python uses a simple object to hold a number. There are no restrictions on what type of numbers can be stored on what type of the number can be stored within a number object, unlike  C, where you need to use a different data type to store the integer and floating-point numbers. In addition to these basic types, Python also supports complex numbers and arbitrarily large integers. You need to know how to introduce numerical constants into your Python programs.

Integer Constants

You can create integer number objects by supplying a sequence of numbers, as in the following example:

number = 1234

number = -1234

The objects created are integers and they are actually stored internally as a C long data type, which is at least 32 bits long and maybe longer depending on the C compiler and processor being used. Also, note that 0 is considered to be a number. 

zero = 0

Again, this is as you would expect. It allows that Python uses integer values when determining the logical value of an expression. As with other languages, 0 equates to a value of false and any other number equates to true. This allows you to use integers for simple Boolean values without the need for an additional data type.

Hexadecimal and Octal Constants

We can specify hexadecimal ( base 16) and octal ( base 8) constants using the same notation available in Perl and C/C++. That is 0x or 0X prefixed to a number forces Python to interpreter it as a hexadecimal number, while a single leading 0 (zero) indicates an octal number. For example, to set the value of an integer to decimal 255, you can use any of the following statements:

decimal = 255

hexadecimal = 0xff

octal = 0377

Since these are simply integers, Python stores them in the same way as decimal integers. 

Long Integers

The built-in basic integer type is limited to a storage width of at least 32 bits. This means that the maximum number that you can represent is 2^31-1 since you must one bit to follow for negative numbers. Although for many situations this is more than enough, there are times when you need to work with long integers. Python supports arbitrarily long integers, you can literally create an integer one thousand digits long and use it within Python as you would any other number. To create a long integer, you must append and 1 or L to the end of the number constant, as in the following example:

long = 123456789123456789123456789123456789123456789123456789L

Once you have executed the long integer, you can execute expressions as if they were normal numbers, the Python interpreter handles the complexities of dealing with the super-sized numbers


// produces the following output:


// Or you can do long integer math:
// which generates


Although Python uses these long integers as if they were normal integer values, the interpreter has to do a significant amount of extra work to support this option, even though support for such large numbers is written in C.

Floating-Point Constants

Python supports the normal decimal point and scientific notation formats for representing floating-point numbers. For example, the following constants are all valid:

number = 1234.5678

number = 12.34E10

number =  -12.34E-56

Internally Python stores the floating-point values as C doubles, giving the objects as much precision as possible. Note that there is no "long" floating-point variable. Remember the floating-point and integer expressions return floating-point values. The easiest way to demonstrate this is to show the output from the same simple calculation, one using integer constants and the other floating-point constants:

>>> print (5/12)


>>> print (5.0/12)


In the above example, that the first expression returns 0 the rounded-down version of 5/12. The second expression prints the expected decimal fraction.

Complex Number Constants

Python employs the normal notation for supporting complex numbers, the real and imaginary parts are separated by a plus sign and the imaginary number uses a single j or J suffix. For example, the following are examples of complex  number constants:

cplx = 1+2j

cplx = 1.2+3.4j

Python uses two floating-point numbers to store the complex numbers, irrespective of the perception of the original. Because complex numbers are a separate entity within Python, the interpreter automatically performs complex math on expressions that include complex numbers.

Numeric Operators

The above table contains the operators apply to numbers. The below table contains a more explicit list of numeric operators used for calculations, these are all the familiar mathematical operations.

Operators Description
x+y Add x to y
x-y Subtract y from x
x*y Multiply x by y
x/y Divide x by y
x**y Raise x to the power of y
x%y Modulo ( returns the remainder of x/y)
-x Unary minus
+x Unary plus

There are also a series of shift and bitwise operators that can be used for binary and bit math; these are listed in the below table. Note these operators can only be applied to integers; trying the operations on floating-point numbers raises an exception. In addition to these base operators, a series of augmented assignment operators was introduced with Python 2.0. For example, you can rewrite the fragment

a = a + 5

using an augmented assignment operator, as

a += 5

More clearly the expression

x = x + y

can be written as 

x += y

The following is the full list of augmented assignment operators, the effects of these operators are the same as for the base operators listed in the below table

Operators Description
x<<y Left shift ( moves the binary form of x, y
digits to the left)
ex:1<<2 = 4
x>>y Right shift ( moves the binary form of x, y
digits to the right)
ex: 16>>2 = 4
x&y Bitwise and
x|y Bitwise or
x^y Bitwise exclusive or (xor)
~x Bitwise negation