Non-Programmer's Tutorial for Python 3/Print
version
Contents
1 1. Front matter
1.1 Other resources
2 2. Intro
2.1 First things first
2.2 Installing Python
2.2.1 Linux, BSD, and Unix users
2.2.2 Mac users
2.2.3 Windows users
2.2.3.1 Configuring your PATH environment variable
2.3 Interactive Mode
2.4 Creating and Running Programs
2.4.1 Program file names
2.5 Using Python from the command line
2.5.1 Running Python Programs in *nix
2.6 Where to get help
2.6.1 Python documentation
2.6.2 Python user community
3 3. Hello, World
3.1 What you should know
3.2 Printing
3.2.1 Terminology
3.3 Expressions
3.3.1 Arithmetic expressions
3.4 Talking to humans (and other intelligent beings)
3.5 Examples
3.6 Exercises
3.6.1 Footnotes
4 4. Who Goes There?
4.1 Input and Variables
4.2 Assignment
4.3 Examples
4.4 Exercises
5 5. Count to 10
5.1 While loops
5.1.1 Infinite loops or Never Ending Loop
5.2 Examples
5.2.1 Fibonacci sequence
5.2.2 Enter password
Non-Programmer's Tutorial for Python 3/Print version - Wikibooks, ope...
https://en.wikibooks.org/w/index.php?title=Non-Programmer's_Tutorial...
1 sur 74 14/01/2016 19:33
5.3 Exercises
6 6. Decisions
6.1 If statement
6.2 Examples
6.3 Exercises
7 7. Debugging
7.1 What is debugging?
7.2 What should the program do?
7.3 What does the program do?
7.4 How do I fix my program?
8 8. Defining Functions
8.1 Creating Functions
8.2 Variables in functions
8.3 Examples
8.4 Exercises
9 9. Advanced Functions Example
9.1 Recursion
9.2 Examples
10 10. Lists
10.1 Variables with more than one value
10.2 More features of lists
10.3 Examples
10.4 Exercises
11 11. For Loops
12 12. Boolean Expressions
12.1 A note on Boolean Operators
12.2 Examples
12.3 Exercises
13 13. Dictionaries
14 14. Using Modules
14.1 Exercises
15 15. More on Lists
16 16. Revenge of the Strings
16.1 Slicing strings (and lists)
16.2 Examples
17 17. File IO
17.1 File I/O
17.2 Advanced use of .txt files
17.3 Exercises
18 18. Dealing with the imperfect
18.1 ...or how to handle errors
18.2 closing files with with
18.3 catching errors with try
18.4 Exercises
19 19. The End
20 20. FAQ
Non-Programmer's Tutorial for Python 3/Print version - Wikibooks, ope...
https://en.wikibooks.org/w/index.php?title=Non-Programmer's_Tutorial...
2 sur 74 14/01/2016 19:33
1. Front matter
All example Python source code in this tutorial is granted to the public domain. Therefore you may modify it
and relicense it under any license you please. Since you are expected to learn programming, the Creative
Commons Attribution-ShareAlike license would require you to keep all programs that are derived from the
source code in this tutorial under that license. Since the Python source code is granted to the public domain,
that requirement is waived.
This tutorial is more or less a conversion of Non-Programmer's Tutorial for Python 2.6. Older versions and
some versions in Korean, Spanish, Italian and Greek are available from http://jjc.freeshell.org/easytut/
The Non-Programmers' Tutorial For Python 3 is a tutorial designed to be an introduction to the Python
programming language. This guide is for someone with no programming experience.
If you have programmed in other languages I recommend using Python Tutorial for Programmers
(https://docs.python.org/3/tutorial/index.html) written by Guido van Rossum.
If you have any questions or comments please use the discussion pages or see Authors page for author
contact information. I welcome questions and comments about this tutorial. I will try to answer any
questions you have as best I can.
Thanks go to James A. Brown for writing most of the Windows install info. Thanks also to Elizabeth Cogliati
for complaining enough :) about the original tutorial (that is almost unusable for a non-programmer), for
proofreading, and for many ideas and comments on it. Thanks to Joe Oppegaard for writing almost all the
exercises. Thanks to everyone I have missed.
Other resources
Python Home Page (http://www.python.org)
Python 3 Documentation (https://docs.python.org/3/)
A Byte of Python by Swaroop C H (http://www.swaroopch.com/notes/python)
Porting to Python 3: An in-depth guide (http://python3porting.com/)
2. Intro
First things first
So, you've never programmed before. As we go through this tutorial, I will attempt to teach you how to
program. There really is only one way to learn to program. You must read code and write code (as computer
programs are often called). I'm going to show you lots of code. You should type in code that I show you to
see what happens. Play around with it and make changes. The worst that can happen is that it won't work.
When I type in code it will be formatted like this:
##Python is easy to learn
print
("Hello, World!")
That's so it is easy to distinguish from the other text. If you're reading this on the Web, you'll notice the code
is in color -- that's just to make it stand out, and to make the different parts of the code stand out from each
other. The code you enter will probably not be colored, or the colors may be different, but it won't affect the
code as long as you enter it the same way as it's printed here.
Non-Programmer's Tutorial for Python 3/Print version - Wikibooks, ope...
https://en.wikibooks.org/w/index.php?title=Non-Programmer's_Tutorial...
3 sur 74 14/01/2016 19:33
If the computer prints something out it will be formatted like this:
Hello, World!
(Note that printed text goes to your screen, and does not involve paper. Before computers had screens, the
output of computer programs would be printed on paper.)
Note that this is a Python 3 tutorial, which means that most of the examples will not work in Python 2.7 and
before. As well, some of the extra libraries (third-party libraries) have not yet been converted. You may want
to consider learning from the Non-Programmer's Tutorial for Python 2.6. However, the differences between
versions are not particularly large, so if you learn one, you should be able to read programs written for the
other without much difficulty.
There will often be a mixture of the text you type (which is shown in bold) and the text the program prints to
the screen, which would look like this:
Halt!
Who Goes there? Josh
You may pass, Josh
(Some of the tutorial has not been converted to this format. Since this is a wiki, you can convert it when you
find it.)
I will also introduce you to the terminology of programming - for example, that programming is often
referred to as coding or hacking. This will not only help you understand what programmers are talking
about, but also help the learning process.
Now, on to more important things. In order to program in Python you need the Python 3 software. If you
don't already have the Python software go to www.python.org/download (http://www.python.org/download/)
and get the proper version for your platform. Download it, read the instructions and get it installed.
Installing Python
For Python programming you need a working Python installation and a text editor. Python comes with its
own editor, IDLE, which is quite nice and totally sufficient for the beginning. As you get more into
programming, you will probably switch to some other editor like emacs, vi or another.
The Python download page is http://www.python.org/download. The most recent version is Python 3.4.3 (as
of February 2015); Python 2.7 and older versions will not work with this tutorial. There are various
different installation files for different computer platforms available on the download site. Here are some
specific instructions for the most common operating systems:
Linux, BSD, and Unix users
You are probably lucky and Python is already installed on your machine. To test it type python3 on a
command line. If you see something like what is shown in the following section, you are set.
IDLE may need to be installed separately, from its own package such as idle3 or as part of python-tools.
If you have to install Python, first try to use the operating system's package manager or go to the repository
where your packages are available and get Python 3. Python 3.0 was released in December 2008; all
distributions should have Python 3 available, so you may not need to compile it from scratch. Ubuntu and
Fedora do have Python 3 binary packages available, but they are not yet the default, so they need to be
Non-Programmer's Tutorial for Python 3/Print version - Wikibooks, ope...
https://en.wikibooks.org/w/index.php?title=Non-Programmer's_Tutorial...
4 sur 74 14/01/2016 19:33
installed specially.
Roughly, here are the steps to compile Python from source code in Unix (If these totally don't make sense,
you may want to read another introduction to *nix, such as Introduction to Linux (http://tldp.org/LDP/intro-
linux/html/index.html)):
Download the .tgz file (use your Web browser to get the gzipped tar file from https://www.python.org
/downloads/release/python-343)
Uncompress the tar file (put in the correct path to where you downloaded it):
$ tar -xvzf ~/Download/Python-3.4.3.tgz
... list of files as they are uncompressed
Change to the directory and tell the computer to compile and install the program
$ cd Python-3.4/
$ ./configure --prefix=$HOME/python3_install
... lots of output. Watch for error messages here ...
$ make
... even more output. Hopefully no error messages ...
$ make install
Add Python 3 to your path. You can test it first by specifying the full path. You should add
$HOME/python3_install/bin to your PATH bash variable.
$ ~/python3_install/bin/python3
Python 3.4.3 (... size and date information ...)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
The above commands will install Python 3 to your home directory, which is probably what you want, but if
you skip the --prefix=$HOME/python3_install, it will install it to /usr/local. If you want to use the
IDLE graphical code editor, you need to make sure that the tk and tcl libraries, together with their
development files, are installed on the system. You will get a warning during the make phase if these are not
available.
Mac users
Starting from Mac OS X Tiger, Python ships by default with the operating system, but you will need to
update to Python 3 until OS X starts including Python 3 (check the version by starting python3 in a
command line terminal). Also IDLE (the Python editor) might be missing in the standard installation. If you
want to (re-)install Python, get the MacOS installer from the Python download site (https://www.python.org
/downloads/release/python-343/).
Windows users
Download the appropriate Windows installer (the x86 MSI installer (https://www.python.org/ftp/python
/3.4.3/python-3.4.3.msi), if you do not have a 64-bit AMD or Intel chip). Start the installer by double-
clicking it and follow the prompts.
See https://docs.python.org/3/using/windows.html#installing-python for more information.
Configuring your PATH environment variable
Non-Programmer's Tutorial for Python 3/Print version - Wikibooks, ope...
https://en.wikibooks.org/w/index.php?title=Non-Programmer's_Tutorial...
5 sur 74 14/01/2016 19:33
1 / 74 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !