Professional Documents
Culture Documents
The Complete
Building Your
Own PC
Man al
-
-
-
Android Google
Manual Manual
--
The Complete
Outdoor
Photography
Manual
� BDM's i-Tech Special
Starting to learn to code isn't easy but we're here to help you get going. In the
pages of this book you can learn how to start coding using Python, C++, Linux
scripting and FUZE BASIC with the Raspberry Pi. We cover the common pitfalls
and mistakes every coder falls into and how to avoid them in the future.
There's a section filled with some great Python coding examples, to showing you
tips and tricks to help make the system do what you want it to; and we look at how
to get help with your code and how to test it in a safe and secure environment.
With programming you never stop learning, so Coding -Tips, Tricks & Fixes is
here to teach you what you need to continue your journey into the wonderful
world of coding.
BDM's J.Tech Spe<l•t I Codlnt-Tips,Tr1c.kJ& Flxts INTER:HATlONALUCENSING-SlackDogMeda hasmanygreat pubi.K.ationsand BOM's1-TtehSC>td�l-COding..Tips,fr1<b&.flusVol27•san ��'dependent
VolumeZ7 •ISSN::l047.ZZ69 forlicensingwortdwicle. publlutlon tndISIUCh dotsnot nttti.SftityrtfW<tthe viewsor opinion�dthe
Published by;: BhCI. Do9 Ml"'dl•lY�llttd (BJ")M) productrs cont�lned wii:Hft. This oubl.btkln isN)(e-ncbw<l or assodate<tr.
Editor: , .Jdl"lMGalf> fi"'Y WI'/w•thThei.Wi).f:oundlbon, fhtR.spbtrryPtFOU�I\ ARM HQidin;.
Art o.tKtat&Produa:Jon; .M.r1tA¥�lo1d �U4 P)<lhon,OeWn P1ojrt(l., l.m.l•Mll�l.,. l.tn0\10, De-lL H�.P•tkt�d.
E<kol��dn.ign •rctM� 0P<IPl'ftUI;Limited .,d isreprodutt'd unde1
Production�ger. 1� �Nd tic.lft(tto�Dog�.No l)ollttof thisp!AIIic:.lt)c)nmiYbetepJoduct<l in....,.
�.�or*"'' M�odtt e Of tH•II«•(.01'1'\1)11'1)'.AI<OCI'ItiOIU.trtdttnlltstf'd
tfo0Koltffodtf�f'\:�fottht�MC�HM'e�Relevant
Dt'si9A: Aoblftt>'f'W,l�V.'Irv.P.ft fOf"' �il'll�a! S)'SttrnOfintt'gr.M:tdini:Oft"'))��c»t.tbbst
Editorilt Of�f1109r.M�tht��petr"USsb'lolt:hepublls:hH .,....._,_..,_"""'""of........ -.P-o.t.
�H.tt-�trd
�noucumsuruss.bouldUlh. P' lbl.cat)on endtscont.t!'ntSberesold,� s.nw�fl.lll1t<MoloQofl.....'"""'"'"'ond--
SubEditor., PN�¥'Ct
AdcfdonttlfNOts�'Nit!'WIct.pub4k.c;on.rereproducedunderkenc:
e
PrintedMISboundInGrHtenu.nby. 01.1(Of\Midin<In)"formb'jYi''Joltrade�tthept.4ltishe(s..ntteftoenni5S.O'L
• • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••
38
Equipment You Will Need
Getting to Know C++
8 Choosing a Programming Language 40 How to Set Up C++ in Windows
10 Learning to Code- Study Tips 42 How to Set Up C++ on a Mac
12 Coding Tools and Resources 44 How to Set Up C++ in Linux
14 Being a Programmer 46 Other C++ IDEs to Install
18
Say Hello to Python
Why Python?
•
50
Coding on Linux
Why Linux?
20 Equipment You Will Need 52 Equipment You Will Need
22 Getting to Know Python 54 Transfer Mint to DVD or USB
24 How to Set Up Python in Windows 56 Installing VirtualBox
26 How to Set Up Python on a Mac 58 Testing Linux Mint's Live Environment
28 How to Set Up Python in Linux 60 Installing Linux Mint on a PC
30 Installing a Text Editor 62 Installing Linux Mint in VirtualBox
64 Getting Ready to Code in Linux
•
34
Say Hello to C++
Why(++?
66
68
Creating Bash Scripts- Part 1
Creating Bash Scripts- Part 2
•
Beginner Python Mistakes
Code Project Examples
86 Beginner C++ Mistakes
88 Beginner linux Scripting Mistakes 126 Python File Manager
90 Code Checklist 128 Number Guessing Game
92 Where to Find Help with Code 130 Polygon Circles
94 Test Your Code Online 131 Random Number Generator
96 Python OS Module Error Codes 132 Basic GUI File Browser
98 Python Errors 134 Vertically Scrolling Text
136 Text Adventure Script
Programming with the FUZE 138 Python Digital Clock
140 Hangman Game Script
102 Introducing the FUZE Project
142 Where Next?
104 Setting Up the FUZE
144 Common Coding Terms
106 Getting Started with FUZE BASIC
• • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••••
to
Programming is one of the most valuable skills you can pick up these days,
particularly for your career prospects; and great just to test your brain and get to
create something cool too. When you're new to coding, it can be hard to know where
to start and it's easy to get sucked down paths that could waste a whole lot of your
time and money. If you're just getting started on your coding journey, here are some
tips to set you off in the right direction.
''
• • •
• • • •
• • • •
• • • •
• • • •
It may sound obvious but
•
•
: •
•
: Take some time to look at the things coding languages are used for; so if you •
� ........ .
• •
: choosing the •
• : are interested in web design, look at how Javascript is being used on the sites •
•
• • • •
: right coding or •
• : you use every day; if you are in to mobile apps, look into Java or Swift and see •
•
•
•
: programming language to • :• how they make apps work. It can also be useful and encouraging to check out
•
•
• •
:• start learning is important •
• : what qualified coders can expect to get paid by looking at local job sites. We •
•
• • •
: if you want the creative •
• : are not saying that if you learn to code Python you are immediately going to •
•
;
• • •
process to be successful. If •
• :• be offered a £70,000 a year job but it can help spur you on to study harder. •
•
•
: you need learn to code for a specific reason, • •
•
•
l=-
• • • •
• " - -- ·
: finding a job for example, then you obviously : • .. c•·
· �:::-.
•
•
,.. D-- •- .
• •(!•
.
• .
• .._ � _ ,. ... _
.. _ - a_.._ , __ eo- ""' -·
·
- · - �- - -- •
need to learn the language that's relevant. If
:
•
: : • MOM.$111
. . .
•
•
; ;
• •
you simply want to learn coding, then picking • ------ •
• •
•
:
•
a language like Python, that is both powerful : •
•
•
•
• •
•
: and relatively easy to learn, can make success : •
•
•
• • • __
.., •
: more likely. •
•
•
•
•
-
- -- - ..
•
•
• • ---..-· •
• • •
• • • • • • • • • • • • • • •• • • • • • • a • . • ••• • • • •••••• ••••• • • • • __________
·-
...
__..,..__... - •
C:tr"''"'-
-...--..._..._____.._
..,.____..__..____
• ... ___ •
• ---- -·..-- ·-- - - --
..- .. . - -
--·-.....--. .. - - •
---- ---
• • •
• • -- ..
- •••••• •
• ·
•
•
-�-
-- --·-· • •
•
• •
• ·---
• ·-- •
•
-=.
• ·-- -· •
• • ·-- - � -
•
�
• •
• •
•
. ····················· ··········· · · · · · · · · · · · · · · · · · · · · · · · ···················
•
•
•
•
• •
• •
......................�.................................................................;
. .I
• . • • • •.••••••••..... . . . . . . . . . . . . . . . . . •
•
• •
I ·--
.
f
• •
-
• •
-
•
• . .
; choose a task you want to perform with code, and then work
•
• •
•
•
;
• •
: in web design, look at how Javascript is being : programming language study. There are several free "Introduction to •
•
; ;
•
used on the sites you use every day; if you are : Computer Science· courses available online, including a great one from •
www.edx.om
• •
:• in to mobile apps, look into Java or Swift and see : :• Harvard University (via the website). Another study tool that can
•
•
• •
: how they make apps work.IL can also be u seful : : be useful is to play coding games. Two of the best of these types of learning
•
•
;
• • •
: and encouraging to check out what qualifie d : games are Code Combat and CodinGame. Search online to find them. •
•
; ;
• •
coders can expect Lo get paid by looking at • •
....... '·�-·':""
• •
: local job sites. We are not saying that if you :• ...
- -
..
_ --- .......
• •
• ,, ..,lo ..._,_ - .... .. .... •
• • I _., .. _ ...... •
0 --·11•
: learn to code Python you are immediately •
•
•
•
' -
I •
•
•
:
• •
• ....... ........,WI()
•
: going to be offered a £70,000 a year job b ut it •
•
•
• •
•
•
;
• • •
can help spur you on to study harder. •
•
•
•
•
•
• • • •
• • • •
• •
r·· 'I;> · O · Q.· ....
• •
• u • • •
•
I'!;>
• • •
•
• ,.., 1:1 N� V.t.rlablu } : •
•
•
•
. ...... ,, "
1- . . .
• • • •
c;...., •:
• •
• E: 1\.Mn/blSII/"o,t(UJA.punii/MwA(A • •
•
• = /'UM:n/bU·/�S/Aput\1/�ew/otJC • •
•
•
:: /UKn/t»s•t_,/....,../h - �. • • ••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • •• • • • • • • • • • •• • ••••••••••••••
•
•
•
...... . .-: = /'l)fM'S/bu./Ptof«U/A,ptMwi/h«wAPt .. •
•
• S /UW:n/bu VPTo,KU//II,fiUN/"-� • • •
.
• •
.
•
•
•
•
��==�� •
•
•
•••• • • ••• • • •• • • • •• • • • • • ••• • •••• • • • •••• • • •• • • • •• • ••••• • ••• • • • ••
•
•
•
• • • •
• • • •
•
•
• •
• : •
'
• •• • • • • • • • • • • • • • •••••••••••••••••••••••••••••••• • •
• • •
• • •
• • •
• • •
;
• •
•
answers to problems and to ask your own questions to other •
,.
•
2 3
• • •
• • •
•
•
•
•
: afraid to ask questions. You might find that others are a bit •
•
;
• •
•
•
•
• snappy if you don't follow the questioning rules of that forum •
•
•
•
•
•
• : but just figure out what you did wrong and learn to ask better •
•
• • •
•
9
: questions. Don't worry though, ask your questions, listen to
• • •
• • •
• • •
•
•
•
•
•
: the answers and remember to thank people for their help, •
•
;
• •
•
•
•
•
•
remembering to come back when you a re more experienced •
•
•
•
•
•
•
: and answer a few quest i ons yourself if you can. •
•
• • •
• • • •
• • • •
· ..
. .
-
• • •
• -
• • • •
• • • •
• •
l,..
•
• • c::::II DIZ :::I
.I III:II I::II:::
I' ' if •
• • •
• •
•
•
• • -
·- - -- •
• • • •
.
•
--
• • . '
•
• • --- -- •
• --·- ..
-
• --
• • •
--
•
.......--.....--....-
• • •
• • • . . ' . •
• • • ---
- - - •
• • • •
' _...._____
• • • •
• • . .- ._ � •
• -
-
• • • -- -- •
•
•
•
DON'T SET YOURSELF DEADLINES •
•
•
•
•
• •
---
•
-
-------
•
•
•
• • • -- -
•
• • • •
• • • . . ' ·--....----.... •
• • • ---
.. - - -· •
;
• • •
If at all possible, that is. Deadlines. although a part of a • •
• •
•
-
• • • lliltcl_... •
• ---
•
:• professional programmers life, can definitely hurt your • - - -
• • •
•
•
• • - • - .,....._...,.___ _ ·---
•
•
: of learning to code will not do you any good, and may make
•
•
•
•
•
• •
Too s a n"""
"'"... 0 """"'
Reso u rces
Learning to code isn't just about studying the syntax of a language and memorising
commands. If you don't have the tools and resources needed to test and express that
language, you won't be able to challenge and refine your skills.
GITHUB 0 . ___,. - -
Any kind of file can be up loaded to GitHub but it's 151 , __ . d3 � n���W...... •
-- _..._.
popular. It provides access control and several · -- -·
•• ___ ,.......
.. ,_. ·-- ·-
collaboration features such as bug tracking, feature •• --- ·--
·- --.- .....---- -
possible project. GitHub reports having mo re .... . --
•
-- ---- --.
•
HEROKU .. .. ... ,_
-
• o-- -
v
If you are d evelo ping a web app, you will need to host it before
people can access it. GitHub can host your code but that doesn't --
< -
mean that end-users will be able to use the working app. This -
- - - --
is where a service like Heroku comes into its own. Heroku is a
cloud platf orm that lets you build. deliver. monitor and scale
-- .... .
-........
apps, the fastest way to go from idea to URL, bypassing all those - -· ·- - ·--
-
·-
infrastructure headaches. It makes the processes of deployi ng, •III
configuring, scaling, tuning, and managing apps as simple and
._...,.___
.. _
s t raightforwar d as po ssible, so that you can focus on what's most .·--------
--- ·-·--
-· ...._.__..,___,...._
important: building great apps that delight and engage customers. --··- --
------·--------
---
INTEGRATED DEVELOPMENT
ENVIRONMENT
Integrated Development Environments, unlike text
editors, offer a co mple te coding environment. This
makes it easier for some programming beginners to get
to grips with a new language. Integrated Development
Environments, also known as Code Editors, are software
applications that provides comprehensive facilities to
computer programmers f or software developmenl
An IDE normally consists of a source code editor, build
automation tools and a debugger. Most modern IDEs
have i ntelligent code com pletion.
,_.,. for professional use of repeated code that is common to othe r functions. Snippets
--- -
-·---· -- -·--
-
It's built and many times. The main id ea is to make the process of reusing as easy
. ------ -..
--...
- - ;;.;;: .:;:_-
-- - ---
- run by users as as possible to avoid wasting your valuable time on writing the same
• -·-
-
• .... • ::::--::::.:
--=: part of the Sta ck code again. You can download the Snippets manager free from
-
---
http://sojooets.me/.
-
- ----- - :Er..:cr:-- Exchange network
.. ----·..--- ..
_ f..":.?-.7-'=" of Q&A si te s . With
·> "". its users h elp ,
about programming. This site is all about getting answers; it' s not a
discussion forum so there's no chit-<hat. Good answers are voted up
and rise to the top and the best answers show up first so that they
are always easy to find.
wwVLbdmpyblicatloos.com
Coding and Programming
Volume 27
Being a Programmer
I int t,spe«O•q;
wid d s(i jWIIIIP4) Naturally, time has moved on and with it the technology that we use.
{
st•tic .,t •�t; However, the fundamentals of programming remain the same; but what
exactly does it take to be a programmer?
, ...
...
��� if(j�·l)
t··,;
ds� t-; The single most common trait of any programmer, regardless of what
•indow(2,1S·t,11,2S);
�prlntf(• '"'); they're doing, is the ability to see a logical pattern. By this we mean
cprintf(•
JIIIWV\NW!l");
cpdntf(" NIMIIIINII"'); someone who can logically follow something from start to finish and
Jo4 c.,-in tf(" MIWII'Inl'l..) ;
tl)f"if'l lf(" Jll fiiWINIIVIIn "); envisage the intended outcome. While you may not feel you're such a
cprintf(" � � ) ; "'
cprintf(" nNVIANIMIIM n ") ; person, it is possible to train your brain into this way of thinking. Yes, it
cprintf(" MMIUWin "):
if( Ju� .., II j_.•Jll takes time but once you start to think in this particular way you will be able
U cpdntf(" n��n M "h
4 C.Pf'intf(" M "" "); to construct and follow code.
4 ) else if(a··l)
. {
44 cprintf(" nMn noo ");
4 cpri ntf(" """ " ); Second to logic is an understanding of mathematics. You don't have to be
•• •..2;
47 ) at a genius level but you do need to understand the rudiments of maths.
•• else 1t(oa..2)
•• { Maths is all about being able to solve a problem and code mostly falls under
.. cprintf(" MN M ");
cpdntf(" "" ")J the umbrella of mathematics.
52 a•l;
I
54 cprintf("
ss dehy(spud)• Being able to see the big picture is certainly beneficial for the modern
)
void obj() programmer. Undoubtedly, as a programmer, you will be part of a team
{
st•tic iftt x•G,sc�•l; of other programmers, and more than likely part of an even bigger team
if(•••S6 && t(•)
. { of designers, all of whom are creating a final product. While you may only
,.'
'-<",....
be expected to create a small element of that final product, b eing able to
Being able to follow a logical pattern and see an end
under stand what everyone else is doing will help you create something
result is one of the most valued skills of a programmer.
that's ultimately better than simply being locked in your own coding cubicle.
• • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••
18 Why Python?
20 Equipment You Will Need
22 Getting to Know Python
24 How to Set Up Python in Windows
y Pyt o n?
There are many different programming languages available for the modern
computer, and some still available for older 8 and 1 6-bit computers too. Some of
these languages are designed for scientific work, others for mobile platforms and
such. So why choose Python out of all the rest?
PYTHON POWER
Ever since the earliest home computers were available, enthusiasts, users and professionals have toiled away until the wee
hours, slaving over an overheating heap o f circuitry to create something akin to magic.
These pioneers of programming carved their way into a new Regardless of whether you use an Android device, iOS device, PC,
frontier, forging small routines that ena bled the letter '/>: to scroll Mac, Linux, Smart TV, games console, MP3 player, GPS device built-in
across the screen. It may not sound terribly exciting to a generation to a car, set·top box or a thousand other connected and 'smart'
that's used to ultra high-definition graphics an d open world, multi appliances, behind them all is programming.
player online gaming. However, forty-something years ago it was
blindingly brilliant. All those aforementioned digital devices need instructions to tell
them what to do, and allow them to be interacted with. These
Naturally these bedroom coders helped form the foundations for instructions form the programming core of the device and that core
every piece of digital technology we use today. Some went on to can be built using a variety of programming languages.
become chief developers for top software companies, whereas
others pushed the available hardware to its limits and founded the The languages in use today differ depending on the situation, the
billion pound gaming empire that continually amazes us. platform, the device's use and how the device will interact with its
environment or users. Operating systems, such as Windows, macOS Python was created over twenty six years ago and has evolved to
and such are usually a combination of C++, C#, assembly and some become an ideal beginner's language for learning how to program a
form of visual-based language Games generally use c++whilst web
. computer. It's perfect for the hobbyist, enthusiast, student, teacher
pages can use a plethora of available languages such as HTML, Java, and those who simply need to create their own unique interaction
Python and so on. between either themselves or a piece of external hardware and the
computer itself.
More general-purpose programming is used to create programs,
apps, software or whatever else you want to call them. They're Python is free to download, install and use and is available for Linux,
widely used across all hardware platforms and suit virtually every Windows, macOS, MS-DOS, OS/2, BeOS, IBM i-series machines, and
conceivable application. Some operate faster than others and some even RISC OS. It has been voted one of the top five programming
are easier to learn and use than others. Python is one such general languages in the world and is continually evolving ahead of the
purpose language. hardware and Internet development curve.
Python is what's known as a High-Level Language, in that it 'talks' So to answer the question: why python? Simply put, it's free , easy to
to the hardware and operating system using a variety of arrays, learn exceptionally powerful, universally accepted, effective and a
,
getTargetException( ) ) ;
24 } while (•tte.pts I• t alld "•" in -ord...t�o�eued):
25 } print((•\nVou h•ve (} •tt �
..pts r�lnlnl " ).f o •t(ett
..ptt))
26 jolned_liiOrd • " " .jolA{wol"d_autt.Ud)
prtnt(jolntd_�)
....
•
Python is a more modern take on BASIC, it's easy to learn
and makes for an ideal beginner's programming language.
E� u i me n t You
•
I Nee'"""
You can learn Python with very little hardware or initial financial investment. You
don't need an incredibly powerful computer and any software that's required is
freely available.
D COMPUTER
Obviouslyyou're going to need a computer in order to learn how to program in
Python and to test your code. You can use Windows (from XP onward) on either a 32
or 64-bit processor, an Apple Mac or Linux installed PC.
:=J AN IDE
An IDE (Integrated Developer Environment) is used to enter and execute Python
code. It enables you to inspect your program code and the values within the code, as
well as offering advanced features. There are many different IDEs available, so find
the one that works foryou and gives the best results.
D PYTHON SOFTWARE
macOS and Linux already come with Python preinstalled as part of the operati ng
system, as does the Ras pberry Pi. However, you need to ensure that you're running
the latest version of Python. Windows users need to download and install Python,
which we'll cover shortly.
D TEXT EDITOR
Whilst a text editor is an ideal environment to enter code into, it's not an absolute
necessity. You can enter a nd execute code directly from the IDLE but a text editor,
such as Su bli me Text or Notepad++, offers more advanced features and colour coding
--·-·-· when entering code.
--
-
•
D INTERNET ACCESS
- -
- -
- -
- - • -
-
- • -
Python is an ever evolving environment and as such new versions often introduce
new concepts or change existing commands and code structure to make it a more
�,;=-�,
-·�
· - - - - - - - - - -
..r •e •- • .�•
::.;_
--=..
:...
::.:_
..;:; ___� I efficient language. Having access to the Internet will keep you up-to-date, help you
,._
-
-....-
�
.
. out when you get stuck and give access to Python's immense number of modules.
- -
...- -
-
- -
�
·
-
--·
- ... - ..
---
- . Despite what other books may lead you to believe, you won't become a programmer
..... . .... ..... . __ _
_ II
-··--
··---- ..
,,,_,..
in 24·hours. Learning to code in Python takes time, and patience You may become
··· -
.
.. --.
·
- .
.,.. _.. -----·
.
-
. ...... stuck at times and other times the code will flow like water. Understand you're learning
.
,.. ---- ----.-...
.
,.___.. -.. .
. . _ ,.,..
.. -.. __ .. .
something entirely new, and you will get there.
THE RASPBERRY PI
Why use a Raspberry Pi? The Raspberry Pi is a tiny computer that's very cheap to purchase but offers the user a fantastic learning
platform. It's main operating system, Raspbian, comes preinstalled with the latest Python along with many Modules and extras.
You can pick up a Pi ror around £32 or as a part of kit for £50+,
•
:
•
•
•
•
: depending on the kit you're interested in. •
• •
•
• ••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • •• • • •• • • • • • • • • • ••••• • ••
• • • • •• • • • • • • • • • • •• • •• • •• • • •• • •• • •••• • • • • • • • • •• • • • • • • • • •• • • • • • •
•
•
•
•
•
FUZE PROJECT •
•
•
•
•
•
The FUZE is a learning environment built on the latest model •
•
•
of the Raspberry Pi. You can purchase the workstations that •
•
come with an electronics kit and even a robot arm for you
•
•
•
••••••••••••••• • • • • • • • •• • • • • •• • • ••••••••••••••••••••••••••••• •
•
• •
• •
• •
BOOKS
• •
• •
• •
• •
• •
• •
• •
••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••• • •
•
We have several great Raspberry Pi titles available via www. •
•
• •
•
•
•
•
• •
•
bdmp ub l a
ic t .
i ns
o cm
o . Our Pi books cover how to buy your •
•
• • •
•
first Raspberry Pi,
RASPIAN
• • •
• • •
• • •
•
•
•
•
•
•
• set it up and use •
•
• • •
• • • it; there are some
: The Raspberry Pi's main operating system is a Debian-based • •
•
•
• • great step-by
: Linux distribution that comes with everything you need in • •
step project
• •
• •
: a simple to use package. It's streamlined for the Pi and is an
•
• •
• • •
• examples and
: ideal platform for hardware and software projects, Python
•
•
guides to get
•
• • •
.
• •• • • • • • • • • • • • • • •
• ··- •
•
•
� -
......-....
..
.
r:: ::...;.
--
..
-:-..
··'1.
"". _
-n
...._
-
" ___
---
...,., -·
--- ·
·- ·--'-
•
•
•
. •
--
-
• •
·---·-·-
• - •
. = •
-
. - •
• - •
. - •
-
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
•
• • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••• • • • • • • • • •
'-'etti n to
Kn ow P t o n
Python is the greatest computer programming language ever created. It enables
you to fully harness the power of a computer, in a language that's clean and easy
to understand.
WHAT IS PROGRAMMING?
It helps to understand what a programming language is before you try to learn one, and Python is no different. Let's take a
look at how Python came about and how it relates to other languages.
.
. . . .. . . . . . . . . . . . . . . . . . . .... . . . . . .
•
• •• • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • •• • • • • • •• • • • • •••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • •
• •
• •
•
•
•
•
PYTHON
•
•
•
PROGRAMMING RECIPES
'
•
• • -- .
• • Programs are like recipes for computers. A
• • 1 put tee aroms of self·raisina flour in a bowl .
: A progra mming language
•
•
•
•
recipe to bake a cake could go like this: 2 Add tee arams of butt•r to th• bowl •
•
instructions can be as simple •
•
Add 100 grams of butter to the bowl.
: as displaying your name
•
• Add 1 00 millilitres of milk.
• •
: or playing a music file, or •
• Bake for half an hour.
• •
: as complex as building a •
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••• ••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
• •
: whole virtual world. Python •
•
•-..., a--
lllelfi'C
,
� ·•'
..,. ollfiJ.l'.rn
:
•
is a programming language
•
•
•
CODE '-"'�· •- - o- ·- ..,.
• o cn�•(ohj•��) :
'
•
: conceived in the late 1980s • 1n1t (3-elt):
• • Just like a recipe, a program consists of instructions that you follow •elt.1nqredlente - II
: by Guido van Rossum •
• oook (aelf, 1ng�edi ent5) :
:
• in order. A program that describes a cake might run like this: I •H�t� O'J t"":ltk� • • •"
I
•
bowl = D
•
: Informatica (CWI) in the •
I
• •
bowl • ll
: Netherlands as a successor to
•
• Aour = 100 flour • 100
I l
•
- :.o
•
butter
: the ABC language. •
• butter = 50 •llk - 100
I
• • baw1.app�nd( lflour,bultar,m1lkJJ
•
•
•
•
mi.lk = 100
: Guido van Rossum, the
•
• I bowl.append([Aour, butter,mi.lk])
I
• •
: father of Python.
•
•
•
•
cake.cook(bowl)
• •
• • • ••••••••••••••••••• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••
• •
• •
• •
•
•
•
•
•
•
•
PROGRAM COM MAN OS
•
• •
•
•
•
•
You might not understand some of the Python commands like bowt.append and cake.cook(bowl). ,
• •
•
•
•
•
The First is a list, the second an object; we'll look at both in this book. The main thing to know is
•
that it's easy to read commands in Python. Once you learn what the commands do, it's easy to
•
• •
• •
•
•
•
• Figure out how a program works.
• •
• •
• •
•
•
•
•
,.._ 1 & 2 ... . a •
Pe ,. V'lell � � -
� tte
" ,.... ... .- ,., ---
r:o::
.,
,_=-:-:-
,, ,_�
l(,:.,�
,.=":"'=
,. ><"'
"""•'= .� I•
• •
.,...,. .,.,
•
•
•
•
'"' • 1-
»•
"'•-=
o J:=:
J1-;;
,,.,
:--------....,., t••••l•n )
' "·' 11 _ u_, ..u,
�t#t4
•
•lac.-e()• ,_ -• ---Is-.
•
• • ,,. ·- •u ...
•b• •
• •
•
• II
•
...
,
,._ .. ,.
• •
lliUttiN' .. ,.
• •
•1Uc • ,.
• •
wnu. •ul•H
• •
• • ..., ...,.IC'I-.
...
• •
• • ,.,.. "'.(...1)
• •
• •
• •
• •
• •
•
• •• • • • • •• • • • • • •• • • • • • • • • • • • • •••••
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . ............... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
.
·--
i.mport thi.s into Python and it wilt display all the principles.
• (> ==.. ===
� :-!.- 0
. .
-
-
- ·-
-
-
- � _ .. __ _- •
·-
__.. _
Python J.4.2 (d•f•ult. OCt 19 2014, tJ:J1:11)
·----··-·------ -·--- ·-·----·--- (6CC -4.9.11 on hnull
-
----·
- •. �
-· ------,_
·
-
-· ..
. ._..__.,__ _.
_
--
. ___
.
------....-·--· _
Typ• •copynatn", "cr.-dtts" or "lcente()"
i for 1110r-e 1nfo,...tton.
-----·
.. ...__
--··-- ...
-
>» 11 pc
-
-
-·-
- ·--
-
---··-----·-·-----· this
- - ...
..
___-_____ _.,
-
.----·--·-
...-------·--
- __ -·-·-··--
.
P
The zen of ython, by Tia Peters
-
-
--- ·-------·---- --
-
--
-"------···--·---
·-...�-
...-
-- ...-----
-... ·--- - ·-
·-
-
--
_ ---
-- ... ...
----·----·------- '-
-
- . Bnutlfu l ts bttt•r than U fi ly.
__ ---....
.. �- - - �- -·--- ..-
.. ..._..
than lapllClt .
,__
- -
Expltctt i s better
cc-ple•.
....
--· ·--..........
Sllflple is better thin
-
·
-
on w:a.n)O:
T� "copyr:a.�t",
>>>
,,
let d ) ) e .: wi::l : �
...
....., e t U
•
on 1 n _ows
Windows users can easily install the latest version of Python via the main Python
Downloads page. Whilst most seasoned Python developers may shun Windows as the
platform of choice for building their code, it's still an ideal starting point for beginners.
Start by opening your web browser to www.python. Choosing the Customise option allows you to
STEP 1 STEP 3
erg/downloads/. Look for the button detailing the specify certain parameters, and whilst you may
download link for Python 3.x.x (in our case this is Python 3.6.2 but as stay with the defaults. it's a good habit to adopt as sometimes (not
mentioned you may see later versions of 3). with Python, thankfully) installers can include unwanted additional
features. On the first screen available, ensure all boxes are ticked
and click the Next button.
Lookmgfor Python with a different OS? Python forWmdows. fid Python lftt IU•te
lulUx/UNIX. Mdt OS X. Other P.J py lluncntr PI fOf' Ill uttn (,.qvirts tlevat•on)
• • • • • • • • • • • • • ••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • . .. . . . .. .. .. . .. ..
.. .. . .. .. . .. . . .. . . . .. .
... . . .. .. . .... .. .... ... .. .. . .. . .. . .. . .. .
STEP 2 Click the download button for version 3.x, and The next page of options include some interesting
STEP 4
save the file to your Downloads folder. When the additions to Python. Ensure the Associate file with
file is downloaded. double-click the executable and the Python Python, Create Shortcuts, Add Python to Environment Variables,
installation wizard will launch. From here you have two choices: Precompile Standard Library and Install for All Users options are
Install Now and Customise Installation. We recommend opting for ticked. These make using Python later much easier. Click Install when
the Customise Installation link. you're ready to continue.
� c�romue ·�tllltton
ChMM kH.
.toOl'!Alf'df-...u
python
J -
python
� 1!211nst.,IIK!nd'ler lot'II UNf'l. (r'tor;»mmended) r
windows CJ Add Python 3 6 to PATH a
C
nce
l windows Cancel
You may need to confirm the installation with the Clicking on the IDLE (Python 3.6 32-bit) link will
STEP 5 STEP S
Windows authentication notification. Simply dick launch the Python Shell, where you can begin
Yes and Python will begin to install. Once the installation is complete your Python programming journey. Don't worry if your version
the final Python wizard page will allow you to view the latest release is newer, as long as it's Python 3.x our code will work inside your
notes, and follow some online tutorials. Python 3 interface.
python
fnr
wind ws Cl
ost
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Before you close the install wizard window, If you now click on the Windows Start button again,
STEP 6 STE P 9
however, it's best to dick on the link next to the and this time type: CMD. you'll be presented with the
shield detailed Disable Path Length Limit. This will allow Python Command Prompt link. Click it to get to the Windows command line
to bypass the Windows 260 character limitation, enabling you to environment. To enter Python within the command line, you need
execute Python programs stored in deep folders arrangements. to type: python and press Enter.
Again, click Yes to authenticate the process; then you can Close the
installation window. il <-- -
Close
. ............................. .. . . . . . . . .. . .. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. .. . . . . . . . . . . . . . . . . . .
H ow to ...., e t U
o n on a
If you're running an Apple Mac, then setting up Python is i n credibly easy. ln Fact a
version of Python is already installed. However, you should make sure you're running
the latest version.
INSTALLING PYTHON
Apple's operating system comes with Python installed, so you don't need to install it separately. However, Apple doesn't
update Python very often and you're probably running an older version. So it makes sense to check and update first.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • •• • • • • • • • • • • • • • •••••••••••••••••••••••••••••••••••••••••••••••
Open a new Terminal window by clicking Go> CUcko n the latest version of Python 3.x, in our
STEP 1 STEP 3
Utilities, then double·click the Terminal icon. Now case this is the download button for Python 3.6.2.
enter: python --version. You should see "Python 2.5.1" and This will automatically download the latest version of Python and
even later, if Apple has updated the OS and Python installation. depending on how you've got your Mac configured, it automatically
Either way, it's best to check for the latest version. starts the installation wizard.
...._.._,_UJ..-- ....
·· -
I k for a spte•flc.
ooing
IIII'Ylbfo'
P)'flon rtlffttlby'lfol"«<fl
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Open Safari and head over to www.Qvthop.ora/ With the Python installation wizard open, click on
STEP 2 STEP 4
downloads. Just as with the Windows set up the Continue button to begin the installation. It's
procedure on the previous pages, you can see two yellow download worth taking a moment to read through the Important Information
buttons: one for Python 3.6.2, and the other for Python 2.7.1 3. section, in case it references something that applies to your version
Note, that version numbers may be different due to the frequent of macos. When ready, dick Continue again.
releases of Python.
.. �
..,...., ., l
.ft
,_..,.._.........,.,...
.. ,..,
. t1rWcOIX tO.&.....fllr
. ..
..... � -
!,_, 1!11---�Ji----- 64
�
,...... ...,
.,... ,,.. .. .... ... ........ ..
__...........................�..,
.
. .......
.. ... .. .. ....
.-..,: .... .. .
....... .... ....
.... . . .,. .. ..... .... . .... .....
--- • .,. ,...., ....... ., .. .-filf. ..._ ..,._
..
...
...
....caa ... .,.....,... � ..... .......
.. .,
.......
I'!III IIOd
..,.,_,._
..
_....,
. ....
.... .. ,..
('trP!Jdt ,
._•
•, =
-tii(W"''"'·....-�
..
-�,., ....... .. ,..... ....
. .. ,.,...
,,_�--�-CII:III'r
_ .._
..... --� �.,....... ... .... .... ..... ""' .... ..... ..
..tllflowt!UINIll �_,-�-...,o,N .....
�...,...�_ ......__..,.�- ...,._ .
.... .. ....... .. .... �
- ��� ....... ,..uo.
--------
..
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
STEP S The next section details the SOftware License There's nothing much else left to do in the Python
STEP S
Agreement, and whilst not particularly interesting installation wizard so you can click the Close button.
to most folks, it's probably worth a read. When you're ready, dick on If you now drop back into a Terminal session and re-enter the
the Continue button once again. command: python3 version you can see the new version is
-- ,
now listed. To enter the command line version of Python, you need
to enter: python3. To exit, it's: ex\t().
,....,
. .......... ,........... ,,.,_)
..,t_ , . • . ,
hll• INI(t.. ,.............. I
-�=�
...- ��������
.. ,... f.ulcltOOMfluMIIIt'NO!IIon�M._COc>or...liilt
NII:IOftlll......,_lrlj�fCMll-...1/NWW.t'lllMI:ft
ln .QJa)
".-..1111\ v..,. •
._ lit.....--al..."""",,...
tcmllt tfl...
hM!Itloot O.llltlllldlllt��....��-·����
�oom.IOifrllhtlie�Pytloni..MMINIII.InOdDbltOIIM
,..p•,
.. 1111I'VNiniAIM..,_mo..tl-.OlpllllC�e•tontI'IOW
ltclf QOitof
tfl
lcn, ...l!lp/WihflOI)f.oGI'II}hiOOI,litPyi\Otl
IO�t<WII fot.IIIOIO...,.Ill,-Nll>t!llo-�n..,.tlfJ•••
� IIIOn>O'(I Iii OI;I'I �CI ..Iedl!l�loV•n�
....IMtl�ll
... ....flt' l»pt COIPOI
..OIII•IIIPOfiiOIIrlg
.......,ol,.,.,
• • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Fina lly you're be presented with the amount of You need to search in Finder for the Python IDLE;
STEP 6 STEP 9
space Python will take up on your system and an when you've Found it, click it to launch and it
Install button, which you need to click to start the actual installation should look similar to that of the Windows IDLE version shown on
of Python 3.x on to your Mac. You may need to enter your password the previous page. The only difference being the Mac detected
to authenticate the installation process. hardware platform it's running on.
• ll
6.D
Pyt� l 6.1 (vl ' I •t4r79l,loll, Doc 22 211,, 17 Zl IJ)
[GCC •.z I (._,le In< ...nl4 5664) (ok>l l)) .., ._,..,.
t� •co,yrt�l·.·c�e4\tl• or •tlc�••()• for ..re tnfor..tlon
n..•••t.lt 110 ...., tMtt._..,._ t-...cr �u UJbtlkG� 1'he W�I\Oft •f Tcl/T� (I S.t) in ute eey M ""'t••l•
Oil\ IM:IAto.......•� ��-cl
Y\0\t htu·/1- pyt� O<"t/-IM4/..Vlclll<l '"" cvrr..
t ••f..-t•on
.. .-..._....._��MD'.
Jou I
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • •
• • •
The installation shouldn't take too long; the older Older Mac versions may have trouble with the
STEP 7 STEP 1 0
Mac Mini we used in this section is a little slower newer versions of Python in which case you will ,
than more modern Mac machines and i t only took around thirty need to revert to a previous Python 3.x build; as long as you're using
seconds For the Installation Successful prompt to be displayed. Python 3.x, the code in this book will work for you.
....., e t U
o n i n Li n u x
Python version 2.x is already installed in most Linux distributions but as we're going
to be using Python 3.x, there's a little work we need to do first to get hold of it.
Thankfully, it's not too difficult.
PYTHON PENGUIN
Linux is such a versatile operating system that it's often difficult to nail down just one way of doing something. Different
distributions go about installing software in different ways, so we will stick to Linux Mint 18.1 for this particular tutorial.
• • • • • • • • • • • • ••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • •
First you need to ascertain which version of Python Some Linux distros will automatically update
STEP 1 STEP 3
is currently installed in your Linux system; as we the installation of Python to the latest versions
mentioned, we're going to be using Linux Mint 18.1 for this section. whenever the system is updated. To check, first do a system update
As with macOS, drop into a Terminal by pressing Ctrl+Atl+T. and upgrade with:
sudo apt-get update && sudo apt-get upgrade
d�vod(;�VldofTlont -
file Edo
t View Search Termonal Help
Enter your password and let the system do any updates.
file Ecit V
oew Se• rmonol H
rth Tt ep
• • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • •
Next enter: python --version into the Terminal Once the update and upgrade is complete, you
STEP 2 STEP 4
screen. You should have the output relating to may need to answer 'Y' to authorise any upgrades,
version 2.x of Python in the display. Ours in this case if Python enter: python3 --version to see if Python 3.x is updated or even
2.7.12. installed In the case of Linux Mint, the version we have is Python
.
• •• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
However, if you want the latest version, 3.6.2 as per Within the Python folder, enter:
STEP S STEP S
the Python website at the time ofwriting, you need
./configure
to build Python from source. Start by entering these commands into
sudo make altinstall
the Terminal:
This could a little while depending on the speed of your computer.
sudo apt-get install build-essential checkinstall
Once finished, enter: python3 . 6 --version to check the
sudo apt-get install libreadline-gplv2-dev
installed latest version.
libncurseswS-dev libssl-dev libsqlite3-dev tk-dev
libgdbm-dev l ibc6-dev libbzZ-dev
+ •
-
•
+
-
• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
Open up your Linux web browser and go to For the CUI IDLE, you need to enter the Following
STEP 6 STEP 9
the Python download page: www.oytbon.org/ command into the Terminal:
downloads. Click on the Download Python 3.6.2 (or whichever
sudo apt-get install idle3
version it's on when you look) to download the source Python-
3.6.2.tar.xz file. The IDLE can then be started with the command: idle3. Note, that
IDLE runs a different version from the one you installed from source.
dJivt
� tbvt 3.
ckn J nt -tOownto..b/Pyth on-:6...2.
.., E di "'""' surm r...- Help
l�!Vw.�....
._...,_»
lfllftlr;n•liZ•d'- ,,t ..
«1
,_ �,_,..,...Mrt
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
In the Terminal. go the Downloads folder by You also need PIP (Pip Installs Packages) which is a
STEP 7 STEP 10
en tering: cd Downloads/. Then unzip the tool to help you install more modules and extras.
contents of the downloaded Python source code with: tar -xvf Enter: sudo apt-get install python3-pip
Python-3 . 6. 2. tar . xz. Now enter the newly unzipped folder
PIP is then installed; check for the latest update with:
with cd Python-3 . 6 . 21.
pip3 install --upgrade pip
When complete, close the Terminal and Python 3.x will be available
via the Programming section in your distro's menu.
�
--�- .. . - + X
���...�
ile E dl Vlfto SHfd't Tttl'l'liNt
f t
..
... ,�... � •- •.,.<1n -1111 '1""' 1"'·'-'11 tY'�'I<)n} PlC
d�.,g p.iC�Joo(}f' '. 1 �!\
fl:�a O�nf'
Bt�tl1tnq -1rrA'n,1rn<J ''""
lil:eadt.,g �ta�f' l''"or•Hton
" 0.0'14'
TM follo..trq li·1dltl�n.ll p.hl..ll<)il'\ .Jl·. t><e l<"\T-ll :,.<1
P;'T,.,on y:j; •"'�
��c� �, d�d p•c� •q��
P';'ll'1on� 1..
�- p�TI'1on' '"'"j;loo1\ p•l"'<l"d •"'""1
Tn,._ foll<Nl"Q �U· pAd.,1QII'� on\\ bf' IMIAI1f'tf
P'fT1'1on-ptp ..n: p·,.tl'1onl pip
e to •lp(jr-'11� . ? to l'lf' ...'.'j' \fl\•all 8 to ...
.!)..... .Jll'lr1 1� not to upqr-ltf.-.
Fc.;o�d to get 1. ;>19 kll o' arrnt·'•'
Aftf'r 1111\ o�f'r.llton, 1,7A() �A of .vl<ltlton.-·, c'll\\0. 'f'·Hf' w1l\ bf' u\f'd.
Do 'IOU want to COf\ttnu�' IY;r•l I
l nsta 1 n a
Text E""" i to r
It's not entirely necessary (as you can use the IDLE) but a text editor will help you
immensely when you're entering code. A normal word processor inserts its own
unique characters, paragraph settings and much more, so it's not a good platform for
Python code.
SUBLIME CODE
Sublime Text is an excellent, cross-platform text editor that's designed for entering code. It has a slick interface, many
features and performs magnificently. In short, it's an ideal starting point.
• • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • •
STEP 1 Let's begin by navigating to the Sublime Text Whichever version you choose, download the setup
STEP 3
webpage, to download the latest version for files and double-dick them to begin the set up
whatever operating system you're currently running. You can find process. If you're using Windows, which we are in this instance, then
the website at www.syblimetext.com, together with a download you see the standard installation wizard. The defaults wilt suffice, so
button for the detected OS that you're using. go ahead and install the program.
- •• • • • • #
- -· .. .. ·- ·-
Sublime Text
-
-·-··----...--...-
--·
·------- -
Welcome to the Sublime Text 3
�--
----
- =
- ���
---
-- -,
-- Setup Wizard
·-
·
..
·
·-
11'os W1l ntol 5<blmoText IUid 3126 on yoo..- """""' •
. .
·
·- Ita'""""''"'oded lhot you dooo ol olher aq:jcat>onsbefore
contrur>g.
·-
·
·
·
·-
............ ........
.
,.,- . ... .
_.......-.....
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
However, if you want to specify a particular When installed, Sublime defaults to a black
STEP 2 STEP 4
operating system version, then click on the background and white text; whilst this is perfectly
Download link found in the top menu bar on the si te. This will open fine for most users, it's not always the most comfortable viewing
a page with links for the latest version for OS X, Windows, Windows setup. Thankfully, there are countless themes you can apply by
64-bit and Linux machines. clicking Preferences > Colour Scheme. We've opted for IDLE in
this screenshot.
- 0 X
- -
Sublime Text 3
Download
�1-l•Ooft......llllbec..I..----.Jt& ..-
-
·...-
NS!l.twt for Qllllln.llfCI �.w Tllw• 1!1 Oll'
�=
• ••
w
.......
.=
l ....
.
""
= ...
l
BDM's 1-Tech Special I Volume 27
Installing a Text Editor
Sublime Text offers some excellent features over Sublime isn'tjust for Python either. With it you can
STEP S STEP S
that of the standard Python IDLE. For example, build code for a number of programming languages.
enter the following: Click on Tools > Build System to see which languages you're able to
build with in Sublime.
pri.nt ("Hello everyone!")
- 0
This is an actual Python command, which will print the words
Hello everyone! on the screen. Notice how Sublime automatically ,.. ,.. _ ....
..,_ .
3
...-� ..
lf ,.,..,
·
t t -u. -,.-"}
· -
,..t•..: {"T�u u to>wl"j
[�!!.t.!
...., !i:
= ==:::;::fl
. .... .
...
........ Cloi•VI.ft•t
• "
lll-•lo•..
(to SitroJIIIF·h
,.,.
'
0
......
pnntn • "
. . ' Clii•Q .......
1 print (.") . . •· 'f
•. ·-
·�
t.�h
'
' .,.,."
•
• ( O'IINI
lolll'ocl "\1
l"' •
•
.;.;....__
..,
...
s,..tllIf'�:�
Nr"' II ...,I,Jiem
Soon, as you become more Python-savvy, you'll find Sublime comes with a number of preinstalled
STEP 6 STEP 9
that the standard IDLE isn't quite up to the task plugins for Python code, allowng you to experiment i
of keeping up with your code, alterations and injections of code with your code in real·time. They're probably a little bewildering at
mid-way through a long program. However, Sublime will keep you this point in time but you will likely find them useful as your Python
updated and you can even utilise indents easily. skills increase.
API Reference
_,
IMWif(twiG htrrOM1 • ·-
·
print (•Hello rv�ryone·) ·
2 if lf'VC: . ...
·
::s pt'i.nt ("'th11 b trvel"')
. ......
tM
• !mif! •
"....
......
. ....
....
. .. ...
• h·- .. h��-lo.._
· _ ,.,C)ooiWI"\o-����lfw-1\CIIll�jlll llllfi-UUO!
• ••·•-•·'"'•�<.1-0...<•.••- •• 0.,..,.1(•• ...(ll!fOIII hi..
o ••·•- -t o
• h ..'· '"' ..,.,.,.,,,,..- 11\fl llll lllj>lll, !1-llflllol•\ ....Wll>(lfeol
/•
• ,...._.,...,•
•.,
1...... .. �... ,_ ,...
· . ,••'111011
011 -.tit�\
• "-"""''�•'•" "'�\ <lllll <rt,!IIIOMIIII••hl •n 111101Itollot t,!l!l\'1'1
• ••.-.,..,.�,,,,..._...u,..._..,,...,_,, Wt,�Pol•., "'"""*',., . lle!OI• +h ..,,..,
• • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••• • • • • • •• • • • • • • • • • • • • • • • • • • • .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ... . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
We're not going to get too heavily into the However, we recommend you use the IDLE to
STEP 7 STEP 1 0
code right now but an indent is part of Python begin with. Although the Python IDLE isn t as
'
programming, where a statement indicates that the following advanced as Sublim e, it's a perfect base on which to build your skills.
indented commands must be run until a particular event has Once you've mastered Python, and the way it works, you can move
happen ed; after which the indents stop. Pressing Ctrl+) will indent a on to a text editor for the better features.
line of code in Python.
-- - - -
-
·
·-
·-
....fl1do ...
1'plllnf
, • ·-
-
,..lnt ("1Wllo �
...
)'01'
.,."") ·-
·
1
2 if li'"Uir: ·
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
34 Why C++?
36 Equipment You Will Need
··-··· ··-
y
C++ is one of the most popular programming languages available today. Originally
called C with Classes, the language was renamed C++ in 1 983. 1t's an extension of the
original C language and is a general purpose object-oriented (OOP) environment.
C EVERYTHING
Due to how complex the language can be, and its power and performance, C++ is often used to develop games, programs,
device drivers and even entire operating systems.
Dating back to 1979, the start of the golden era of home computing labs and by regular people who didn t have access to the
'
computing, C++, or rather C with Classes, was the brainchild of mainframes and large computing data centres.
Danish computer scientist Bjarne Stroustrup while working on his
PhD thesis. Stroustrup's plan was to further the original C language, C++'s impact in the digital world is immense. Many of the programs,
which was widely used since the early seventies. applications, games and even operating systems are coded using
C++. For example, aU of Adobe's major applications, such as
C++ proved to be popular among the developers of the '80s, Photoshop, lnDesign and so on, are developed in C++. You will find
since it was a much easier environment to get to grips with and that the browser you surf the Internet with is written in C++, as
more importantly it was 99% compatible with the original C
, welt as Windows 10, Microsoft Office and the backbone to Googte's
language. This meant that it could be used beyond the mainstream search engine. Apple's macOS is written largely in C++ (with some
1 f i nclude<iosL ream>
2 using namespace std;
3 void main ( }
4 0 [ char ch;
� cout<< "gnu�r a chdraLer Lo check iL i s vowel or noV ' ;
6 cin> > ch;
7 switch (ch)
0 0 I
9 case ' d' : case 1 A1 :
10 cout<<ch<<" i s a Vowe l " ;
11 break ;
12 'e' :
case case ' I·; ' :
13 cout<<ch<< " is a Vowe l " ;
14 break;
1� case ' i ' : case · � · :
16 cout<<ch< <" is a Vowe l " ;
17 break ;
18 'o ' :
case case ' O ' :
19 cout<<ch< <" is a Vowel " ;
20 break ;
21 case '
u1 : case 1 y - ' :
22 cout< <ch< < " i s d Vowel " ;
....
"'9., , -c.......;q. 201 5.0.26lll 1 X
Wot- lndMduol components Langwgo poets
YMdoo""U Summary
1
, It
...
.H£T deJltop de
u
d 1' v �ktop de...lopment with c._
••
C...w..,-.llbOM fQrtl'of' I.Jnio.�W,._.PlltlorN •� C• ......WjJf w
rclow5 Form�Mid<� �1Jon5-.tl'w
VI.....,� OJOf"'ONt,C•• Jro.:(TJ:�k. -
••
"""'-w' �
....... ,... :t
..... ...
lcp
,..
Clot"""
Ortbop . ... � ...-th c ...
dopl
.
1.-d cWs< � �boas«� .,... .,QI'IS\ISif'l9t"� pow.�of VIc l!J VC•• �1h141 1001set C.86..£C
V�Wtii. C•• 100bc1, AlL,. .,-.ctop1.o0Mi tt.,-tt Ike MfC,_; � c-- �-
� v.�- 10SOl (10.0.4l9:ull
� V-C·• ooolsfo<CM...
l:.!l Y�tulll C• • ATISUpport
Won61wo1 &.1 SO.,_a,.,d UCRTSDK
r'"llt... Azure dewlopment Wll'l�1.ICP suppor1forC ••
� A...-ure SOl(, tools •nd ptOJectsfOf'developw-og ctoud lpp$ al'ld MFC .nd AT. support(�86 and*641
<:rca� resources.
C··!CU-
Ca.ng!C2 (t'�ntr�e,.,t.tfl
I t
u,ncl•rd l.tbt-•r y Mod..,le-s
�
Nodl'jJ development D•t• storage •nd p-ocets:ng
0 lyCOt'1N � ���'QfH tott'l' I cet'Sfll:lttht
�� K•l•bk nottwor• •PPka11'Qns "''"9 l'lodor-J\ •n •S}I"dwonous Conne<t.. dCW'Iop and tHt d•t. solut:onsus;mg SOl � Alur c
Vw Stoldot01on)'01.1 wccco. 'Nt • so of"crtnt
t'Wnt-dr ,.en J.w.Xropt n.ont•mt' O•t1 LoNe H«<ooporbre ML
10 1��oOO...Cid
... Otf\efs.eotNfftwthVlrua
M ac
programs controls and umpteen other computing tasks.
,
Windows
C++ is also extremely efficient and performs well across the board
as well as being an easier addition to the core C language. This
higher level of performance over other languages. such as Python,
BASIC and such, makes it an ideal development environment for
modern computing, hence the aforementioned companies using it
so widely.
Getting to use C++ is as easy as Python, all you need is the right set
of tools in which to communicate with the computer in C++ and
you can start your journey. A C++ IDE is free of charge, even the
immensely powerful Visual Studio from Microsoft is freely available
to download and use. You can get into C++ from any operating
system, be it macOS, Unux, Windows or even mobile platform s.
Just like Pytho n, to answer the question of Why C++ is the answer
is because it's fast, efficient and developed by most of the
applications you regularly use. It's cutting edge and a fantastic
language to master.
I Nee_
You don't need to invest a huge amount of money in order to learn C++, and you
don't need an entire computing lab at your disposal either. Providing you have a fairly
modern computer, everything else is freely available.
C++ SETUPS
Thankfully, Python is a multi-platform programming language available for Windows, macOS, Linux, Raspberry Pi and more. If
you have one of those systems, then you can easily start using Python.
D COMPUTER
Unless you Fancy writing out your C++ code by hand on a sheet of paper (which is
something many older coders used to do), then a computer is an absolute must have
component PC users can have any recent Unux distro or Windows OS, Mac users the
latest macOS.
D AN IDE
As with Python, an IDE is used to enter and execute your C++ code. Many IDEs come
with extensions and plugins that help make it work better, or add an extra level of
...... . ,. functionality. Often, an IDE will provide enhancements depending on the core OS
- - - - -�
- - being used, such as being enhanced for Windows 10.
·-�........... ...
...........---.-- ... ..... .._.
D COMPILER
.......
.,
-
�
"'' .. ..... ..
� "'
-- --
...........
... .
·-.. A compiler is a program that will convert the C++ la nguag e into binary that the
_
...,... ....
....
C !....
'f .W ._._
'oo
.... computer can understand. While some IDEs come with a compiler built in, others
don't. Code:: Blocks is our Favourite IDE that comes with a C++ com piler as part of the
package_ More on this later.
.. _
D TEXT EDITOR
� :
, Some programmers much prefer to use a text editor to assemble their C++ code
-'
before running it through a compiler. Essentially you can any text editor to write
-' -
:
-·-
.
-":"'
code,just save it wi th a .cpp extension. However, Notepad++ is one of the best code
!f� :: - -
·
·
text editors available.
• •
·
-
•
·
== •
·---
I
. -·- ·-
----:.:: ,:\---·--
-
·
··---·
...-
While it's entirely possible to learn how to code on a computer that's not attached
to the Internet, it's extraordinarily difficult You will need to install the relevant
software, keep it up to date, install any extras or extensions, and look for help when
coding_ All of which require access to the Internet.
--- -
----
· - Yes, as with Python, you're going to need to set aside significant time to spend on
learning how to code in C++_ Sadly, unless you're a genius, it's not going to happen
• ••
--
•• •
overnight, or even a week_ A good C++ coder has spent many years honing their
craft, so be patient, start small and keep learning.
OS SPECIFIC NEEDS
C++ will work in any operating system, however, getting all the necessary pieces together can be confusing to a newcomer. Here's
some OS specifics for C++.
n o � ... ...
• • • •
"
• • •
. ,
,....
• • .. .. •
• • IU 11m ...,.. -�
.... " ""''' ,.,. .
�. .n
.. ..."' ""'"" ,., •
.. '. • • .,,.,, ·- II •O • '- '- • " I> � •
...... ""i. - .�... •
"
_. tltl • • .... � .. .......-'lo··-' •
• ••
0
• • •
------
-
-·- · �,
• • •
•
• • •
-
•
··-·-
• • --· •
z--.,_. . ...----·
.
.,_,_ ·--
.. .....
.
·"-··�
• • •
......
. ,. _
. .. _,
• • •
• •
__,..
_... ...
.... .. .
., , . . . . .
. ... . . .. . .. "
•
--·- ·�· ...... ..
• • •
• • •
•
•
•
•
•
. ...... ==-•
• • •
•
. .....
...... .... ....
••
- . ,.. -
.. ·- •
•
• • • •
. ,. .. ..
..
• • • •
- -
• • • •
- - - - -
. ... - · - - ---··
.....
• • • •
-
• • • ·- .. -·-.-- �
•
•
•
•
•
•
• . ..
· ··
=---·---
-
-
•
•
• • • •
• • • •
• • • •
• • • •
• •
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
-
! �� �-
• • • •
• , -·· • • •
• · • • •
• ·- • • •
• •
•
• The Raspberry Pi's operating •
• • Mac owners will need to •
• • • •
•
•
system is Raspbian, which •
•
•
• download and install Xcode •
•
• •
to be able to compile their
• •
•
•
is Linux based. Therefore, •
•
•
•
•
•
• • •
you're able to write your code
•
•
•
•
•
•
•
C++ code natively. Other •
•
--� ••••
...
-
·
-
-
·-
�--
!'....
--
-
._...
. , �.=::�: !•:-
: · -
•
,__
·-
• . -0
-----.,.tlgjl-- ..
- •
...... •
�e tti n to Kn ow
C++ is an amazing programming to learn. If your dream is to become a games designer
or work at the cutting edge of science or engineering technology, then being able to
code in C++ is a must. Remember, you're never too old to learn how to code.
:
•
C with Classes, which added
•
•
•
<>. The directive instructs the &j *newcode.cpp X
: features to the already popular : pre-processor to include a
• •
: C programming language, : section of the standard c++ #include �1o s _ r�a >
•
: while making it a more •
code. For example: #include
• •
•
: user-friendly environment. • <i.ostrealll> includes the
• •
• •
• • iostream header to support
• •
: ofC++.
•
•
•
• • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
• •
• •
• •
•
•
•
•
•
•
•
INT MAIN() *newcode.cpp (-/D
•
• •
•
•
•
•
int main() initiates the File Edit View Search Tools Documents Help
• •
•
•
•
•
declaration of a function, which
• •
• • is a group of code statements
• •
•
•
•
•
under the name 'main'. All
• •
•
•
•
•
C++ code begins at the main
•
•
•
•
•
•
function, regardless of where it [c'J *n ewcode . cpp X
• •
• • actually lies within the code.
#include
• •
•
•
•
•
1ostream>
• •
• •
• •
•
•
•
•
int main ( )
• •
•
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
The open brace is File Edit View Search Tools Documents Help
something that you may
not have come across
-
before, especially if you're
used to Python. The
open brace indicates the � *newcode.cpp )(
beginning of the main
function and contains all #1nclude 1ostream>
the code that belongs to
that function. int main ( )
{
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
COMMENTS <<
Unes that begin with a double slash are comments. This means they The two chevrons used here are insertion operators. This means
won't be executed in the code and are ignored by the compiler. Why that whatever follows the chevrons is to be inserted into the
are they there? Comments are designed to help you, or another std::cout statement. In this case the words are 'Hello World', which
programmer looking at your code, explain what's going on. There are to be displayed on the screen when you compile and execute
are two types of commenl: I* covers multiple line comments, II a the code.
single line.
•newcode.cpp (-!Documents)
� *newcode.cpp x
c· *newcode.cpp
, x
#include f •
#include • dm>
int main ( )
int 11ain () {
{ II My first C++ program!
II My f1rst C++ program! std: :cout << ( H llo Worldl \n J
I
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
STD OUTPUTS
While std stands for something quite different, in C++ it means Leading on, the ("Hello World!") part is what we want to appear on
Standard. It's a part of the Standard Namespace in C++, which covers the screen when the code is executed. You can enter whateveryou
a number of different statements and commands. You can leave like, as long as it's inside the quotation marks. The brackets aren't
the std part out of a code, but it must be declared at the start with: needed but some compilers insist on them. The \n part indicates a
using namespace std. new line is to be inserted.
•newcode.cpp (-!Documents)
,._ VI
int mun( J
{
II My f1rst C++ prog ram !
I
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• . ........................................... "..................
COUT ; AN D }
In this example we're using cout, which is a part of the Standard Finally you can see that lines within a function code block (except
Namespace, hence why it's there, as you're asking C++ to use it from comments) end with a semicolon. This marks the end of the
that particular namespace. Cout means Character OUTput, which statement and all statements in C++ must have one at the end or
displays, or prints, something to the screen. If we leave std: : out we the compiler will fail to build the code. The very last line has the
have to declare it at the start of the code, as mentioned previously. closing brace to indicate the end of the main function.
·newcode.cpp (-!Documents!
,.. .....
.... _
.. ,
(
�1•. �
' ..., , .• c- ..... -
.. I '-
)
c:· *newcode.cpp x
hnclude
int a�un( J
{
II My f1rst C++ pr og ram !
std: : cout
H ow to ...., e t U
•
1 n _ ows
Windows users have a wealth of choice when it comes to programming in C++. There
are loads of IDEs and compilers available, including Visual Studio from Microsoft.
However, in our opinion, the best C++ IDE to begin with is Code::Blocks.
CODE::BLOCKS
Code::Blocks is a free C++, C and Fortran IDE that is feature rich and easily extendible with plugins. It's easy to use, comes with
a compiler and has a vibrant community behind it too.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• . . ..
.. . .. .. . . . .. . . . . .. . . .. . .. . . ..... . . . . . . . . ...... .. ... . ... ... .
.. .. . . .
STEP 1 Start by visiting the Code::Blocks download site, at STEP 3 When you've located the file, click on the
www.codeblpcks.oro/downlqads, From there, click Sourceforge.net link at the end of the line and a
on the 'Download the binary releases' link to be taken to the latest download notification window appears; click on save File to start
downloadable version for Windows. the download and save the executable to your PC. Locate the
downloaded Code::Blocks installer and double-click to start. Follow
Code::81ockl ·-- -..::=::=:�=-=-=;:---- the on-screen instructions to begin the installation.
X
- --·---·-
·-·-· ..
··
·- .............
·-
·
·-
·-··
_..._________..._______
, ___
Welcome to CodeBiocks Setup
·-
·-- --..-----·--··----�--------------.. ..
-
·- ........._..._______ _,..___
·--
_ ____
·-
........,_.,,..,,...
·-
Seto.c>w1 � you IIYOU(II> !ho nstallation ofCodrlllnd<S.
-·-
-�
·-
··
�==
==
-"· -
-== -===== �� -=-
-·
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
There you can see, there are several Windows Once you agree to the licencing terms, a choice of
STEP 2 STEP 4
versions available. The one you want to download installation options becomes available. You can opt
has mingw·setup.exe at the end of the current version number. At for a smaller install, missing out on some of the components but we
the time of writing this is: codeblocks·1 7.12mingw-setup.exe. The recommend that you opt for the Full option, as default.
difference is that the mingw·setup version includes a c++ compiler
and debugger from TDM·GCC (a compiler suite). X
a.o.e �:·
0.:.0.wtd'l fr•vnofCo.,..ds you want tonstal.
�
� 11 � �
....... - - =
-= .. == .Otot
====1 · O>tdt tho - lOt
Its you- to-ond ..
!Chod tho _ Its -clonl-.,t to
...
c
? ?' 0
'*ltlJ
1 f
- Or - """ nstal. Cld<NoxtlD ""'"""."
....,,a.....
_ """
--11Ow:yaI
;- ...,.
s.loct tho typo ofnstall: f\11 : AI�. al .,.. , ),ato
�
c
I •t7 Q..
,• ...,..,
. .....
Or. soled tho apbonol • 0 Def•ult lnltol
� lltr\
tsyou..,... tD
__,1 .,_.. ........,_ '"'"' nstall: •0 Ccontrtl "'-
0C: :8 CI!P-
IIIO?t. l\ecodroWtcbl, r2..._..,......,...c.. -....._•...,.. ,._�.._,, 12,..._........__..• 111'-*ilrc. 0 c: :& CC Tat
......ll' flwwltr....,_ '-....... _......
IIIOTt. r-.. c ..,._ �c.........QOO......_.....lC*.GC:C�5lt �Oil.. 0 C::8 -·Cconftg
(!l-<:CI
0C::8 �
IIOTl' T\ooC1MWIll'
Irrwtie 1)!....
._...
.,... . ... _..������-• lllllo,... llw,...,...,e.,........., ...._ •..,..,.,.....
.,1 .
.r-'\ ..-··,.. ,�� -------
�·_..... ....,..._
!letac>tJOn
tt-I!IMH.- 'tNX
'"
•II 1 1""V•-.,-' Spacer�«!: 25J.4Me "
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • •
Next choose an install location for the Code::Biocks There's a lot you can do in Code::Blocks, so you need
STEP 5 STEP S
files. It's your choice but the default is generally to dig in and find a good C++ tutorial to help you
sufficient (unless you have any special requirements of course}. get the most from it. However, to begin with, click on File > New >
When you click Next, the install begins; when it's finished a Empty File. This creates a new. blank window for you to type in.
notification pops up asking you if you want start Code::Biocks now,
!J"---�··
so click Yes. i.. .... Moe � ..... .... ,... ,.,.,. ....... s..;,. .....
....
"'-fh
-.
iM4
:
(. • .J. • " :
� k1l ._":;;
il "' "! � '-': t: 4.: to I L
•
;;;;
1 ;-; ;;;;
r.;
• ;;;;
"
- � ,.;"
;;;;
r ;"
<;;;l;
o..r - .. ....
,_ __
- ..1
_..,.
..
,_
Extract: Qftl. 7.
Do you want to run Codt::Biodts now?
Extract: Ql:l. 7•.
If s- •
..,.,.....
. (bl.$ut.S
OUtput folder :
OUtput folder:
0 (IOMtlt <.WoV
(,.......... ...
(\WIIo\'
r
'"-
-....
-- .n -
- --
·--
- -----
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • •
The first time Code:: Blocks loads it runs an In the new window, enter the following:
STEP 6 STE P 9
autodetect for any C++ compilers you may already
have installed on your system. If you don't have any, click on the #include <iostream>
first detected option: GNU GCC Compiler and click the Default
Int mai.n()
button to set it as the system's C++ compiler. Click OK when you're
ready to continue.
{
//My first C++ program
- X Std: : cout « "Hello World!\n";
• •
The program starts and another message appears Click File > Save as and save the code with a
STEP 7 STEP 1 0
informing you that Code::Biocks is currently not .cpp extension (helloworld.cpp, for example).
the default application for C++ files. You have two options, to leave Code:: Blocks changes the view to colour code according to C++
everything as it is or allow Code::Biocks to associate all C++ file standards. To execute the code, click on the Build and Run icon
types. Again, we would recommend you opt for the last choice, to along the top of the screen. It's a green play icon together with a
associate Code::Biocks with every supported file type. yellow cog.
.. .. .. -
• � .......
Qollo_.....-.........._.._
... •.-...
. c;.c
·-- ..-··-·
...._....,.__
. _ ... ._ ...._
... .
,__...,....
,_ ,_.,...... ...
-
-- ....
c.....-� --(1···-
....
....., _a.. . .. -.-- ..... ....
,.,..___
H ow to ...., e t U
on a
To start C++ coding on a Mac you need to install Apple's Xcode. This is a free, full
featured IDE that's designed to create native Apple apps. However, it can also be
used to create C++ code relatively easily.
XCODE
Apple's Xcode is p rim a rily d esig ned for users to develop apps for macOS, iOS, tvOS and watchOS applications in Swift or
Objective-C. but you can use it for C++ too.
• • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Start by openi ng the App Store on your Mac, Apple When you're ready, click on the Get button which
STEP 1 STEP 3
Menu > App Store. In the Search box enter Xcode and then turns into 'Install App'. Enter your Apple ID
press Return. There will be many suggestions filling the App Store and Xcode begins to download and install It may take some time .
window but it"s the first option, Xcode, that you need to click on. depending on the speed of your Internet connection.
c'"
;;. --
"-
""
. • -- • •
• • • 0 • - - • • 0
-
* --- -- ·- -
-----'-'-- ___ ..::-=-�-
* ·- -
---- ---
-
-- ..
---
-- --
...
Xcode
-
. .
- -
..... -
-...
�· . ·- - ...
__ _
' 1 :=-
- .:·- II!
liiiiii � --·-
,-
- ---
�
.
-oo::·· �--
-
---
.. -
..
_,.,
- a- -
-- �-- --
-
·--------· _______ ...________ --
- -
---·
!!&-·
...
_ ...__
__
.,.
______.. ... _ ...___________ -
- ---
_,._
-.-
- ... -
-
�·-
.
- -
. __... -
�·-�--.... --
-··--·--...-·-· - ..
g:;:a -·- !!!!\ -
,_
- --
,_..,
..
� -::.·-� - � -:::: - - --
., "_:-··- 1"!1 --
-
--
..""'
-
-··-..
- �a- , _ ... _ _ _ _ _
. . .
•
·•-::
·
-
• •
' .
--
-----
Take a moment to browse through the app's When the installation is complete, click on the Open
STE P 2 STE P 4
information, including the compatibility to ensure button to launch Xcode. Click Agree to the licence
you have the correct version of macOS. Xcode requires macOS terms and enter your password to allow Xcode to make changes
10.12.6 or later to install and work. to the system. When you've done that, Xcode begins to install
· -·---- . ------ - ---- additional components.
* • • ·-
• •
• 0
...._..
... .:::-:�
:: ;:::::;.
·-·--- :: ��-:'
-::.=::
-·�
-� --
- --.....
.
-
--
- --
---
-
-..· - -
·- ..
-
--- -
,......
. .....u... ...
.
. .
..
Xcode
• •
-- - - -·-
•
• •
----
- - �---
----
-·
-
- - .
--
----
g :":"
- .... �IOIIIIU.--........ , .. �-
.. ..
-C IIIO.. C AU' ____..,....
..
�- -.. --. .... . . uua.
... ....
-..- ..._ ... ... ..... .... .... . _.,AU.
�
-- at N .......
OUOIITII
.......
....IlfiOIIotr. OI.ICI..II .... .......,.. ,_,
D
D
..- a
a D P _..
,__.. � "'
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
With everything now installed, including the The next step asks where to create a Git Repository
STEP 5 STEP S
additional components, Xcode launches, displaying for all your future code. Choose a location on your
the version number along with three choices and any recent projects Mac, or a network location, and click the Create button. When
that you've worked on; although for a fresh install, this shows blank. you've done all that, you can start to code. The left·hand pane
details the fi l es used in the C++ program you're coding. Click on the
main.cpp file in the list.
_ I) .
--·L ••·. _.,. .__ -- --
- •·-
• - ... - ---
·
-
- .c.
-.,...,. .
.
. -
.
_..
,.. _ ....
.. _.._.,.,
..._
- .._
. .
·- --- ·
·- --
·---·-
Welcome to Xcode -----·-
-- - - .
---
No Pecont Prqects - -- --
--
----
--- -- --- -- - •
-
�
�
·--
-
"e....IOI.MII
·---
J OttIQittf""'
L.». ...
r.q��o<•,_.. ......�,,._.._,v. •
• 0
f)
1!:!1c..1e -
• Xoochllfu!K1
c..-'"app1r:1r
.fiN)no.111M. we,AlleN Wllkf\orAllPI• "'·
·-----
0
II ---· ,._
-·-
9 C*riHMI'" ....
nn,�trofolct ..---·-
1&1 IWI .........e.\1111�
1111'1 110111Ill tOol'-POtiiCMY
- 0
--·--
•
a _....,__ ,_
-·--
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • •
Start by clicking on Create New Xcode Project; this You can see that Xcode has automatically
STEP 6 STE P 9
opens a template window from which to choose the completed a basic Hello World program for you.
platform you're developing code for. Click the macOS tab, then the The differences here are that the int main () function now contains
Command Line Tool option and finally, Next to continue. multiple functions and the layout is slightly different. This is just
Xcode utilising the content that's available to your Mac.
-- - -
'!""!'
•
o.:-•...... ,.,_""""'-
e
..-
• - .,. --o.
o
.;
.;. ,,;;- -- .. ··-- -
---
�--
- -·-- .
-..- --·-·
· - - .
••
-· --- .. --
-- .
-
•
-- --
•
- -
--- - - - ·-U I ..___
-
..,._
-
-- ---..-
-·
-
· ·G--
--
-- ..-- .
--
• •
.
0
.....
11 - ··
...... --
--
a ·--- ·
--...-
a
...-
....--- ·
·-....
.. ..... .
- . . ...... --·- ...
_ .... .._
.. .__,..... -- .
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • . ........................................... "..................
Fill in the various Fields but ensure that the When you want to run the code, click on Product >
STEP 7 STEP 1 0
Language option at the bottom is set to C++. Simply Run. You may be asked to enable Developer Mode
choose it from the drop·down list. When you've filled in the fields, on the Mac; this is to authorise Xcode to perform functions without
and made sure that C++ is the chosen language, click on the Next needing your password every session. When the program executes,
button to continue. th e output is displayed at the bottom of the Xcode window.
• • • ....- ....- _
_ , .._ t··-
� ....__ ...._ -·- 0 0
. --
--
---
.
--
. -- .
.
---
· - ---
-·--·--
==--
- -
-·------
-
...
-
- .
... ... . ·
-
-- -·
.
.. __ _ __...,... .
--- ..___
_.. _ _
_
-·
,.,.,_. ......,.
-
C•• .......t • · ·;...-
- --
--
--- .
-- .
0 • •
II --
-- - ·
� .,; .. ·----
D ----
c
--- -- ·
-� -
---- ·
a -·--
H ow to ...., e t U
i n Li n ux
Linux is a great C++ coding environment. Most Linux distros already have the
essential components preinstalled, such as a compiler and the text editors are
excellent for entering code into, including colour coding; there's also tons of extra
software available to help you out.
LINUX++
We're going to be using a fresh installation of Linux Mint for this particular tutorial. More on Linux Mint can be found in the
next section of the book.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
· -
,.. �-
---
to-
· --
·-
·- .__
-
.,_
�-
,_ ·�·
· -- ·-
··--... ·-
.....-
.. .....
·-·-
·-
·--
·-�-
•··-
·-
·�
..-
...
..
·-· � '
,.,,._1• • _.... I -·-
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Most linux distros come prein stalled with all the STEP 4 In Xed, or any other text editor you may be using,
STEP 2
necessary components to start coding in C++. enter the lines of code that make up your C++ He llo
However, it s always worth checking to see if everything is present,
' World program. To remind you, its:
so still within the Terminal. enter: sudo apt-get 'install
#include <iostream>
bu'ild-essent'ial and press Return. lf you have the rig ht
components. nothing is installed but if you're missing some then int main()
they are installed by the command.
{
•
//My first C++ program
·u� Documer.tl x
#tnctude <tostre•a>
1nt •a1n()
{
//Hy ftrst C•• Program
std::cout << "Hetto wortdl\n";
}
• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
When you've entered your code click File > Save As Before you can execute the C++ file you need to
STEP S ,
STEP S
and choose a folder where you want to save your compile it. In Linux it's common to use g++, an open
program. Name the file as helloworld.cpp, or any other name just as source C++ compiler and as you're now in the same folder as the
long as it has .cpp as the extension. Click Save to continue. C++ file, go to the Terminal. enter: g++ helloworld. cpp and
press return.
_ ,
... _ _ ...,
. .. ..
..
. ... .. .. , · · · -
........
File Edit View Search Term1nal Help
��� ll ..t , ...
,......
,,.. ,Ill, - ... l• �.l.l\•
• • • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
The first thing you can see is that Xed has There will be a brief pause as the code is compiled
STEP 6 STEP 9
automatically recognised this as a C++ file, since the by g++ and providing there are no mistakes or
file extension is now set to .cpp. The colour coding is present in the errors in the code you are returned to the co mmand prompt. The
code and if you open up the file manager you can also see that the compiling of the code has created a new file. lfyou enter ls into the
file's icon has C++ stamped on it Terminal you can see that alongside your C++ file is a.out.
-- .
·�· . , . .
STEP 7 With your code now saved, drop into the Terminal The a.out file is the compiled C++ code. To run the
STEP 10
again. You need to navigate to the location of code enter: ./a . out and press Return. The words
the C++ file you've just saved. Our example is in the Documents ' Hello World!' appears on the screen. However, a.out isn't very friendly.
folder, so we can navigate to it by entering: cd Documents. To name i t something else post-compiling, you can recompile with:
Remember, the Linux Terminal is case sensitive, so any capitals must Q++ he lloworld . cpp -o he lloworld. This creates an output
be entered correctly. file called helloworld which can be run with: ./helloworld.
d..vid@mint�t• -/Doce
E<k V
oew S.arch Te� Help
File Edit View Search Terminal Help
t er I D Es to l nsta
,
"'""' + +
I f you want to try a different approach to working with your C++ code, then there are
plenty of options available to you. Windows is the most prolific platform for C++ IDEs
but there are plenty for Mac and Linux users too.
DEVELOPING C++
Here are ten great C++ IDEs that are worth looking into. You can install one or all of them if you like, but find the one that
works best for you.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Eclipse is a hugely popular C++ IDE that offers Codelite is a free and open source IDE that's
ECLIPSE CODELITE
the programmer a wealth of features. It has a regularly updated and available for Windows,
great, clean interface. is easy to use and available for Windows, Linux and macOS. It's lightweight uncomplicated and extremely ,
�
:'-::=-------------
--- 111
• ••
-·
·
..
..
.a e; ....,....c
. •• �-.....
.. '\" I - - - - ...... � -·
Fo•
-. -
,
.......
-..
_- ::------"
;;;.
.,. u.. �. ,4_ ..._..,... -.. ,.... .... ...... ._
- -
.. •
�
::a - C · IJ · � · • · 4\ · � - 0 · 0 - Cl. · �·o ,f · [!.· I ' .. ·
- - •
--
.. . , �
- -..--
·-
-
--
" ..
g
c •
.- -
....- - -
<>.
- .... .. _ -
- •
..� - X ·-
""
.
ws......,.. •• ..,.. - · ..
., .....
-·-
,_ .... .
-
......
-
- -
--·
--·
·- .... ..... �
,;.;
� ,;:;;;;.;;�,;;;::::.;;:;;=--- _
--- .. -
__ _
.
- ___ ......., _
." _ _
, .._ __..
-- ....-� I _...,._"""'"__... -.... .
. --- -
'""'-
1!1 ...........,........ t'horl.., 1L.•t-• •
-
- -•
• tii Oo
• •'-
�
.=::
::
�:
-=
oolt( :
:: ::..
c-=,���
-·- -
• •__. I
_,.,,
.
. .-..
--.-
..... . ... . ....
... _ t . ___... ..
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • •
The GNAT Programming Studio (GPS) is a powerful Another popu lar choice is NetBeans. This
GNAT NETBEANS
and intuitive IDE that supports testing debugging , is another excellent IDE that's packed with
and code analysis. The Community Ed ition is free, whereas the features and a pleasure to use. NetBeans IDE includes project based
Pro version costs; however, the Community Edition is available for templates for C++ that give you the ability to build applications with
Windows, Mac, Linux and even the Raspberry Pi. You can find it at dynamic and static libraries. Find out more at www n
. etb eao so
.rg /
www .adacore.com/download. features/cpp/index.html.
•
r ... _.,. _ ,.... ... ..,. _ _ _
....... .
& • UI X III . •
-
• • ea & • O -
- - -
- -- . -
·
·-
···
� ... ..1 ' :1\
� t .S.
'I\••
'' •••
- ----
-
-
--
.
•
, • ·· -
, ,_
- ::==
::==:
. . --
- · ·-
-
. . ....... ··- -
·· -
_, --
.
. '
.... I --- -
· -- �- . " 'U•.·-�
,a
··-
·
··�-
··-
- · -- .
·-
···-
. .. .....
...
�
:�• :::...
-
..___ ·- ··- -
• .. ••
ll..__.
l. -
. ·
..
: a::
• •
�
' " """'*"'-
·:=-- -
....,__ _--- �-· --
·---
....... ·-
.. -----·
--- I -----.--c-. · - -
--··· ·- - _, • _
.
.,
..... ,_
.., .,. ·
-
�
u•
ve
--· · -
.
--·
•
� ..
· = -
•
. ---·-
.._.,.. _ -�- --·
- . _ ..._ =- ·
- - -
..
- - - -..... -
.. I -
•
·--
.. :
-
• --
- -- -
·- - --
•
-
-
-- · - I
-- ·- - -·
•
-··
·
... . .. · - --·
� ,. ·-
· -�---
-
==-::
..
.. ... "" ,
=
--
=
.
===
·-
·--
--
--
-:- -�
,........,.'-><_
· �'--"
·--
••
·-....-
·
·- �:�
: ;�
-
-�
� _
- - ..
••·
·=-=
.� =:
. _- ____
.
_ :;,_..
.
•
,_ ,_
..
-
·-
·-- - ---
_
_.._
...
. ,. -·.
_
.....
•
"""-·
..__ ·-·-....--...
.__
- .,
t_4C.,..,._�-u. --
,_...,..
-· -
•
� - ·- __
--- --
. -·- .._
_
..;;;; ;;;·�·
;,:;;
.. _
•••
__
..._
. ._
--·---
. . __..,. --�
-- - ....
,_.._ '--... =-
-== ,,.�Cool-•
. __ ..
- ·-···
_ -- ,.....,._
- ·-·--
--- --- - -----
--·..._
·---- - -
. ._ ..
...!
.. • • I •· - • • "�--.cal-· - -------·----...-
-· .......,._ __.._.._
.
· ·- ..
II 1
... ·-...t-tw ·
--· ···-- .
_ _,... ..........
--·- · -
o .a.tiHt
.. _ .. ..
· · ·� ==
-�
-:-
·
__,.,..._
• a c:.
,_�u..
--·
-
:r,
-
..
. ..... _ ...
__...
.
• •• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
ll'
" ,,
- --
.
� --
..
•
.. "
'" • ' ,, I ·
·
·--- ,....... . - -.
= ....
� . == JI
" .... .. .. ""
,..,.,.,..
""' ·•-
•
• '" ••
• o ,.
..,
· ··
•
..... 11
·- � ·-·
1-l.o�dt- .... I ·I ( "
..
o ·- �
-Ce)l . .. -- ·-...
·-
· -- ,_..,
"
"
-·� ....l
., l•t) ( •COIIfi0-5t�M " hiiJNol(l
''" toI ,.,."' te ,,.��-� ,..
..,..,_ '
" I ......,.•.
f.,.,._ .. t..n . " I•I.
1
" -11•II
" 1cw•• .._._._
.., •
._,,.�,, II hth<hl(o•l oo) (
lf l-.l > lt
....... (•• . .........(•ol
..... .. (0\A<cll\l(l,jlccflld,
1111
"" " -··
..
"' ..,. o I
I "
"
•
. ...... ..
.,,. ""- ...,U I
. ... .. _. .
, •4 11
1 ""
t ot
•l.. , troo )l
•• - . .....
- -h
•- tt
-
·� • •
• - ...... .
'
, •) ,,
o.buf 0 a,;..,.. Dm4! 1kA1\) 0..,. 0
Build�
:.. 1 0 I
I
.. .. .... � . .
• • lettWUI<III ..
�
,__.,.,.:
i'I
� llblllr!iCI
.. .-..�
.
IO«WY �
..
.,
...... ..
· ·-
. . . . . . . . . . . . . . . . . . . . .. . . . . . . . . ...... .. . . . . . . . . . . . . . . . . . . . . . . . . .
. . • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
0
- -
- --
.J
•
-
'"
. .._,.
.... ..
...
.. ..
..
..
-· .
...
. III••
. .. - - ..... ..
. . c-...... .......,
. .... - - ...
._ ..,
·- - · ·
-··
'
.. ..
·-- .. .
.. ....•-
. .
... ....., __ ___ ..•..-ut.�,.
..
f ·- --
- ... I
:
:·
:
· :
·:::·�-
- ···
_ .. . ..... . -:
-
..
·
, ·
·
·-...
-· -
·
··
-·
- � ..
-
...
· . �
.. ,__..,u.� •
_
· -·· -
-
I ''""""'"
....... ' .. •
,_ -.z. ' -
�
Go·--
·
··.. ... . ., ...,._..... . -
....
....
CitW'IIo( , ..
Gt�....�
. .
... .
�
....,..... .... !
Gll....-..-.u
. ' •
- .. �1·....1.:.0 .. . . ..
•
Gil_._. olll..l 0 • •I ( •
�......
l
.................1.....�-.•••
--u
..,
......._Ot•J.•o
. ..
...,...... - 1-·-···
.
•
..
•
•
.
'� ..I . ...,
• .. --'
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••• • • • • • • • • • • • • • • • • • • • •
Bloodshed Dev C++, despite its colourful name, Ultimate++ is a cross-platform C++ IDE that boats a rapid
DEV C++
is an older IDE that is for Windows systems only. development of code through the smart and aggressive
However, many users praise its clean interface and uncomplicated use of C++. For the novice, it's a beast of an IDE but behind its
way of coding and compiling. Although there's not been much complexity is a beauty that would make a developer's knees go
updating for some time, it's certainly one to consider if you want w
wobbly. Find out more at w y
w,lj
tmt
aep
o.r
og n
/idx
e.t
hml.
b
.
something different: www o
los
dbd
e.e
nt e
/dyp
cpb
.tml. • - .... - ----�· - --
,.,.. _ __ _ _ _ _
"' . . ····-· . .. . . .
.
.. _ _ _ _
;:..,
= .-
_
_
-
-
'
=r i=
=-
-
· -
-- -
-:::-
-
-
.,. ,,_ ft
• .,ILl .:II n_.n ' a - -
-
·
-
;_
- -
-
au•• <�"••'•hS..IP••�"H•-. 0011•ac uu, rtu !cll.UI! t.u.�. U'l'LI., -
· ·
Of z::f
•
NO. O, Ol 1
U l nt -
-
_.,_
-
- •
·
t L • I • '• • ' = ::-·. -
�
H
·-
1• o.r-rrrrrrrt
. . . .
.
lt t<h't t a•
• a ..
' -
-
·
- .... -
-· ·
··
·---·
--·
·- §-
• • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • •
so Why Linux?
52 Equipment You Will Need
54 Transfer Mint to DVD or USB
56 Installing VirtualBox
58 Testing Linux Mint's Live Environment
60 Installing Linux Mint on a PC
62 Installing Linux Mint in VirtualBox
64 Getting Ready to Code in Linux
66 Creating Bash Scripts - Part 1
68 Creating Bash Scripts- Part 2
70 Creating Bash Scripts- Part 3
72 Creating Bash Scripts- Part 4
74 Creating Bash Scripts- Part 5
76 Command Line Quick Reference
Many developers, across all the available programming languages, use Linux as
an operating system base for their coding and testing, but why? Linux has many
advantages over other systems and while it also has some quirks, it makes for a great
place to learn to code.
The first thing we need to address is that there is no such operating Linux works considerably differently to Windows or macOS. It's
system called Un ux. Linux is in fact the operating system kernel, the free for a start free to download, install on as many com puters as
,
core component of an OS. When talking about linux what we, and you like and use for an unlimited amount of time. lt's also free to
others, are referring to are one of the many distributions, or distros, upgrade and extend using equally free programs and applications.
that use the Linux kernel. No doubt you've heard of at least one of This free to use element is one of the biggest draws for the
the current popular distros: Ubuntu, Linux Mint, Fedora, openSUSE, developer. While a Windows license can cost up to £100 and a Mac
Debian, the list goes on. Each one of these distrosoffers something one considerably more, a developer can quickly download a distro
a little different for the user. While each has a the Linux kernel at and get to work coding in a matter of minutes.
its core, they provide the user with a different looking desktop
environment, different preloaded applications, different ways in Alongside the free to use aspect comes a level of freedom to
which to update the system and get more apps installed and a slightly customise and mould the system to your own use. Each of the
different look and feel throughout the entire system. However, at available distros available on the Internet have a certain 'spin',
the centre lies Linux which is why we collectively say, Linux. in that some offer increased security, a fancy looking desktop, a
p fj x n •• tl• h "'*•
• 1111 I IJOI!e
•ttul udo • h
ht blkco\or•t,
tftt ,color•,,,.._ ;
•
ll..t thttA,J,Y,a,s,o;
lflllt'_,..u........ \\ \\ );
uu:.q.eol•n,cC)
-..a�tllidlatCt
eld�/.:
eJ.ttf4Uy/ :
fllNt �I" ,dfti'H,.-.dll;
lJit � ;
lilt MIC,KY,,_,,.,,$X,$Y,..._,.f"ad ,Sl•rad- , l!l..•nd-
chr at..-11 1:
(( c\•ar4wuofJi
•tottalhtylt-1 ,INho\o•l;
(lf(\Otoact�,old"f,r.C- )l
.....,,.. ,...,
. . . .•)', );
;..._ _
..!..!.>.
. .._____,=, ._.thlhtylol , J.
c i rc:lot011
b;.01 dy. r -'•
fl oood h\\( a
l
dX•f'ad• , IU
dy,
• ,
lo
-. ,llllU1<-...,
_ _,: h!ta-th!.l.l
t •(-
gaming specific spin or something directed towards students. This However it's not all about C++, Python or any of the other more
extensibility makes Unux a more desirable platform to learn coding popular programming languages Using the command line of Unux,
.
on, as you can quickly shape the system into a development base, also called the Terminal, you're able to create Shell scripts, which are
including many different kinds of IDEs for the likes of Python, web programs that are designed to run from the command line and are
development, C++, Java and so on. made up of scripting la nguages They are used mainly to automate
.
tasks or offer the user some form of input and output for a certain
Another remarkable advantage is that Unux comes with most of operation. They are surprisingly powerful and we look at how to
the popular coding environments built-in. Both Python and C++ are create them within this section.
preinstalled i n a hig h percentage of available Unux distros, which
means you can start to program almost as soon as you install the Finally, although there are many more advantages to list, there
system and boot it up for the first ti me. are thousands and thousands of free programs a nd apps available
that cover near every aspect of computing. Known as packages,
Generally speaking, Unux doesn't take up as many system resources there are (at the time of writing) over 8,700 specific programming
as Windows or macOS. By system resources we mean memory, hard applications just for Unux Mint atone.
drive space and CPU load; the Linux code has been streamlined and
is Free from third party 'bloatware' which hogs those systems
- Linux therefore, is a great resource and environment for
resources. A more efficient system of course means more available programming in. It's perfectly suited for developers and is
resources for the coding and testing environment, and the programs continually improving and evolving. If you're serious about getting
you eventually create. Less use of resources also means you can use into coding, then give Unux a try and see how it works for you.
Linux on older hardware that would normally struggle or even refuse
to run the latest versions of Windows or macOS. So rather than
throwing away an old computer, it can be reused with a Linux distro.
..
e (sSUSE
ubuntu
�
•• -=
s ' '"'
a-'
1-= kw_
c"'
•• (Odebian Mandriva "'-=
' a
r
, e
...,_
- -
-
••••
MPl __ .._•
.., __
J\ There are thousands of free packages Each distro offers something unique to the
/:i.l, available for programmers under Unux. user but all have Linux at the core.
u i me n t You
•
I N ee'"""
Out of all the many different distros available, Linux Mint is considered one of the
best for both the beginner and more advanced user alike. It's an excellent coding
platform, with many languages built-in. Here's what you need to get up and running
with Linux Mint.
'
• • • •
• • • •
• • • •
• • • •
•
• : : You can install Unux Mint onto your computer via USB or :
•
• • •
: DVD. We'll look into each a little later on but if you're already :
• •
• •
• ••• • •••• • • •• • • • • • • • ••• •• ••• • • ••• ••••• ••• ••• • • • • • • • • •• • • ••• • • • • • • • ••• • • ••• • • • •• • • ••• • • ••• • ••• •• • •• • • •••• ••••• • • • •• • • ••• • • •••• •
• •
• • • •
• • •
•
• • •
•
INTERNET CONNECTION
•
DVD INSTALLATION
• • •
• • • •
I
• • • •
• • • •
• • • •
• • • •
• •
: DVD installation of Linux Mint simply requires a blank DVD-R • : It goes without saying really, that an Internet connection is •
• • • •
• •
: disc. Of course, you also need a DVD Writer drive before you • : vital for making sure that Linux Mint is up to date with the •
• • •
•
• •
• • : further software. Although you don't need an internet •
• • •
• • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • •
: connection to use Linux Mint, you're sure to miss out on a •
•
•
•
: world of free sortware available ror this dislro. •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
. . . .
•
• •••••••••••••••••• • • • • • • • • • • • • • • • • • • •••••••••••••••••• • • • • • • • •
• •
• •
• •
•
•
•
MAC HARDWARE •
•
•
• •
• •
• •
•
: Although Linux Mint can be installed onto a Mac, there's •
• •
•
: a school of thought that recommends Mac owners use a •
• •
: wanting to breathe new life into an older Mac, make sure it's •
•
•
•
: an Intel CPU model and not the PowerPC models. •
•
•
• • ••• ••••• •••••• •• •• • • • • • • • • ••• • • ••• • • • • • • • •• •••••• •••• •••••• • •
•
• •
• •
• •
•
•
•
VIRTUAL ENVIRONMENT •
•
•
• •
• •
• •
•
• •
• ·- �
-
: testing and using Linux distros. Linux Mint works exceedingly •
--
•
: well when used in a virtual environment. More on that later. •
• •
•
: There are many different virtual environment apps available; •
• •
--�
•
: however for this book we are using Virtualbox from Oracle. •
• •
• •
• •
• •
• •
• •
VirtualBox
• •
• •
�.
• •
• •
• •
•
Welcome to Virtu1IBox.org! •
•
__... ____ __ _...__..______...,
_ ...
•
- ..-
- --- ·---
... -----�-··- --
. .... ....
.
• ---·-------,-- ...-.,--�- .
• ___, ...,.______,,- -·
.
·-------------�"'""'-"'-
•
•
-
- ---.---·-.... ___JIIN.OI--1 -
==
--
:
1.......M ..
...
_ ,OL-·!•f<o.••-·• -
. .... �
.....-4__
• •
!Z:.Io
--- ......
,
•
------�--------......_.....___.... .
..
--
•
• .-....-..---·--...... .. ,________ - -·
_.... .
- _.. .._ ..... . ..--·--
rr£ ;
-
• _ .. ... ,..... __....,._ ....., __ .
s
.......
•
•
•
Download
1
•
•
• •
•
•
VlrtuaiBox • •
•
• •
• •
• •
• •
• . .
..
, . - �--
- __ •
. ._ _ _ .
...
.
...___
....,._ ....�
•
. --_,.,., •
._--
•
• •
• •
F2¢+t •
•
• •
•
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Tra ns e r M i n t to
DV ...... or
STEP 1 First locate the ISO image of Mint you've already STEP 3 Right·click the Mint ISO and from the menu select
downloaded. You can usually find this in the Bum Disc Image. Depending on the speed of the PC,
Downloads folder of Windows 7, 8.1 and 10 computers; unless you it may take a few seconds before anything happens. Don't worry too
specified a different location when saving it. much, unless it takes more than a minute in which case it might be
worth restarting your PC and trying again. With luck, the Windows
Disc Image Burner should launch.
�
...
..
.. ,"'
.
c:.
..::. ..:.
.._...,�,-
. ... ......
, ... 0.,.•
_
_.._,_.._ ..... .... m
� Mount
--· ....
, n
i
l••••
HI.I·dc..-
-- . -·
1'" '11'1
1 - -
.. ·
·� M
..
Ui_,, .,. ca. ....,
.. ,l\...
..6'bo
� lonuocmont·11.1.td Bum di1< 1m09•
� lin""""ll.l·mtt
"'· c•6'bo ffi Scon...U. Wondows D<f"'<k<-·
i!!!} lmdc.Z.20150J·<oMtm0<1·64b4 Opt:nwith
Shift: with
.. .......
1 ... . '"""""
8 Open w•h WinRAR
.. �...,._..lG" • Add to tJcl'li>n. .
•
· -·
• Add to "t•nuxn·unt- 18.1·c•nnamon-64bllrar"
1! -
�- • Compr�s and email...
Jt ...
- .
.. _
• Comprtis: to "lmuxmint·18.1·cinnamon·64bit.ra�
- .
• • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • •
STE P 2 Next insert a recorda ble DVD disc into your Right click the Mint ISO and from the menu select
-
STE P 4
computer's optical drive. After a few seconds, while Burn Disc Image. Depending on the speed of the PC,
the disc is read, Windows will display a pop·up message asking you it may take a few seconds before anything happens. Don't worry too
what to do with the newly inserted disc; ignore this, as you're going much, unless it takes more than a minute in which case it might be
to use the built·in image burning function. worth restarting your PC and trying again. With luck, the Windows
Disc Image Burner should launch.
Status
Burning disc image to recordable disc...
l
BDM's i·Tech Special I Volume 27
Transfer Mint to DVD or USB?
First open up a web browser and go to www.rufus. STEP 4 When you're ready, click on the Start button at the
STEP 1
a
k e o.e. Scroll down the page a little and you come to
j bottom of the Rufus app. This will open up another
a Download heading, under which you can see the latest version of dialogue box asking you to download and use a new version of
Rufus (2. 1 2 in this instance). Left click the link to start the download. Syslinux. Syslinux is a selection of boot loaders used to allow a
modern PC to access and boot from a USB flash drive. It is necessary,
• so click on 'Yes' to continue.
S>·
.
. ...... " ·-_,...,
.
, �..... �·--.n•,.," .,..,. "" '
• Milk�tlttltdl-101IIOS01VtR
, �. �·"
•
n.lt..._._t,111r..a6MIZC1'1212NttllllflflmlonMily
T.\IOIItlti �jlf'll
w
�...lfllltll�t�Oftf+ln101'�UliZOl'-•Of.
""
•f'I)III
N.afi
_.. .. ........ .tJfltWw�tloM-�.e110t �c
c-p-..e....ctiOH•nOifott.
._.'tM,_II*I01IINI•IncI�.,_.tiltwoMldiliotlolfla
'•'-'OpiJons --..."........_,"" " ..--rldh<!�.¥'w'td"'-.IKO
C!CIIotll.......ui...W.,_,_, lflriM w
S.ltd'VII'..(lflllfl(1......llltlt!MI.....,_,""lfofttfiln
QCht<JtMM•'•INdb!MkJ II'IKt
S.ltd Nt'leCMC...llw.,...11oft
•
P.Q.Nt.-
e(1..-u...."' .. � .... 1'10 ...... .. 0�•-.t
�
f:a(lt,lh• ....•4klutlllf
.., GO"""' ...... f
lit.
,.....�·.....
. ..""""''""""'��
P(o...t•""'--"""'-"""'lllo1
0C.ueor•""'*"'"""..,,.iUo'IN� _,...,.. ..... .�
.. . ... ��y. ,....
I •• I .,
• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • •
Double-click the downloaded Rufus executable; you STEP 5 The next step asks which image mode you want
STEP 2
can click 'Yes' to the Windows security question and the Mint ISO lo be written to the USB flash drive
'Yes' to checking fo r updates. With Rufus launched it should have in. Both methods work for different situations but generally, the
already identified your inserted USB flash drive; if not just remove recommended ISO Image Mode is the more popular. Make sure this
and reinsert. mode is preselected and click OK to continue, followed by OK again
to confirm the action.
#' Rufus 2.11.995
v
QWr.te.tn 00 Image mode
FATI2 (Default)
OK
Cluster size
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
At first glance the Rufus interface can look a little The Mint ISO is now bei ng transferred to the USB
STEP 3 STEP 6
confusing, don't worry though it's really quite flash drive. The process shouldn't take too long,
simple. To begin with, click on the disc and drive icon next to the again depending on the speed of the USB device and the PC. You
'Create a bootable disk using..' section and the 'FreeDOS' pull-down may find Rufus will auto·open the USB drive in Windows Explorer
menu. This will launch a Windows Explorer window where you can during the process; don't worry you can minimise or close it if you
locate and select the Linux Mint ISO. want. When the process is complete, click on the Close button.
-- -- -- -
�· , • Create elltendtd label and 1con Illes
---·-
--
- ...
-- �-- �
-- - - �-- --
�-
- ·· -
- -·
�-
-
-- -·-
�....
-;
� . � "'""
--- - - -·
- -- - • --._____
-
-·- =
-
�,.- -----'
·
--
·
a-·---·
·-
a------
--� Copying ISO files...
--
.........
-
....-.. .
cancel
- - -
·-- - � -
..
··--
---- log
Vi rtua Box
•
l nsta
If you don't want to dedicate an entire computer to running Mint, one option is to use
a Virtual Machine. VirtualBox is one of the best VMs and with it you can run a virtual
version of other operating systems within your already installed OS.
GOING VIRTUAL
Using a Virtual Machine {VM} will take resources from your computer: memory, hard drive space, processor usage and so on;
make sure you have enough of each before commencing.
• • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
The first task is getting hold of VirtualBox. If you With the correct packages downloaded, and before
STEP 1 STEP 3
haven't already, head over to www.yjrtualbox,org we install anything, you need to make sure that the
and click on the large 'Download VirtualBox 5.1' box. This will take computer you're using is able to host a VM. To do this, reboot the
you to the main download page. Locate the correct host for your computer and enter the BIOS. As the computer starts up, press the
system, Windows or Mac, the host is the current installed operating Del F2 or whichever key is necessary to Enter Setup.
system, and click to begin the download.
+ • -...
VirtualBox
Wolcomo to V1rtui1Box.org!
____...._.,.____.,..... .. ___...___....,._...._____.........
- -- ____ .__ .. .... ....
... .... ..
____ ,.____ ..._OW\) _ _
.....-�
..._,__..._ --- ____.,.._,...,._,.,__..__....
....., .....
- �----·· ....._�. ·-�· �-.··-·· --
-- __...,.__,....,._______
... ......
.,. _____..,...__..__,
-- -·-....·--.---�-·- .._,_
s
---
Download
-
1
-
VirtuaiBox •
p
i
tfi
ii
j
Next, while still at the VirtualBox download page, As each BIOS is laid out differently it's very difficult
STEP 2 STEP 4
locate the VirtualBox Extension Pack link. The to assess where to look in each personal example.
Extension Pack supports USB devices, as well as numerous other However, as a general rule of thumb, you're looking for Intel
extras that can help make the VM environment a more accurate Virtualisation Technology or simply Virtualisation; it's usually within
emulation of a 'real' computer. the Advanced section of the BIOS. When you've located it, Enable it,
save the settings, exit the BIOS and reboot the computer.
Vi rtualBox
Download VirtuaiBox
O.Onertnon
. . . . . . . . . . . . .............. .. .. .. .. . . .. .. .. . .. .. .. . .. .. . . .. .. .. . .. . . . . . . . . . . . . .. . .. . . . . .. . . . . . .. . .. . . .. . .. . . .. ............. .. . . .. . .. . . .. . .. . . . . . . . . . . . . . . . . .
With the computer back up and running, locate When installing VirtualBox your network connection
STEP S STEP S
the downloaded main VirtualBox application and will be disabled for a very brief period. This is due
double-dick to begin the installation process. Click Next to continue, to VirtualBox creating a linked, virtual network connection so that
when you're ready. any VM installed will be able to access the Internet, and your home
network resources, via the computer's already established network
f!J Oroclo VM Virt�IBox 5.1.10 Solup connection. Click Yes then Install to begin the installation.
. . . . .. . .. . . .. . .. . .. . . . . . . . . . . . . . . .. . . .. . .. . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The default installation location of VirtualBox You may well be asked by Windows to accept a
STEP 6 STEP 9
should satisfy most users but if you have any special security notification; click Yes for this and you may
location requirements click on the Browse button and change the encounter a dialogue box asking you to trust the installation from
install folder. Then, make sure that all the icons in the VirtualBox Oracle. Again, click yes and accept the installation of the VirtuaiBox
feature tree are selected and none of them have a red X next to application. When it's complete, click finish to start VirtualBox.
them. Click Next to move on.
I
Oracle VM Virtual Box 5.1.10
installation is complete.
. . . . . . . . . . . . . . . . . . . .. . . . . ......... .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . .. . . . .. ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
STEP 7 This section can be left alone to the defaults, should With VirtuaiBox up and running you can now
STEP 1 0
you wish. It simply makes life a little easier when install the VirtuaiBox Extension Pack. Locate the
dealing with VMs; especially when dealing with downloaded VMs, as downloaded add-on and double-click. There may be a short pause
you may encounter in the future. Again, clicking Next will move you while VirtualBox analyses the pack but eventually you receive a
on to the next stage. message to install it; obviously click Install to begin the process, scroll
down the next screen to accept the agreement and click 'I Agree'.
!;210'
..test.ort-..en....,
..11:• ,..._on 1he _...,
1;210'
1;210'<011:. ,..._.. 1he �Uul<ha. � ... ..-.- . ..... . ....... ��,.....
c:::
:::J:!ii!'o �
Testi n Li n ux M i n t's
Live Envi ro n m e n t
With the DVD or USB boot media ready you can now test Mint in a Live Environment
before deciding to install it. A Live Environment is a functioning version of Mint that's
running from the boot media as opposed to running off your computer's hard drive.
UEFI BIOS
The Unified Extensible Firmware Interface (UEFI) is used to id e ntify hardware and protect a PC during its boot up process. It -
replaces the traditional BIOS but can cause issues whe n i nstalling Linux Mint .
• • • • • • • • • • • • • •••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • ••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • •
Insert your DVD or USB flash drive into your PC and, With UEFI turned to Legacy mode, there are
STEP 1 STEP 3
if you haven't already, shutdown Windows. In this now two ways of booting into the Mint Live
instance we're using the USB boot media but the process is virtually Environment The first is via the BIOS you're already in. Locate the
identical. Start the PC and when prompted press the appropriate Boot Sequence and change the first boot device from its original
keys to enter the BIOS or SETUP; these could be, for example, F2, setting, usually Internal HOD or similar, to USB Storage Device for
Del or even F1 2. the USB media option; or DVD Drive, for the DVD media option.
I
c..rd Reder ..... I I
USB-CDROn ..... I I
USB HDD ..... lo I
LAn . . . .. I I
Ois�tbled -... I I •
There are different versions of a UEFI BIOS, so Alternatively use the Boot Option Menu. With this
STE P 2 STE P 4
covering them all would be impossible. What you're option you can press F12 (or something similar)
looking for is a section that details the Boot Sequence or Boot to dis play a list of boot media options; from there, you can choose
Mode. Here you have the option to turn off UEFI and choose Legacy the appropriate boot media. Either way, you can now save and exit
or disable Secure Booting. Mint does work with UEFI but it can be a the BIOS by navigating to the Save & Exit option and choosing Save
tricky process to enable it to boot. Changes and Exit.
<Inter Sehp>
TESTING MINT
With the UEFI BIOS side of things behind us, it's time to see what the Linux Mint desktop has to offer, albeit in the test,
Live Environment.
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
STEP 1 Linux Mint now boots up and you are taken directly STEP 4 To the far right of the Panel, you can see several
to the Mint default desktop; we have the Cinnamon icons; these indicate the current logged in user
mainstream version in the screenshot here. You can see three icons account (which is live Session User at present), active network
on the desktop: Computer, Home a nd Install Linux Mint. connections (where you can adjust or setup your WiFi or wired
internet connection), volume controls, time and date, and All
Windows, which when clicked displays all opened apps.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
STEP 2 Along the bottom of the desktop is the Mint Panel STEP 5 There are also some quick launch icons on the
and the Mint Menu. Click the Menu and it displays Panel next to the Mint Menu: Show Desktop, which
the core applications along with a search bar and various icons lined will return you to a blank desktop while minimising all open apps;
up down the left-hand side. These are quick launch icons that will Firefox web browser; the Terminal command line; and Nemo file
change to the more recently used apps as you use Mint. manager. You can use any of these in a live session but anything
stored won't be saved on quitting.
- .
. - •• . .. .
- - • 11::1
-- .-..-
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • •
STEP 3 The three icons to the bottom of the quick launch Before you install Mint, you need to make sure you
STEP 6
strip indicate the session options: Lock Screen, have an active Internet connection. If you have a
Logout and Quit (shutdown Linux Mint). Just above the Lock Screen wired, Ethernet connection, and it's not already plugged into the
icon is Files, clicking this will launch Nemo, the linux Mint Cinnamon computer, do so now. If you're using Wi-Fi. click on the network
file manager. connection icon in the bottom right of the Panel, find your router id
and enter the details.
�
-
--- -·--
-
- -
I nsta i n """"' Li n ux M i n t
o n a P�
You've picked your Linux Mint desktop version and you've played around in the Live
Environment. Now it's time to get Mint onto your PC as a permanent replacement for
Windows. Thankfully the process is extremely easy.
GOING MINT
We're assuming at this point that you're still in the Live Environment and that you've set u p and activated your Wi-Fi
connection, or plugged your computer into your router via a wired connection.
• • • • • • • • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••• • • • • • • • • • • • • • • • • • • • • • •
Providing you're connected to the Internet (if not After clicking Continue you're asked ifyou want
STEP 1 STEP 3
then do so now) and you're in the Live Environment, to Install Third-Party Software. Providing you're
start the installation process by double-clicking on the Install Linux connected to the Internet, as from Step 1, then tick the box. This
Mint icon on the desktop. will make sure that hardware drivers, Adobe Flash and codecs for
MP3 and video files are installed with the main Mint system. Click
Continue for the next stage.
�-.- ......
....
. - --·- ____,......,.
• • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • •
Launching the Install Linux Mint app will open up This next stage asks you how you want to manage
STE P 2 STE P 4
the Welcome screen. Make sure your language is the installation of Linux Mint onto your PC. In our
selected From the list on the left and if you want to click the Release example, we're going to Erase Disk and Install Linux Mint, which will
Notes link to read what the latest changes and additions are to Linux wipe the current OS and ALL DATA replacing it with Mint; make sure
Mint 18.1 Cinnamon. When you're ready, click the Continue button. you have a good backup, just in case. Click Install Now to continue.
- - ----- ------
�
ri --·--
,·.� ,
,
,�
--··--
---� · .
... ""
..... _...,__.. .... . ..........,......,
-- o
Ot-_.,...,._.,......,.
.
·--- - - �
__......_·
-
....-
---··-
- _.....__.............
.. ..
-
----·�-·..--
-
-
-....-.'-- --- ·--.-----
__ __,
.. - ....
__
�
....�-'' -- ______..,..._____ ....-
.. -- JJ
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
STE P S Before the installation process can begin, you're In this stage you need to set up your Mint username
STEP S
asked if the choice you made regarding the erasure and password. Enter your Name to begin with, then
of the hard drive is correct. This is your last chance to back out. If Computer Name, which is the name it's identified on the network as.
you're certain you don't mind wiping everything and starting again Next choose a Username, followed by a good Password. You can tick
with Linux Mint, click Continue. the Login Automatically option but leave the Encrypt Home Folder
option for now.
nt.-.t.•--•�w..-
..,..... ................_.. • -- o. _
... .. .. 0
u ,.... '*'
,
,. ..,.,
.
,.Uh,..,... ....
,,...,.,.., ,... ,,...,•._."'••·•- ,........w.-.. . ....
.., ,.,.��w , '""""""' .
..
.,. -....·-
. - · 0
.... -·-
= ··-..
�----
"""""',.-,.'!h. ,.. ,
,
,11
>y ••!IOWon•
•"IWI
..... ,_."-,."'...,.,_ _,.,..,.11111,.._Nt l,.9f)"'!t•M�
"'-··-
:=--c
..... �
---: 0
'IWIW!f!t>tt<UNftriN�-"1"�11 ,.... . ...
-.... .
.......... ·--·
"""' ''
"'- -- 0
VUl !OOIIIf"l.ol
,,..,�,... ..
_. --'
'lwY-tl ...'''� ••
·
•P'f"'""'
••
r .. olo!l
o .....�-•
. .,
llllflotlll•l oUUI't.I.IUWiti••.�M
p..-!t>U\1�01 ihU Wil
Ill l ., ioflllfl - ....,.._......
• • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
STEP 6 This next stage determines your location. You can The installation process will now begin and you can
STEP 9
enter the nearest major dty, or even try your local see what's being installed along the bottom of the
town and see if it's in the list of available choices. When you're install window. You can also use the arrows on the screen to browse
ready, click Continue. through some of the features available in Linux Mint.
..,
• .,.w
...r-_.�...,..
....,._...,...... ... .. ....
.. .. .t I c- Or- All
t.. t:'.k l'cw HilltP)' Joot'r¥'u )*$ ttc!P
rtIc.tlClrul'll·fi.AI ..
tO!Ir$ a •
--·
•
II ......
r.� -
-
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • •
STEP 7 Next up, use the options to pick which keyboard When the installation is complete you are
STEP 1 0
you're using. In most cases it will be the option presented with a completion box, asking you if
with the Extended WinKeys. Use the Type Here. . . box to test your you want to continue with the Live Environment or restart the PC
keyboard setup is correct. Click Continue when you're ready to with Linux Mint as the main operating system. Click on the Restart
move on. Now button, followed by Enter and remove the Installation Media
when asked. Congratulations. Linux Mint is now installed.
----·
l nsta i n Li n ux M i n t
i n Vi rtua Box
With Oracle's VirtualBox now up and running, and continuing from the previous
section, the next task is to create the Virtual Machine (VM) environment into which
you install Linux Mint.
CREATING THE VM
There are plenty of optio ns to choose from when creating a VM. For now though, you can set up a VM adequate to run Mint
Cinnamon and perform well.
• • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
With VirtualBox open. click on the New icon in The next section will define the amount of system
STEP 1 STEP 3
the top right of the app. This will open the new memory (RAM) the VM has allocated. Remember
VM Wizard. this amount will be taken from the available memory installed in
your computer, so don't give the VM too much. For example, we
9 OMdt'f ·
$
M'IIrt m....... D X
have 8GB of memory installed and we're giving 2GB to the VM.
... _ _
When you're ready, click Next to continue.
�-
•
a -
..
.......... _ .. ..... ...,..,.. . • _
-IIIObrlao!IM.o:I'IC•fi-
.. __. ...... .. _ r;.�
....�.-bn...,.lofltl_....._,.
. • . . _.. Unux Mint Cinnamon
- ....
� ......--.,,..,._·'t:l- '
,_.. .........
.,
cw.
,._...,_...,..._('Ill
..,.
..
__
- -'
'
'---
...
·-
...
...
-
• • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••• • • • • • • • • • • • • • • • • • • • • • • •
In the box next to Name, type Linux Mint and This section is where you start to create the virtual
STE P 2 STE P 4
VirtualBox should automatically choose Linux hard disk that the VM will use to install Mint on to.
as the Type and Ubuntu {64-bit) as the Version. If not then use The default option, 'Create a virtual hard disk now', is the one we're
the drop-down boxes to select the correct settings; remember using. Click Create to move on.
Mint mainstream is based on Ubuntu. Click Next when you're ready
to proceed.
� -
- - -·
•
�----
-----
- ---'.-
--
--� ·�
��==
- ==
�
'
'
==�
, a - ,_.,. - -
......... .......
c. "'".._ .... ..,.
�:...
_ _..._
_
Unux MintCann.amon
'
__
____
_
.
•c.--·- ........... ... _
...... . __. .......
_ ..........
� ..... .....
. _.. Unux Mlnt Onnamon ._ ....
________....
� ..
"� . ..... .........
.
,
..... .,.,._.,.
_
--·-
...
.... ...._.....
_,____ ..
"-to-. ....._ .. ....._..w..,... ........
f'f .._..... ,.,.... OIII
....__
...._
....llt4!d .,.........._...........
.........
. ...
�.
.. ..
_...__ ... -..-�--··--
_ ....._ ... w..
O o.
............, .... ..
--
.
_ _.... @)o- ....
,..,..._ ... -
·-- Ow.iill-.. ..-...hotofM..
-- ..
[
........, ,...O'tod..,._
,....
_____ _!:
�? � ('�
ill -
....
.,. ._....o--..,...,...., 11.)1•.
______ _,
...�- .......,..,.......
• •• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
The pop-up window that appears after clicking After clicking Create the initial set up of the VM
STE P S STEP S
Create is asking you what type ofvirtual hard disk is complete; you should now be looking at the
you want to create. Use the default VOl (VirtuaiBox Disk Image) in newly created VM within the VirtualBox application. Before you
this case, as the others are often used to move VMs from one VM begin though, click the Settings button and within the General
application to the next. Make sure VOl is selected and cUck Next. section click the Advanced tab. Using the pull·down menus, choose
'Bidirectional" for both Shared Clipboard and Drag'n'Drop.
I X -
-
__
l-iard disk fl� type
....
._ _
_
"-_._...tywei.. __.,.._.._..,,__.,.,...,.
Nt .__ ...._ � ..._
NrddLtt,....,.,,.,.......1,..11\.... .....
..
...,
.to.....
... ;Orol
an!NwftstmnOllfdl-.ci, EJ �....-: '·( l'u.l�--� ,...�
® atw.,..,.MINOr) Im '*'•9"
0 'II"()(Vilutltt.rdo.J � AudiO
0 *OII:f'*IUoll�o.t.)
"""
..
-
......
r...
....,
., �----r-:-
-:-
-: -_
r.- ._!�
1�
"'
• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
The question of whether to opt for Dynamically Follow that by clicking on the System section,
STEP 6 STEP 9
or Fixed sized virtual hard disks may come across then the Processor tab. Depending on your CPU
as being somewhatconfusing to the newcomer. Basically, a allocate as many cores as you can without detriment to your host
Dynamically Allocated virtual hard disk is a more flexible storage system; we've opted for two CPUs. Now click on the Display section,
management option. It won't take up much space within your slide the Video Memory up to the maximum and tick 'Enable 30
physical hard disk to begin with either. Ensure Dynamically Allocated Acceleration'. Click OK to commit the new settings.
is selected and click Next.
•.__.s._,. l X
• • !11! -
I'J
Iil
__.,..,
�
.
!;;; ,.:D
� -
�
- ------- .. ..
___,....._llftoll.....................
...........t,......
..
. .._.._...._�
....
�
.---
--·
- -------
-. (j }] e;;;;;;;)]
I
-
...
llnux MintOnnamon �
•.
,. , ..� "-"1-k_,.-..._____"" �
.J,....,.... ......
, _ .. �� · G-ll Unux Mtnt
...... .. ..
. .. .. .. _
-
-..-.-_.........
s:. ......
. . --Ga---1)--
e!WII'.wll-
,....,._...,._.._ _,.,..
..,.____.....
, .... u.....»-�
l!:l
®- CJ "'-'FeldciS
o--
.... ...
..�
,I
.......... ,.....
G- _
---.l...-
.. II> ....
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
The virtual hard disk will be a single folder, up to the Click on the Start button and use the explorer
STEP 7 STEP 10
size you state in this section. Ensure the location button in the 'Select Start·up Disk' window to
of the virtual hard disk, on your computer, has enough free space locate the downloaded ISO of Mint; the explorer button is a folder
available. For example. we've used a bigger storage option on our with a green arrow. Click Start to boot the VM with the Linux Mint
0:\ drive, named it Linux Mint and allocated 25.50GB of space to the Live Environment. You can now install Linux Mint as detailed in the
virtual hard disk. previous Installing Linux Mint on a PC section.
l X
•
a -
....
.......,,.11.1-G"NMCCI
• ..(1.6tGI)
a-
---- � c-..
----�, -.... ....- - .., ........, ,...--,-
.,. -----------i
�o e i n Li n ux
Coding in Linux mostly happens in the Terminal or the Command Line. While it
can be a scary looking place to begin with, the Terminal is an extremely powerful
environment. Before you can start to code, it's best to master the Terminal.
TAKING COMMAND
The command line is at the core of Linux and when you program with it, this is called scripting. These are self-contained
programs designed to be run in the Terminal.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••• • • • • • • • • • • • • • • • • • • • • • •
The Terminal is where you begin your journey What you currently see in the Terminal is your login
STEP 1 STEP 3
with Unux, through the command line and thus name followed by the name of the computer, as you
any scripting from. In Li nux Mint. it can be accessed by clicking on named it when you first installed the OS on to the computer. The
the Menu followed by the Terminal icon in the panel, or entering line then ends with the current folder name; at first this is just a tilde
'Terminal' into the search bar. H. which means your Home folder .
• • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
The Terminal will give you access to the Linux The flashing cursor at the very end of the line is
STE P 2 STE P 4
Mint Shell, called BASH; this gives you access to where your text-based commands will be entered.
the underlying operating system, which is why scripting is such a You can begin to experiment with a simple command, Print Working
powerful language to learn and use. Everything in Mint and Linux . Directory (pwd), which will output to the screen the current folder
as a whole including the desktop and GUI, is a module running from
. you re in. Type: pwd and press Enter
' .
daVId�clavi
davi�clavid- F
ile Ed
it v
- search Te
riiW\oll p
Hel
File Edit View Search Terrrnnal Help
. . .. .. . .. . . . .. . .. . . . . . .. . . . . .. . . . . . . . .. . . . . . . . . . . .. . . . . .. . . . . . . . . . . . . . . . . . . . .. .. . . . . .. . . .. . .. . . .. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
All the commands you enter will work in the same The list of available Unux commands is vast,
STE P S STEP 6
manner. You enter the command, include any with some simply returning the current working
parameters to extend the use of the command and press Enter to directory, while others are capable of deleting the entire system in
execute the command line you've entered. Now type: uname -a an instant. Getting to know the commands is part
and press Enter. This will display information regarding Linux Mint. of learning how to script By using the wrong command, you could
.
In scripting, you can use all the Linux command-line commands end up wiping your computer. Type compgen -c to view the
within your own scripts. available commands.
d.>v•d@d.>vul-mont -
File Ed1t View Search T erm•nal Help
File Edn View Search Term1nal Help
HERE BE DRAGONS!
There's an urban myth on the Internet that an employee a t Disney Pixar nearly ruined the animated movie Toy Story by
inadvertently entering the w rong Linux command and deleting the entire system the film was stored on.
. . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . ..
--
- ·- -
--
. ................. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
However, the Terminal doesn't offer that luxury. If Second, research before blindly entering a
STEP 2 STEP 4
you were to access a folder with files within via the command you've seen on the Internet. If you see the
Terminal and then enter the command: rm * *, all the files in that . command: sudo dd if�/dev/random of=ldev/sda and use
folder would be instantly deleted. They won't appear in the Rubbish it in a script, you'll soon come to regret the action as the command
Bin either, they're gone for good. will wipe the entire hard drive and fill it with random data. Take a
moment to Google the command and see what it does.
david�david-mint -/Ml
Go gle .. ___,..
_,.,.,�··00 �.a.. .
! _ _,...,-4
.. ....... .....,�,, .. .......
---' ...,. ....-.. .. ...,.
... ...... _
...
-..
...-y _ ...... f-MJR ,_Cit'tfl•.->
... ,... ....ltpee
can *Cid Ndev/Ufo ot-Mevflda• ph'plcally damage 'f04II hard diSk
.,..t._cenla.lll'q •ao.!l�• ta!IO ����•PQM, C..:.toe.• •
.•.
lol ••
•.,. 4 ••'"-'! M,.,,.,." •�-·._ .,""'... *'
- "- ''
•
'""
'
""
'.-
. t.. •
...
. ...,..
GET SCRIPTING
A Bash script is simply a series of commands that Mint will run through to complete a certain task. They can be simple or
remarkably complex, it all depends on the situation.
• • • • • • • • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • •••••••••••• • • • • • • • • • • • • • • • • • • • • •
You'll be working within the Terminal and with a To begin with, and before you start to write any
STEP 1 STEP 3
text editor throughout the coming pages. There scripts, you need to create a folder where you can
are alternatives to the text editor, which we·U iook at in a moment put all our scripts into. Start with mkdir scripts, and enter the
but for the sake of ease, we'll be doing our examples in Xed. Before folder cd scripts/. This will be our working folder and from here
you begin, however, run through the customary update check: sudo you can create sub-folders if you want of each script you create.
apt-get update && sudo apt-get upgrade.
davi<
da vi< e
Fi
l Edit Vi
ew earch Te
S r
iTllllol l Hel
p
Fi
le Edit VI
ew earc
S iTllllol l
h Ter H
elp
' � ·r. · ' �udo apt ge t updat e M sudo apt - ge t upgr
!sudo) passw ord f o r d�vtd:
H1t:l http://pp� .l�t rr
lchpad . r l�t/oper l \hot .develope rs/ppa;ubunt u
H1t:] http://pptt.ltHmthp.td . net/peterlevl/ppt!i/ubuntu xen1al I n
Hlt:3 http://atchlve.t.ttlOillt.tl .toB/tJbtJtltu xental InRelease
Htt:4 http:llpptt.l�un(hp.:td.nf"tl thor. .:ts- schl ex lblenderlubunt u x
H1t:S http:ll�rrhtvP.trbtJnttJ. comltrbtJntu xen1al InRelease
Ign : 6 htt p : IIWWW. fill r ror �.. rv1cP . org/s 1t esIpackages . l Hluxflll nt . c
Get:7 htt p : ll�rchlvf" . tJbtJitttJ . co�ltrbtJittu xer11al-updates InRele a
Htt:8 http:llppa. latrrtchJ1ad.rtetlwlrtelwlne-bulldslubuntu xen1al
Hlt:9 http:llwww.mtrrorservlce. org/slteslpackages. \tnuxmtnt.c
H1t:l6 http:llr�posltory.spotlfy.co� stable InRelease
Get : l l h ttp : l /�t· chtvP .t ibt rr
t t tr.com/tJ btJnt u xen 1 al · bac k p orts I nR
• • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • •
There are several text editors we can use to create Windows users will be aware that in order for a
STE P 2 STE P 4
a Bash script: Xed, Vi, Nano, Vim, GNU Emacs and batch file to work, as in be executed and follow the
so on. In the end it all comes down to personal preference. Our progra mmi ng within it, it needs to have a .BAT file extension. Linux
use of Xed is purely due to making it easier to read the script in the is an extension-less operating system but the convention is to give
screenshots you see below. scripts a .sh extension.
- .
-
Fti
e Edit lf
t
ew Search Terrro
NI Help
--
--�-
. . . . . . . . . . . . . . .. . . . . . . . . . . .. .. .. .. . . .. .. .. . .. .. .. . .. .. . . .. .. .. . .. . . . . . . . . . . . . .. . .. . ... .. . . . . . .. . .. . . .. . .. . . .. . . . . . . . . . . . . . .. . . .. . .. . . .. . .. . .. . . . . . . . . . . . . . . .
STE P S Let's start with a simple script to output something When you enter ls again, you can see that the
STEP S
to the Terminal. Enter xed b ell owo rld Isb
l helloworld.sh script has now turned from being
This will launch Xed and create a file called belloworldlsbl In Xed, white to green, meaning that it's now an executable file. To run
enter the following: # ! /bin/bash, then on a new line: echo the script, in other words make it do the things you've typed into it,
Hello World!. enter. . /be lloworld. sh.
File E ot
d Vw S
oe earch Term
onal Help
_] , I• lj : .j I 1' '. • Cd hclloworld . �h
-1' : 1 '. . . I I
•
1 •
ls
h el\oworld. sh
: L I I I : : ol I I
• • I 'I <.hmod " helloworld.sh
j.< • '
r .1-1. I I ' . '
I h
Edot Voew Search Tools Oocumen<s Help 'H• \ , :
1 "' '· r 'j ·• fl
rJ.1,1 ,j :1.I ' " . I ·tt•
:I'.. . /h('\loworld. sh
Hel lo World 1
rJ.,•1d d o�.· 1 ,j
j
,, I 'I ! -I,.. I
l •helloworld.sh x
*llbin/buh
The # ! /bin/bash line tells the system what Shell Although it's not terribly exciting the words 'Hello
STEP 6 STEP 9 ,
you're going to be using, in this case Bash. The hash World!' should now be displayed in the Terminal.
(#) denotes a comment line, one that is ignored by the system, the The echo command is responsible for outputting the words after
exclamation mark (!) means that the comment is bypassed and will it in the Terminal, as we move on you can make the echo command
force the script to execute the line as a command. This is also known output to other sources.
as a Hash-Bang.
File Elk
File Edot View Starch Tools Oocumencs Help
File Ecit If- Starch Tools 0ocurne<u Help
Ill ·helloNorld.sh )(
·helloworld.sh )(
*I /bin/bash
*!/bin/bash
echo Hello World ! Th1s 1s •Y ftrst scr1pt in L1nux Mi
echo Hello World !
. . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . .. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
You can save this file, clicking File > Save, and exit Think of echo as the old BASIC Print command. It
STEP 7 STEP 1 0
back to the Terminal. Entering l s, will reveal the displays either text, numbers or any variables that
script in the folder. To make any script executable, and able to are stored in the system, such as the current system date. Try this
run, you need to modify its permissions. Do this with chmod +X example: echo Hello World! Today is $(date +%A). The
b
ell
owor
lds
I b. You need to do this with every script you create. $(date +%A) is calling the system variable that stores the current day
of the week.
davi
File Edot Vi
e
w Searc
h Ter
1111 nal H
elp -
VARIABLES
Just as in every other programming language a Bash script can store and call certain variables from the system, either generic
or user created.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••• • • • • • • • • • • • • • • • • • • • • • •
Let's start by creating a new script called be!lo.sb; The output now will be Hello David. This is because
STEP 1 STEP 3
xed b
ell
o .s
b. In it enter: # ! /bi.n/bosh, then, Bash automatically assigns variables for the user,
echo Hello $1. Save the file and exit Xed. Back in the Terminal which are then held and passed to the script. So the variable
make the script executable with: chmod +X b ell o .sb. '$1' now holds 'David'. You can change the variable by entering
something different . /hello . sh Mi.nt.
. / hello. sh
Hello
;:] ,) . ; j
c '
". ' . : (� ' ' ./hello.sh Dav1d
Ed•< Vtf:w Search Toots Don.men<s Help Hello Dav1d
rl .I : r1 �
• r' I ·, ' .d r ,0 . , • • ./hello.sh Mlnt
Hello M1nt
rL1,lri j' I
'
, ·,ld ' l ' I�
ocho Hollo $1
I
• • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
As the script is now executable, run it with . I STE P 4 You can even rename variables. Modify the hello.
STE P 2
hell o.s b . Now, as you probably expected a simple sh script with the following: firstname=$1,
'Hello' is displayed in the Terminal. However, if you then issue the surnome=$2, echo Hello $firstname $surname. Putting
command with a variable, it begins to get interesting. For example. each statement on a new line. Save the script and exit back into
try ./hello. sh Dav'id. the Terminal.
Fil
e E dll V
oe
w S
eorch Te
rmo
nal Help
Ill ·helo.sh )(
"/bin/bash
f.r t •Sl
�" - s2
echo Hello Sf1rstna� Ssurna.e
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
STEP S When you run the script now you can use two Let's expand things further. Create a new script
STEP S
custom variables: . /heHo. sh David Hayward. called greetings . sh. Enter the scripting as below
Naturally change the two variables with your own name; unless in the screenshot . save it and make it executable with the chmod
you're also called David Hayward. At the moment we're just printing command. You can see that there are a few new additions to the
the contents so let's expand the two-variable use a little.
. script now.
d grt't'tings.sh
� greeongs.sh x
I! /bin/bash
echo .. n t c at 1s your na 7 •
read firstname
echo -n t1 ' J, and wP.at lS your surnan:�e7 '"
read surname
clear
echo Hello Sflrstna�e Ssurname, how are you today?
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Create a new script called addi ti.on. sh, using We've added a n to the echo command here
STEP 6 STEP 9 -
the same format as the hellg.sh script, but which will leave the cursor on the same line as the
changing the variable names. Here we've added iirstnumber and question. instead of a new line. The read command stores the
secondnumber. and used the echo command to output some users' input as the variables firstname and surname, to then read
simple arithmetic by placing an integer expression, echo The sum back later in the last echo line. And the clear command clears
is $(($firstnumber+$secondnumber)). save the script, and the screen.
make it executable ( chmod +x addi ti.on. sh).
dav
addition.sh File Edot View Search Termonal Help
11 e Edit W?:w search Tools Documents H"IP
� additoon.sh x
•IIbin/bash
f rs .. nt111be • Sl
c;eco1jm'"lit. •$2
echo The su• is SI( Sflrstnumber+Ssecondnu�ber) )
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
When you now run the addition.sh script we can As a final addition, let's include the date variable
STEP 7 STEP 10
enter two numbers: . /addition . sh 1 2. The we used in the last section. Amend the last line
result will hopefully be 3, with the Terminal displaying 'The sum of the script to read: echo He1lo $firstname $surname, how
is 3'. Try it with a few different numbers and see what happens. are you on this fine $(date +%A)?. The output should
See also if you can alter the script and rename it do multiplication, display the current day of the week. calling it from a system variable.
a nd subtraction.
greetmgs.sh
david t VIew
File Edo Search Tools Oocumem Help
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
STEP 1 Let's look at the If, Then and Else statements now, The next line, ELSE, is what happens if the variable
STEP3
which when executed correctly, compare a set of doesn't equal 'David'. In this case it simply outputs
instructions and simply work out that IF something is present, THEN to the screen the now familiar 'Hello.. .'.The last line, the Fl
do something, ELSE do something different. Create a new script statement, is the command that will end the loop. If you have an If
called greeti.ng2. sh and enter the text in the screenshot below command without a Fi command, then you get an error.
into it.
!l o o; ...
� *greetlngs.sh x
"/bin/bash
.... . - .. � ._
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Greeting2.sh is a copy of greeting.sh but with a You can obviously play around with the script a
STEP2 STEP4
slight difference. Here we've added a loop starting little, changing the name variable that triggers a
at the if statement. This means, IF the variable entered is equal to response; or maybe even issuing a response where the first name
David the next line, THEN, is the reaction to what happens, in this and surname variables match a specific variable.
case it will output to the screen 'Awesome name,' followed by the
variable (which is David). greetings2.s
d.1vidl!! d.1vi
�"' S.. arch Tei'I!Wial Help
m greeungs2.sh x
"lbintbuh
MORE LOOPING
You can loop over data using the FOR, WHILE and UNTIL statements. These can be handy if you're batch naming, copying or
running a script where a counter is needed.
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••• •••••••••••••••••••••••••••••••••••••••••
STEP 1 Create a new script called count. sh. Enter the STEP4 The UNTIL loop works much the s ame way as the
text in the screenshot below, save it a nd make it WHILE loop only, more often than not, in reverse.
executable. This creates the variable 'count' which at the beginning So our counting to a hun dred using UNTIL, would be: until
,
of the script equals zero. Then start the WHILE loop, which WHILE [ $count -gt 100 ] ; do. The difference being, UNTIL count is
count is less than (the LT pa rt) 100 will print the current value of greater than (the gt part) one hundred, keep on looping.
count in the echo command.
..count.sh (
count.sn (· File Edit View Search Tools Documents Help
File Edit View Searcn Tools Documents Help
p {] 0; ,..
!] {] 0; ,.. �
� •count.sn x
• • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Executing the count.s h script will result in the You're not limited to numbers zero to one hundred.
STEP2 STEP 5
numbers 0 to 99listing down the Terminal screen; You can, wilhin the loop, have whatever set of
when it reaches 100 the script will end. Modifying the script with the commands you like and execute them as many times as you want
FOR statement, makes il work in much the same way. To use it in our the loop to run for. Renaming a million files, creating fifty folders
script, enter the text from the screenshot into the count.sh script. etc. For example, this script will create ten folders named folder1
through to folder1 0 using the FOR loop.
•count.sh
Fri
e Ecit VIew Search Tools Documents Help
File Edit v,.,,. Search Tools Documents Help
p {] 0; ,..
l( 19 � Q. 9.
� •count.sh x
#1/bin/bash � •count.sh lC
#!/bin/bash
for count in {8.. 188}; do
echo Scount
for count in{�.. 18);do
let count-count+l
•kdir Folder$count
done let count-count+1
done
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
The addition we have h ere is: for count in Using the FOR statement once more, we can
STEP3 STEP6
{0.. 100}; do. Which mea ns: FOR the variable execute the counting sequence by manipulating the
'count' IN the numbers from zero to one hundred, then start the {0 ..100} part. This section of the code actually means {START..END..
loop. The rest of the script is the same. Run this script, and the same INCREMENT}, if there's no increment then it's just a single d igit up to
output should appear in the Terminal . the END. For example, we could get the loops to count up to 1000 i n
two'swith: for count in {0 . 1000 2}; do. . . .
Ill •countsh x
*count.sh
#!/bin/bash File Edit voew se�rch Tools Oocunnenu Help
11
M()( Sll 'liUT "I)
-
• • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • • • • •• • • • • • • • • • • • • •••
,'!fMI()( te# u<ll \II 1( n11 t 1L" )I ,.
�\() ( lt (( Sc"� •• up
lf [[ kll tin
1tl!.11 ( (\"
1l111111 ((lH 11\
: H ll
IUC•· llllJ ..,..I)
\t (( tt ll • ,,
••
.... h\11111\
lf II S\ tt SUI )ltflllll \... Jt\1)
STEP2 If you follow the script thro u gh you soon get the " II
•l,•tPIO( •h•-u1((l•l))l
H
""''""" u •
l-ofh� ·tt Sl"
f ••1CC\
h •t
ll 't
.
OJ
... ...,
, ·-· ··"
itlo.ll •'t•· e1n
hang of what's going on, based on what we've lf ll SJ U Sl
u- (( \.ffr•• " • II
1tllee """""""�·"'"''••••h• IIIIMI11
II
I .,... ... t. "'
, ,,
I 1111•H• flt\
)l tfl<ffl
...l61'tll'l1Ht l.
��
....... f'
already covered. WHILE, IF, and ELSE, wit h the Fl closing loop llff'
.-.,, IO�tOH'III �afttrlt\1.4\l
'1''
11111()-i II;ICAO;fOOl;llfllloi;}
SoC() ( uc�.IWII· IS\IS.ICII<• I) ,._,.
statement will run through the options and bring you back to the f\( )( - ;k ""'.. • ..t11 ••- 'l..J .....
jl'fii i)I IIUI
l .l �
..llll (( "lOa I• " JJ I .. c- S\ lll
I,
... ,,. &�..
•
... ·�HNI''
\,at-( \t \II
., s--;'IC;U II ,..�.. .. . IUiflll
start if you pick the wrong option. 1) s-:JtSC.tlt [ s.c.... ;U... ..
11-ur
�'lt\lfl ll
U �;\<,U ..�- ..... ..f ·lo
... •\AJ(te.tll
••
l,iof ;U;fl;;
)) s.--tJ1SC.1\t 1\MII ., • \.A6 I I hflll
"\.eJ(
••
•) s-;'IC;U
)) 1� 1C.1U
,...,.. ••
Soc.,..- IJ; U..
:t...,. tlloP ..t•
.,,._ "\tt.l""
1111f�11
...
eft\l!o 1U,fl11
I) t.-MIIC!U ( 'k,... ••
( 1
)1...... e.to\\ 1,1\ u
_,.._,,_
STEP 1 A very basic backup script would look something STEP4 Once the -/backups folder is created, we can now
along the lines of:#! /bi.n/bash, then, create a new subfolder within it based on the
tar cvfz -/backups/my-backup. tgz -/Documents/. This nme Stamp variables youset up at the beginning. Add mkdi.r -p
wi ll create
a compressed file back up of the -/Documents folder, $dest/"$day $month $year". It's in here that you put the
with everythi ng in it, and put it in a folder called /backups with the backup file relevant to that day/month/ye ar.
name my·backup.tgz.
Ill bac�1.sh M
•ltbin/buh
backup1 _,., (-/scripts)
clear
File Eck VIew Starth Tools Docurntnts Http
1 TlJDe ta
s •p
da)'•$ (d•t• •'\A)
mont �•S(date •\81
year-S(d•t• •'Y I
�bic�l.sh x
Jt Fo\ders
•!/bin/bash dest-tbackups
• u t•- /D ocu
..ntJ
tar cvfz �tbackups/�·backup.tgt �toocumentsA
if I •d Sdtlt I; then
echo
e\se
echo
echo
fi
r� ·P s kY t •I'll ·S
.tdlr · P Sdest/ Sd�y �nth Syear
While perfectly fine, we can make the simp le script STEP 5 With everything in place. you can now enter the
STEP2
a l ot more interactive. Let's begin with defining act ua l backup routine, based on the Tar command
some variables. Enter the text in the screenshot into a new backup. from Steps. Combined with the variables, you have: tar cvfz
' $dest/"$day $month Syear"/DocumentsBackup.tgz
sh script. Notice that we ve mi sspell 'source' as 'sauce', this i s
because there's already a built·in command called 'source· hence the $sauce. In the screenshot, we added a handy "Now backing up..."
different spelling on our part. echo comm an d.
...�,..,... .
•!,"-!...,.
Ill badwpl.sh IC
• Tue ll.-p
"/bin/bash •S(hte .V.I
DuAt�-t(�t• ��1
re-•·S{�te �'t)
clear
• folO.n
• TJ.me stamp _,_.....,.
•-IDo<-.MS
d •S(date +V.)
.anth•S(date •'B) if ( d Sde•t 1: the.
•th•
yea =S(date +%Y)
I
., ...
1 • 1 1 o wt ll
• Folders
d
r..-d p p,,. '� kw '' 1 nl t
dest•-/backups •k4ir ·P Sdt-!il/' Sday St�C�I\ttl he�•
sauce - /Doc umentsl
· clear
eclle l. < OJ V P .,
tar cvh Sdf'<;t/'Sll�'f $-ofttfl $)'1>�1 /Docu�"'UII�f�llll 191 t�·•uffl
• • • • • • • • • • • • • • • • • ••••••••••••• • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • •• • • • • • •
The previous script entries allowed you to create Finally, you can a dd a friendly message: echo
STEP3 STEP6
a Time Stamp, so you know when the backup was "Backup complete. All do ne ... ".The
taken. You also created a 'dest' variable, which is the folder where compl eted script isn't too over-complex and it can be easily
the backupfile will be created (-/backups). You can now add a c u s tomi sed to include any folder within your Home area, as well as
section of code to first check if the -/backups folder exits, if not, the entire Home area itself.
then it creates one.
·�·.... "
"-
••tllbtbash , ...... ,,..,..
-st•t• ....,,
clear
• llM st�
� --st•t• •\Ill
•'lNU �\YJ
•S(date ._�1
aihlate .U) !On-
1•""---- .S
t r.l...tS
/0K-t
•i(Ute �Y)
" ( �• t.tKt I; ,
...
• Fo\den -
o\M
drest..../Nckups •• 11ac I •
saut--/OOC�nts �..
fi
� p �1 -I
if I d
$dt�t J: theft
·
·
llikfu p We�tl u.y '-"till h••r
echo Ia k
else c\..,
STEP 1 Let's start by creating a script to help display the STEP3 Each of the sections runs a different Terminal
Mint system inrormation; always a handy thing command, outputting the results under the
to have. Create a new script called sysi.nfo. sh and enter the appropriate heading. You can include a lot more, such as the current
following into Xed, or the text editor of your choice. aliases being used in the system, the current time and date and so
on. Plus, you could also pipe all that information into a handy HTML
file, ready to be viewed in a browser.
•••nt� lllfo..-- tiOftl
MH ·e ol •• - . .. d�v.ded.lvtd·nunt -/scripts
hOn-..eul
...
File Ed1t View Search Term1nal Help
• �llt ,,,,.. d1s' ''�r• ..,_,.;
MH ·e 114 I YT" • ••
d t . ..
• • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••• • • • • • • • • • • • • •
looU.sh (-;
We've included a
couple of extra commands in File Eli! VIew Search Tools Ooc"'""nt> Htlp
STEP2
this script. The first is the-e extension for echo,
this means it'll enable echo interpretation of additional instances
of a new line, as well as other special characters. The proceeding .. look4.sh x
'31 ;43m' element enables colour for foreground and background. "/bin/bash
targe1•-/
read nat�e
o •S( find Starget ·lnaee • Snaee 2> /devtnull )
if I I ·n Soutput I I ; then
echo soutput
etse
echo t h to d
fi,
When executed the script waits for input from the The output from colours.sh can, of course, be
STEPS STEPS
user, in this case the file extension, such as jpg, mixed together, bringing different effects
mp4 and so on. It's not very friendly though. Let's make it a �ttle depending on what youwant to the output to say. For example,
friendlier. Add an echo, with: ech o -n "Please enter the white text in a red background flashing (or blinking). Sadly the
extension of the file you're looking for: ",just blinking effect doesn't work on all Terminals, so youmay need to
before the read command. change to a different Terminal.
• ,sh
�r,.&
• b) )(
• tlbin / bash
soutput I t
e $output
. ,.
e No Match found
h
• •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Khe ft · l
rud rarstn.
KM ft ·
read sur...-.
c:l..,.
H�lk tW I ...... .
t
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
• ••••••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••
We briefly looked at putting some colours in the
STEP7
output for our scripts. Whilst it's too long to dig a Whilst we're on making fancy scripts, how about
STEP9
little deeper into the colour options, here's a script that outputs using Zenity to output a graphical interface?
what's available. Create a new script called colours . sh and enter Enter what you see below into a new script, mmenu . sh. Make it
the text (see below) into it. executable and then run it. You should have a couple of dialogue
boxes appear, followed by a final message.
•<ofoi.I"S.Sil IC
11/tlill}bUtl
...tn!!WnUYI IC l
clear •1/llotii/N..,
.ctlo e · �or11a
ectlo • �0••4 y 1 I I ' I
•ctlo ·• 'ilr•• ••
ed1o ·e ' ..
ectlo • ,.. 0,_
echo • · on••
....
eclllo • .. ..
. ••
.... • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • •
eclilo •e
t<N
ec:.._
·
·
•
e STEP 10 While gaming in a Bash script isn't something
e
dte ·•
t<M · e
that's often touched upon, it is entirely possible,
ec: ... e
ecM
·
ec...
. · •
.... •
...
ec: · • make the script executable and run it. It's in Polish, written by
.... · •
.. .. • Martin Bruchanov but we're sure you can modify it. Hint: the title
ec:... · •
.... · • screen i s in Base64.
.... •
ec:... · •
....
.... •
.... ..
.... · •
.... •
.... · •
M ... · e
.... •
e<M · e
ec:he · •
.... •
e< ... · •
.... · •
.... •
.... · •
..... · •
... .. ·•
..... · •
wwVLbdmpyblicatlpos.com Coding · Tp
i s, Tricks & Fixes
Coding on Linux
"'-omman'""' Line
•
UIC Re e rence
When you start using Linux full time, you will quickly realise that the graphical
interfaces of Ubuntu, Mint, etc. are great For many tasks but not great For all tasks.
Understanding how to use the command line not only builds your understanding of
Linux but also improves your knowledge of coding and programming in general. Our
command line quick reference guide is designed to help you master Linux quicker.
TOP 1 0 COMMANDS
These may not be the most common commands used by everyone but they will certainly feature frequently for many users o f
Linux and the command line.
The cd command is one of the commands you The mv command moves a file to a different
will use the most at the command line in Linux. It location or renames a file. For example mv tile
allows you to change your working directory. You sub renames the original file to sub. mv sub
use it to move around within the hierarchy of your -/Desktop moves the file 'sub' to your desktop
file system. You can also use chdir. directory but does not rename it. You must specify
a new filename to rename a file.
• • ••••••••••••••••••••• •••••••••••••••••••••••••••••••••••••••• • • ••••••••• ••••••••••••••••••••••• •••••••••••••••••••••••••••••
The ls command shows you the files in your The chown command changes the user and/
current directory. Used with certain options, it lets or group ownership of each given file. If only an
you see file sizes, when files where created and file owner (a user name or numeric use r 10) is given,
permissions. For example, 1 s -shows you the files that user is made the owner of each given file, and
that are in your home directory. the files' group is not changed.
• • •••••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••• • •• • • • • • • • • ••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
The c p command is used to make copies of files The chmod command changes the pe rmiss ions on
and directories. For example, cp file sub makes the files listed. Permissions are based on a fairly
an exact copy of the file whose name you entered simple model. You can set permissions for user,
and names the copy sub but the first file will still group and world and you can s et whether each can
exist with its original name. read, write and or execute the file.
• • •••••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••• • • ••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••••••
The pwd command prints the full pathname of The rm command removes (deletes) files or
the current working directory (pwd stands for directories. The removal process unlinks a filename
"print working directory"). Note that the GNOME in a filesystem from data on the storage device
terminal also displays this information in the title and marks that space as usable by future writes. In
bar of its window. other words. removing files increases the amount
of available space on your disk.
• •• • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
The clear command clears your screen if this Short for ·make directory·. mkdir is used to
is possible. It looks in the environment for the create directories on a file system, if the specified
terminal type and then in the terminfo database directory does not already exist. For example,
to figure out how to clear the screen. This is mkdi r work creates a work directory. More than
equivalent to typing Control-l when using the one directory may be specified when calling mkdir.
bash shell.
. . . . . . . . . . . . . . . . . . .. . ... ...... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
free sed
top deluser
grep
A G
adduser Add a new user dd Data Dump, convert and gawk Find and Replace text
copy a file within file(s)
arch Print machine architecture . . ... .
.............................. . ............................................ .
Compress or decompress
named file(s)
be An arbitrary precision
. .
calculator language
. .. ..
E
H
c
echo Display message on screen
head Output the first part
ed A line oriented text editor
cat Concatenate files and print of file(s)
(edlin)
on the standard output
hostname Print or set system name
e g rep Search file(s) for lines
chdir Change working directory
that match an
I
chgrp Change the group exten ded expressio n
ownership of files
env Display, set or remove
i.d Print user and group ids
chroot Change root directory envi ro nment variables
info Help info
cksum Print CRC checksum and expand Convert tabs to spaces
byte counts install Copy files and
expr Evaluate expressions
set attributes
cmp Compare two files
. .................................... .
............................................
F
comm Compare two sorted files
•
line by line
• ........0•.............. .............................'•• '•••••••••••••••••••••••
facto r Print prime fa ctors
J
cp Copy one or more fi les to JOln Join lines on a
another location fdi.sk Partition table common fie ld
. .
manipulator for Linux
......... '............ '............................................ . . . . . . . . . . . ..
K
run at a later time fgrep Search file(s) for lines that
. .. . .. . . ..
. . .. .. . .. . .. .. . . .
.... ..... . ... . . .. . . .. . . .
.. . . . .. .... .. . .. .... .... ... .
match a fixed string
cspli.t Split a file into context
ki.ll Stop a process
determined pieces find Search for files tha t meet
from runni ng
a desired criteria
cut Divide a file into
several parts fmt Reformat paragraph text
fold Wrap text to fit a L
D
specified width less Display outpu t one screen
a t a time
format Format disks or tapes
date Display or change the
ln Make links between files
d ate & time fsck Filesystem consistency
check and repair locate Find files
de Desk calculator
u
logname Print current login name rep Copy files between
. . .. .
. .. . . ... . ... . . . . . ... . . ... .. ... .... . .. ... ..... . .
.. .. . ..... .... .
two machines
l pc Line printer .. .. ... . ... . .. .. . ... . ... ... .
. . ... . ... .. .. . . ... . ... .. ... ..... ... ... .. ... ... ..
umount Unmount a device
control program rm Remove files
'.. .... .. ····· ..
. . . . ... .. une xpand Convert spaces to tabs
lpr Off line print rmdir Remove folder(s) . . . . . . .. . . . . .
. .. .. . ... . . .. . .. . .. . . . . ... . . ... ... . . ... . . ... ... . . ..... ... ... . . .
s
useradd Create n ew user a c cou nt
ma n See Help manual
us e rmo d M odi fy user account
mkdi r Create new folder(s) screen Terminal wi ndow manager
users List users curre ntly
mkfifo Make FIFOs (named pi pes) sdiff Merge two files interactively
logged i n
mknod Make block or character selec t Accept keyb o ard inpu t
s pe ci a I files
v
seq Pri nt numer ic seq uences
. . . . .
more Display output one screen
. .. ......... .. .............. .................................. ...............
N
spl i t Split afile into
fixed-size pieces
w
watch Execute or display a
mc e Set the priority of a
program periodi cally
su Substitute user identity
command or job .. .
. .. ..
p T
who Print all usernames
curr ently logged in
passwd Modi fy a user password
toe Concatenate and write
past e Merge lines of files whoami Print the current user id
files in reverse
and name
pat h c hk Check file name portability
t ai l Output the last part
pr Convert text files of files
. ... . .. ....... . .. ............... ............... ... .......... ............ . . .. ............ ....... ......... .... .. .... .. .... .... . . .. .... . ..
R t s o rt Topological sort
• ••••••••••••••••••••••••••••••••••••••••••••••••••
98 Python Errors
'-ommon Mista
X=MISTAKE, PRINTY
There are many pitfalls for the programmer t o be aware of, far too many to be listed here. Being able
t o recognise a mistake and fix it is when you start to move into more advanced territory.
) too much thought and planning but longer and more in
depth code requires a good working plan to stick to and
1! (n - 0)
help iron out the bugs.
return:
II
II Loop unrolllnq.
II Clrfor
.... t
�
detln,.d 1 c -
T
) l (
..
·-
)
Common Coding Mistakes
Asking for help is something most of us has struggled If your code makes multiple calculations then you need
with in the past. Will lhe people we're asking laugh at us? to ensure that the maths behind it is sound. There are
Am I wasting everyone's time? It's a common mistake for thousands of instances where programs have offered
someone to suffer in silence. However, as long as you ask incorrect data based on poor Mathematical coding, which
the in the correct manner, obey any forum rules and be can have disastrous effects depending on what the code is
polite, then your question isn't silly. set to do. In short, double check your code equations.
set .,..,.......S:I.SI
wt Y"'""f:(�.Stl.�lol
wt t.l!pat. 1
te't WllfiP\ts -
wt �le l'll
wt ,.00 -��
wt sue �uen
_ , ..
.. _
'lf\ot _..U.-e/tll,1/t•.�te•l•.r•.•• •tttt•
Working with Code
Python is a relatively easy language to get started in where there's plenty of room
For the beginner to Find their programming Feet. However, as with any other
programming language, it can be easy to make common mistakes that'll stop your
code From running.
DEF BEGINNER(MISTAKES=1 0)
Here areten common Python pr ogra mming mistakes m ost b eg inners find t hemselves ma king . Being able t o id entify these
mistakeswill save y o uhea dachesin t h e future.
• • • • • • • • • • • • • • • • • ••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • •• • • • • • • • •••••••••••••••••••• • • • • • • • • • • • • • • •• • • • • • • •
• -�an:.rn:
pUol"f-t .. PN--��J�o. �u. tO. au
� pl•rer
�! .._lt�U (i.av._a.r_,) I
'. .
"' 1 • 1nvldu•1
• •0
I ) ta �.,.q.tU) I
aaY•4-� PVV•..·'·��•�'*•• '••v,
• lO. 2�1
1-�DdiUrtY.cl�t)
• - 10
y - ..
H' u•.clue
�r ..toev•U• (,..U•t 1
vdll • �YQ'.ft.lle.AC'<::t.(60, UO, UO, JO)
t1._U.2 '"Pl'0'4M.AU:t(211, UO, UO, )01
vdl' • 1>\'C-.l..-.t ., UU, 120, 120, JOI
using others' code, but you need to know how the code works and the code, you nee d to add c om m ents as to what's going on. Is this
what it doesbefore yo ug o blin dly running it on your own computer. function where you lo se a l ife? Write a comment and help you, or
anyone else, see what'sg oing on.
Create/delete a .txt file In a python program
I ae;. up PYOUte
0
..,..............- ._,Udt-...-�-- •.,.,._.lftillftII� bl.c-*debbbit
T .... e�•cn•tclllrY t aet up the v�
lnh�tw.........
. ..,.,_...�.._.so•t•*-'h$ t*lilltl•c.ts vidt.b • 800
* hel.Q.b.t • 700
,,
hn•...w.e ,..,
. •ind..,...rw"""'"._.
• ""' «.-..!Nstnwodl!dletme so..,I"Wtpot
screen • pYQa.e.dl.spl•y. aec_�e ((wldt.b, helQht), 0, 32)
� . ..... .... .....,. III'P'«
..
pyqame .chsplay. aet_c.ptl.on( c.ap'U r )
.,..
., .
. . - .... .. . . I 3et up .ovcaent variable•
·-"-·• . . .. .
..
u ... til) move.Lett. - r�•e
NJI,. • • ....I""J(U\>
..
aoveR1ob� • ' l•e
JDOvetJp =
,..,.. t......
.. ,. u ....,
...
. h !'loll>ll<l . t 3et up d1rect100 v•r1able•
d.Mi
t C:e
DOiiNI.EfT 1 -
pr.llo\ 'lllloUe
lo te ._...,.. H t..,
..i, )
•··•-o DOWNRIGHT • 3
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Remember that in Python a loop It's common for beginners to forget to add a
COUNTING LOOPS COLONS
doesn't count the last number you colon to the end of a structural statement, such as:
specify in a range. So if you wante d the loop to count from 1 to 10,
class Hangnan:
then you wilt need to use:
def guess(self, letter):
n = list(range(1, 11))
And so on. The colon is what separates the code, and creates the
Which witt return 1 to 10. indents to which the following cod e belongs t o.
selt.vorG - word
Python S.6.2 (•S.t.2 :tf4SSb$, Jul I 2017, Ot:14:St) IMSe v.ltOO !2 bie .Intel)» selt.�se4 letter• • II
on w1..oS2 •elt.o�e••ei_le,,er• • IJ
Type •copyri;h�M. •cre41t•M or •11cwn••<l• for .ore �nfor..�1on.
>» • gu••• l••lr, lett•r)l
--- RESTART: C:\U..u\4av1d\DoeUMnti\PY�boft\5paee lnvaders.py -- � letter •elt.word � l•tt•r selt.quesseG_lec�ers:
>>> n • un tun;e: 11. U) J •elt. o�••••4 l•tt•r•.a�ndtletterl
>>> p rlnqn) •·�· le:;;er l 1i1t.word lett.er •elf .ll'l1eee4_lettere:
[1, l, J, •• 5, '· ,., •• 9, 10) _ Letun , aJ!'S)end (htter)
•elc ,NJ.ueCI
»> I 1 '
t •
tet.IUD TJ: e
•t han�n-�•r l•eltJ 1
�t•t. •elf.h•n.cJ-n_wonU
'
• •••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••••
rtn
Python is a case sensitive
•
CASE SENSITIVE
•
letter ••lt.word;
let.�er eeU:·Otk•.ll�d_lct.tc.u :
programming language so you wilt ,
rtn ...
need to check any variables you assign. For example, li. v es=10 is a
different variable to lives=10. catting the wrong variable in your code
can have unexpected results.
c - 10
d - 10
c -- d tcrue be , -- .. 1'\
•
Everyone forgets to include that extra
BRACKETS
bracket they should have added to the end • • •••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••••
de! pr1nc_oame_scatus (self) : Linux you need to use clear. You need to solve this by capturing
pr1nc (board[len (self.missed_leccers) ) ) the error and issuing it with an alternative command.
pr1nt ( : + se1f.h1de_wor d ( ) )
pr1nC ( •t;l � .1: ' , )
to 1etcer J self.missed leccers: I Code to detect error for us1no a �tferenc OS
run=l
pr1nc (letter, )
punt ()
t...6 :
pr1nt ( t - d: • ' )
f letter self. ouessed_1etcers: exe r.• OSError:
(1eccer , ) os.systea( �. )
print '
There are many pitfalls the C++ developer can encounter, especially as this is a more
complex and often unforgiving language to master. Beginners need to take C++ a
step at a time and digest what they've learned before moving on.
VOID(C++, MISTAKES)
Admittedly it's not just C++ beginners that make the kinds of errors we outline on these pages, even hardened coders are
prone to the odd mishap here and there. Here are some common issues to try and avoid.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
X t9 tl 0.. 9
#1ncl 1de <lost
using na-space std;
int ma1n ( )
� testl .cpp
lllinclude
X
1o�t ream>
{
int a , b, c ,
a•...-.� ;
d;
b= .
d-
•
C• .
int m a i n ( ) •
l •.
{
s t d : :cout << x •· cout << a, b, C, d;
l }
• •••••••••••••••••••••••••••••••••••••••••••• •••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Remember that each line of a C++ If you're compili ng in Linux then you will no
SEMICOLONS GCC OR G++
program must end with a semicolon. doubt come a cross gee and g++. In short,
If it doesn't then the compiler treats the line with the missing gee is the Gnu Co mpiler Collection (or Gnu C Compiler as it used to
sem icolo n as the same line with the next semicolon on. This creates be called) and g++ is the Gnu ++ (the C++ version) of the compiler.
all manner of problems when trying to compile, so don't forget If you're compiling C++ then you need to use g++, as the incorrect
those semicolons. compiler drivers will be used.
c, d;
test1. cpp: ( . text•6•2a) : undttf 1 �d reference n to std: : cout
{
·
b=C=38..8;
.
't�p/ccA
I .:
Szhtg . o: In func t1011 statlc lnlt!al1zat1on and des
tes tl . cpp : ( .text•6•Sdl: u ef1ned reference to std: :1os base:
nd
testl. cpp: ( .text•6•6c l: undefined reference to std:: 10s base:
• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • •
of readable identifiers throughout the code makes i t very difficult easy to include or miss out one or the other facing brace when
to look back at how it worked, for both you and someone else. Use writing code; usually when writing in a text editor, as an IDE adds
more comments. them for you.
• dKt�-
• ...... <C'o�ctOI") #include <l�t PJ
• v* •UJIO"H,_.,
oo=.I"C -•" tt•; using namespace std;
int main ( )
�··� d,
.till.. C(ll .., 'o,OII\II.h<. II
I, ( cl11,...-4)) l II
.. rer t
t I C11�r f ,._
{
int x ;
string mystring ''This is a str�ng! \n" ; •
'
This means if you create a variable called x then, potentially, it is
t· ,. -
given a random number from 0 to 18,446,744,073,709,551,61 6,
.
.. ... .... s-c;to ..... which can be difficult to include in an eQuation. When creating a
... -�01(1
I
... .
variable, give it the value of zero to begin with: x�0.
.,,... .,..,.... - ,.
..
( t-
.... .
�. ..
n n ludc e
using space std;
(�-.
� - 111\11
(..
(..., - .,
na
..
.."...
int ma�n()
{
int x;
X• ;
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
cout << x;
While it's necessary to have a
EXTRA SEMICOLONS
semicolon at the end of every
C++ line, there are some exceptions to the rule. Semicolons need to }
be at the end of eve ry complete statement but some lines of code
aren't complete statements. Such as: • •••••••••••••••••••••••••••• ••••••••••••••••••••••••••••••••••
wwVLbdmpyblicatlpos.com Coding · Tp
i s, Tricks & Fixes
Working with Code
Be"""inne r Linux
•
tin Mista
Linux scripting is a Fantastic way to automate tasks and even create some cool
command line-based games. Bash hackers around the world often post some clever
scripts to try out but before you go copying and pasting them, it's worth highlighting
some of the common mistakes.
BASH HACKERS
Even bearded seasoned Bash programmers make a mistake from time to time- including the non-bearded ones. Being able to
eliminate the common errors saves a lot of time when scripti ng .
• • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••••••
Hash-Bang, is one of the most common type the name of the file in. If you simply
mistakes to make for the beginner scripter. The Hash-Bang is the enter MyScript into the Terminal. it will attempt to execute a built-in
interpreter that tells the system what shell to use and that what command called MyScript- which doesn't exist. Instead, you need to
you're running is in fact a shell script. place ./ at the start: ./MyScr'ipt.
david,<lrrint -mate
'Hello �/orld'
D'1l•
J1 •
r�1·, s . /Hello . sh
# ! /bin/bashl • • ••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • •
PERMISSIONS equals sign for variables. So, word = Hello is wrong, whereas
beginners when their script doesn't want
,
word=Hello is correct.
to work, is that they've forgotten to give it executable permissions.
After you've created your script you need to chmod +X it to allow
Ill Helo.sh X
the system to flag the file as executable. Once it's an executable you
Ol/biA/bosh
can then run the script.
word • Hello
...
david@mint-mate - 10oc1 echo word
• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
COPYING Setting a script that copies files from one place CHECK FIRST Always be cautious when copying scripts
to another. such as a backup script, can be tricky. you've found online into your system
The part that stumps most folks is the naming of some files. lfyou and executing them. There are some Linux commands that kill
set variables as the file and target, you need to encapsulate them your system beyond repai r, forcing you to reinstall the OS. The rm
in quotes. This way any whitespaces and extensions are considered. -rf command, for example, wipes all the files and folders off your
Such as. cp "$file" "$target".
-- system. Always research script contents before executing.
# ! /bin/bash • •runcmd.sh x
#1/bintbash
ctear
echo Cf pt w1l\ peed up your comput r!
steep Ss
echo
cp - - ' $file " " $target ' echo ...
steep 2s
echo - n now speedtng up your yst�m-
st••p 55
ctear
acho � J t wtped all the files off you system••
echo Spass sudo - S r. - r f
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
do it in a script though the end result can be failure or something MORE BEWARE Following on from the previous
totally different. Before you save and execute the script, have a common mistake, never blindly enter
quick look through to check you've not mistyped a command. a website into a script or the Terminal that downloads and executes
a script. There's a good chance it could contain something malicious
...
•Hello.sh (-/Documents) or wipe your files. The command wget http://somewebsite -0 Ish
downloads a script and automatically executes it.
File Edit View Search Tools Documents Help
IS "Hello.sh x
11 ! /bin/bash
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••• • • • • • • • • •
amusing to see someone who's written Flit Edit V�VW S-earch Tool� Dotl,l'flotnb �lp
., "Hello.sh (-!Documents}
clear
•<�••
•<l•o
Ell "Hello.sh x
echo -ne d.awidemmt
�e -
ID
ocume
sl"P 1
•cho -ne
11! /b in/biiSh sleep 1
echo ·ne
echo ·n•
cht�r
clear
echo leo to The Hat nx, N o• •cho Here it is l
cls
echo >llow th wh1tc r bb1t1 18
ist
PLAN AHEAD
We've compiled a brief checklist that you can start using to
help you iron out any issues. Then you can ready y our code so
it's well on the way to being used elsewhere.
Therefore, having an effective plan that you can run through once
you've successfully created a program helps you immensely and
ensures that common issues are identified and dealt with. The
checklist we've created here is merely an example you can use to
start with; as you build on your experiences as a coder, and as you
stretch your coding skills, you will undoubtedly expand the list to
include your own identified checks.
Checklist
0 Does the
code wor
k? Does it per
intende form the
d functio
n?
Q Is the
code eas
ily und
Have You erstood ?
added
commen
ts thro
0 Can the
code Wor
ughout?
l< on oth
0 Do all the
variable
er ope
rating
systems?
s make
0 Is out
putted sen
sense?
Are they
readabl
e?
sitive data
encryp
0 Do any
third-pa
ted?
rty utili
ties req
0 Does the
code or
uire lice
nsing?
program
fashion termin
for the ate in a
user? suitable
0 Are there
any acc
ompany
to use ing ins
the pro truction
gram? s on how
0 Have You
tested
every out
com e of the
0 Is the
code as Progra
m?
efficient
Have You as pos
eliminat sible?
ed any
Perform
0 Mistakes -
enter any
ance bot
tleneck
s?
rid of the mistakes You
m next find to
time. help get
w
ww,
bdm
oub
lka
tj
ggs
.r
gm
Working with Code
e re to Fin He
wit e
Before the Internet was readily available, coders would pour over immense books
like sorcerers in search of the fabled Philosopher's Stone when they came across
something they couldn't fix. These days, help is just a click or two away.
HELP == CODE
Here are ten top places you should bookmark as a beginner coder. These places offer invaluable advice, help, hacks, tips, fixes
and everything else to do with your code.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • •
One of the biggest programming Many experienced and serious programmers use
STACKOVERFLOW REDOIT
communities on the Internet, Reddit as a resource of useful information. There
StackOverflow has millions of experienced and beginner users who are many communities within dedicated to coding and there are
are ready to offer help and advice. Within you can ask questions also a tot of codi ng jokes, the sort that only programmers would get.
about Python, C++, scripting networking and countless other topics.
, Find out more at: www .ww w,red di .
tcom /rlp rogram mn
j g/,
Check it out at www ,
sta c
kg yerf l
ow .com /, • -
- - -- - -- - - - -�
- .-
__.... .. - - - - _ ,_.
-
�reddit --- - - - - ... - ---------�-�_j •
Wek:ome to Reddil.
•
..
.:
- · ·-.... --·---·-·
.
0 .,..",.I4IK.
-
.. .. JCJo...,r. f>er.,..,..
llo-Kt, WI.
.. ..__ _
--·-- --
. ... _ ..._ _ _ _
-
...... a- · -
. --
. .. . - -·
· - · - - -
'"-··· ,_ ,. _
____
...
+ L•llrod� llle �''""'"OO"",.,.,''"ii 1 r
n11
'(fuo
-t ur "
-moo•�
· - -
w
..
,. ,., , � •-•""illo"" '"'"' w"'
.._.
.,.,
,
...,.. .., , ...-
�...-
et
- -..�- .
II'(
_...
0 0 l
_
, _ __ _ _
Quora deals with a wide range of topics, from who A part of the StackOverflow
QUORA STACKEXCHANGE
would win a fight between Popeye and the Hulk network of communities,
to how do I pass a sudo password through C++ code. Once you've StackExchange is by far the largest programming led community on
logged in you can browse the questions, search for specifics and the I nternet. You can ask any programming specific questions (as
post your own. Login at: www.www.guora.com/. long as it follows the rules) and it gets answered professionally and
expediently: w ww, st
a clsexcb aog e.com /.
• --
·
-
. ' --
Quora ·- -- -
. ..
. - ... . ·-
0 --···
....··-----·-..-·- -···-- --·--·---
n ..
.... ..
..
.
--------..
--
___
0 ..,__.____._.,.__ ...____ ... ,_........._
.,. ___
.................._
- --
--
..,_______ ,...
. ........___
... _.....,
...
·--·- _
--..-·.. _
_ ,,....___ --
_ ....
_
..._,.,_,
0 ' - .. --...--..--- -·-
---·-
-·-·-.....-.....
-...---·---·
• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
- - -
OZone
-
-·------..-- •.. & Ell
..,� , ,.., ..,, ,....
.
·-·�._ � - . --· 1
- - -- - - _ ...., Wiro
·• - w - -.. .., -
---....-·-
' ___,.,..,.._IIU
__... •
--·
... �
-·-
_ __ (ofo_... ...._._ - .. ...
. - ·- .... .
-·---·-
..._.... ,
___...._
'*''' ·-,..........-.{
. � ,
.... ,.. ""'--..-....-
.. -.....-
d
'""' ·-...
0 .
·-·-c-.....--......_
-- ... .... -
cv•�• ....-
. __
, ,
___
·---
__
..
,. • MUN'o.....
l
fil
!FI
I
E 0 _.
Ool.
__..,__,_
- - - -
I
.. .
..
�-
.. ___.._
....
_ ......-.....-...,._,....
_...,_
-�---oO.,.,
...._,_
· --...-
�........
----0 -
-----·
...-
----·----
--··
____........
... .
----- ·-
- -
--
.. -
--- ·-- -·-----
_ _ _..._ _
_
• • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
Google Groups has encompassed all This is a kind of a social media network for
COOGLE GROUPS FINDNERD
the alt.comp IRC groups these days developers, where you can ask a fellow 'nerd'
and made them viewable without too much difficulty. There are a question and they answer you as quickly as possible. There are
countless programming specific groups available; all you need to do also blogs, tutorials, projects and much more to discover:
is find one that suits you and get posting: w
w wg
.ro
uos
.go
ogl
e.c
om. w
w w.
fi
ndner
d.c
o
.mt.
·- .. -
. .. I
0 -· - .... .
.. .... ·-
. ,_
ARE YOU A..
� �...,__.,'-'Ol&.ll Uot_...8il (1)
Old•.,. •'"'""'' �� B ...... ......* �et� •-
ICOIIriO .......,.....
"...... .....,.,
. • lllt'-1.10� f'i
-·
B &tNP tfMCII i I
1M*fl!!f- (1)
B "' ""' .
Tech Ne
rd lncltpenden1 Sofrwllt V�
or S t anup/ SUB
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
This friendly discussion board is a great This site is aimed more at students who are
CODERANCH CHEGG
place For new coders to start looking for studying coding at school, college or university
help or advice. It's an easy to use setup where you can learn about levels. However, that doesn't mean non-students are excluded from
and ask questions on programming languages, books, careers, the huge resources available. There are plenty of sections that cover
engineering and much more. Check it out at w
w w.
cgde rancb .
c om/. programming, so dive in and have a look around: www.chegg.com/.
1
: - �p?
:!!A
' w.r,.. IN'altable
When homework geb - NHd last mMute
tough, get tougtMr "� 24n
-·•.-.....0
� 1>11....•'!1101.•
Test Your
The modern Internet has drastically changed the way developers work and test
their code. The bare metal testing still applies, where you test your code in a virtual
machine for example, but these days you can test it online in a perfectly safe and
secure environment.
WWWJESTINGCODE.COM
You can test all kinds of programming languages online or just one in particular. It all depends on what language your working
in and how you want it tested. Here are ten testing sites for bookmarking.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Codepad is arguably one of the most popular This simple JavaScript editor a nd compiler offers a
CODEPAD JS BIN
code testing and online compilers on the clean interface and simple to use controls to help
Internet. With it you're able to paste in sni ppets of code for testing you rest your JS code snippets alongside HTML and CSS. There are
that cover C, C++, PHP, Python Ruby and a lot more. There are also
, tons of other features available via the many links at the top of site,
examples available and you can see what others have pasted in too. so it's worth registering and getti ng to know JS Bin.
.
c
www o
depa
d.or
g/. ww
w .
jsb
jn.
comOb
tml.
oyt
oyt.
• c . - -··
- -···· ---
-·-
---··
--
-
...... . ...
....-·
..k .. . .
... .
.._... •
"'-__ ....,..,.........
.. ,..._.,..,_UIL,_...,.___,•_•_ ..
-
.....
·--·�---
oc ·---··
o(.u . -- - - -- - -
• -- . -
·-·
- ..--· ·
_ _... ,, --
- - - - - -···
-
-··-
-
� .. .......
_ _...,
-·-·.. .,._,,
_ _
_. ....
.
·- ._, _ _ _
-·-·-··
_, .........
..
..Jk
...
_..
- ...... u. -
�
..-.;'>ol...,...,
-
·
Ill
ot ·•I""
,:,
:�::
= -
:==
-=========:::!.
.. ••
. . . .... ..
. .. ... . . . . . . .. . ' . . . . . . . . . . . . . . .... . . ... ... ...
. . . .. . . .. • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • •
This is an online PHP editor that also allows you to execute your inputting. There are links to collaborations, exporting to Github, and
code, displaying the results in a separate window at the bottom of a code tidy feature to help iron out any bad habits. You can find it at
the page. lt currently runs PHP 5.4 and ca n be found at w
w w.
j
s f
jdd
le.
net
/.
www.wrjteobponline.com/.
.. " Q _..,_,_
--
-·-
-
-- - ·-u.-w •
----
. . . . . . . . . . . . . . . . . . . . . . . . . . .. .. .. .. . . .. .. .. . .. .. .. . .. .. . . .. .. .. . .. . . . . . . . . . . . . .. . .. . . . . .. . . . . . .. . .. . . .. . .. . . .. ............. .. . . .. . .. . . .. . .. . . . . . . . . . . . . . . . . .
CSSDESK Beyond scripting and using stylesheet CLOUD9 Using the Amazon Web Services (AWS) cloud
languages CSSDesk is a splendidly developed , infrastructure, Cloud9 is an excellent IDE
site that displays HTML, CSS and the output from both in a large designed for collaborations, testing, debugging and editing. It
screen area to one side. You can set certain options and either share includes essential tools For the major programming languages
or download your finished code when you're done. including Python, PHP, JavaScript and more. Being cloud based, you
ww w .cssd esk c
.o m /. can Quickly share your work with others and create a development
environment. w w w a
.w .
sama zo n.com/ cloyd9 aorigjn-c9o i.
•
<I> A ......_
Hello
World!
- -
-
.. . .. . .. . .. . . .. . .. . .. . . . . .. . . . . . . . .. . .. . . .. . .. . . . . .. . . . . . . . . . . . . . . . . . . . . . .. . .. . . . . . .. . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
and Coog le s commitment to creating This online platform helps developers work
COOGLE CODE '
CODECHEF
maintaining good code is certainly through and test their code in a huge range
commendable, regardless of what you may think of the company of supported languages There's a dedicated section to help hone
.
as a whole. Its embraced the open source community and created your coding craft and even a monthly contest and Code Cook-offs
Coogle Code, where you can test code, see great examples, get hold with cash prizes available. www.codechef.com/ide.
of APis and tools and much more. www .
co deg
.o og e
l .com /.
·--- 8 ••
• •
. . , .
-- -
-
-=
- ..:.
._
--- .......... .. . ..... .._. .... ....
.. ___ ----
·- ·
. ... ... -..-·---
...,.,
__,
--......_...._
-- -- ... ---�- _,.__,�--
-----· --- -""'----� -
.......____.,._. --......�eo.....
_ .. • •
·-- -
------ - ., .... . ! ,.... . - ..... ....... •
. . . . . . . . . . . . . .. . . . . . . . . . . . . . . . .. . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . .. . . .. . . . .. . . . . . . . . . . . . . . . . . . ..
There are samples available and you can view recent code entered. Micro computers. Jsbeeb fills the nostalgic gap with a BBC Micro
ww w.d
j eon ec
. om /, emulator where you can code, save, share and more.
www.bbc.godbolt.org/.
. - -oo �..�._...
ideooe · -- &
0phere onlb
....... .._..
-
.. - -
:::: :
...
- "-"--
...
- ---��
-·
...... - ·
- _ .. ._
_...._ __
- - -
..:. ::- '-----
•
- ...
- �. -
- - - -
- ... - -
,__ Cllooo-c -
· - -
- -
... -
· -
- - �
- -
Pyt on Erro rs
It goes without saying that you'll eventually come across an error in your code, where
Python declares it's not able to continue due to something being missed out, wrong
or simply unknown. Being able to identify these errors makes for a good programmer.
DEBUGGING
Errors in code are called bugs and are perfectly normal. They can often be easily rectified with a little patience. The important
thing is to keep looking, experimenting and testing. Eventually your code will be bug free.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Code isn't as fluid as the written word, no matter Thankfully Python is helpful when it comes to
STEP 1 STEP 3
how good the programming language is. Python is displaying error messages. When you receive an
certainly easier than most languages but even it is prone to some error, in red text from the IDLE Shett, it witt define the error itself
annoying bugs. The most common are typos by the user and whilst along with the line number where the error has occurred. Whilst in
easy to Find in simple dozen-tine code, imagine having to debug the IDLE Editor this is a tittle daunting for lots of code; text editors
multi-thousand line code. help by including line numbering.
.,....
..._
. .-..
..-
_-
.�....
....-. ..
.
.... . . .._.,
- - .. ..
......
.. ., ..
··-··- ..·- -· - - -
-- - . ...,_ .. ---
• -
.,._ -- -
- ��1 t·- -. "'
..
' 1).
•
,... ·- h
.....-.,. ,_ -- 1.. ..
,. c u.
.
,.. •. ,
..
.._ .....
,_ , _.
.- __
....
,._, ••h.,.,_,Ke
... ....... cw 1�. Cta
,.......-. . .....
___ ... . ,_ ..J. )0
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
The most common of errors is the typo, as we've Syntax errors are probably the second most
STEP 2 STEP 4
mentioned. The typos are often at the command common errors you'll come across as a programmer.
level: mistyping the print command for example. However, they also Even if the spelling is correct, the actual command itself is wrong.
occur when you have numerous variables, aU of which have lengthy In Python 3 this often occurs when Python 2 syntaxes are applied.
names. The best advice is to simply go through the code and check The most annoying of these is the print function. ln Python 3 we use
your spelling. print("words"), whereas Python2 uses print "words".
e . a.t.alill � - = X
Python ],A,l (def.ult. Oct If 201•. 1):)1:11) Python ].4.2 (6tf*'lt. OCt 19 2014. 1S:3t:tt)
(GCC •.9.11 on hnux (G<C 4.1.11 en I�
Type ·copyrtrt'". -c.r�•ts'"' or '"he�()'" fM 110re t.nfo,..uotl. T1pe •c opyr�
t t ·, •credlts• or •Jtcense()• t� �r• tnfor-.tlon.
>>> IIIP1H•10
P :>n prt
n t-.... Il . I
•» plrnt(iiPPlH) SyntaAError: �•ltd SJ�t••
Tr.cett.d (aost rt<:Mt ull l.st) »>
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Pesky brackets are also a nuisance in programming All excellent way to check your code step-by-step
STEP 5 STEP S
errors, especially when you have something like: is to use Python Tutor's Visualise web page, found
print(balonced_check(input())) at www ,ovtboo ty totc om /y
jsuatjze,
b tm l# mod e-e d jt
,Simply
Remember that for every '(' there must be a n equal number of ')'. paste your code into the editor and click the Visualise Execution
button to run the code line-by-line. This helps to clear bugs and
1 1�rt sys any misunderstandi ngs.
2
3 • del bo\anc� <M<k(dltll : •
• •
4 stack • IJ
5 characters • tilt data ·--
6
7• for cl\aracter in characters: ---.,....-0 -·- -·· ..
So reference • ( _,_
...
..
. ......
.. ........ . ·- -
9 '( " : ' ) ' ,
_
.. -·...
.
-
.. ..
- - -·· - ..
.. ._ .. _,,_...... -·
- ·-
_ ...
..
-
'(' : ')'
_
18 ,
t.A-·-- --·
11
'(': ')'
12 I ... .
_ _ __
- ·--- ---- - --
,
.........
. .......-
16. e\if character in reference .ve\ues() and \en(stack) > &:
17 char stack. pop(l
•
-..-
if refertnce .get(char) I• character:
...
18
19 return •NO•
8 else:
21 return *NO*
n_
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
There ar e thousands of online Python resources, Planning makes for good code. Whilst a little old
STEP 6 STEP 9
code snippets and lengthy discussions across forums school, it's a good habit to plan what your code
on how best to achieve something. Whilst 99 per cent of it is good will do before sitting down to type it out. List the variables that will
code, don't always be lured into copying and pasting random code be used and the modules too; then write out a script for any user
into your editor. More often than not, it won't work and the worst interaction or outputs.
part is that you haven't learnt anything.
8
v
Indents are a nasty part of Python programming Purely out of interest, the word debugging in
STEP 7 STEP 1 0
that a lot of beginners fall foul of. Recall the If loop computing terms comes from Admiral Grace
fro m the Conditions and Loops sec tion, where the colon means Hopper, who back in the '40s was working on a monolithic Harvard
everything indented following the statement is to be executed as Mark II electromechanical computer. Accordi ng to legend Hopper
long as it's true? Missing the indent, or having too much of indent, found a moth stuck in a relay, thus stopping the system from
will come back with an error. working. Removal of the moth was hence called debugging.
0
expected .., indented block
r=-
-
[ II« J
• ••••••••••••••••••••••••••••••••••••••••••••••••••
The FUZE Project is a learning environment that's built around the Raspberry Pi
and a custom programming language based on BASIC. The FUZE Workstation is the
hardware side of the project, incorporating a Raspberry Pi inside a stunning retro
themed case, complete with a full-sized keyboard, 10 board and connectivity. The
software side is FUZE BASIC, available for both Windows and as a boot image for
Raspberry Pi models 2 and 3.
• • • • •
• • • •
• •
: programmable red coloured F·keys. •
•
• •
•
•
• with a ribbon cable, with the other •
•
•
• • • •
: Note the holes at the top edges: these •
• • •
connections branching From it: HDMI, •
• • • •
•
• • •
: are compatible with many popular •
• •
•
• power, SD card and USB. The USB •
• • •
•
: plastic building blocks and electronic •
•
• ports have been routed to a powered •
•
• • • •
•
•
construction kits. • •
•
USB hub. attached to the rear of the •
.
•
. ... . . . . . . . . . . . . . . . . . . . . . .. .. . . . .
•
•
•
workstation, providing the user with
•
•
•
•
• •
• four USB ports accessible through the •
• •
•
rear 10 back plate; there's an Ethernet •
•
•
port too. •
•
• • ••••••••
• •
•
•
•
•
•
•
•
•
•
•
•
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••
•
•
•
The Rear 10 back plate is where you
• •
•
•
plug in your power, connection to a •
•
•
• •
monitor, mouse, SD card, audio and
•
• • •
• •
• •
any USB devices you want to include.
•
•
• • •
• • •
•
• From left to right, across the backplate, •
• •
• • •• • • • • • • •• •• • • • ••• • • • • • • • • •• • • • •• • • • • • • • •
•
•
there's an on/off switch, a power port, •
• •
•
•
• • • •
•
• a bank of four USB ports, the Ethernet •
•
•
The FUZE 10 board, located in the wide •
• •
•
• •
•
• port, SD card slot and HDMI port. You • • slot at the top of the workstation, is •
• • • •
• •
• can remove the four corner screws •
• • the connection to extended hardware. •
•
• • •
•
• •
• of the backplate to gain access to the •
• • With it you can connect, program and •
•
• • • •
•
•
Raspberry Pi, USB hub and cables inside •
•
•
use sensors, LEOs, robotics and all •
• •
• •
• •
•
•
the workstation if needed. •
• • manner of amazing electronic projects. •
• •
• •
• • • • • • • • • • • • • •• • • • • • • • • • • • •••••• •••••• • • • • • ••••••••••••
•••••• • • • • • • • • • • • • • • • • • •• • •
•
•
• • • • •• • •• • • • • • •• • •• • • • •••• •
••••••••••••••••••••••••••••••••••••••••••• •
•
• •
•
: Alongside the other components with •
•
• : the FUZE workstation, you also get either •
• • •
• • •
• •• • • • • • • • • • • • • • • • • • • • • • • •
•
a wired USB or wireless (batteries are •
•
•
: included if necessary) mouse and 'FUZE' •
�,#
• •
,. • The kit comes with two ring-bound books •
• •
o ",)
.i' 1J;
•
•
•
containing project ideas for the electronics •
•
•
o
f'�
'•
d-C( • • •
• •
q' •
•
••
for FUZE BASIC. If you've purchased the •
•
• FUZE kit, then it's certainly worth your
•
•
• while reading through this book and
•
•
• familiarising yourself with how
•
•
•
everything works.
•
• ••••••••••••••••••••••••••••••••
•••••••••••••••••••••••••••••••••••••••• •
•
• •
• •
• Depending on which FUZE workstation •
• •
•
• kit you've purchased, you could also •
•
• •
•
•
have a robot arm that requires building, •
•
• •••••••••• •
•
•
along with Four 0-sized batteries, a BBC •
•
• •
• micro:bit or even a Capacitive Touch kit. •
• •
• •
• Needless to say, there's plenty of project •
• •
•
•
potential with the FUZE. •
•
•
• • ••••••••••••••••••••••••• ••••••••••••••
....., e ttin'-1 u
Thankfully the FUZE Project comes with everything you need to get up and running;
you just need to supply the monitor and an Ethernet cable to your network (or you
can go Wi-Fi with the Raspberry Pi 3). Before you begin though let's see how to set ,
up the workstation.
. . . . .... . . .. . . .... . . . . . . .. .. ... . . . . . . . . . . . . . . . . ... . . ... . . ... . . . . . . ... . . .. . . ... . . . .. . . . . . .. .. . . . . . . . . . . . . . . . . ... . . ... . . ... . . .
' '
Grab a spare monitor or if your existing monitor (or STEP 4 Next, open up the box contain ing the mouse and
STEP 2
TV) can support more than one HDMI connection plug it into one of the USB ports on the rear 10 of
even better. The FUZE comes with a quality HDMI cable, remove it the FUZE workstation backplate.
from its bag and connect one end to the HDMI port on the rear 10
backplate of the FUZE and the other to the rear of the monitor or TV.
•
- -
-
•
• •
• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••
Now open the box containing the power pack and Ifyou want the Wi·Fi to be the active network
STEP S STEP S
plug it into the power point at the wall and finally connection, look to the bottom right of the
to the FUZE workstation itself. The FUZE will power up immediately desktop for the two arrows (one pointing up, the other down). Click
and start to boot into the custom FUZE Raspbian OS on the SD card. the arrows and the current Wi·Fi access points will be displayed.
Connect to yours as you would normally. You can now unplug the
Ethernet cable if you wish.
• • • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
You may need to change the source of the monitor Beyond the different desktop presentation, the
STEP 6 STEP 9
or TV's input to the HDMI, or the numbered HDMI FUZE setup works exactly the same as any other
port that you've connected the FUZE to. Once the signal is found by Raspberry Pi Raspbian system. You can click the first F (the white
the monitor it displays the FUZE desktop. F on a black background) to open the system menu detailing the
available apps and programs. The second F launches FUZE BASIC,
which we'll look at in the next tutorial.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
BACK TO BASICS
Let's begin our programming journey with FUZE BASIC, an environment where you can create anything, from simple scripts to
complex games with graphics and sounds.
• • •••••••••••• •••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••• •••••••••••••••••••••••• •••••••••••••••••••••
In the bottom right panel, the one that's coloured The retro style interface of FUZE BASIC has several
STEP 1 STEP 3
white on a red background? Click it and you launch themes that you can cycle through, depending
the FUZE BASIC, complete with a C64·style retro interface. You can on your taste. The default view is that of a Commodore 64 but if
also double-dick the FUZE BASIC V3 icon on the desktop. you want a different view press the Insert key to cycle through the
available interfaces. You'll no doubt recognise some of them, so find
one you like.
Don't worry if you don't have a FUZE Workstation. The screen you're looking at now is called
STEP 2 STEP 4
FUZE BASIC is available for Windows, the BBC Immediate Mode; pressing the Enter key will reveal
micro:bit and the Raspberry Pi (since it's already running on a RPi). a cursor where you can start to enter code. Try this: press Enter,
Open a browser to w
ww.f
yze
.co
.y
k/d
owp
lodf
a -u
zeht
. ml and follow then type: Hello everyone and press Enter again. The output on the
the download instructions for FUZE BASIC for Windows and the screen will display whatever you've typed into the quotation marks.
step-by-step instructions to install it on a Raspberry Pi.
-. ----
.. _,....
_______....
___.. ---·--
---
--·--·------
- - · · · .. · - ··
-
·-----
-- - ::::- :.:::..--· . :o:-____
----
....-----
... .. .. ... . . .. _ _ - · . -�
�----
.
• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••
STEP S You can also Print the total output of several STEP S Before we get into variables and other such
numbers from within the Immediate Mode. For programming terms, let's have a little play around
example, try: print 10 + 20 + 30, and press Enter. The sum of with a quick listng to ask for user input. Enter this:
i
• • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • • • •
STEP 6 If you find the screen getting a little full, en ter cls
to clear the BASIC display. BASIC in Immediate Mode
is also capable of storing variables, something which we'll look at
in more depth in the next tutorial. For now, try this and press Enter
after each line:
A=10
Print a
• • •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
coding came with line numbers. FUZE BASIC works the same way.
Whilst still in Immediate Mode, enter:
10 pri.nt "Hello"
20 goto 10
Now enter run. The word Hello should now cycle down the screen. • • • • • • • • • • • • • • • • • • • • • • • ••••••• ••••••••••••• • • • • • • • • • • ••••••••••
wit F ZE
Part 1
Variables are used in programming to store and retrieve data from the computer's
memory. It's a specified location in memory that can be referenced by the
programmer at any point in the code, as long as it's created and valid.
Enter the Program Editor, by pressing the F2 key. Press F2 to enter Editor mode and click on New. Now
STEP 1 STEP 3
Within the Program Editor enter the following, let's expand on the simple code. Enter the following:
pressing Enter after each line:
Let X=l0
Let X=l0 Let y=20
Pri nt x Let Z=30
Pri.nt x + y + z
Now click on the Save button, along the top of the screen and save
the program as "Variables1'. Click the OK button to return to the Save as "Variables2' and Run it. You now have the output of 60 on
Editor and the Run button to execute the code. the screen, as you've assigned X, Y and Z with numerical values, and
prnted the to tal
i .
• • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Let X=l0
After clicking Run you drop back into Immediate
STEP 2 Let x=x-1
Mode and the display will output the number 10. To
Print x
break down this simple code, you've created the variable called X,
and you've allocated the value 10 to it. The second line simply prints To begin with X equalled 10 but the next line subtracts 1 making it
the current value of X- which is of course 1 0. , 9, then prints the current value of X. Imagine this as lives in a game,
starting with 1 0 lves, losing 1 and leaving 9 left.
i
STEP S We can extend this further with more commands. The code from Step 7 introduced some new
STEP S
Try this: commands. First we clear the screen, then ask
for user input and store it in the variable name $. line 3 starts the
let x=10
conditional statement, if the user enters the name 'Dave' then the
Cycle
program will print HAL's 2001 infamous lines. If another name is
Pri nt x
inputted, then it will print something else.
let X=X-1
Repeat until xd0
Pri nt "Blast Off ! "
End
This creates a loop that will minus 1 from X until it reaches 0, then
prints Blast Off!
r,�.,
� r :�,....�-----=�..;;;;;;�-���'"!�ll<lla����iiiiii�;;�;;::;
��•�
9
" • • ••••••••••••••••• •••••••••••••••••••••••• ••••••••••••••••••••
7
"
'
STEP 9 Programs store all manner of information, retrieving
'
J it from memory in different ways:
?
1
B � <�� s: t Dl • ' cls
"il".'l�y
Data "Monday", Tuesday " , "Wednesday" "
The variables f$ and s$ store input from the user, then printed it Day 0f t .., P UP"'Io: numh P r 1 IG M or:d�y
Day cf ' "" n u �t� b Q r 2 ls r u .- s d ., y
back to them on the same line. Da y 0 f t .., e
u " " lo:
u ee lo: n u m b e> r l
. IG W c> dn e o; d a y
Day 0f t r "1 i" Ui'i'lo: n u �t� bl' r 4 I S l h u r � d a -:�
D ay 0 f t r1 e ueek n u m b C' r � 10 F r t d .l y
.��--=-----------��--- I<
D ay 0 f ' "" U e> e> lo: n u �t� h ., r " s � t u r- d .;J y
Hoi l c Ho�
o ��ld � �y��rd arr YO L J t o��� ? D ay 0 f
R e> a d y
'"• u e e lo: number 7 IS Sunddy
R�d d�
>I >I
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
wit F ZE
Pa rt 2
Moving on from the previous FUZE BASIC tutorial, let's expand everything you've
done so far and see if we can apply it to something other than counting numbers or
asking for someone's name. In the grand tradition of BASIC programming, let's create
a text adventure.
A text adventure game is an ideal genre to explore your BASIC skills in. There are variables, events, user input, counting and if
you want, even a few grap hics here and there to inject and use.
. .. . . . . . . .. .
... . ... . ..
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Enter the Program Editor and begin with a simple Let's add a way whereby the user is required to
STEP 1 STEP 3
clear screen, as it's always a good way to start. What press a key to continue, this way you can leave
we need to do is set some basic parameters first, so start with the instructions on the screen for an indefinite period:
number of lives a player has, for example 3.
Printot (15, 15); "Press the Spacebar to continue_."
Cls While inkey <> 32 cycle
Let lives=3 Repeat
This prints the message whilst wailing for the specific key to be
pressed on the keyboard: the Spacebar.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Now you can introduce the game and let the player Now we can start the 'story' part of the adventure:
STEP 2 STEP 4
know how many lives they currently have. You can
Cls
do this by adding the following to the code:
Print "You awake to find yourself in an airlock
Printat (41,0); "You have " ; Hves; " Hves left." onboard a space station . "
Printat (0,0); "Welcome to Cosmic Adventure ! " Input "There are two buttons in front of you :
Green and Red. Which do you press?" button$ ,
The printat command will specify a location on the screen to display
If button$="Red" then
the text using x,y.
Let lives=lives-1
Print "You j us t opened the airlock into space. You
are dead ! "
Print "You now hove ";lives; " lives left."
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
STEP 5 Now add: STEP S We can now continue the game from line 29, adding
another press the Spacebar routine, followed by a
If lives�0 then goto 25
dear screen ready for the next part of the adventure.
Print "Press the Spacebar to try again."
While inkey <> 32 cycle Print "Press the Spacebar to continue_"
Repeat While i.nkey <> 32 cycle
Goto 8 Repeat
Else Cls
Print "The door to the interior of the space
station opens , lucky for you . "
The Goto command goes Loa line number and continues with the
code. Here you can use i t to start an end of game routi ne.
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
the most elegant code you will ever see, it brings in many different
elements and shows you what can be done with FUZE BASIC.
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
This closes the If statements then goes to line 29 (If you pressed the
,
• •••••••••••••••••••••••••••••••••••••• ••••••••••••••••••••••••
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
the game all over again from line 1, the goto 1 part.
The last tutorial had you creating the foundations for a text-based adventure game.
Whilst it works perfectly fine, it would be nice to include some graphics and maybe a
few other elements to have it stand out from the usual BASIC programs.
ADDING GRAPHICS
FUZE BASIC employs a variety of different commands to display graphics, either drawn on the screen or by displaying a n
image file.
• • •• • • ••• •• • • ••••• ••••• • •••• ••••• •• • •• • • ••• • • •• • • • • •• • • ••• • • ••• • • • • • • • • • • • • • • • • ••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • •
You're going to start by making the game full Now create a new line 13, by getting the cursor to
STEP 1 STEP 3
screen, then adding an appropriate image that sets the end of line 12 and pressing Enter. For the new
the theme of the adventure. From line 2 press Enter, to create a new line, type in:
line 3, and type in the following:
Hidesprite (spriteindex)
Fullscreen�l
This command will remove the image from the screen, allowing you
Spriteindex�newsprite(l)
to include a new image for the next step in the game.
EarthS�"planetEarth.png"
Loadsprite (earthS, spriteindex, 0)
Plotsprite (spriteindex, 200, 200, 0)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . ... .... .
. .. .... . . . . . . . . .
... . .. . . .. . .
buttons$�"buttons . png"
• • • • • • • • • • • ••••••••••••• • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • loadsprite (buttons$, spriteindex, 0)
plotsprite (spriteindex, 300, 400 , 0)
The code from Step 1 will import and display an
STEP 2
image of the Earth; the image itself is already Make sure the image is called before the Input command!
available in the /Desktop/fuze·basic/extras/images folder. It's now
classed as a sprite and can be manipulated through the various
graphical commands of FUZE BASIC. Any unique images you want to
include should be copied to this folder to add to your game.
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Hidesprite (spriteindex)
U;date
Print "The door to space station opens . . "
ISS$�"ISS. png"
Loadsprite (ISS$, spriteindex, 0)
Plotsprite (spriteindex, 200, 200, 0)
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • •• • • • • • • • • • • • . ................. .. . . . . .. . . . . . . . . .. . . . .. . . . . .. . . . . . . . . . . . . . . . . . . . . .
By now your code is getting quite hefty. Don't Naturally you can continue with Cosmic Adventure
STE P 6 STEP 9
forget that with each new lineyo u re entering, the ' yourself, adding choices, graphics and keeping tabs
original Goto values will be different. It's best to return to the code on the number of lives and whatever else you can think of. As we
and update the lines where Goto is referenced. said, it's not the most elegant code and it's as far from a triple-A
game as you can imagine; but at least it s given you a head start '
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • •
. . .... .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . . .
"'
tu uww
ftdlsu-1
Ntrlt...,._,... .. ,�(l)
..
...
-�·-\lA<lft ....
a....�� ..., •-•• ...-u••,... .,
Fh'"-�� ,..,,�..--" -- .M. 0•
......ut
.. ....
(f1,0>. ..... .
...... .u h••• l<ift .
(lt'...,U! ft,OI "WI.- U <-•• .....
,.....u· (�.11o1 . ,.
..
.., ...,,
.. ,.
-.... te ..-.- ..
""
·-·
�'· ,...._ .. » ·�'·
...
....
.u
,
,.-In! -,...-, ,... •• 4oM
.
-'....... h· ...--.-
- •a-- ...
·'"""· ,..., .,
,.,.,.,.,..Jt.. c ....-u••••· 100 1110, .,
,.. l nt lrof ef� Ptf'�1 �...._. .. tiiOI'I ... i.. •
....� tl'lf'l 0 Jl �)..
"
·-
...
._ _ ·_ ...
-
--·
--
GPIO
The Raspberry Pi enables you to access electronic pins, known as GPIO (General Purpose Input and Output). These are used to
interact with external electronics like LED lights and switches. Below you'll learn to build circuits using a Bread boa rd .
The FUZE Workstation comes with a breadboard The red and blue lines are power rails: red is for
STEP 1 STEP 3
and some basic electronics components -you can positive and blue is for negative. The holes do not
follow along with this tutorial by getting a breadboard, 1 x blue and provide any power themselves; instead they just connect to each
1 x red breadboard wires, a Smm LED, a 220hms 5% resistor, 9V other. So if you plug an item into one hole, and another item into
battery, and a 9V snap battery clip. Your local electronics shop will a connected hole (along the line), then the two are connected as if
help you out. you'd physicallyjoined the two things together.
. .
-
' .
a
'
Get out the breadboard, hold it up vertically and The two columns of holes on the outside are
STEP 2 STEP 4
take a good look at it. You should see four vertical connected all the way down the line from the top
columns. The two pairs, on the left and right, both have a red and to the bottom. Take a 9V battery and attach a snap battery clip.
blue line running verti cally alongside them. In the middle are vertical Connect the positive wire (red) to the topmost red hole on the left.
columns with letters and numbers. There are typically two main it will provide positive power to any wire or component connected
columns, lettered A·E and F·J. in any red hole all the way down to the bottom. Add the blue
(negative) wire to the topmost blue hole on the right.
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
The two Get the LED and look at it closely. Notice how one
STEP 5 STEP 6
columns leg is longer than the other. That's the positive wire;
on the inside of the the shorter one is negative. Take a resistor and wrap one end of it
breadboard work around the shorter wire on the LED. Take the positive wire from the
completely differently. PP3 battery clip and touch the LED; lauch the negative wire to the
They are not wired resistor and see the LED light up. We've numbered these 1, 2 and 3
vertically, but horizontally so you can match them in lhe next steps.
along the row of each
columns. So if you look at
row 1, the holes marked
A, B, C, D and E are
connected; and the holes
in rows F, G, H, I and J are
connected. What do we
mean by "connected"?
Let's do it physically first
to find out.
STEP 2 Now
take the
LED, find the long end,
and slot this into hole 61.
This is the equivalent of
number 1 in our physical
connection. The red cable
is connected vertically
along the powerline,
and then to row 1 on the
breadboard where it is
connected horizontally to
the LED on row 1.
Boa r'-'
So far our breadboard hasn't been connected to the FUZE or Raspberry Pi in any way,
but all that's about to change. We're now going to remove the battery and slot our
breadboard into the FUZE.
GPIO PINS
The Raspberry Pi's GPIO pins act as a physical interface between the Raspberry Pi and electronic items. On the FUZE these are
safely connected to the 10 board, and can then be connected to your breadboard.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
This
STEP S STEP 6 Start FUZE BASIC and enter:
isn't any
different to what we
PinMode (0, 1)
had before, so let's spice
DigitalWrite (0, 1)
things up. Remove the
red cable from the 3.3V The LED turns on. The first part, PinMode, tells the Raspberry Pi that
10 socke t and connect GPIO 0 is going to be used, a nd the 1 part says it will be output. The
it to the socket marked DigitalWrite command sets GPIO 0 on. Enter DigitalWrite (0, O) to
0 underneath GPIO. The turn the LED off.
LED will turn off. This is
because this socket won't
provide any power until
we tell it to.
GETTING INPUT
We're now rea lly steaming alo ng Our Raspberry Pi . -powered FUZE is turning on LED lights in the outside world. Next we need
to look at input; how we can get information from our breadboard to our Raspberry Pi.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••• ••••••••••••••••••••••
STEP 1 Remove the LED and resistor from the breadboard Press F2 to open the Program Editor. Enter the
STEP 3
and remove the GPIO 0 and GND cables. Place the following program:
Push button switch in the same place as the LED (61 and D2) and
PinMode (0, 0)
place the blue cable in the hole next to it (E2). Take a look at the
Until DigitalRead(0) Cycle
photo if you need help placing the items in the right holes.
Repeat
Print "Button Pushed"
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • •
Now take the blue cable in E2 and connect the Here's what happens Power is flowing from the
STEP 2 STEP 4 .
other end to GPIO 0. Fi nally, connect the red cable 3.3V socket to the switch where it stops. Meanwhile
from the first hole in th e power rail to 3.3V. Our circuit is complete. our program has set GPIO 0 to 0 (input mode) and a Cycle Repeat
Current will go from the 3.3V to the power rail, and from the power loop is waiting until input comes through on 0 (via DigitalRead).
rail to our switch. The switch is connected to our blue cable, which When we push the button a connection is made, power flows to
connects to GPIO. GPIO 0 and it alerts the program. It then prints the message
"Button Pushed".
s1n"""
wit
As part of the educational kit, the FUZE Wo rkstation can be purchased with an
accompanying robot arm. This is a 149 piece kit-form robotic arm, that requires
assembly and is powered by four 0-type batteries. It's connected to the RPi or FUZE
via a USB cable and is also Windows compatible.
I, ROBOT
We won't go into the construction of the robot arm here, the instructions which come with the arm are easy to follow and it
can be completed and ready for use within a couple of hours or so. Let's look at how to get it working.
. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . .
The robot arm is one of the first external hardware To begin with, let's look at a few commands to make
STEP 1 STEP 3
components that was released and fully compatible the robot arm move. In Immediate Mode, in FUZE
with the Raspberry Pi; as such, it's an excellent project to get into , BASIC, enter:
from the construction of the arm itself, to operating it via the
ArmBody (1)
FUZE Workstation.
This starts the arm rotating clockwise (looking down on it).
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
CODE REPOSITORY
From Fractal g e nerators to encryption programs and animation, here are ten excellent examples of what others have done
with a little patie nce and a lot of code.
• • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • •
FRACTALS James Cook's Tree of Pythagoras is an This code will load any image
SCROLLING IMAGES
impressive fractal, constructed from squares, and make it scroll across the
that looks remarkably complex but takes up surprisingly little code. screen From right to left. Put your image either in the /extras{
You can find it at www.Fuze.co.uk/code=reposjtorv:just enter the images folder or simply in the same folder as the code itself. Save
code and Run it to be amazed. and Run and enjoy the image moving across the screen. See if you
can modify it to Full screen, or more.
• ••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••
While in the same folder as Shooter.fuze, look for FuzeFighter, also found in the Games
3D BOX BEAT'EM UP
Box.fuze. This is a great animation program that folder, is another prime example of what
displays a rotating three dimensional box on the screen until you can be done with FUZE BASIC. There's in-game music, sound effects,
press the Escape key. It's a fantastic learning resource and with a animations, collision detection, scoring and a two-player element
little time you can bend it to your will and use it in your own code. that can be worked into your own routines.
• • • • • • • • • • • • • • • •••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • •
Dogs in Space is a fun little game The Robot.fuze file, in the Games
DOGS IN SPACE ROBOT CONTROL
(found in /fuze-basic/D emos) that folder, is an extension to the
features in-game music, sprite animation, collision detection, scoring previous tutorial's robot movement BASIC program that you entered.
and keyboard controls. Whilst it may not amuse you For too long, However, this time there's graphics and animations to help improve
it's benefit lies in the code examples that you can turn to your own the process and make it a more flexible (excuse the pun) program.
future programs.
Live 2 53.1
Click the Load button, and browse Finally, snake .fuze is a good example of a
SPACE INVADERS SNAKE
to /fuze-basic/Games. Open the combination of programming elements. Graphics,
file silv.fuze and have a look thr ough the 784 lines of code before scoring, collision detection and some interesting routines t o help
clicking the Run button. It's Quite complex but when you run it you improve your overall program can be found within this code.
can see why. Those of a certain age will no doubt recall spending a
for tune on Space Invaders in the arcades!
CODE ST RIPPI NG
Many of the legendary programmers from the golden era of home computing stripped the code from snippets posted in the
magazines o f the time. They bent the code to their will and created something as close t o magic as possible.
• • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • •••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • •
one. Load up aball.fuze from Demos and see what you can use. take the key codes you need and insert them in your own programs.
. · · - .. .
••
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
CLS • • •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
LOOP
Including the use of a gamepad
PRINT "Sense Accelerometer X2"; SENSEACCELX JOYSTICK CONTROL
orjoystick in your games or
PRINT "Sense Accelerometer Y2"; SENSEACCELY
code is a great addition to also being able to redefine the keyboard.
PRINT "Sense Accelerometer Z2"; SENSEACCELZ
This code will detect and display the states of each of the axis
REPEAT
and buttons .
END
1 Uf"UII!lr� 8
2 RJINISU£ ())
J�P'M(O)
...,.,.
S CLS2
6 I .. - CMtANIC'O£
"'"NJNTAT (0, OJ; ·� dtttctH joysttck hu•
8 J.. • UJQITQU(W
911'1UIIrfTAT (0 , 1, •kt"ton5: -!
10 I- - Y'[LLOW
11 NJfifT .......�� 0) 1
12 J.. • UJQITC"A££W
ll PIUWTAT (0, 21" "'AN.l09 Sttcb 6 Trt�s: ";
U I- - Y£LLOW
15 NTNT lo;IJJ!,A:.:r (0)
16 l,.. ... BUGHTGIUXN
17 PIUWTAT (0, )) ; ..Oigiul
pAds: "';
U t• Y£LLOW
•
g
16 lNit - ORANC'ol
li �fl(fAT (0, U); "oi i ul pad states"
38 FO« hat - 0 TO �1.. (0) AT S
1-i
1 lOOP
39 INK BRT('MT('.Rf"f"N
•
untitiPd
1 CLS
2 LOOP
) PUNT S�tl);e C�S X••; S(NS((OMPASSX
4 PRINT Sen�e C<»p..SS y.•; S{NS(COMPASSY
5 P R I N T "'>M• Cu.pUS Z•• i S£NS£COMPASSZ
6 REPEAT
7 £Ntl
• • • • • • • • • • • • • • •••••••••••••••••••• • • • • • • • • • • • • • • • • •
Copy the code below into a New > File and save it as FileMan.py.
Once executed it will display the program title, along with the
current time and date and the available options.
import shutil
import os
import time
import subprocess
de£ Raad():
path=input("Enter the fil e path to read:")
fil e=open (path ,"r")
print(file.read())
- -·
def Add():
path=input("Enter the fila path:")
text=input("Enter the text to add:")
file=open(path,"a")
file.write( '\n'+text)
def Delete():
path=input("Enter the path of file for deletion:")
if os.path.exists (path):
print( 'Fila Found')
os.remova(path)
print( 'File has bean daletad')
else:
print( 'File Does not exist')
def Check():
fp=int(input('Cback existence of \nl.File \n2.
Directory\n'))
if fp-1:
path=input("Enter tha file path:")
os.path.isfile(path)
Python File Manager
I
else: ,, ))
print( 'Directory Not Found') if dac==l:
Read()
def Move(): if dec==2 :
pathl=input('Enter the source path of file to move:') Write()
mr=int(input('l.Rename \n2.Move \n')) if dec==3:
if mr= =l: Add()
path2=input('Enter the destination path and flle name:') if dec==4:
shutil.move(pathl,path2) Delete()
print('File renamed') if dec==S:
if mr==2: Dirlist()
path2-input('Enter the path to move :') if dec==6:
I
shutil.move(pathl,path2) Check()
I
print('File moved') if dec==7:
I
Move()
def Copy(): if dec==8:
pathl=input('Enter the path of the file to copy or rename :') Copy()
path2=input('Enter the path to copy to:') if dec==9:
shutil.copy(pathl,path2) Makedir()
print('File copied') if dec--10:
Relnovedir()
def Makedir(): if dec-11:
path=input("Enter the directory name with path to make Opanflla()
\neg. C:\\Bello\\Newdir \nWhere Newdir is new if dec=12:
directory:") exit()
os.maked irs(path) run=int(input("l.Return to menu\n2.Exit \n"))
print('Directory Created') if "'ln 2:
exit()
def Removedir ():
path=input('Enter the path of Directory:') •••••••••••••••••••••••• • • • • • • • • • • • • • • • ••••••••••••••••••••••• •
•
treedir=int(input('l.Deleted Directory \n2 .Delete • •
• •
Directory Tree \n3.Exit \n')) •
•
•
•
• •
if treedir•�l : • •
• •
os . rmd ir ( path) •
•
•
•
• •
if treedir==2: • •
• •
shutil.rmtree(path) •
•
•
•
• •
print( 'Directory Deleted') • •
• •
if treedir==3: •
•
•
•
• •
exit() • •
• •
• •
• •
• •
daf Opan1lla(): • •
• •
path=input('Enter the path of program:') •
•
•
•
• •
try: • •
• •
os. startfile(path) •
•
•
•
• •
except: • •
• •
print('Fila not found') •
•
•
•
• •
•
Imports
•
• •
run=l •
•
•
•
• •
while(r1ln=l):
: There are three modules t o import here: Shutil . OS and •
•
try: •
os.system( 'clear') ; Time. The first two deal with the operating system and file •
•
•
: management and manipulabon; and the Time module simply •
except OSError: • •
os.system( 'cls') : •
•
•
•
print ('\n>»»»»>Python 3 File Manager<<«<««<\n')
print('Tha currant time and date is:',tima.asctima()) ; Note how we've inc luded a try and except block to check if
print('\nChoose the option number: \n') : the user is running the code on a linux system or Windows.
•
dec=int(input( '"l.Read a flle :• Windows uses CLS to dear the screen, while Linux uses clear.
2.Write to a flle : The try block should work well enough b ut it's a point of
3.Append text to a f1le
4.Deleta a fila
;
•
possible improvement depending on your own system.
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••
N u m er uess1 n
N UMBE RGUESS.PY
Copy the code and see if you can beat the computer within
five guesses. It's an interesting bit of code that can be quite
handy when your implementing a combination of the Random
module alongside a while loop.
import random
guessesUsed = 0
Name=input( 'Hello! Wha tis your name? ')
numbe r = random.randint(l, 30)
print('Greetings, • + Name + ', I\'m thinking of a
prtntt 1 ..,, f.,. .,.., !M I"UUdMr J -...s thin-�"1 of n • ,....,, ' done, • +
correctly in • + guessesUsed + ' guesses.')
if guess != number:
number = str(number)
print('Sorry, out of guesses. The number I was
thinking of is • + number)
rGu...
Numt> py '11 m f o � IlO Code NJrrilflGuos S py H 21 - o •
lf au"HS = ,..,..,.r:
psH5UMd • str(p&suus.ct)
pnnt( •• • ..,_. • 1
, .,.ss ''"' �=
�r • str(nu-bet)
pnl'l t( ....,. •IH'S t� �r I lUI tfl�'"l of is • ...,.)
.,
Number Guessing Game
,... • 8 •
--
·
· -- --..
--· � . ..,
r..--·-· .,
·- ·
- ..-· ..,
• ..-·----· ... ... �--)
• ..-·---- __,
..-.�- �
-- ..-·- ......
'
•
_., ____
"
•
• ••••••• ••• •••• • •• •••• ••• •••• •••• ••••••• •••••• • •••• ••• ••••••• •••• ••• •••• ••• ••• •••• • ••• • •• •••• ••• ••• •••• •••• ••••••••••• ••••••• •• •
•
• •
•
: Code Improvements •
•
• •
• •
•
: Since this is such as si mple sc ript to apply to a situation, there's For example, as per the screenshot provided, you could use •
•
•
•
: plenty of room to mess a roun d with it and make it more something along the l ines of: •
•
•
•
: interesting. Perhaps you can include an opti on to take score, the •
•
: best out of three rounds. Maybe an elaborate way to congratulate EndurancezO •
•
: the player for getting a 'hole in one' correct guess on their first try. CR
...O
•
• Luck...O
•
•
Endurance = random. randint(1, 15)
: Mo reover, the number guessing game code does offer some room
CR = rancloaa.randint(1, 20)
; for implementing into your code in a dif feren t manner. What we
Luck = rancloaa.randint(1, 10)
: mean by this is, the code can be used to retrieve a random number
• Print(�Your character's atata are aa follOifll:\n")
: between a range, which in tum can give you the start of a character Print(�Endurance:•, Endurance)
•
: creation defined function within an adventure game. Print(�Combat Rating:•, CR)
•
• Print(�Luck:", Luck)
•
: Imagine the start of a text adventure written in Python, where
•
: the player names their character. The next step is to roll the
• The player can then decide to either stickwith their roll or try again
•
• virtual random dice to decide what that character's combat
for the hope of better values being picked. There's ample ways in
: rating, strength, endurance and luck values are. These can then be
•
which to implement this cod e into a basic adventure game .
: carried forward into the game under a set of variables that can be
•
: reduced or increased depending on the circumstances the player's
•
: character ends up in.
•
•
•
•
• C" ract..Siats .v /
horne " Oocu non COde/Cha ct..Siats py 3 4 �� - 0 •
•
• Bit £<k F gnn a
t Bl6\ Qptlonl W!ndowt U�
•
•
•
• • . t . I) (1ft hnu:�t
•
• '"copynatlt'". ·crf'da\1" or "hctnse()" for 11ore anfonnatton•
• •
• •
• •
•
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
pendown();goto(x2,y2)
t+=inc
• • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • ••••••• •
•
•
•
Graphical Enhancements •
•
•
•
•
There are several ways in which you can improve this code to make For example: •
•
• • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••• • • • • • • • • • •
Polygon Circles/Random Number Generator
see the results of that interaction. nmbl=int(input("Enter the start nU111ber : "))
nmb2-int(input("Enter the last nU111ber : " ) )
x = randint(nmbl, nmb2)
print("\nThe random number between ",nmbl,"and",nmb2,"is:\n")
print(x)
• • • • • • • • • • • • • • • •••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••• • • • • • • • • • • • • • • •
•
•
: More Input
•
: prompt the user for more input. Perhaps you can provide them
•
: with addition, subtraction, multiplication elements with their fro�� random import •
; numbers. If you're feeling clever, see if you can pass the code import turtle
: You can also introduce the Turtle module into the code and turtle.left(90)
•
: turtle.forward (nmb3)
perhaps set some defined rules for drawing a shape, object or
•
turtle .left(90 )
: something based on a user inputted random value From a range
• turtle.for ward (nmb4)
: of numbers It takes a little working out but the effect is certainly
turtle.left(90)
.
: really interesting .
•
•
•
•
Whilst it's a little rough around the edges, you can easily make it
•
• more suitable .
•
•
•
• - " .
•
•
•
-au- - • .
• •
•
•
•
too• (« �· ""' .... .....
.
..:
.:.:
•:.;,;_
' =-.:...______
.. _
-
•
.
,_,......,
• •
......., tw1cl• •
•
•
•
..,.,.1('\II• ..... ....f. UCU<I...,.J •
•
,.,•.,.U.,..,.t( an _.., •n •
•
• ,..,.,_iMC�t lf'f' .. � ...... )J •
• ......U'l(�ti"''MH ... ftl....... )) •
IEM.M' Hie 'llart -�' Zl ,....,.l( ....
• t ( ..... ... ·-·· ..... u •
..,.... tt. _.... ....,, ... Jt •
•
•
....
,.., * ,
1111 11 • ..,.. '" •
enw u. f -u. ._.. n u
... ··-·-----·-·--
• �tle.I_"C....t) •
• -----·-- ..., tur-tJ••J.ft(tt) •
... •
• tlllftJ•.•-•t.-J)
• twtJ•,l•ttC"J •
• t\11"1.1•.,.,_ 't,...J) •
•
• tW1J•.l•lft..)
&It� the u•r' ".,..' • •
• tlllftt..••�•c,..,)
p
• ...,t�r th- ....... ,...,..,, ., •
lUitl•.l•fUMJ •
• eu•r the ttur• 01IIIIH11 •
• �t•r tiM f-r1'! """"'' ?* ,.. ,,....,.te,...,. �n
•
• ••• I ""''"''(-'"''..., ,....._ � M�· I'Mti.".W"',...,.J,"'u·VI•J
•
•
•
• •
•
• ••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
Tkinter is the main module in use here but we're also using
idlelib, so you may need to pip install any extras if the
dependencies fail when you execute the code.
root = Tk()
root.title( " File Browser" )
- M)OJ• •.-.-. _. IC••U•.,;-•. fiW'Io
... _ �·-
--
sc = ScrolledCanvas(root, bq="white", -
.
...- _.-
- ••...,..
_... _ ·---��
hll
•
•.to.
u .:
•- _
hiqhliqhtthicknass=O, ta.kafocus 1) J
wr-�-· I •-
item = FileTreeitem(os.qetcwd()) !
node = TreeNode(sc.canvas, None, item)
node.expand()
......
. .,
.. root.mainloop()
1-
1'
, _.,
8ooiMI"'f• « PI
- -... ..
I' C ikP'Lpy
,. (...,....lfiY
, ()c(ll\�
I! Ol<
t ll\21'1')'
, fiMI!Ir-tpy
FH....._ py
, Goo;l•'••td'I.P'f
, Gooqi.Strdl.py
,_.,
,. M�motto..."•w
I' l"otwontwdet. P'J'
RnciNtMnGen.py
I" IWII"t uworclpy
;.> Sc roUtt.P)'
" TlorutT.Oiu,py
I' fu"kUntt py
I' N21!1in. py
.. ""'"'"
� b k l4) py
c•lt�w.py
, di)'Sl'ltnew.h.py -·
I' ecept1onl PI -·
I' ««t>Ptlot\2PI
I' •t�PCIOJ'\I.P'f ,.
·-
· -·
_
" -·
·
_
.,
ht......py .
..
......
�::::,�
,_... ..
-..
..
__,
.,._ ,
,_
r- tdlelt.b.Tn�hdl'ft
OS
root • T1c:()
root.utle( •• )
. ..
•
root.a.atnloop()
Basic GUI File Browser
•••••••••••••••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• •
•
•
• •
: Advanced Filing
•
•
•
•
• •
: When executed, the code will display the current directory's try: •
•
•
• with open(name,'r') aa UaeFila:
: contents. Ifyou want to see the contents of another directory, you •
•
• print(Usel'ila.raad()) •
• can run the code from a command line within the chosen directory; •
•
• except: •
; just remember to call the code from where it's located on your print(�No filas opened")
•
•
: system, as per Lhe second screenshot. You can also double-dick any
•
of the file names shown in the directory tree and rename them.
Titla = root.titla( "l'ila Opanar")
This is an interesting piece of code and one that you can insert into labal • ttk.Label(root, taxt •"l'ila
your own programs. You can exlend the code to include a user Open",foreqround:"red", fonta("Helvetica", 16))
specified directory to browse, perhaps your own unique file icons label.pack ( )
too. If you're using linux, create an alias to execute the code and
then you can run it from wherever you are in the system.
manu = Manu(root)
root.conflg(manu=manu)
Windows users may have some trouble with the above code, an
a lternative can be achieved by using the following: file = Menu(menu)
)
exper menting with to see what you can do with it
...)______
print (n
i
__
roo� • TkC J
-:t OpenFib() :
n� • &atoPtntlltn&me(lnlelaldlr•"C t•,
t11ee�• •(( "T� ae tJ.le •, "•.t•�"), ("All FJ.l�� ".""·""H.
t:itle • " • file.•
I
punt (n.ame:)
.eAU • MenU(l'OOt)
root.coatlQcunu.-.enu)
root u.l.Al.oop()
•
l.ft: 11 Cot 21
• •
• .......................... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . .. . . .. .. ............................. . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . .. . . .
.
Text
EPICSCROLL.PY
import pygame as pg
from pygama.locals import *
p g .init ()
I remember
The dark woods, masking slopes of sombre hills;
The grey clouds' leaden everlasting arch;
The dusky streams thatflowed without a sound,
And the lone winds that whispered down the p
asses.
class Credits:
def init _ (self, screen _ rect, 1st) :
self.srect = screen ract
salf.lst = lat
self.si za = 16
salf.color = (255,0,0)
se lf.buff _ centery - self.srect.height/2 + 5
self.buff lines = 50
self. timer = 0.0
self.delay = 0
self.make _ surfaces()
te:ct. rect
self. taxt.append(l)
���f.ces(.-lf):
self.tnt • 1J
t. llne ..,...,.ttCnlf. bt):
def update(self): 1 • s;elf.- ._te-.:t( hne)
l i1 J.y ·� t•self.llufi_h,....
•
runn101 • r •
screen.fill((O.O.O))
cred = Credits(screen rect, text list) cnd.upd•tt()
cred. render(screen)
pa.dtspl�y.u�tt( )
clock. tick(SO)
while running:
for event in pg .avant.get():
if event . typa == QUIT:
running = False
•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• •
•
•
•
•
•
• A Long Time Ago ...
•
•
: The obvious main point of enhancement is the actual text
! itself. Replace it with a list of credits, or an equally epic opening
: storyline to your Python game, and it will certainly hit the
•
: mark with whoever plays it. Don't forget to change the screen
•
•
•
resolution if needed; we're curre ntly r unning it at 800 x 600.
•
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
import time
print("\n" * 200)
print(">>>>>some >>>Awe Adventure<«<«<<<<\n")
print("\n" * 3)
time . sleep(3)
print("\nA long time ago, a warrior strode forth from
the frozen north.")
time.sleep(l)
print("Does this warrior have a name ?")
name=input("> ")
print(name, "the barbarian, sword in hand and looking
- for adventure!")
•
.-
- - -- time.sleep(l)
--- - print("However, evil is lurking nearby.. . .")
time. sleep(1)
print("A pair of bulbous eyes regards the hero...")
time.sleep(l)
print{"Will", name, "prevail, and win great fortune ... ")
time.sleep(l)
int("Or die by the hands of great evil . . . ?")
pr
time. sleep{l)
print("\n" *3)
print("Only time will tell . ..")
time.sleep(l)
print ( ' . . . )
'
time.sleep(l)
print ( ' . .. )
'
time.sleep{l)
print (' . . . )
'
time.sleep(l)
print(' . . . ' )
time.sleep(S)
print("\n" *200)
def start() :
print("\n ----------")
-
·
..
-c-· . ... print("Do you approach the. . .")
...�. _ ... .. .. .. ...... .......- ... .- .
..
.. _
.... .... ... . ... . - . . - -- .... print("\n")
_ _ _ _ .. _ - · ... - - --- -·
tiM •"--n
print("l. Ragged looking man")
_..,
--·...- ·-·
print("2. Dangerous looking guards")
--.... ..
- ._ ........ ... .
-
·-· .. ..
....... . .........
.,._ .. ,..
_ ....
_ �....t ·...
-- ..... -
_
.......
.. .. .... .. .... .
t_,,_.,
cmdlist=["l", "2"]
cmd=getcmd(cmdlist)
Text Adventure Script
time.sleep(2)
if cmd "1":
=
ragged()
elif cmd = "2": def getcmd(cmdlist):
guards () cmd input(name+">")
=
if cmd in cmdlist:
def ragged(): return cmd
print("\n * 200)
" elif cmd = "help":
print("'You walk up to the ragged looking man and print("\nEnter your choices as detailed in
greet him. the game.")
He smiles a toothless grin and, with a strange print("or enter 'quit' to leave the game")
accent, says. return getcmd(cmdlist)
"Buy me a cup of wine, and I' 11 tell you of elif cmd = "quit":
great treasure . . . ' " ) print("\n-----------"1
time. sleep(2) time.sleep(l)
print("Sadly you return to your homeland without
def guards(): fame or fortune . . . ) "
: Adventure Time
•
•
: This, as you can see, isjust the beginning of the adventure and
•
: takes up a fair few lines of code. When you expand it, and weave
•
: the story along, you'll find that you can repeat certain instances � �-----
prtl'lt(\1'1
� - ..
•200)
;
•
such as a chance meeting with an enemy or the tike.
•
•
: We've created each of the two encounters as a defined set
• pn" t ( n. _,..t.n ,.. t1 th .. . for • Nfcl hfe. '" )
: of functions, along with a list of possible choices under the pnnt( Pre< [ o r 1 tl&u � t •, �. n '")
inpl,ft()
: cmdlist list. and cmd variable, of which is also a defined function. Str.ntth•r�.rtndint( t.lO)
• pn.ntcn_.,, ..,.. • s • t'- ue 41 , Str.,.th)
: Expanding on this is quite easy, just map out each encounter and pnnt( .nlt 1 • d hfe 4, •ll ""'rthl re born r 1n. )
• prml(..Pr• £nt 0 1 dt•e 4 .. •t tt tlf'C
.. IOI , n..e• )
: choice and create a defined function around it. Providing the user tnp.�t( )
• CR•randa-.r•ndtnt(1, JO)
: doesn't enter quit into the adventure, they can keep playing. pn,.t(na.e. 1\1• • C•
prtnt('"\nYo� � rh
•t at"'I t · . C«)
�::>
the tal f ,v, � r�l'h � C�t �•tana.·>
• prll\t(•Pr� Ent� to " . n,..• • , �••lth walue •)
•
• tnput()
: There's also room i n the adventure for a set of variables designed He�lth•Strength*CR
prant(n.... has a Health wa!� of , Hellth)
•
prtnt('"\nfveryorw need� • c•rt.au1 • .,t of lud Ul surwtv• ) .
: For combat, luck, health, endurance and even an inventory or ,
prantCPrns Enter to roll tl'le dace lnCI 'lee twwr Ju.rky•, n.,..., ·••· )
input( )
; amount of gold earned. Each successful combat situation can Luck·r�ndOM.r�ndlllt(1 . 1$)
: reduce the main character's heal th but increase their combat skills 1f LVC;k > 1):
print(niMe, "ls luc• &ndted. and has 1 Lv<� value of: · , Luck)
•
e.a.s :
: or endurance. Plus, they could loot the body and gain gold, or earn prtnt(na.e, 'has 1 l�k walue of , Luck)
• tt.M.sleep(5)
: gold through quests. prtnt( \nM *200)
• print("H•re·s yu cl'laracter tats:\� )
• prtnt(naM)
•
prtnt( ,n(�blt Aat1n1 ••• CR)
: Finally, how about introducing the ra ndom module. This will enable prtnt('"Strt:''lth - strenatl'l)
•
• prtnt("Health • , "-•lth )
: you to include an element of chance in the game. For example, i n prant('"'-ucllo ··, Ludc)
• prtnt(""\n .,,
: combat, when you strike an enemy you will do a random amount prtnt('"Prns (:nt te� r ...,'
,,Ht.l' )
ittput()
; of damage as will they. You could even work out the maths prtnt('"\� •200)
; gold (again, improve the chances of winning by working out your � SUr-t():
Pf"t.nt( \n )
: luck factor into the equation). pt'tnt(-oo rou ICifi'NCh tJM •)
• p.-tnt(" n")
• ptlftt('"'l. ...... lMklftc ......,
• p.-lftt('"'2. O.W,..I'- loolo.JM: INiltG )
•
: Needless to say, your text adventure can grow exponentially
: and prove to be a work of wonder. Good luck. and have fun with
•
: your adventure.
•
•
•
•
•
•
•
•
•
•
• • • • • • • • • • • ••••••••••••••••••• •••••••••••••••••••• •••••••••••••••••••• •••••••••••••••••••• ••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • •
import time
import tkinter as tk
def tick(timel="):
I get the current time from the PC
time2 ti me.strftime(''liH:%M:'IiS')
=
if tim e2 != timel :
timel = time2
cloclt.confiq(text;=time2)
II'Jthon J:•••t (.f....U.
. OCt It ltU. tJ:Jt:lt)
(6CC 4.P.11 on IUM��;
,,.. ·copyra4f't'". ·c�"ats"' « ·uc...,.()'" f� ..,. lftfO,.,Utft. cloclt.after(200, tick)
-------- --· -•
--··-· MIT/IIfr -
··
·
----
-- -
·-·
....
· - .. -
�>> ------
- -- MS"TMT
root tk.Tk()
--------
--- -··---
»> =
,.,.,. MS1/III.T
clock tk.Label(root, font;=('arial', 20, 'bold'),
--·-..--
-···-· ·-• -
··
- ·
- ·-
··
- -
·
- -
Ml'YMT -·-·---··---
--···- -
bg='green')
,.,.,. --·---·---- --··
>>>
clock.pack(fill='both', expand=l)
tick()
root.mainloop()
• 0 X
D �I
clodk.•fter(200. tick)
root • tk.fk()
clock • tk.Libel(root, font•( 1 , 2'0. 'MI4' ), bl• &reM' )
clock.pack(flll• �th . ·���1)
t lc:k( )
root·••inloop()
r-------c;���;;����;t���;���,- 0 X
•
f t1Ck(t.-e1• ):
" pt U. current ta.t fr• the PC
t...2 • u••.strftwe( --, U' )
tUte.l I• ta.t1:
ta.tl • tUIM2
clodk.confL&(t••t·t�Z)
clock.lfter(200, u..ck)
root • tk.Tk()
clock ... tk.Loabd(root. font•( rtll , 10. Mid' ), bg•'&r•.n' )
clock.pack(flll• bot� . tKpand•l)
tlCk ( )
root.uinloop( )
Python Digital Clock
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
• •
• @classmethod
• Tic kTock
•• def main (cls) :
••
• This is a piece of code we've used many times in the past to keep tkintar .NoDataultRoot()
•• root tkinter.Tk()
• track of time while working on mul tiple monitors and with just a •
import random
board = [ '"
>>>>>>>>>>Hangman<<<<<<<<<<
+---+ l
I I
I
I
I
I
====1 1 1 HI
1
+---+
I I
o I
l
�
,
+---+
I I
o I
I I
I
I
----
-
-- --
-- --1 11
,
\II
+---+
I I
o I
/1 I
I
I
"
====' ' ' '
+---+
I I
o I
/1\ -=
====''' "'
,
;I'
.
-
II +---+
,
'.
I E I
1I
' ,
,I
'
I
···-····· . . .
o I
/1\ I
I
,,
!- ,I
'I
' .
I ,
_
. II III I:
140 BDM's I·
.'
.
pecial Iyolume ?7
Tech S ,I ·./
·
I
I I (\
' • . '
Hangman Game Script
I I def main():
o I game = Hangman (rand word())
/I\ while not gsme.hangman _ over():
I \ game.print - game - status()
user input = input( '\nEnter a letter: ')
''') game.guess(user _ input)
=====
t _ game_ status()
rin
gama . p
class Hangman: if game.hang man won():
def init _ (self,word) : print ('\nCongratulations! You have won!!')
self.word = word else:
self.missed _ letters = () print ('\nSorry, you have lost.')
self.guessed _ letters = () print ('The word was ' + game.word)
elsa: : QUIT()
•
•
•
•
• •
return False
;
•
Since this is the last example in our Python code repository we , •
return True •
•
: thought we'd go out with a bang and feature the hangman •
•
•
return self.hangman von() or (len(self.missed : Don't worry if it looks misaligned in the text here, this is merely •
•
•
letters) � 6) ; due to the differences between using the Python IDLE editor •
•
•
: and pasting the code into a word processor (which formats •
• •
return False
; There's plenty you can do to improve, enhance and expand on •
•
•
: what we've presented here. You can include a rou tine that •
• •
rtn = " : can include extra points for someone who guesses the entire
for latter in salf.word: ; word in one go rather than one letter at a time and you could
if letter not in self.guassad letters: : perhaps add Chopin s Funeral March should you lose the game;
'
•
else: •
•
•
rtn += letter •
•
return rtn •
•
, ..
.
_. .. ..
. ... --
... ...
-
= = .,__
- _ ___ .....
• ..
.�·-:.... .. _........
-
�- ..
·- -
•
• _
..,. ........
_
def print _ game _ status(salf): •
•
•
print (board[len(self.missad _ letters) )) •
•
issed letters:
for letter in self. m •
•
•
print (latter,) •
•
print () •
•
•
print ('Letters Guessed: ',) •
•
for latter in self.guessed _ letters: •
•
print (latter,) ; Consider replacing the bank of words too. They're found under
print ()
; the bank list, and could easily be swapped out for something
: more difficult. If you download www -
ait
h yb.com/ dwy l
/eoqli
str
def rand word():
words
•
: Perhaps you could swap the words in the bank to instead read
acco mmoda tion accounting beautiful bookstore
: the contents of the text file:
calculator clever engaged engineer enough •
• • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • •
</> u T r •
I I .
"' ( \
l
Code Project Examples . . '
.
,
' I •
·.I �. '
,
. ,·.
I' T II . '
ere Nex t?
Coding, like most subjects, is a continual learning experience. You may not class
yourself as a beginner any more but you still need to test your code, learn new
tricks and hacks to make it more efficient and even branch out and learn another
programming language.
#INCLUDE<KEEP O N LEARNING>
What can you do to further your skills, learn new coding practises, experiment and present your code and even begin to help
others using what you've ex perienced so fa?r
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • •
Twitter isn't all trolls and antagonists, among the well Look for open source projects that you like the sound
STEP 1 STEP 3
publicised vitriol are some genuine people who are of and offer to contribute to the code to keep it
more than willing to spread their coding knowledge. We recommend a�ve and up to date. There are millions of projects to choose from, so
you find a few who you can relate to and follow them. Often they contact a few and see where they need help It may only be a minor .
post great tips, hacks and fixes for common coding problems. code update but it's a noble occupation for coders to get into.
·- · - ·- -
(")
-
•0 - .. • .. R
' oO 00 ' •
� p 10-r �
- -- - - - - I � LinuxM1nt
...._
... - --
-- g....... . ".... .
-
""" ,. -
........ -
---
..- -- � ...,.
".....
·-�
.......
�
-- -
·----
-�:' --·
·-... -----·-·-
-··-·- A _ __ _.,...___ ."""""" . ........ •o.
. . -----
,., ...
c.-.-.... .·=·...
...,. _
""
.
...
____
___
- .._,.._... ·- ..,_.,. - '"
Qo Uo tJ ...
.;
Ml _,..,.__
.
\:.1
� ,,-._....
. . . ....,. � ,
• __._or_.....,.,..,,._..w
·�
.. ..
- • ..._-, ..... ,__.......
.., ..
_.. (_.
-·..... ........... _
..• �._... '""' _ •• .,., ..
t. � -...
- .
••o ..... --_,._..__,,_
_ _,,....,.....
...
..
... _ ..
_....
Vo tl• U Mmo
A
--·
·-•r-
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •
STEP 2 If you've mastered Python fairly well. then turn your STEP 4 developm nt
Become more active on coding and e
attention to C++ or even C#. Still keep your Python knowledge sites, such as StackExchange. lf you have
skills going but learn ing a new codi ng language keeps the old brain the skills to start and help others out, not only will you feel really
ticking over nicely and give you a view into another community, and good for doing so but you can also learn a lot yourself by interacting
how they do things differently. with other members.
•
StackExchange .._ �...., -
-
2.7U.28t .-..
---1
0 -.--· ---·-·
_...._
.._.
-·_.._._ .......-....
..____..
.- .... ...- _....,...._
---
0 "'wwl
. ...... _ ..
.._
______.,..__ .. ...._...._... ....-.-...,.......
-.. .. _
...__..__.._
�. - ,,�·----�
• :..,.,...._...._._,.,...
. .
--·---·--........4........
.. ........___
. .,..___..__,_
_..�..-..--..........
_.•,.. --
0 --
-.....-�
........_.,
...
.. .. -..-
_,..... .. .
... .
.-.........._........,..,....._ ... ...... .... . ... ...
..
---�
• �_...,..IJI,.....
·-·��............ ..,._...
,. ,... _,
c.. .. . ...
"'-".IW>ollo..- ,...
... . ,..,._... ......·-......
_....,.
__
- _
_--
,
_
'
'
•
, Ij I) .
i
i! ' J' JI (
.
) I ,
·I .
I' ) .._
,
I ',
,] I \ '
;
,
I II I
,I p 1 I ,I
' (I ,'
(J II '•' ·jI,
: ',
� 'J ,,
y '
I ' >'
l •
q (I '' ' I .
·
'
I
'J ll
I l: I!
'• •:
. '
.I
J k I'
,
'
,
'
, ••
'·
I
, I
I
Where Next? </>
t I' I. ''
'I ,.,
- i
'
j
' '
' 1--!)I
,
• -. , ,
,-
.
_ .....
'I
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • •
STEP 5 The mobile market is a great place to test your STEP S Ifyou've learned how to code with an eye for a
coding skills and present any games or apps you've developer job in the future, then it's worth starting
created. If your app is good, then who knows, it could be the to build up an online portfolio of code. look at job postings and see
next great thing to appear on the app stores. It's a good learning what skills they require, then learn and code something with those
experience nevertheless, and something worth considering. skills and add it to the portfolio. When it comes to applying, include
a link to the portfolio.
My Wor-k
' '
Chronieled.org
·-
·•
""' II>
...
--X
-
..,._
-
• __ .... • o...-
I
••••• ••••• •••• •••• ••0•• ..... .... ..
• • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • •
STEP 6 Online courses are good examples of where to STEP 9 Can you teach? If your coding skills are spot on,
take your coding skills next, even ifyou start consider approaching a college or university to see
from the beginner level again. Often, an online course follows a if they have need for a programming language teacher, perhaps a
strict coding convention, so if you're self-taught then it might be part-time or evening course. If not teaching, then consider creating
worth seeing how other developers lay out their code, and what's your own YouTube how to code channel.
considered acceptable.
LEARN C++ ....--
....---.
Learn to code tnteracttvely, for free. IN ONE VIDEO
.
....., ...�
- .
.,....,.......p
- - - --
e;
a••
••---• _..,. ,.
-- --
-
......
'
'
'
I
a...
...-
--. .
"'-""" � ...... � ...... O:Mhoeu-��
< _ /> "'
·-- -·
--- ..
,_ ___
----
a-....
...
.,..
.
--·
-· r ._...__.., ..,..
e
--·-
..
•
····� � .
.
- .
···'·
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
STEP 7 Get sharing, even if you think your code isn't very STEP 1 0 Contributing to hardware projects is a great
good. The criticism, advice and comments you resource for proving your code with others and
receive back help you iron out any issues with your code, and you learning from other contributors. Many of the developer boards have
add them all to your checklist. Alternatively your code might be postings for coders to apply to for hardware projects, using unique
utterly amazing but you won't know unless you share it. code to get the most from the hardware that's being designed.
II rl cl I H cr ' '' ' II 'il ,'
I )
' '
"
(
'X 6
r
.1 �J 'I /\ ,
· II F- I
.' '' .
I
I .J
' ' ( I
'
'I ''
·
.' ;I I! "
" II I.
'
' I •
',I f ... I'
I'
1 I I . '
'-a m mo n Te rms
Trying to include definitions for every programming language would require many
more pages than we have here. However, we have created a list of some of the most
common terms you will encounter as you get started on your coding journey. As you
gain experience and try new things, your coding vocabulary will naturally expand.
DJANGO
A Full stack, also known as a
software stack or bundle, is a
Foundation. i CALL j set of software components
j To run the code in a Function; ! A free open source web needed to create a complete
AJAX j also referred to as "running", application framework written web application.
AJAX stands for: asynchronous j "executing" or "invoking" in Python that follows the
JavaScript and XML. It is a set of FUNCTION
j a Function. model-view-controller
web development techniques . (MVC) framework. A set of instructions that are
utilising manyweb technologies on j CLASS written once to obtain a particular
DUMP
the client side in order to create ! In Python, a template for result and can then be used
asynchronous web applications. j creating user-defined objects. j A list of data that is saved if a whenever necessary by 'calling' it.
! Class definitions normally j program crashes, often as a
G
API ! contain method definitions j text File. lt is very useful for
An API is an application j which operate on instances of ! diagnosi ng problems.
programming interface. It is a set ! the class.
of routines, protocols and tools
GIT/GITHUB
for building software j CODING A micro web application
j Coding is the act of computer Framework written in Python and
applications. APis express
software components in terms j programming in a given EXECUTABLE based on the Werkzeug toolkit
j coding language. j A program, usually a single file, and Jinja2 templa te engine.
of their operations, inputs,
outputs and underlying types. j ready to be nun.
j COFFEESCRIPT GUI
j EXPRESSION General User Interface, refers to
B
j CoffeeScript is a programming
j language that trans compiles 1 A piece of syntax which can be the 'Front end' of a piece of
1 to JavaSCript j evaluated to some value; An software that the end user
BACKBONE.JS j accumulation of expression actually sees and interacts with.
i COMPILER
Backbone.js is a JavaScript j elements like literals, names,
framework with a RESTful JSON
interface and is based on the
j This is a program that takes the
j code you have written and
j translates it into the binary ones
j attribute access, operators or
function calls.
H
model-view-presenter (MVP)
j and zeros of actual machine code. j EXPRESS.JS
HAML
application design paradigm. HTML Abstracti on Markup
j Express.js is a Node.js web
j CONCATENATION Language is a lightweight markup
BOOLEAN SEARCHING j application server Framework,
! Combining two things together, language that's used to describe
Boolean searches allow you to j designed for building single·
j such as two lists or strings of text. ! the HTML of a web document.
combine words and phrases page, multi-page and hybrid
using the words and, or, not !
web applications.
'IJ I1 J k i
</>
I. ''
' .
.
Common Coding Terms
I I, I' '•
.lJ I . . ''
r\ •• I
,. l I' '. . II '- .
... I j .I I.
I I '
HASHABLE
J N s
.
:
.
An object is hashable if it has a ..:
hash value which never changes . .
during its lifetime (it needs a JQUERY � NESTED SANDBOX
. .
-hash_0 method), and can be : jQuery is a cross-platform � When one thing is contained . A place to run a program for
compared to other objects. JavaScript library designed to � within another it is said to testing and experimenting.
simp�fy the client-side scripting of :. be 'nested'.
HTML HTML.jQuery is the most popular
. . SASS
HyperText Markup Language, �. NODE.JS .
Sass is a scripting language that
JavaScript library in use today.
commonly referred to as HTML, � Node.js is an open source, . is interpreted i nto cascading
is the standard mark up language JSON : cross-platform runtime Style Sheets (CSS). SassScript is
used to create web pages. This is A fo rmat for transmitting � environment for developi ng the scripting language itself and
often the very first techn ology information between locations � server-side web applications. consists of two syntaxes.
that beginners to web that is based on JavaScript. Many : Node.js applications are written .
� in JavaScript and can be run . SLICE
development will learn. APis use JSON.
. An object usually containing a
� within the Node.js runtime on .
L
HTTP REQUEST � multiple systems. ..
portion of a sequence. A slice is
.. created using the subscript
HyperText Transfer Protocol is an . .
!0
application protocol for notation, [] with colons between
distributed, collaborative,
LAMP STACK numbers when several are given.
. ..
hypermedia information LAMP is an archetypal model of
web service solution stacks: � OBJECT ORIENTED . SOFTWARE
systems. HTTP is the foundation
Linux operating system the [ PROGRAMING (OOP) DEVELOPMENT KITS
of data communication for the ,
World Wide Web. Apache HTTP Server MySQL . A 'software development kit' or
� OOP is a programming paradigm
.
,
I
system and the PHP : that are data structures containing . for the creation of new
programming language. � data in the form of Fields, oflen . applications for a specific
,
p
Python is an interpreted development and distribution. .. .
..
..
T
language, as opposed to a
. LOGICAL OPERATION .
compiled one, though th e
The use of simple Boolean logical � PHP .
distinction can be blurry because
such as and or and not. � PHP is a server-side scripting
of the presence of t he bytecode .
,
. TYPE
� language designed for web . The type of a Python object
compiler. . LOOP � development but also used .
A piece of code that keeps . determines what kind of object it
INTERPRETER � as a general purpose . is; every object has a type.
running until a certain condition
Some languages do not need a . �. programming language.
is fulfil led; or is n't fulfilled in the
compiler but instead use an
'interpreter' Lhal Lranslates to
machine code as the program .
case of an 'infinite loop' that will
crash the system running it.
[ PYTHON
� Python is a widely used general
.
.
v
� purpose, high level programming VALUE
M
.
is run.
� language. Its design philosophy .
A piece of data that can be
lOS SWIFT .
� emphasises code readability and .
contained inside a variable. Every
iOS Swift is a multi-paradigm MONGODB � its syntax allows programmers to value has a type.
compiled programming : express concepts in fewer lines
MongoDB is a cross-platform VARIABLE
language created by Apple Inc document oriented database.
� of code than would be possible .
.
� in languages such as C++ or Java. A way, used by many
for lOS, macOS and watchOS and : Classified as a NoSQL database. ..
.
programming languages, to
tvOS development.
R
. MVC ..: store a piece of data that can
ITERATION ..
.
then be modified at any time.
Model-view-controller (MVQ is a . .
A sequence of instructions that software architectural pattern for
[
w
RECURSION
are repeated. For example to implementing user interfaces. It
� When something refers to itself. .
,
www.bdmpubl ications.com
· -· - · -·-
G
aGuilaX
debooky
PLUS
(++
Learning a programming language isn't C++ is one of the most powerful, high performing
and efficient programming languages you can learn.
easy. Where do you start? How much does Web browsers, games, applications and even entire
operating systems are coded using C++, which makes
it cost? What do you need? What language
understanding it a highly sought after skill to have.
do you start with? Questions such as these Where do you begin though? We look at how you get
are always the most asked when it comes started with C++ on Windows, macOS and Linux and
how you get from entering a few lines of code to having
to programming; and that's even before something appear on your screen.
you start to code.
Coding on Linux
With Coding - Tips, Tricks & Fixes we Linux is an open source operating system that's a
superb foundation for any would-be programmer to
take a look at four popular programming build on. It's free to download, install and use; with it you
languages: Python, C++, Bash scripting can use all of the popular and mainstream programming
languages through a variety of different front-end apps.
and FUZE BASIC on the Raspberry Pi. There
However, Linux is also great when it comes to creating
are tips on how to get started with each, scripts. With scripting you can create useful, everyday
programs to help back up a system to a remote location,
where to get the latest versions of the user-interactive code and much more. By using the
languages, what you need and how to Bash Shell in Linux, you can interact with the entire
system and its users, as well as any Python code you've
enter your first few lines of code.
already created.
There's plenty within these pages for More Tips, More Tricks, More Fixes
beginner and intermediate programmers, With this book, you can learn how to become a better
coder, how to manage your code and structure it and
so dive in and get coding.
how to identify pitfalls and problems before they even
arise. We've even thrown in some great Python coding
This guidebook can be used with the following examples for you to type out and try, use, improve and
programming languages: extend into your own coding projects.
I' python 01 m 6 � !\ Linu x Discover how to work with code and make coding
work for you.
""'111111