IntroductionPython is a dynamically typed, interpreted, general purpose programming language. It's useful for everything from system scripting, to web applications, to full graphical desktop programs. Because of that, it's no surprise that demand for Python programming skills is only increasing, and top companies like Google, Mozilla, Instagram(Facebook), and Reddit rely on it as part of their core technology stack. Not only that, but Python is a favorite in both academic and scientific circles and is gaining ground in the financial sector. Top universities are even using it to teach programming in their computer science programs.
With all of that said, you'd probably be thinking that Python is something super difficult to learn and only accessible to the elite in the technology field, but you couldn't be more wrong. Python is easy. Python is really easy. In fact, Python is one of the first languages used to teach children to program. Python was designed to be very clear and simple to understand. It reads like plain English, and its syntax makes use of spaces rather than brackets and semicolons, so it always looks clean and uncluttered. It's very difficult, if not impossible, to wright messy Python. This helps out new programmers and programmers new to Python big time because you can always tell what you're looking at, or at least, get a decent sense of what it does. This way, you can look at code examples from established open source projects to get an idea of what professional grade Python looks like and how it's used.
Python and Linux work incredibly well together. It wasn't all that long ago that Python supplanted Perl as the de facto scripting and "glue" language on Linux systems. This means that many scripts and utilities that ship with modern Linux systems are written in Python. As a result, most Linux distributions have Python installed by default, but there is a bit of a catch. There are two current versions of Python. Python
3.X.Xare both current. Syntactically, they are very similar, but Python 3 has some features that Python 2 doesn't. That means that they are not entirely compatible and many distributions package them separately. So, your system may have Python 2, but not Python 3 or vice versa. This guide and the others in the series are going to cover Python 3. It is the future of Python, and it's not so bad to go back to Python 2 after you've worked with Python 3.
Installing PythonYou may not need to install Python on your system. The current setup may be exactly what you need. However, if not, the rest of this guide will be dedicated to telling you how to install both Python versions, so you can be sure that you have everything that you need going forward.
Debian/Ubuntu/MintDebian based distributions break up Python 2 and Python 3 packages, and they refer to them separately as well. Python 2 is referred to just as Python and Python 3 is referred to as Python3.
$ sudo apt-get update $ sudo apt-get install pythonPython 3
$ sudo apt-get update $ sudo apt-get install python3
FedoraMuch like the Debian based distributions, Fedora designates Python 2 as Python and Python 3 as Python3.
# dnf install pythonPython 3
# dnf install python3
CentOSCentOS actually doesn't package Python 3 by default. It is available from Fedora's EPEL repository, though, so installing it isn't a problem, once you've enabled the repository on your system.
# yum install pythonPython 3
$ wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm $ su # rpm -i epel-release-latest-7.noarch.rpm # yum install python34If you want to add the repository key, it is available on Fedora's website
OpenSUSEOpenSUSE is a lot like Fedora and the Debian distributions. Python is broken into Python 2 and Python 3 packages that are installable separately.
# zypper in pythonPython 3
# zypper in python3
Arch LinuxArch is actually the exact opposite of the others. Arch uses Python 3 by default and packages Python 2 separately. Python 2
# pacman -Syu # pacman -S python2Python 3
# pacman -Syu # pacman -S python
GentooPython is heavily integrated into Gentoo. Gentoo's package manager,
Portage, is written in Python, as are many critical scripts. Because of this, both versions of Python are installed by default on Gentoo. If you want to change which versions of Python the system uses, you can specify them in
/etc/portage/make.confand run a full system upgrade. /etc/portage/make.conf
~~~~~~~~~ PYTHON_TARGETS="python3_4 python2_7" ~~~~~~~~~
# emerge --sync && emerge --ask --update --newuse --deep --with-bdeps=y @worldYou can choose the version of Python in use with
# eselect python list # eselect python set 2Be very careful messing with Python version in Gentoo. Since so much of the system is tied to Python, it is very easy to break things. Your best bet is to leave things alone, unless you know exactly what you're doing.
Table of Contents
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions