A walk in Python
Day 1
- Introduction to Python
- Starting with Python
Introduction
What is Python
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. There are two versions of Python, Python 2 and Python 3. The latter is the one we are going to see throughout this course.
We can say that Python is:
Interpreted
The program directly executes the instructions written, without requiring them to have been previously compiled into a machine language program.
High-level
It is a programming language with strong abstraction from the details of the computer.
Cross-platform
Refers to a language that is supports in multiple platforms, such as Windows, Linux, Mac, Raspberry Pi, etc.
Multi-paradigm
It is a language that can use procedural programming, functional programming and/or object-oriented.
Dynamically and strongly typed
It is dynamically typed because we don’t have to define the type of a variable.
However, starting from Python 3.5, type hinting was introduced, allowing developers to optionally specify the expected types of variables, function arguments, and return values. This helps improve code readability and enables better static analysis tools.
It is also strongly typed because the interpreter keeps track of all variable types and does not allow implicit type conversions that could lead to unexpected behavior. For example, attempting to add a string and an integer will result in a TypeError.
Why Python?
Python is an easy to learn language since it has a simple syntax similar to the English language. It also has syntax that allows developers to write programs with fewer lines than some other programming languages.
There are many more advantages, but these two qualities were the most important for me, since like many of us, we are self-taught developers.
Having both an easy to learn and a very rich language, that can be supplemented with lots of libraries, are the things that makes Python a very powerful one.
Clarifying
One of the key features of Python is that everything is an Object, and the type is just one attribute of an object. As an illustration, we can assign a single integer to a variable, and use the Python built-in function dir for finding out the attributes of the object.
Having said that, you might read that Python is by definition a Object-oriented language. As we have seen before, Python is multiparadigm, so don’t be confused with these two concepts.
The Zen of Python
It is a collection of 19 guiding principles for writing computer programs that influence the design of the Python programming language.
The principles are:
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
You can check them out, only running python on the console and then
import this
PEP’s
Python’s development is conducted largely through the Python Enhancement Proposal (PEP) process, the primary mechanism for proposing major new features, collecting community input on issues and documenting Python design decisions.
Python coding style is covered in PEP 8. Outstanding PEPs are reviewed and commented on by the Python community and the steering council.
Starting with Python
Install Python and Pip
https://www.python.org/downloads/
Install IDE (VS Code)
https://code.visualstudio.com/
Pip
Pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes.
Virtual Environments
Most of the times when developing with Python, you might need external libraries or packages. In order to do that, you’ll need to install them. This can be done with Pip command.
But if we don’t create a virtual Environment, those packages are going to be installed globally and we might want to install only the packages we need for a certain program and not another.
To do that, I recommend to use virtualenv.
It can be installed with pip:
pip install virtualenv
Then we’ll need to create our own virtual Environment.
virtualenv <environment_name>
Once this is done we can activate/deactivate with
- On Windows run
<environment_name>\Scripts\activate.bat(deactivate.batto deactivate the environment). - On Windows using PowerShell, run
<environment_name>\Scripts\activate.ps1(justdeactivateto deactivate the environment). - On Windows using Git Bash run
<environment_name>\Scripts\activate(deactivateto deactivate the environment). - On Linux run
source <environment_name>/bin/activate(deactivateto deactivate the environment)
Syntax
Python uses indentation to indicate a block of code. Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
if a > 5:
print("Es mayor a 5")