What is python and how
it works?
Python (and all of its
derivatives, like Python 3 and Python 2.x) is an interpreted programming
language that supports class-based objects and inheritance. There are also
various object types (like lists, sets, strings, etc.), like dictionaries and
so on. Python was created in the late 1980s by Guido van Rossum and was ported
to Linux by Rob Pike in 1991.
Python is a
multiparadigm language, it supports both procedural and object-oriented
programming in addition to string-oriented and even XML-oriented programming.
It supports anonymous functions, which are a kind of functional programming.
There is even a surprisingly thorough profiling tool in Python to help you get
the most of your performance. Python’s underlying execution system, the Python
interpreter, is lightweight, which is one reason why Python can be run from an
always-on workstation like your laptop or desktop computer.
Python was developed
by a small core team at the University of Edinburgh, a relatively small group
of people from all over the world. The initial Python language specification
was finished in the summer of 1988 and the first versions of the interpreter
were released in November of 1989. Although Python was initially called PEP
(PEP stands for “Suggested Extension”), that name was later abandoned and PEP
50 (Python Programming Language Specification) was eventually adopted.
Unlike other
programming languages, the Python interpreter is capable of optimizing the
execution of the program for much different architecture. So when using Python
on your workstation, you don’t have to worry about your computer’s performance.
And while it is true that some languages (like C) are specifically optimized
for x86-64 architecture, the Python interpreter can also run on some other
architectures like ARM-based processors.
In addition to being
used as a scripting language, Python also offers us a vast collection of
libraries to use in our real programs. You can, for example, do common
computing tasks like string manipulation, file management, and web browsing in
Python, and Python also supports these operations on top of an existing
standard (like Git, OpenSSL, and SQLite).
There are two ways to
learn about the Python language. The first is to learn the official Python
Programming Guide. But if you’re looking to get a deeper understanding of the
language and of Python’s way of working, then I recommend checking out one of
these videos. Here are some of my recommendations:
If you have two weeks
to learn a new language, this is the best place to start. Guido van Rossum
himself teaches these Python tutorials, and they are useful even if you don’t
take the course. However, they also teach some of the more advanced concepts of
the language and should only be taken if you have enough time to fully
understand the material.
I highly recommend,
though, reading both A Quick Introduction to Python and The Practical Python
Programming Guide. These are both short books that focus on the fundamentals of
the Python language and they should be enough to get you started.
If you are already
familiar with Python, you can also check out the original documentation written
by Guido van Rossum and Mark Miller, along with various Python packages that
contain code examples.
Looking for something
to use when you don’t want to use Python’s built-in libraries? If so, try by
using one of the many Python packages that are available on PyPI.
This Python Cookbook,
first published in 1996, still remains my favorite book on Python. The cookbook
contains several hundred (and growing!) great recipes for working with Python.
There are also a
number of books dedicated to the topic of functional programming in Python.
Python has a number of dialects, and even if you’re not using the Python
interpreter, you can still benefit from functional programming techniques in
your Python programs.
There are a few
reasons why I use Python a lot. One reason is that it provides many facilities
for me to work more efficiently and stay productive at the same time. Another
reason is that I can use the language to build and manage software that I may
not have been able to create using another language.
Even if you’re not
using the Python interpreter, the following tools and libraries can improve
your Python experience and allow you to get more done.
For common tasks, we
have the standard library, a number of modules that are available as Python
packages, and our own custom modules. And, of course, there is the standard
library, which contains packages for common tasks, like database access,
process management, string processing, file manipulation, and so on.
There are also a
number of modules and libraries that we can use for more advanced tasks like
web development and web framework management.
Without further ado, here
are my favorite Python packages and libraries. All these Python packages and
libraries have their own importance, but these are my personal favorites.
These Python packages
and libraries have helped me get more done in Python and more efficiently in the
process.
Print and puts
In Python, strings are
evaluated on-disk, and functions return objects that are meant to be treated as
raw text. However, strings can also contain escaping characters to allow some,
like single quotes, to be escaped. So, when you want to process a text string,
you can use either print or puts .
This is an example of
using puts for a string:
>>>
print('Hello world!')
(Traceback (most
recent call last): ... Elementary Function() Traceback (most recent call last):
File "<stdin>", line 1, in <module> NameError: name
'main' is not defined
>>> print('Hi
there' >>> puts 'hi there')
Hi there
>>>
print('Hello')
\o
\x
\u
\D
Print
In general, you’ll
usually want to use puts as the last line in a program in order to perform common
string operations, such as string interpolation and concatenation.
The use of puts as the
last line means that if we want to stringify or delimit a string, we’ll have to
explicitly use print . We could use a conditional statement in the beginning of
our string, like this:
>>> print(‘this
is a string’)
\
It looks like a
string.
>>> print(‘this
is a string, but it needs to contain a “space”’)
This is a string, but
it needs to contain a “space”’
Is approximately
equivalent to:
>>> print(‘this
is a string, but it needs to contain a “space”’)
The is roughly
equivalent to:
‘This is a string, but
it needs to contain a “space”’,
But not as error-safe
because it’s more verbose than printing a single space to the screen
If we want to set the position
of a text string, or if we need to print only one character or if the string
contains delimiters, then we can use print without any condition. Here’s an
example:
>>>
print(‘Hello’)
‘Hi there’
>>> print(‘this
is a string’)
‘This is a string, but
it needs to contain a space’
Print
We could also use them
in chained or nested calls. For example, in the following example, we can use
put (or print ) while calling a function:
>>> def
foo(x):
print(‘foo: ‘ + x)
return 1
foo()
foo()
In this example, the
first parameter gets returned as-is while the second parameter is automatically
sent to the function after the first one.
In general, you should
use put to stringify text or delimit text. You should also use puts when
printing individual characters:
>>> def
foo(x):
print(‘foo: ‘ + x)
return 1
foo()
>>> foo()
foo: foo: foo: foo:
foo:
put
Writing code can be
cumbersome, especially if you have to process thousands of lines of data each
day. For instance, it’s not always easy to remember whether the “f” or “G”
characters were supposed to be used. It’s often also difficult to determine
which parenthesized expression to apply first to a sequence of expressions.
This problem is
particularly apparent when you have to read large numbers of text. As it turns
out, the latter is almost always true; most programming tasks have an
inevitable reading phase. To make text processing more efficient, you can use
the Python list comprehension to read or write the whole text line by line. The
list comprehension uses a for loop and avoids parentheses. We can use it in a
for statement:
>>> x = ‘In
the future’ for x in list(filename):
x.print(‘Now’)
Now
>>>
x.print(‘In the future’)
Now
>>>
x.print(‘In the future’)
Now
And we can loop over
the string sequentially:
>>> for x in
xs:
print(‘”‘ for x in
xs”)
Now
If we have more than
one line of text to process, we can also loop through multiple lines:
>>> for x in
xs:
print(‘”‘ for x in
xs”)
Now
>>>
x.print(‘”‘ for x in xs”’)
Now
And we can execute a
separate for loop:
>>> for x in
xs:
print(‘”‘ for x in
xs”’)
Now
One cool thing about
the list comprehension is that it automatically groups their results:
>>> xs =
list(filename)
>>> len(xs) 5
>>> xs =
list(filename)
>>> len(xs) 5
If we want to execute
more than one for loop at the same time, or if we want to merge results, we can
write for comprehension on the results array:
>>> xs =
list(filename)
>>> xs =
list(filename)
>>> xs =
list(filename)
>>> xs.map(x
=> x)
Now
You can also use the
for comprehensions to process text using nested looping:
>>> for x in
xs:
print(‘Now’)
>>> Now
Now
>>> Now
Now
Now
Now
To delete one string
from the list comprehension, you can use replace():
>>> for x in
xs:
print(‘”‘ for x in xs)
>>> x.replace(‘”‘,
‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
Finally, you can use
“it’s” to substitute individual characters in a string:
>>> for x in
xs:
print(‘”‘ for x in xs)
>>> ‘”‘
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
1
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”‘, ‘’)
Now
>>>
x.replace(‘”