LINK to HACMKD: https://hackmd.io/_ZLGXujhQM6pgBbYv2uwIQ?view
In Wikipedia you can find a more filtered list by type of language.
In this link, you can find their relationships.
WHAT IS PYTHON ?¶
Python is an open source and high-level programming language, which means, it is easily understandable and most crucially readable for humans, simultaneously being independent of the platform it is running.
WHY YOU SHOULD USE PYTHON ?¶
Do you want to work with AI ? Create your own social network ? A video game ? …¶
The answer to all these questions is Python.
List of Python applications:
More applications for Python here.
Readable and Maintainable Code¶
age = 16 if age >= 18: print “Eligible to Vote” else: print “Not Eligible to Vote”
Compatible with Major Platforms and Systems¶
Python is supports many operating systems. You can even use Python interpreters to run the code on specific platforms and tools. Also, Python is an interpreted programming language. It allows you to run the same code on multiple platforms without recompilation.
Top 3 popular programming languages¶
According to the latest TIOBE Programming Community Index, Python is one of the top 3 popular programming languages of 2019.
Robust Standard Library¶
Its large and robust standard library makes Python score over other programming languages. The standard library allows you to choose from a wide range of modules according to your precise needs. Each module further enables you to add functionality to the Python application without writing additional code.
Many Open Source Frameworks and Tools¶
You can even use several open source Python frameworks, libraries and development tools to curtail development time without increasing development cost.
HOW TO START ?¶
Python 2 vs 3¶
- Python 2.0 was first released in 2000. Its latest version, 2.7, was released in 2010.
- Python 3.0 was released in 2008. Its newest version, 3.6, was released in 2016, and version 3.7 is currently in development.
- Although Python 2.7 is still widely used, Python 3 adoption is growing quickly. In 2016, 71.9% of projects used Python 2.7, but by 2017, it had fallen to 63.7%. This signals that the programming community is turning to Python 3–albeit gradually–when developing real-world applications.
- On January 1, 2020, Python 2.7 will “retire” and no longer be maintained.
Should you install Python 2.7 and 3 ? Our recommendation is YES, with an environment!
A python environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.
The two most popular tools for setting up environments are:
- PIP (a Python package manager; funnily enough, it stands for “Pip Installs Packages”) with virtualenv (a tool for creating isolated environments)
- Conda (a package and environment manager)
1. Install Anaconda¶
Anaconda is a Python distribution intended for massive data processing, predictive analysis and scientific programming that simplifies the management of packages. There are other ways to install python and the necessary packages for OpenCv but they are more complex and Anaconda makes it easier for us to work.
In this link you can find a nice tutorial to set up your anaconda
Step 1 - Visit official Anaconda website and download it¶
There are different versions for different Windows, Mac and Linux operating systems. Download and follow the instructions to install the one that suits you best.
In my case is Ubuntu ( Linux )
You can check that you have installed it by opening a command line and typing the following:
Step 2 - Creating an environment from an environment.yml file¶
Use the terminal or an Anaconda Prompt for the following steps.
Create the environment for Python 3.7:
conda create -n yourenvname python=x.x anaconda
And press Yes [y]
To activate your environment:
conda activate nameenv
To deactivate an active environment:
Create the environment for Python 2.7:
conda create -n yourenvname python=2.7 anaconda
Manage your environments like a pro
More information about managing environments.
Install a package:
conda install -c conda-forge <name_package>
WORKING WITH PYTHON¶
Mainly, one can execute a script in three ways:
- Using a python interpreter (like PythonLauncher in MacOS)
- Running a python script in the command line:
➜ python app.py
- Making the file executable. For this we need to tell it which interpreter to use.
To check where an application (in UNIX) or program (in Windows) resides in your computer, you can check it in the CL by:
➜ where python /Users/macuser/anaconda2/bin/python /usr/local/bin/python /usr/bin/python
bash, windows and linux users
Instead of where, type in
Then, the very first line of the file should be:
#!/Users/macuser/anaconda2/bin/python. Then, we can make the file executable by:
chmod +x app.py
And run it by:
SOME BASIC STRUCTURES (LOOPS / CONDITIONALS)¶
for i in range(6): print ('Hello')
a = 0 while a < 10: a = a + 1 #alternatively a += 1 print(a)
a = 1 if a > 5: print("This shouldn't happen.") else: print("This should happen.")
Super basic example:
➜ python Example_00.py Hello
A bit more complex, with main structure:
def main(): print ('Hello') if __name__ == '__main__': main()
➜ python Example_01.py Hello
Hello world with argparsing¶
Let’s pass some arguments:
import argparse def main(name): print ('Hello', name) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("--name", "-n", default = "Antonio", help="Tell me your name") parser.set_defaults(keep=False) args = parser.parse_args() main(args.name)
➜ python Example_02.py Hello Antonio
➜ python Example_02.py -n 'Oscar' Hello Oscar
➜ python Example_02.py -h usage: Example_02.py [-h] [--name NAME] optional arguments: -h, --help show this help message and exit --name NAME, -n NAME Tell me your name
How about the booleans?
import argparse def main(name, repeat): print ('Hello', name) if repeat: print ('I dont really want to repeat it') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("--name", "-n", default = "Antonio", help="Tell me your name") parser.add_argument("--repeat", "-r", dest='repeat', action='store_true', help="Number of repetitions") parser.set_defaults(keep=False) args = parser.parse_args() main(args.name, args.repeat)
If we run it, we have a subversive code:
➜ python Example_03.py -n Oscar Hello Oscar ➜ python Example_03.py -n Oscar -r Hello Oscar I dont really want to repeat it
More info about argparsing here.
Make something like this: