FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Python

Python

Python is now a mainstream computer language: among the top five in the TIOBE index, the fastest growing, and the most popular in introductory college computer science courses. There are many reasons for its ascent:

  • it's easy to learn and write, thanks to a clean syntax
  • it's easy to read, for the same reason
  • it scales up (from tiny jobs to huge distributed applications) and across application domains
  • science, and hot areas like data science and machine learning, use Python heavily
  • Python programmers make good money
  • the Python development culture is helpful and inclusive
Taken together, Python is a very productive

A Little Python

When Guido van Rossum started tinkering with a new language over Christmas in 1989, he didn't foresee that it would grow into a major force. He named it after the Monty Python comedy troupe, and the Python culture has always had a lighter feel than many others.

Python is a standard part of UNIX, Linux, and OS/X. If you're using Windows, visit http://www.python.org/ and download a copy of the Python interpreter for Windows, and use a terminal application to try the examples in this chapter. You may also try a service like http://pythonanywhere.com/ to run Python programs over the web without installing anything. 

Some computer languages are more difficult than others. Python was designed for ease of use, but not by making it a toy language. You can do serious work with Python, as many large and successful companies like Instagram and Youtube know. Its balance of clean syntax and power makes it quite productive.

Python Versions

"No one expects the Spanish Inquisition", as the Monty Python skit declared. No one expects -- the unexpected. Python's original design was solid, but eventually needed improvement in some areas. The realization that there were more characters in the world than on an ASCII keyboard caused Python's string handling to adapt to Unicode. Other internal fixes and optimizations led to the current version of Python: Python 3. On the surface, Python 3 and 2 look almostg identical. Python 2 has proved itself useful, maybe even too useful, which delayed its replacement by Python 3.

What's In the Box?

You can run Python code in one of two ways:

  • the interactive Python shell (or other IDEs)
  • a standalone Python program

The Python shell is analogous to the UNIX shell. It contains a Python language interpreter and a small set of input and output helpers.

If you go to the command line (in a terminal window or a plain text display) and just type python, you'll see something like this:

$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

This was run on a Mac.

That initial >>> is your Python shell prompt. You type Python code, line by line, and the Python interpreter evaluates each line after you hit enter.

$ python3
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Let's see how helpful help is:

>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help()
Welcome to Python 2.7! This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
help>

Data Types

Like most computer languages, Python supports various data types. 

Numbers include integers and floating point. In Python, you indicate an integer as one or more digits, with an optional minus (-) or plus (+) sign before them.

Floats (floating point numbers) are also sequences of digits, but with a decimal point somewhere.

Strings are sequences of text characters:

strings

bar_song = """one tequila
two tequila
three tequila
floor"""
 
 
poem = """I wish I may
I wish I might
I wish I were
A trilobite.""" 

 

lines = []
poem = file.open('poem.txt', 'rt')
for line in poem:
    lines.append(poem)
close(poem)

 

for file.open('poem.txt', 'rt') as poem:
    lines = poem.readlines()

 

for file.open('poem.txt', 'rt') as poem:
    lines = poem.read()

 

control flow

functions

modules

The Standard Library

web

client

server

database

...

Getting Other Python Code

pypi

github

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.