When coding with Python we realize that the fundamental idea is creating objects and apply operations (addition, concatenation, etc) on those objects. Objects are essentially pieces of memory with some data and operations associated with them, objects can be built-in by default in Python or they can be created using Python or other languages tools.
In a broader perspective, Python programs can be broken down into modules, statements, expressions, and objects. Thus a Python program contains at least one module, which in times contains statements that are built with expressions and those expressions create and process objects.
In lower level languages, implementing data structures, managing memory allocation, implement search and access routines, and so on, are really time-consuming tasks. Python provides a powerful set of built-in objects. Thus, coding objects implementations is not necessary unless some special properties are required.
Let's review some advantages of using built-in objects:
- Simple task can be carried out entirely with built-in objects
- More complex objects are usually implemented on top of built-in objects such as list (collections) and dictionaries (search tables).
- Built-in objects have already optimized data structure algorithms that are implemented in C for achieving greater speed
- Python's built-in object are fully standardized.
Object types. Numbers
Numbers in Python range from integers (those without fractional part), floating-points numbers (numbers with a decimal point in them), and some more complex types (fixed-precision decimals -pi, e, etc; rational fractions, complex numbers with imaginary parts, and some more).
Ordinary mathematical operations are supported as well. The plus sign (+) performs addition, a start (*) is used for multiplication, two stars are used for exponentiation (**). For now, this will suffice to describe this object type, more advanced tools are available by importing some modules shipped with Python or using third-party modules. Since their application is more specific, they will be covered in other posts.
Object types. Strings
Strings are a positionally ordered collection of bytes generally used to record textual information or some other random data. They are the simplest versions of an array of one-character string called which are called Sequences.
Strings support operations that involve positional ordering between elements. Let's review some examples:
>>> S = "string" >>> len(S) # This will show the length of the string 6 >>> S # This will show the first element of the string from left to right "s" >>> S # This will show the fourth element of the string from left to right "i" >>> S[-1] # This will show the first element of the string from right to left "g"
The first position is enumerated with the number 0. Also, negative indexes are added to the string size, and the element on that position is then printed. One of the cool features of Python is that its syntax is really general. If a function is expecting us to input some value, we could use a literal, a variable, or any other equivalent expression. For instance, we can fetch the last item of a string in the above example by typing either of the following expressions.
>>> S = "string" >>> S[-1] "g" >>> S[len(S)-1] "g"
More on strings and other Python objects will be covered in the next part of the series
Basic Concepts. Part 1
Basic Concepts. Part 2
Program designing. Part 3
Program designing. Part 4
- Learning Python, Mark Lutz. O'Reilly, 4th edition
- Python Para Todos, Raúl González Duque. (Licensed under Creative Commons Attribution ShareAlike 2.0)
- Python 3 for absolute beginners, Tim Hall and J-P Stacey. Apress
- Python for Software Design, Allen B. Downey. Cambridge University Press