Tải bản đầy đủ (.pdf) (979 trang)

IT training the python book the ultimate g unknown

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.63 MB, 979 trang )


W
NE


The ultimate guide to coding with Python
Pi 2
projects
inside
Learn to use Pythont Program games t Get creative with Pi



Welcome to
PyThethon
Python is an incredibly versatile, expansive language which, due to its


similarity to everyday
language, is surprisingly easy to learn even for inexperienced programmers.
It has seen a
huge increase in popularity since the release and rise of the Raspberry Pi, for
which Python
is the officially recognised programming language. In The Python Book,
you’ll find plenty
of creative projects to help you get to grips with the combination of your
Raspberry Pi and
Python’s powerful functionality, but there are also plenty of tutorials that
focus on Python’s
effectiveness away from the Raspberry Pi. You’ll learn all about how to code
with Python


from the very beginning with our comprehensive masterclass, then go on to
complete
tutorials to consolidate your skills and become fluent in the language while
also improving
your computing experience. You’ll learn how to make Python work for you
with tutorials
on coding with Django, Flask, Pygame and even more useful third-party
applications and
frameworks. Get ready to become a true Python expert with the wealth of
information
contained within these pages.



PyThethon
Imagine Publishing Ltd
Richmond House


33 Richmond Hill
Bournemouth
Dorset BH2 6EZ
+44 (0) 1202 586200
Website: www.imagine-publishing.co.uk
Twitter: @Books_Imagine
Facebook: www.facebook.com/ImagineBookazines
Publishing Director
Aaron Asadi
Head of Design
Ross Andrews

Production Editor
Alex Hoskins
Senior Art Editor
Greg Whitaker
Designer
Perry Wardell-Wicks
Printed by
William Gibbons, 26 Planetary Road, Willenhall, West Midlands, WV13
3XT
Distributed in the UK, Eire & the Rest of the World by


Marketforce, Blue Fin Building, 110 Southwark Street, London, SE1 0SU
Tel 0203 148 3300 www.marketforce.co.uk
Distributed in Australia by
Network Services (a division of Bauer Media Group), Level 21 Civic Tower,
66-68 Goulburn Street, Sydney, New South Wales 2000, Australia Tel +61 2
8667 5288
Disclaimer
The publisher cannot accept responsibility for any unsolicited material lost or
damaged in the
post. All text and layout is the copyright of Imagine Publishing Ltd. Nothing
in this bookazine may be reproduced in whole or part without the written
permission of the publisher. All copyrights are recognised and used
specifically for the purpose of criticism and review. Although the bookazine
has endeavoured to ensure all information is correct at time of print, prices
and availability may change.
This bookazine is fully independent and not affiliated in any way with the
companies mentioned herein.
The Python Book © 2015 Imagine Publishing Ltd

ISBN 9781785460609


Part of the
bookazine series


Py5IFthon
$POUFOUT
8(FUTUBSUFE


8PSLXJUI1ZUIPO
$SFBUFXJUI1ZUIPO
XJUI1ZUIPO
.BTUFSUIFCBTJDTUIFSJHIUXBZ
74$SFBUFEZOBNJDUFNQMBUFT
108#VJMEUJDUBDUPFXJUI,JWZ
6TF+JOKB 'MBTLBOENPSF
1SPHSBNOPVHIUTBOEDSPTTFT
16FTTFOUJBM
78.BLFFYUFOTJPOTGPS9#.$
112$SFBUFUXPTUFQBVUIFOUJDBUJPO
DPNNBOET
&OIBODF9#.$XJUIUIJTUVUPSJBM
6TF5XJMJPGPSTBGFBVUIFOUJDBUJPO
5IFDPNNBOETZPVOFFEUPLOPX
844DJFOUJöDDPNQVUJOH
1165XJUUFST0"VUIQSPDFTT
(FUUPHSJQTXJUI/VN1Z

#VJMETJHOJOHSFRVFTUT
88*OTUBOUNFTTBHJOH


1201SPHSBNB4QBDF
(FUDIBUUJOHVTJOH1ZUIPO
*OWBEFSTDMPOF
.BLFUIFCBTJD1JWBEFSTHBNF
943FQMBDFZPVSTIFMM
6TF1ZUIPOGPSZPVSQSJNBSZTIFMM
124"EEBOJNBUJPOBOETPVOE
&OIBODFZPVS1JWBEFSTHBNF
1ZUIPOFTTFOUJBMT
981ZUIPOGPSTZTUFNBENJOT
)PX1ZUIPOIFMQTTZTUFNBENJOJTUSBUJPO
128.BLFBWJTVBMOPWFM
1SPHSBNBCPPLTUZMFHBNF
26$PEFSPDL QBQFS TDJTTPST
1024DSBQF8JLJQFEJB
1VUCBTJDDPEJOHJOUPBDUJPO
6TF#FBVUJGVM4PVQUPSFBEPõ
JOF
120
321SPHSBNBIBOHNBOHBNF


74
6TF1ZUIPOUPNBLFUIFDMBTTJDHBNF
381MBZQPLFSEJDF
5FTUZPVSMVDLBOEZPVSDPEJOH

44$SFBUFBHSBQIJDBMJOUFSGBDF
"EEJOUFSGBDFUPZPVSQSPKFDUT
50#SJOHHSBQIJDTUPHBNFT
"EEJNBHFTUPTJNQMFHBNFT
128
56#VJMEBOBQQGPS"OESPJE
88
.BLFZPVSPXOBQQXJUI,JWZ
62.BLJOHXFCBQQT
6TF1ZUIPOUPDSFBUFPOMJOFBQQT
661ZUIPOUJQT
&TTFOUJBMLOPXMFEHFGPS1ZUIPOVTFST
65IF1ZUIPO#PPL


144
166
66


148
6TF1ZUIPOXJUI1J
1541SPHSBNNJOHJO1ZUIPOPO
3BTQCFSSZ1J
50
-FBSOIPXUPPQUJNJTFGPS1J
1581SPHSBN.JOFDSBGU1J
1ZUIPO
1MBZB.JOFDSBGUHBNFPO1J
8FCEFWFMPQNFOU

162#VJMEBO-&%.BUSJY
UJQT
134%FWFMPQXJUI1ZUIPO
6TF1JUPDPOUSPMMJHIUTFRVFODFT
8IZ1ZUIPOJTQFSGFDUGPSUIFXFC
1663BTQCFSSZ1JDBSDPNQVUFS
140#VJMEZPVSPXOCMPH
(FUXIFSFZPVSFHPJOHXJUI3BTQCFSSZ1J
#FHJOEFWFMPQJOHZPVSCMPH
144%FMJWFSDPOUFOUUPZPVSCMPH


"EEDPOUFOUUPZPVSTJUF
i1ZUIPOJTFYQBOTJWF CVUZPVMMCF
148&OIBODFZPVSCMPH
$PNQMFUFZPVSCMPHXJUIBEEPOT
BOFYQFSUCFGPSFZPVLOPXJUw
5IF1ZUIPO#PPL7


Get started with Python
Get started
Python


with
Always wanted to have a go at
programming? No more excuses,
because Python is the perfect way to get started!
Python is a great programming language for libraries to create a windowed

application, or you could both beginners and experts. It is designed with
try something a little more ambitious like an app such
code readability in mind, making it an excellent
as creating one using Python’s Bluetooth and Input
choice for beginners who are still getting used to
libraries to capture the input from a USB keyboard and
various programming concepts.
relay the input events to an Android phone.
The language is popular and has plenty of libraries
For this tutorial we’re going to be using Python 2.x
available, allowing programmers to get a lot done with
since that is the version that is most likely to be installed
relatively little code.
on your Linux distribution.
You can make all kinds of applications in Python:
In the following tutorials, you’ll learn how to create


you could use the Pygame framework to write
popular games using Python programming. We’ll also
simple 2D games, you could use the GTK
show you how to add sound and AI to these games.
8 The Python Book


Get started with Python
The Python Book 9


Get started with Python

Hello World
Variables and data types


Let’s get stuck in, and what better way than with the A variable is a name in
source code that is associated with an programmer’s best friend, the ‘Hello
World’ application! Start
area in memory that you can use to store data, which is then
by opening a terminal. Its current working directory will be your
called upon throughout the code. The data can be one of many
home directory. It’s probably a good idea to make a directory for
types, including:
the fi les we’ll be creating in this tutorial, rather than having them
loose in your home directory. You can create a directory called
Python using the command mkdir Python. You’ll then want to
Integer
Stores whole numbers
change into that directory using the command cd Python.
The next step is to create an empty fi le using the command
Float
Stores decimal numbers
‘touch’ followed by the fi lename. Our expert used the command
Boolean
Can have a value of True or False
touch hello_world.py. The fi nal and most important part of


String
Stores a collection of characters. “Hello
setting up the fi le is making it executable. This allows us to run

World” is a string
code inside the hello_world.py fi le. We do this with the command
chmod +x hello_world.py. Now that we have our fi le set up, we
can go ahead and open it up in nano, or any text editor of your
As well as these main data types, there are sequence types
choice. Gedit is a great editor with syntax highlighting support
(technically, a string is a sequence type but is so commonly used
that should be available on any distribution. You’ll be able to
we’ve classed it as a main data type):
install it using your package manager if you don’t have it already.
List
Contains a collection of data in a specifi c order
[liam@liam-laptop ~]$ mkdir Python
[liam@liam-laptop ~]$ cd Python/
Tuple
Contains a collection immutable data in a
specifi c order


×