You are on page 1of 148

Discover more of our complete

manuals on Readly today...


-
� ,..- -­
The Complete �.;: The Complete �-=--
� The ---- ----,
-:-
Complete

�thon Ubuntu LinuxMint


&C++ Manual Manual
Manua •
... ...

The Complete
Building Your
Own PC
Man al
-
-
-

The Complete � The Complete

Android Google
Manual Manual

--

The Complete

Outdoor
Photography
Manual
� BDM's i-Tech Special

Tips, Tricks & Fixes


Your guide to better coding
Everything we interact with in our modern digital world has been programmed
by someone. Whether you're sat in front of your computer, changing channels
on your TV, sending someone a text or even putting the washing machine on,
there's some form of programming interacting with you.

Programming is everywhere. From clicking an icon on your desktop to open


a web browser to calculating the vast distances between the stars. It's one of
the most important skills you can have in the modern world and it's only going
to get bigger as we move into a new generation of interconnected devices
and mediums.

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

N.frn\'leb<Y�� W.weprideOI.I"Sffw:sonthequalityofd'leinfotmatJonwepnMde,MadeDog hOI'n$bWJ'tmtdsm


---�-F (O'"""t-11<! Medrolllmlb!dreserwsthe'ri<)ht:notto�heklrespo:lStieforill'lflisuUsOf -........-.� .... . __aod_..._., chonjje...
... .
W\olltQIII'«<ftfowdwtfwlu.t2Jilofthis.pWiicabon.Outtothenat\nofthe lnfOt"''N!bbO''...COfT'tU.tbrl'ltolPf¢SomtClilfMrlt"""�bftn�
--�s.,mo. oo•-umc«� -... -tt.c____......_..._....,...., ..-..-........-..........---·•<r«>tt.c
2.(ast��....__-.�t. t.ondon£.CtA9f"'
�tJIRMobiaftOS.ttfl'l"'lii'tStht�S'SOit�to� �ol cuNfflbpnotto�
--� lhtta!!!l;..,o/ct.booil..andbcontentfot�,...� ���o.Mf'd
Ane RC"W
apoieo,.� l...f'!Wfd YROt '-¢:ern onU.hontM'Idlwdt<J:N«l!t�solelyfot�pufJIQ5fSandMenotrepr�
For.U�rtisin91ndswomotJonll�COf\UCt: o#mrttnt.w.�•potHltJal�-e-:stoc:hed:lsmgpnortop!.rd\as.etot
� � 1\'.(1!"' �oladJJ.ii!COf'ltelt.AiecU:orialopn.onh«mts.ttwr:olcne�.san
lf'IIISMcflNI.and•notrepresertatiw-dtJwpubishefor¥tfofbaffi��
iatf'S.
tht""*"'hold:5f'IQ r�inrtg:ardtoeditofWlopinionand�

• • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••

a @bdmpubs � BDM Publications www.


bdmpu blicatjoos.com
• Coding and Programming 36

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

BDM's 1-Tech Special I Volume 27


70 Creating Bash Scripts- Part 3 108 Coding with FUZE BASIC- Part 1
72 Creating Bash Scripts- Part 4 110 Coding with FUZE BASIC- Part 2
74 Creating Bash Scripts- Part 5 112 Coding with FUZE BASIC- Part 3
76 Command Line Quick Reference 114 Using a Breadboard
78 A-Z of Linux Commands 116 Using the FUZE 10 Board
118 Using a Robot Arm with FUZE BASIC
Working with Code 120 FUZE BASIC Examples- Part 1
122 FUZE BASIC Examples- Part 2
82 Common Coding Mistakes
84


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

wwVLbdmpyblicat!oos.com Coding - Tips, Tricks & Fixes


Coding and Programming

Being able to program a computer is an


amazing skill. While you use a computer,
you're interacting with everything the
programmer has created. Surfing the
Internet, playing a game, using a word
processor and even just looking at the
desktop and double-clicking an icon:
all have been specifically programmed
by someone. Imagine being able to do
this yourself?

Don't kid yourself though, you won't


become a programmer in 24 hours,
overnight or even in thirty days . Learning
how to code, and how to code well, takes
time and patience as well as learning all
the tips, tricks and fixes available to you.

Coding- Tips, Tricks & Fixes helps you


start to become a good programmer. We
have plenty of everything within these
pages to inspire, discover and learn. We
cover Python, C++, Linux scripting and
programming on the Raspberry Pi with
the FUZE project.

Read on and let's become better coders.

• • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••••

8 Choosing a Programming Language

10 Learning to Code- Study Tips

12 Coding Tools and Resources


14 Being a Programmer

BDM's 1-Tech Special I Volume 27


Index

www,bdmpublkatjggs.com Coding ·Tips, Tricks & Fixes


Coding and Programming

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.

•• • •• • • ••• • • ••• • • • ••• • • • •••• • • • • •• • • • ••• • • ••• • • • • • •• • • •• • • • • • ••• • •• • • • • •• • • •• • • • • • •• • • •• • • • •• • • •• • • ••• • •• • • •• • • •• • • • • • •• • • •


• •
• • • •
• • • •
• • • •

• CHOOSE YOUR LANGUAGE • •
LEARN WHAT BEING A CODER MEANS •

''
• • •
• • • •
• • • •
• • • •
• • • •
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
• •
-
• •

-

• . .

PUT THEORY INTO PRACTICE -



• .

• •
• •
• •
• •
• •
: If you are completely new to coding, learning any programming - =.=:..·.:=:....::::.. - - •

; language can seem like a huge task. Getting completely bogged -


--·-··- ·-
---·------
--

:• down in pages and pages of code examples and explanations ---


-
--- •

: is not likely to make the learning process much fun. In our


- -- -- •


• •
: experience it is often better to learn through practice, or to "

; choose a task you want to perform with code, and then work

• •

; backwards to learn how to complete that task. By learning small ll:=--- •




:• chunks and repeating what you have already learned at differing -- •


:• intervals is called Spaced Repetition. o ..... ______



• •

• • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••• • • • • • • • • • • • •• • • • • • •• • • • • • • • • • • • • • • • • •• • • • • ••••••••••••••••••••••••••••••••

BDM's I·Tech Special I Vo lume 27


Learning to Code- Study Tips

• • •• • • • • • •• • • •• • • ••• •• • • ••• • • • • • • • • • •• • • • • • •• • • • • • ••• • ••• • • • • • • • • • • • •• • •• • • •• • • • •• • ••• • •• • • • •• • • • • • • ••• • •• • • •• • • ••• • • • • • •• • •


• •
• • • •
• • • •
• • • •



SET UP YOUR ENVIRONMENT •





STUDY IN DIFFERENT WAYS •


• • • •
• • • •
• • • •

:

Take some time to look at the things coding •

: Sometimes, though admittedly not always, it's a good idea to get a more


• •
:• languages are used fo r; so if you are interested : : rounded view of computer science before you dig down into more specific •

;
• •
: 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- . . .
• • • •

1:1[ ··---·- ....


• •
-- -..,..



• . • • •
OtbU9
• .... •

•�
• • • •
• NtwA,ptan� (ltu by A.ppUatlotll • 'sl • • •
•if'ltuby

• � 'd : •




,Jt ltllbv Tlwucl - J bftt:e»• • 'sl :
-
• •
• • •

• � lubo( Ti\rtael J (Jitt:e»• • '« : • •

• # a..brTtwucl J• tlrt� at c�cM.>nto.


• •


... +'sl • • •
• • •
• 5/I.IH't"S /bUit/�S/A.ptaN,��-A,pc ...•_rt • • •
• • •
• 5 1\MI"'/blllt/f'rOJtC.U/A.pt.at\af"-twAPt .. ••ft • •


• •

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/"-� • • •
.
• •
.




��==�� •


•••• • • ••• • • •• • • • •• • • • • • ••• • •••• • • • •••• • • •• • • • •• • ••••• • ••• • • • ••



• • • •
• • • •


• •
• : •

LEARN FROM OTHER CODERS


• •
• • •

'
• •• • • • • • • • • • • • • • •••••••••••••••••••••••••••••••• • •
• • •
• • •
• • •
• • •

: The fantastic Stack Overflow is one of the best places to find


• •
• •

;
• •

answers to problems and to ask your own questions to other •

,.

... .. . . ... . . . . . . . . . . . . . . . . •••..••••................. ...... . . •


: coders around the world. There are many other language· •

• •

:• specific forums online but whichever site you pick, don't be
• • •

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 • - - -
• • •


• • - • - .,....._...,.___ _ ·---­

: progress when in the process of learning to code. This is


• - •
··--
• ---- -- -
· -- --
• •
• • • - --· •
_u_.._....._-
: especially true of self-imposed d eadlines, and at this point • •
-
• • . ---- •
• • --- - - • •
• • • •

: they almost certainly will be. as they can damage your


,
• • •
• •
. .. . . ... . . . .. . . . .. . . ... . . .. . .

: progress when not reached. Trying to rush through any st age •

. . .......... . . . . . ..... ..... ... . .


: of learning to code will not do you any good, and may make





• •

: later stages harder if you do not fully understand the basics.




• • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • •


• • • • • • • • • • • • • • • • ••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • •• • • • • • •

wwVLbdmpyblicatlpos.com Coding ·Tp


i s, Tricks & Fixes
Coding and Programming

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 . ___,. - -

Qt-· '-M ... ..,_ ' """' ....


You can use GitHub to create a personal project,
whether you want to experiment with a new
programming language or host your life's work. ...
. -- •
..

Any kind of file can be up loaded to GitHub but it's 151 , __ . d3 � n���W...... •

designed particularly for code files and is hugely --� ·-

-- _..._.
popular. It provides access control and several · -- -·
•• ___ ,.......
.. ,_. ·-- ·-
collaboration features such as bug tracking, feature •• --- ·--

requests, task management and wikis for every ·- ....-....


. .. "--

·- --.- .....---- -
possible project. GitHub reports having mo re .... . --

...01..... ..... ·--


--
·­
than 14 million users and more than 35 million ·­ ·-·
- .


-- ---- --.

repositories, making it the largest host of source a-.. --·-- -

code in the wortd.

BDM's i·Tech Special I Volume 27


Coding Tools and Resources

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.

One of our favourite IDE's is Aptana Studio 3. Aptana


allows you to develop and test your entire web
application using a single environment. There's
support for the latest browser technology specs
such as HTMLS, CSS3, JavaScript , Ruby, Rails,
PHP and Python.

STACK OVERFLOW SNIPPETS


� overf tow aml lmll!:l�*'d§•• Stack Overflow Snippet is a pro grammi ng term for a small region of reusable source
e!lszac:;k 1411
¥1I
is a question code, machine code or text. Snippets are often used to clarify the
.
-
0 ·-..·-··---··...--....... -
-
and answer site meaning of an otherwise "cluttered" function or to minimise the

·

,_.,. for professional use of repeated code that is common to othe r functions. Snippets
--- -

.... - ---� .. ----·-


--
-..... and amateur is a powerful application for Mac and Windows that stores the most
"'"' "'
22
-- · --
'

- programmers. valuable pieces of code that you can reuse in different projects
....__
., --·---·-·-· ----·-- -·
_

-·---· -- -·--
-
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 ,

the site is building a library of detailed answers to every question


- - ... -

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.

The key to getting the most from Stack Overflow is to focus on


questions concerning actual problems you have faced. Include
details about what you have tried and exactly what you re trying to' CockStloppd ��$tlippm1 'Wt'lf�?
do. Tags make it easy to fi nd interesting questions. All questions are � . ....�--· �..._.....,.._.., � ......-
- ... ....
____.. ,.,
.... ... ....
..
-�- ---. .. ..
... ..,.,.. . -.� ....
-....�-....�
•• .. .... . ..... _.,. .....,..
ta gged with their s ubj ect areas. Each can ha ve up to 5 tags since a
oa..�o ...c. _,.,.... ..... ......
, oot..l _,.,... . ,"_
... .,.. • ... """'m.•..,_ ..........._,............

question might be related to sever al subjects.

wwVLbdmpyblicatloos.com
Coding and Programming

Bei """ a Pro ra m m e r

Volume 27
Being a Programmer

MORE THAN CODE


For those of you old enough to remember the '80s, the golden era of home
computing, the world of computing was a very differ ent scene to how it is
today. 8-bit computers that you could purchase as a whole, as opposed to
being in kit form and you having to solder the parts together, were the stuff
of dreams; and getting your hands on one was sheer bliss contained within
a large plastic box. However, it wasn't so much the new technology that
computers then offered, moreover it was the fact that for the first time
ever, you could control what was being viewed on the 'television'.

Instead of simply playing one of the thousands of games available at the


Times have changed since programming in the '80s, time, many users decided they wanted to create their own content, their
but the core values still remain. own games; or simply something that could help them with their homework
or home finances. The simplicity of the 8·bit home computer meant that
#includr<,tdio.h� creating something from a few lines of BASIC code was achievable and so
dntlUOf"<do\,h>
•tntlud�<itdlib.h> the first generation of home-bred programmer was born.
•tn�lud�< uni1 t
void cetup()
{
tcxtc:olor(8LACX)J From that point on, programming expanded exponentially.lt wasn't long
te�tbatka�ound(lS);
cl,..scr(); before the bedroom coder was a thing of the past and huge teams of
• "indo.(11,2,78,3);
11 CPf'{ntf("PrtU X tO tdt, Pr�n SpMIP I:CI ]�"); designers, coders, artists and musicians were involved in making a single
11 ldndoW(Gl,l:,H,J);
1 C:Pt"intf("SCOIU "), game. This of course led to the programmer becoming more than simply
4 tdnc:br(1,25,18,25);
for(tr. ...;xc79... ....) someone who could fashion a sprite on the screen and make it move at the
cpdntf ("n" );
tu:tc:olOt"(t); press of a key.
I

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.

Finally, there's also a level of creativity needed to be a good programmer.


Again though, you don't need to be a creative genius, just have the
imagination to be able to see the end product and how the user will interact
with it.

There is of course a lot more involved in being a programmer, including


learning the actual code itself. However, with time, patience and the
determination to learn, anyone can become a programmer. Whether
you want to be part of a triple-A video game team or simply cr eate an
automated routine to make your computing life easier, it's up to you how
far to take your coding adventure!

Coding ·Tips, Tricks & Fixes


Say Hello to Python

In the '80s, during the golden age of


the home computer, students, home
computer users and enth usiasts all
strove to program their computers
to perform a certain task. These
early coders, at the frontier of home
computer programming, created
amazing things with their code.
Regardless of whether it was simply
having the computer scroll the word
"Hello!" down the screen or creating
one of the many thousands of hugely
entertaining games of the time, these
pioneers of programming created a
generation of digital imagination.

With the rise of the Internet and


modern computers and consoles,
home programming became
something o f a lost art. However,
the bedroom coder has returned and
thanks to the power of Python you can
become part of the new generation of
home computer programmers.

It doesn't matter if you fondly recall


the '80s or barely remember the
PlayStation 2, this section will help
you get started with the latest version
of Python and what you need. We
cover Windows, macOS and Linux, so
read on and let's see how to start you
off on your Python adventure.

• • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••

18 Why Python?
20 Equipment You Will Need
22 Getting to Know Python
24 How to Set Up Python in Windows

26 How to Set Up Python on a Mac


28 How to Set Up Python in Linux

30 Installing a Text Editor

BDM's i·Tech Special I Volume 27


Index

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


Say Hello to Python

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

: ; ' Bomb<· CUI


. • Tl'"lllf • (d: lui'I ""\Cttllob\Ao o <Oy(ttl .cpp windDWli-1 757] { elCilonple
•} r:l 1@
Ln 639, Cl
o iS
¥. �I'll GJI

I!Joombs IIJ� Se tCursor(p. y);


!!Jc�lllb G�1t Ctrl:: ChtldGotFocus();
liJ C�ICore �����\pn; )
liJ RichText 0 <prj-aux>
liJ Pcfllf ow • <dt•aux> void ArrayCtrl::ChitdLos tFocus ()
llJ Drew • <tetl1)•aux> {
llJ Core
if(cursor >= 0)
RefreshRow(cursor);
Ctrt:: ChlldLostFocus();
# EckC�I.h C Al(oys.cpp " }
C EckF;cld,cpp # Rll:hText.h
# ToxtEck.h CRll:hTtxt'l low.cpp void Arr ay Ctrl ::Paint(O raw& w) {
C Toxt.cpp CP.Ofi"C)t.cpp LTIMING("Paint•);
L cit .cpp
C n.E CHelp.cpp Size SlZ8 = GetSlZe();
C OocEck .cpp Rect r;
# o.teTimeCl�. h
r.bottom = 0;
# SaoiBer.h CDoteTlmeCl�.cpp
CSaoiBer.cpp �ll;or
boot hasfocus = HasFocusOeep();
# -Cbl.h # Bar.h
int 1 = GetLineAt(sbl;
int xs = -header.GetSCroll();
C-Cbl.cpp CBar.cpp int j s;
# An ayCbl. h c� - ­ for(JS = 0;JS < col umn.GetCount() ; JS++) {
e AneyCb'l..... c r-.- 1nt cw = header .GetTabWldth(js);
# [lr()p(holoh o. C T...,.cp .,_ p if ( ( xs + cw ver t grid + (js
· == column.GetCount I l )) >=0)
e ()oql8ox.cpp # St
........ . h break;
c Orqolist. cpp c St-.cpp xs •- cw;
C�.cpp .TobCtrl }
C[lr()p(holoo.cpp # TebCbl.h Color fc = Blend(SCotortnsabted, SCotorPaper);
#�.h C TebCbi.cpp if (! IsNuttlill
CSIU.cpp � T...,Cloi while (l < GetCOunt( l {
# 51*ter. h # TreeCbl.h r.top = GetLineY(i) sb; ·

CSgltter,cpp if(r t op > stze.cy) break;


.
C TreeCbl.epp
r.bottom r.top + GetLlnecy(t);
=
cfT-.epp .DIQ(olor
# SlldetCb1. h # 019Color.h
int x xs; -=

for(int j js; j < cotumn.GetCOunt(); J++ ) {


=

e SlldetCb1.epp c 019eo1or.cpp in t cw = header.GetTabwidth(j);


# Colom'U<t.h C�.cpp int em =
column[J).margln ;
CColoiMUst .epp C�-<PP if C m < c 0)
# Proq-ess.h •fll.sd em =
header.Tab(j).GetHargtn();
CProq-ess.cpp # FloStl .h if(x > size.cx) break;
# Al<oys.h C-�t.cpp r.left = x;

BDM's i·Tech Special I Volume 27


Why Python?

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
,

variables objects, arithmetic, subroutines, loops and countless more


, superb learning and educational tool.
interactions. Whilst it's not as streamlined as a Low-Level Language,
which can dea l d irectly with memory addresses, call stacks and
registers, its benefit is that it's universally accessible and easy to 4-0
70
learn. 71
?e
e0
.
I riDe· : I •• �0
2 mport java . lang.reflect. * ; 100
3 110
4 lass Invoke { 1lii0
5 public static void main( String [) args ) { .., +1
6 try { 130
.., -1
class c = class.forName( args[O) ); 13e
8 Method m = c.getMethod( args[l), new class 14-0
[) {} ); 1e0
9 object ret= m.invoke( null, null ); 180
10 system.out.println( 1ee
11 "Invoked static method: + args[l)
"
190
12 + " of class: " + args[O) lii0 0
13 + " with no args\nResults: " + ret );
210
1-l } catch ( classNotFoundException e ) { liili0
i
300
15 II class.forName( ) can't find the class
16 } catch ( NosuchMethodException e2 ) { 0 OK, 0:1
17 II that method doesn't exist
18 } catch ( rlleg alAccessException e3 ) {
19 II we don't nave permission to invoke that BASIC was once the starter language that early
me thod
?0 } catch ( InvocationTargetException e4 ) { 8-bit home computer user s learned.
21 II an exception ocurred while invoking that
method
22 system.out.println( prlnt(I'W6Wl(8])
att�pts • len(tWtCiKAN)
23 "Method threw an : " + e4. � 1

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_�)

Java is a powerful language t ry:


pl•yer_auett • str(t�p�t("\�Plt•s• select • l�tte� bt�tn A·Z" • "\n> ")) .
that's used in web pages, txcept: • k • I
prl�t(.lhat ts not �•ltd l�pvt. Pl••�• try ac-t"· · >
set-top boxes, TVs and even contil'!u•
cars. du:
if �t pl•y•��ss.is•lphe()l • �k t� tnpu� 11 • r�r� Al�o c �c�� '
�ll'lt(·r�at Is ftOt
• l•���r. PI�•'� t� ...1".·)
C:OI'Itl�
�ltf IHI(pla,..��U) , 1: • II. - e-tt�
�itf t(.That l1 -.rot t� � lottt�r. Pl�-�� �� -a•Ln.·)
COftti�
dif pi�«,....J'�II lll I,_.UH.ln t ..
I"Sl • t rtt ha t I:IHa 1s.ud
P'"l•t(-.,., I'Yv� •1.-...ty ponwd tllat lf't-tf'r. Pluw try ac•tJt.•)
contl-

....

for lette-r ia ,..,.(., lttt(c-.•n·•"..IIIOI"d))l


lf p
l
� JVtu - cJto�•"..
liiiOrct{latt�J:
.ord�sscd(l•tt•r] • pl•7•�-IV"II • epl•• •11 letters in �e �se


Python is a more modern take on BASIC, it's easy to learn
and makes for an ideal beginner's programming language.

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


Say Hello to Python

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.

WHAT WE'RE USING


Thankfully, Py tho n 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 easi ly start using Python.

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.
- -
...- -

D TIME AND PATIENCE


- -
- -
- -

-
- -


·­
-

--·
- ... - ..
---
- . 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.

BDM"s i·Tech Special I Volume 27


Equipment You Will Need

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.

•••••••• • • • • • • • • • • • • • • • ••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••• • • • • •• • • • • • • • • • • • • • • • • •••••••••••••••••••••• • • • • •••••• •



• •
• •
• •

RASPBERRY PI

• •
• •
• •
• •
• •
• •

: The Raspberry Pi 3 is the latest version, incorporating a more •




: powerful CPU, more memory, Wi-Fi and Bluetooth support. •

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


to build and program. You can find more information on the





: FUZE at ww

Juw z
e,
c o,yk,




• •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••

••••••••••••••• • • • • • • • •• • • • • •• • • ••••••••••••••••••••••••••••• •

• •
• •
• •

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

• • •

: programming and even as a desktop computer.


• •



• •

the most from
• •
the Raspberry
• •
• •
• •
• • •
. .. .. - ..- ...
- - ... • •
Pi too.
• •
• •
• •
• • •
·-·-..--
··- ·-·-"'­

.
• •• • • • • • • • • • • • • • •
• ··- •



� -
......-....
..
.
r:: ::...;.
--
..
-:-..
··'1.
"". _
-n
...._
-
" ___
---
...,., -·
--- ·

·- ·--'-



. •
--
-
• •
·---·-·-
• - •
. = •
-
. - •
• - •
. - •
-
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •

• • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••• • • • • • • • • •

wwVLbdmpyblicatloos.com Coding - Tips, Tricks & Fixes


Say Hello to Python

'-'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 •

is a list of instructions that


• 3 Add tee •illilitres of ailk.
: •
• • 4 Bake for half an hour.
: a computer follows. These •
• Put 100 grams of self-raising flour in a 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� • • •"

• at Centrum Wiskunde & •

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• •

----·--- •lf UllrM �


I • If
• • »"' lfSTAU ��.. u. ....,..,.,�.,
.
.. ,.. , - )
• • ,. .. .......
• • ..._WIC uM .
• • ,,.. '.....C:*"il
.•

• •

• II

...
,
,._ .. ,.
• •
lliUttiN' .. ,.
• •
•1Uc • ,.
• •
wnu. •ul•H
• •
• • ..., ...,.IC'I-.
...
• •
• • ,.,.. "'.(...1)
• •
• •
• •
• •
• •

• •• • • • • •• • • • • • •• • • • • • • • • • • • • •••••

BDM's I·Tech Special I Volume 27


Getting to Know Python

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . ............... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
.

HIGH-LEVEL LANGUAGES ZEN OF PYTHON


Computer languages that are easy to read are known as "high-level". Python lets you access all the power of a computer in a language
This is because they fly high above the hardware {also referred to as that humans can understand. Behind all this is an ethos called "The
"the metal"). Languages that "Ry close to the metal; like Assembly, Zen of Python." This is a collection of 20 software principles that
are known as "low-level". Low level languages commands read a bit
- influences the design of the language. Principles include "Beautiful
like this: msg db , 0xa len equ $ - msg. is better than ugly and "Simple is better than complex." Type
"

·--
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
-
·­
-

--- ·-­ COMplex is bett•r th•n co.plsc•ted .



·-­
-
F l�t ts b•tter th�n neste
-
-- ··- d.
- -
, , ..,.
·-- ____
sparse ts better than dense.
-
-
-·- - Re•dability counts.
-
- ·--
·-
Sp•c••l c•s•s •r•n't spt<l
•l tnou� to brt� the rules.
-·-
- AlthoUifl prKttCihty btns purny.
- � ..... '
- --
.

on w:a.n)O:
T� "copyr:a.�t",
>>>

,,
let d ) ) e .: wi::l : �
...

! "copyraltlt'". •cr•dits• '"lactnM{)" f� .of'• tnfo.-..uon.


Type •copyrtObt•. •cr��·· or •11c.a.• Ct'" for �re 10Xor..�1oo. ,.,.,. pr&nt ( P...•
hor'l ). 1 )
Python ).X as M!E$Mf

wwV£bdmpyblicatlpos.com Coding - Tips, Tricks & Fixes


Say Hello to Python

....., 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.

INSTALLING PYTHON 3.X


Microsoft Windows doesn't come with Python preinstalled as standard, so you're going to have to install it yourself manually.
Thankfully, it's an easy process to follow.
• • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • . . ..
.. .. .. .. .. . . .. .. . . .. .. . ..
.. . . .. . . .. . . . .. . . . .. . . . . .. . ..
. . .. . . .. . . . .. .. .. .. . .. .. .. .

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.

Download the latest version for Windows Optional Features


P-1 OOC\U'f*'ltltJOn
Download Python 3.6.2 Download Python 2-7.13 .,
,_________ ..

1'MltJik p whiCI'I can downleacl 1nd NU o�e� Pytno., pack.ageS.


Wondenng which version to u�? Heres more about the difference
(;21 tcVUt 1nd IOU
betwtAen thon l dnd j_

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)

Want to h�lp test dev�lopment versions of Python? Pre releases


python
�-

windqws Next Cancel

• • • • • • • • • • • • • ••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • . .. . . . .. .. .. . .. ..
.. .. . .. .. . .. . . .. . . . .. .
... . . .. .. . .... .. .... ... .. .. . .. . .. . .. . .. .

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.

Install Python 3.6.2 (32-bit) Advanced Options


SNc ....._.,. how to m
1.. Pythc:w'l wth �fit 5E"'1.-..orchoose
Cult�to Mlbator ...,.fNb.Ka
. laAssoo.tte6Ift ..,u.. PythonCreqwa thtpy�
IaCruteShotteuukif lr'lltlitd �tlOnl
IaAdd Pythoft to �t �
Iaf'r� &t�d M:wwy
�at.,....,...__.__
, , ...,.< ��t• ...,. ,. ....c......
c .... 0...__""-'!1 -
CJ� dtbt.lt blnltlel(requ.'..�201 s 0( Ill«)

� 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

BDM's 1-Tech Special I Volume 27


How to Set Up Python in Windows
�>
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • ••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • •

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.

Setup was successful


o n w 1- n !!
Speoal tNnU to M¥tc H� wi1hc,_,,. whoseyears of Type •eopy�1ght•, •e���·· o� •11c�n•• C J • toe �c• 1nto��1on.
lrooly ,,.,.., Window$ t>;p«t1... Py1tlo<l lor Windows would >»
still bt Python for DOS.

Ntw to Python7 S1.wt w11h 1M o


nhn
e t
ut alJncl
oo
dpwmtnt•tloo,

$ Oi!.dbfc p.tth length limit


(.he� you1�fwle"'''''9Uf.U.On to 1llov. p1ogt1ms, tndud1ng Pytflon.to
'YJIMI11'11 260diowtttf'f•MAX..PAlW'm•ttltOft.

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 <-- -

$ Disable path length limit


.. your rN<h•n• configurotoon to •11-p<ogrom� including Python, to
Chang
byp1utho 260 cha111cttr "MAX_PATH" lom�1t10n.

Close

. ............................. .. . . . . . . . .. . .. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. .. . . . . . . . . . . . . . . . . . .

The command tine version of Python works in


STEP 7 Filttt5 V STEP 1 0
much the same way as the Shell you opened in
Windows 10 users will now Step 8; note the three left-facing arrows (>»). Whilst it's a perfectly
find the installed Python ;>< IDlE (Python 3.632·�·� fine environment, it's not too user-Friendly, so leave the command
,,.,
3.x within the Start button line for now. Enter: exi. tO to leave and close the Command
Recently Added section. The Prompt window.
first link, Python 3.6 (32-bit)
will launch the command
line version of Python when
clicked (more on that in a
moment). To open the IDLE,
type IDLE into Windows
start.

P ld!rl rhOn brt)


I

www,bdmoublkatjggs.rom Coding -Tips, Tricks & Fixes


Say Hello to Python

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..-- ....
·· -

-all l M'I IIU .,._....,_ .


... tor - ....... -
-........ --._-_, .. _ .,.. _
....,.....
....
-
� -- .... ..--
,._.,.. ...,-.
.. . .
•.
.
..
..
- _.,...., _ ._
_
- -
• f.m ......_ .. _OIK.

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


,...... ...,
.,... ,,.. .. .... ... ........ ..
__...........................�..,
.

....... ... ._. OIJC N f ... l..trGIIIMf •


...... ...
_.

. .......
.. ... .. .. ....
.-..,: .... .. .
....... .... ....
.... . . .,. .. ..... .... . .... .....
--- • .,. ,...., ....... ., .. .-filf. ..._ ..,._
..
...
...
....caa ... .,.....,... � ..... .......
.. .,
.......
I'!III IIOd
..,.,_,._
..
_....,
. ....
.... .. ,..
('trP!Jdt ,
._•
•, =
-tii(W"''"'·....-�
..
-�,., ....... .. ,..... ....
. .. ,.,...
,,_�--�-CII:III'r
_ .._
..... --� �.,....... ... .... .... ..... ""' .... ..... ..
..tllflowt!UINIll �_,-�-...,o,N .....
�...,...�_ ......__..,.�- ...,._ .
.... .. ....... .. .... �
- ��� ....... ,..uo.
--------
..

BDM's i·Tech Special I Volume 27


How to Set Up Python on a Mac

• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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.

A� " f"/thon Shell



Python 3 . 1 . 2 (r312: 79360M, Mar 24 2010, 0 1 : 3 3 : 1 8 )
,..
,
. .
.... .. ...
.?II .IIIIIHir [GCC 4 . 0 . 1 (Apple Inc. build 5 4 9 3 ) ] on darwin
Type "copyright " , "credits• or "license ( ) " for mo
re information.
>>> I

wwVLbdmpyblicatlpos,cgm Coding · Tips, Tricks & Fixes


Say Hello to Python

....., 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
.

3.5.2, which is fine for our purposes.


doiVid�david-mint-

File Edrt Vrew Search Termonal Help

BDM's i·Tech Special I Volume 27


How to Set Up Python in Linux

• •• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

wwVLbdmpyblicatlpos.com Coding - Tips, Tricks & Fixes


Say Hello to Python

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.
·-
·­
·­
·­

Clid<Next to a>nlnue, or Caned to exitSe1lJp.


·--
·­
·­

·-

............ ........
.
,.,- . ... .
_.......-.....

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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& ..-
-

· � r.o ....... .... .. 9' d· · -


• ,.,_,M ""•.-. •• W-..-
"'h_.,..
• Lo.ili'GID•...,...•...,. •64ll&orll.M14W

s.M fl"'t Tu"'.Y t.. dolo


rlc 1elM erld ...... for frn. � • kenw �be:
f'tl'llfy
1'10 rilrttd �
hi. for h

·...-
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 ,.. ,.. _ ....
..,_ .

recognises this as code and places the quotes and parentheses. 1

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: ·­

priftt ("lhi• i• r� ") ·-


·- .

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Say Hello to C++

C++ i s an amazing programming


language. Most o f what you see i n
front o f you when you power u p your
computer, regardless of whether
you're using Windows, macOS or
Linux, is created using C++. Being able
to code in C++ opens up a whole new
world for you, in terms of desirable
professional skills and the ability to
code amazing apps and games.

C++ is an efficient and powerful


language that's used to develop
operating systems, applications, I
games and much more. It's used
in science, engineering, banking,
educatio n , the space industry and
much more.

We help you take your first steps into


the world of C++ and get your first few
lines of code up and running. A

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

34 Why C++?
36 Equipment You Will Need
··-··· ··-

38 Getting to Know C++

40 How to Set Up C++ in Windows

42 How to Set Up C++ o n a Mac

44 How to Set Up C ++ in Linux

46 Other C++ IDEs to Install

BDM's 1-Tech Special I Volume 27


Index

wwVLbdmpyblicatloos.com Coding · Tips, Tricks & Fixes


Say Hello to C++

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

C++ code is much faster than that of Python.

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 " ;

BDM's I·Tech Special I Volume 27


Why C++?

....
"'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

l ..co. no ssof'";IN't s <t">Sotd StJH


St
..
e
I'l
t y, f:S SC"'

� Microsoft's Visual Studio is a great,


� free environment to learn C++ in.
other languages mixed in depending on the function) and the likes 'I
I �I
of NASA, SpaceX and even CERN use C++ for various applications, Microsoft"

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.

While Python is a great programming language to learn, C++ puts


the developer in a m uch wider world of coding. By mastering C++,
you canfind yourself developi ng code for the likes of Microsoft,
debian u buntu
Apple and so on. Generally, C++ developers enjoy a higher salary
than programmers of some other languages and due to its � Indeed, the operating system
versatility, the C++ programmer can move between jobs and � you're u ing is written in C++.
s

companies without the need to relearn anything specific.


However, Python is an easier lan guage to begin with. If you're
com pletely new to programmi ng then we would recommend you
begin with Python and spend some time getting to grips with
programming structure and the many ways and means in which you
find a solution to a problem through programming. Once you can
happily power up your computer and whip out a Python program
with one hand tied behind your back, then move on to C++. Of
course, there's nothing stopping you fromjumping straight into
C++; if you feel up to the task, go for it.

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.

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


Say Hello to C++

E'-11 u i men t You


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.
• •
·­

-

·

== •
·---

=:J INTERNET ACCESS


--- -
- · ·--·-
--·-

·- -·-
· "
-
=· a::.:-=-:-::::::=._

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.
--- -

D TIME AND PATIENCE


----

· - 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.

BDM's 1-Tech Special I Volume 27


Equipment You Will Need

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++.

• •• • • •• • •• • • • • • • • • •• ••• • • • •• •• • •• • • • • • •• •• • • •• • • •• • • • • • • • • ••• • • • • •• • •••• • • •• • • • • • • • • • • ••• • • • • • • •• • • •• • • •• • •••• • •••• • • • • • • • • • •


• •
• • • •
• • • •
• • • •



LINUX •





WINDOWS •


• • • •
• • • •
• • • •

• Unux users are lucky in Lhal they already have a compiler and •


• As we've mentioned previously, a good IDE is Microsoft's •

• • • •

• text editor built into their operating system. Any text editor •


• Visual Studio. However, a better IDE and compiler is •

• • • •


will allow you Lype out your C+< code, when it's saved with a •



Code::Blocks, which is regularly kepl up Lo date with a new •

• • • •


.cpp extension, use g++ to compile it. •



release twice a year, or so. Otherwise Windows users can •

enter their code in Notepad++ then compile it with MinGW ­


• • • •
• • • •
• • • •





• which Code::Blocks uses. •

n o � ... ...
• • • •

"
• • •
. ,

,....
• • .. .. •
• • IU 11m ...,.. -�
.... " ""''' ,.,. .
�. .n
.. ..."' ""'"" ,., •
.. '. • • .,,.,, ·- II •O • '- '- • " I> � •
...... ""i. - .�... •
"
_. tltl • • .... � .. .......-'lo··-' •
• ••
0

• • •
------
-
-·- · �,
• • •

• • •


··-·- ­
• • --· •
z--.,_. . ...----·
.
.,_,_ ·--

.. .....
.
·"-··�
• • •
......
. ,. _
. .. _,
• • •
• •
__,..
_... ...
.... .. .
., , . . . . .
. ... . . .. . .. "

--·- ·�· ...... ..
• • •
• • •






. ...... ==-•
• • •

. .....
...... .... ....
••
- . ,.. -
.. ·- ­ •

• • • •

--- ···· ' . . .


• • • •
• • • __ _ __ _ , •
• • • •
• • • •

. ,. .. ..
..
• • • •
- -
• • • •
- - - - -
. ... - · - - ---··
.....
• • • •
-
• • • ·- .. -·-.-- �






• . ..
· ··
=---·---
-
-


• • • •
• • • •
• • • •
• • • •
• •
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • ••• • • • •• • • • • •• • • • ••• • • ••• • • • •• •


• •
• • • •
• • • •
• • • •

• RASPBERY PI - -· -
• • • .. . • I





-
! �� �-
• • • •
• , -·· • • •
• ·­ • • •
• ·- • • •
• •

• 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 •

out using a text editor, then


• •



• •

options for the macOS •

• •


compile it with g++ as you •


• include Netbeans, Eclipse or •

• • • •


would in any other linux distro. •


• Code::Blocks. Note: the latest •

• • •
Code:: Blocks isn't available for

• •
• •
• •
• •
• •





Mac due to a lack of •

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • •

• Mac developers. •

• •
• •


• •





•• 1'- • •o


·-' " ' . .
·-
. •
·-


. -· .
·-
- •
.. ..- -
ec




�-
, _.., ..-


_ .. .. •

--� ••••
...
-
·­
-
-
·-­
�--

!'....
--

-
._...

. , �.=::�: !•:-
: · -

,__
·-
• . -0
-----.,.tlgjl-- ..
- •

...... •

www,bdmoublicatjggs.com Coding ·Tips, Tricks & Fixes


Say Hello to C++

�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.

#INCLUDE <C++ IS ACE!>


Learning the basics of programming, through Python for example, enables you to understand the structure of a program. The
commands may be different, but you can start to see how the code works.
• • • •• • • • • • •• • • • •• •• • • • ••• • • • • • •• • • •• • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

• •
• •






#INCLUDE
• •
• •
• • The structure of a C++ program
• •

File Edit View Search Tools Documents Help
: C++ was invented by Danish • is slightly different to that of
• •

: student Bjarne Stroustrup in •


• Python and radically different
• •

: 1 979, as a part of his PhD •



to BASIC. Every C++ code

:

thesis. Initially C++ was called •
begins with a directive #include ,

:

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
• •

: Bjarne Stroustrup, inventor •



input/output operations.

: 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 ( )
• •

• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

BRACES *newcode.cpp (-/Docu ments) ..

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 ( )
{

BDM's 1-Tech Special I Vo lume 27


Getting to Know C++

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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)

•n@wcode.cpp (-/Documents) Ftle Ed1t V1ew Search Tools Documents Help


Ftle Ed1t V1ew Search Tools Documems Help

� *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)

Ftle EdJt View Search Tools DocumeniS Help

,._ VI

� *newcode.cpp x II My flrst CH program!


std: : cout << ( llo World1 \n ll
#1nclude
using na••space std;

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!

File Edit V1ew Search Tools DocumeniS Help


,.. .....
.... _
.. ,
(

�1•. �
' ..., , .• c- ..... -
.. I '-
)
c:· *newcode.cpp x

hnclude

int a�un( J
{
II My f1rst C++ pr og ram !
std: : cout

www,bdmoublicatjggs.com Coding ·Tips, Tricks & Fixes


Say Hello to C++

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.
-·-
-�
·-
··­
�==
==
-"· -
-== -===== �� -=-

It II r-•-nclod that you doco al other Olllbons


lilca
poSIIblo
·--
··­ -��·--
boforo starting Seto.c>. Tho"" """"•t lD ..,dab!
-
·-­
·­
·---
....... -- ....ontIYill!m fieo Md1outM""'J lDreboot yotX
r
·-­
__
_, ....... -
.-.... _,__ COITC)Utef.

-
_.. _...._... -
�::.".._...·
.- Cld< Nut to contiriJo.
- ..-..·--·----·- '--
----
···
--· ..··-·-·-·... ·-------
)j
-·-
·-...........

..-....-_

_.........__
......__..__,.
....�
...______ -�···
,..__...,.
....
__. -·-·---.. ""' - -...- Mj
. ·---
___ __.. ,__

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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 "

BDM's 1-Tech Special I Volume 27


How to Set Up C++ in Windows

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • •

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\'

Created .....,. l'<• Uo


OUtput folder: _
'fro... .....
Cr •a
� shorlalt: C: 'j.Jsen�vld�,.'f\
OIIrinQ \'Iiao
soft\l
l
'indows�""t Men.J'f'. • . "

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";

Notr. AftleriUID-dtllfellon. .c �-�·..... Pi"..nltiiiPffwlheref«e""*!.


}
"-<tlhoiotbdowond_lho_)_,...,.,,...,nlho--
Select you �a ddd � "-
� r. Notice how Code::Blocks auto-inserts the braces and speech quotes.
"" Se
tas de
faJt

' ,_. ·- ..._ t f......


..a I
,,.

.._
o-
U\ •••

""" ""' (•• ,..... ..,


, Hll•il'<d « -..u� lliodiH5"!
'

• •

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • . .................... .. ... . ................. " ..................

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.. . .. -.-- ..... ....
,.,..___

www,bdmoublkatjggs.rom Coding -Tips, Tricks & Fixes


Say Hello to C++

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 _..
,__.. � "'

� :-:. ---...-u �.. ..- - -.,...


-..-...---........_,_
­
.. =
a .. ....... - .......
.. ..
����; ·•-
.... ""' "'
- ..
9.2
..

BDM's i·Tech Special I Volume 27


How to Set Up C++ on a Mac

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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 -·--

www,bdmoublicatjggs,com Coding ·Tips, Tricks & Fixes


Say Hello to C++

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.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

The first step with ensuring linux is ready for your


STEP 3 Amazingly, that's it. Everything is all ready for you
STEP 1
C++ code is check the system and software are to start coding. Here's how to get your first C++
up to date. Open a Terminal and enter: sudo apt-get update program up and running. In linux Mint the main text editor is Xed
&& sudo apt-get upgrade. Press Return and enter your can be launched by clicking on the Menu and typing Xed into the
password. These commands updates the entire system and any search bar. Click on the Text Editor button in the right-hand pane to
installed software. open Xed.

· -
,.. �-
---
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

std : : cout « "Hello World!\n";

·u� Documer.tl x

#tnctude <tostre•a>

1nt •a1n()
{
//Hy ftrst C•• Program
std::cout << "Hetto wortdl\n";
}

BDM's i·Tech Special I Volume 27


How to Set Up C++ in Linux

• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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.

... .. ... ...... ..... ._._ ....,


david�
o n o. ... ...

File Ed1t View Search TenTllnal Help

,.,.,. """c.. ......_


u• c•t - \li •

-- .

·�· . , . .

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • ••••••• ••••••••••••• • • • • • • • • • • ••••••••••

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

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


Say Hello to C++

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 ,

Linux and Mac. Head over to w ww. e j


c.lRse.
o rg /do w n!oads/to powerful. You can find out more information as well as how to
download the latest version. If you're stuck, click the Need Help link download and install it at w
w w.
co deli
teo
.r g/.
for more information.
- -


:'-::=-------------
--- 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-• •

. .... '"'""' _,.c.t •WI


.. - -::=: .:.�-·-
:.
-
t ..
ho' -I•O ( "'"""- -·
-- ....,_
·--
··� ._...,
, ., .
...
- ·
__
... .
...._ '"'­

�); / ...l'l*� ll tto>IICII -}III II



(...\ 44 "II ,...UtliOrlol II" u
_. -.. ,
,·- --
___
--- .
- ...
. ...
· -·
...
...
.
,�..... .
_ _ _
.. .. .. ---·
. ...._,___..______.......
.. ---·-·---..-·­
-......
..--..,.... ----�·----�-
.

-
- -•
• 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,
-
..
. ..... _ ...
__...
.

BDM's I·Tech Special I Volume 27


Other C++ IDEs to Install

• •• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Microsoft's Visual Studio is a The Anjuta DevStudio is a Unux-only IDE that


VISUAL STUDIO ANJUTA
mammoth C++ IDE that allows you features some of the more advanced features you
to create applications for Windows, Android, iOS and the web. The would normally find in a paid software development studio. There's
Community version is free to download and install but the other a GUI designer, source editor, app wizard, interactive debugger and
w
versions allow a free trial period. Go to ww y
.jy
s a u
[std j
o c
.om /to much more. Go to w ww ,an y
jt a o
.rg/ for more information.
see what it can do for you.
� 6lk lflftl [liltcln ... ... 011"4 Olf;lo- ....
, , � -6 - - · •• •• �
...
--·---
_ _ _ .... _ _ - -- I
......
•. .
Ill , ....... • ... . •
-a o
0 ----
.... .,� ---,
.,
- · ··-
•• ._........,. ... _•

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 �
..
.,
...... ..
· ·-

. . . . . . . . . . . . . . . . . . . . .. . . . . . . . . ...... .. . . . . . . . . . . . . . . . . . . . . . . . . .
. . • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •

This cross-platform IDE is designed to This excellent IDE allows developers


QTCR EATOR MONODEVELOP
create C++ applications for desktop and to write C++ code for desktop and
mobile environments. It comes with a code editor and integrated web applications across all the major platforms. There's an advanced
tools for testing and debugging, as well as deploying to you chosen text editor, integrated debugger and a confi gurable workbench to
platform. It's not free bul there is a trial period on offer before help you create your code. lt's available for Windows, Mac and Linux
w
requiring purchasing: w g
w.tj
.o/
g -a
tfetyr
es- r
liba e
risa
-os
jt
-ol
os· and is free to download and use: w w w.m o nod eyel
op.com /.
n
a d
d-jel • 0..0.,1_... . -

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 - -

I'«L c..o.oru.c..- w:•u., � ..,au�,


'
.:.
·­

-
·­
-
;_
- -
-
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 ..
' -
-
·
- .... -
-· ·
··
·---·
--·
·- §-

wwVLbdmpyblicatlpos,com Coding · Tips, Tricks & Fixes


Coding on Linux

Linux is such a versatile operating


system that's both malleable
and powerful, while offering the
programmer a perfect foundation on
which to build their skills. While all the
popular and mainstream programming
languages are available on Linux, as
they are on Windows and macOS,
Linux also utilises its own coding
language, called scripting.

Bash scripting on Linux can be used to


create a wealth of useful, real world
programs that interact with the user,
or simply work in the background
based on a predefined schedule.
Scripting is a powerful interface to the
Linux system, so we've crafted this
section to help you get to grips with
how everything fits together, and how
to make some amazing Linux scripts.

• • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • •

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

78 A·Z of Linux Commands

BDM's 1-Tech Special I Volume 27


Index

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


Coding on Linux

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.

FREE AND OPEN


Linux is a fantastic fit for those who want to develop multi-platform code. The efficiency of the system, the availability of
applications and stability are just a few good reasons.

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

A Linux is a great operating system in


� which to start coding.

p fj x n •• tl• h "'*•
• 1111 I IJOI!e
•ttul udo • h

..tnC J lltlt \ udo t l h


llndl.ldo a.�th
llfldiiCit
1/�y far\t t• �'09r� 111'1(l ode t ...
: :c:o11t "'"' .,.. 1o Wlllrld!\,... 1 wo14 t\tn•t t�t l�t , l�t , lAt , iftt, tftt , f\e�t, ant, tnt )
vot4 h\tnoC ant , lfl l , tftt , tftt , ttlt , itlt , flo�t,ant, ant)

wt i4 o\t�C tnt , tnt , iftt , itlt , itlt, itlt, fl.at,int, int)


wot4 9'tdlint,tet,int, n lt ) :
w•t4 M ll'l( I
I
.,,, tci-OmCT.o-,.,..xlJ,...-y,ouh.•lcfy;
...
titlco\or-,loutcolor•,hioco\or•l,houtcotor�- aincolor• . ,_.color••

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 •(-

BDM's i·Tech Special l Volume 27


Why Linux?

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.

A Linux programming environment can be


as simple or as complex as you need it to be.

wwVLbdmpyblicatloos.com Coding · Tips, Tricks & Fixes


Coding on Linux

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.

FROM FREEDOM CAME ELEGANCE


With Mint's versatility, you have several choices available to install Mint. Take your time and see which method works best
for you.
• • • • • ••• • • ••• • • • •• • • ••• ••• • • ••• • ••• • • • •• • • • •• • • ••• • •• • • ••• • • • • •

• •
• •
• •



SYSTEM REQUIREMENTS •


• •
• •
• •

: The minimum system requirements for Linux Mint 18 are as •



• •
: follows: Obviously the better the system you have the better ,

; the experience will be, and quicker too.





• •
• •
• •
• •


CPU 700MHz •

• •
• •
• •
• •


RAM 512MB •

• •
• •
• •
• •


Hard Drive Space 9GB (20GB recommended) •

• •
• •
• •
• •


Monitor 1 02 4 x 768 resolution •

• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• •
• • ••• • • •• • • • •• • • • • • ••• • • •• • • • ••••• ••• • •• • • •• • • • • • • • •••• • • ••• • • • • •
• • •
• • • •
• • • •
• • • •





• USB INSTALLATION •

'
• • • •
• • • •
• • • •
• • • •

• : : 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 :
• •
• •

; familiar with the process or you're thinking of USB and just ;


• •
• •
• •




• :• gathering the hardware you need, then you're going to need a :


: minimum 4GB USB flash drive to contain the Linux Mint ISO. :
• •
• •
• • • •
• • • •
• •
• • ••••••••••••••••••••• • • • •• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • •

BDM"s i·Tech Special I Volume 27


Equipment You Will Need

• ••• • •••• • • •• • • • • • • • ••• •• ••• • • ••• ••••• ••• ••• • • • • • • • • •• • • ••• • • • • • • • ••• • • ••• • • • •• • • ••• • • ••• • ••• •• • •• • • •••• ••••• • • • •• • • ••• • • •••• •

• •

• • • •

• • •

• • •

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 •
• • •

: latest updates and patches. as well as the installation of


• •
: can transfer or burn the ISO image to the disc. •

• •
• • : 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 •

• •

: virtual environment, such as Virtualbox or Parallels; and why •



; not, macOS is already a splendid operating system. If you're •



: wanting to breathe new life into an older Mac, make sure it's •



: an Intel CPU model and not the PowerPC models. •


• • ••• ••••• •••••• •• •• • • • • • • • • ••• • • ••• • • • • • • • •• •••••• •••• •••••• • •

• •

• •

• •




VIRTUAL ENVIRONMENT •

• •

• •

• •

: Installation to a virtual environment is a favourite method of •

• •
• ·- �­
-
: 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. •
• •

: You can get the latest version from w


w w .
yjrtualbox.org. •

• •
• •

• •

• •

• •

VirtualBox
• •

• •

�.
• •

• •

• •


Welcome to Virtu1IBox.org! •


__... ____ __ _...__..______...,
_ ...

- ..-
- --- ·---
... -----�-··- --
. .... ....
.
• ---·-------,-- ...-.,--�- .
• ___, ...,.______,,- -·
.

·-------------�"'""'-"'-


-

- ---.---·-.... ___JIIN.OI--1 -
==
--
:
1.......M ..
...
_ ,OL-·!•f<o.••-·• -
. .... �
.....-4__
• •
!Z:.Io
--- ......
,

------�--------......_.....___.... .
..
--

• .-....-..---·--...... .. ,________ - -·
_.... .
- _.. .._ ..... . ..--·--

rr£ ;
-
• _ .. ... ,..... __....,._ ....., __ .

s
.......


Download
1


• •



VlrtuaiBox • •

• •

• •

• •

• •

• . ......,, __...__ _.,...__ •

. ...... --··-....-.. . ... .... ....... ...


.
• •

• . .
..
, . - �--
- __ •
. ._ _ _ .
...
.
...___
....,._ ....�

. --_,.,., •
._--

• •

• •
F2¢+t •

• •


• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


Coding on Linux

Tra ns e r M i n t to
DV ...... or

DVD BOOTABLE MEDIA


We're using a Windows 10 PC here to transfer the ISO to a DVD. If you're using a version of Windows from 7 onward the
process is extremely easy.
• • • • • • • • • • • • ••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • •

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.

- - .... ... ..... Name


.. ;;a..-
..__..
-- �an�• ........
t .. c·-ca
---·- �
..
.. � --· .
u
..
, IQ- ont·11.1·
CJnN
mon
· 60bot -


...
..
.. ,"'
.
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.

t• Windows Disc Image Burner X

Disc imagefile: linuxmint·18.1-cinnamon-64bit . 1so

Disc bumer. BD ROM Dnve CD:)

Status
Burning disc image to recordable disc...

l
BDM's i·Tech Special I Volume 27
Transfer Mint to DVD or USB?

USB BOOTABLE MEDIA


USB media is faster than a DVD and it's often more convenient as most modern PCs don't have an optical drive installed. The
process of transferring the image is easy but you need a third-party app first and a USB flash drive of 4GB or more.
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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

Tht ""'9t you Nwwlecttd rs 1n ·tSOHybrid' lll'nlgt. This muns it Qf"'be


8GB USB Fla (E:) [1.9GB) v wnttm CJthu lf'I ISO I!'N9C (ftlc copy) modt Of 00 Image (tslc:
d •m•) mode.
Rufus rec:ommtnek wng ISO lm.agtmodt. s.otNtyou 1h¥1ys 1\wt:full
.c:cessto thr: dnve 41ftffwnttng ft.
Partition scheme and target system type However, ifyou enc:ounter enutt cturinv boot. you ctn tryWfiting this imtgt:
•g••n in DO lm•gr: mode.
MBR p art1tion scheme for BIOS or UEFI v

sele.ct the. modetl\atyou wan'to USItowntethes: •m•ge:


Ple.a.se

File system @W .te tn ISO lm•ge modt (Recommt:ndtd)


r

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 a bootablt d1sl:: usmg ISO Image


- -
--·--
• • -0\. •• -· •

-- -- -- -
�· , • Create elltendtd label and 1con Illes
---·-

--
- ...
-- �-- �

-- - - �-- --

�-
- ·· -
- -·
�-
-
-- -·-

�....
-;
� . � "'""
--- - - -·
- -- - • --._____
-
-·- =

-
�,.- -----'
·­
--
·­
a-·---·­
·-
a------
--� Copying ISO files...
--­

.........
-
....-.. .

cancel
- - -
·-- - � -
..
··--
---- log

www,bdmoublicatjggs.rom Coding ·Tips, Tricks & Fixes


Coding on Linux

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 •

· tw --·�--· ....·· n.... .....__


. �· (--· ...,......,.,_.....-... ,
...
.
,
. .. ..
.,..,. ,
Nip1--J)I;. ----.:..,._,..-..
...,.:.
.... -
- . .......- �..,,_
• .,._.,__...,._,_.....,,_ ....
_.

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

sa--.u VirtuaiBox binaries


0:I..

O.Onertnon

--- . ...... ....


• · oex�
...
• &.--. • lri:ll.
.... .t
• .,.._ lnb
• Vlll't
.... ... ),l.ll or.c;.YM Vlrt IIC.. OCt
.... ...... P� AI �pta�
JuiJPOtl for UM l0 and LM J_G � \lrtudlooo: ReP, 41* .-.ayptiOn, NYMa� PXE
ThfWtntiOn Pkt: � _.. .-.li.IJM � N � P�use <ltv:! ('vOJII.Qoo
,.._111WU1 UW ...,-....,.. � wc-11u.. ..---� ,_.,. tMtiJJad __,of l.lfrtv..
If � Nw � Wrf.....); .I pM.w � tN Utei"'S((ft pjtd ..�.
.. O.J

• VlfoolwilllftoX S.l.ll �IWjiM �rICII (� -...AI pQfloffiM


-

BDM's i·Tech Special I Volume 27


Installing VirtualBox

. . . . . . . . . . . . .............. .. .. .. .. . . .. .. .. . .. .. .. . .. .. . . .. .. .. . .. . . . . . . . . . . . . .. . .. . . . . .. . . . . . .. . .. . . .. . .. . . .. ............. .. . . .. . .. . . .. . .. . . . . . . . . . . . . . . . . .

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.

Welcome to the Oracle VM Warning:


VirtuaiBox 5.1.10 Setup
Network Interfaces
Wizard
The Setup WlzMd wllllnstol Or..:lo W1 \'lrtual3ox 5.1.10 on lnstallr>Q 1he Or..:lo VM vrtuol8ox 5. I.W Motworblg
your-tor. OckNoxt ID conb,.... or Cancel ID exit 1he ..turo will raot YOU' ""twork <01
r 1ooc
bonond -;riy
Setup Wlllll'd. chG?moct you from 1he""twork.

Proceed will> lnotanattonnow1

. . . . .. . .. . . .. . .. . .. . . . . . . . . . . . . . . .. . . .. . .. . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

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.

Or..:lo VM 'l'rUJalllox 5.1.10


IC)Ilbbon.
!;21srartOr..:loVM 'Wtuolllox 5.1.w art.r nstalatton

Tt.s featu:e req.ites 168P-6 on


yow t.a-d '*"'•- Ithas 3 of3
subfeatures selected. The
subfeabses req.ire 716KB on yo...
( >

location: C:'frogrom Fllos\Orldo\lltrtual!oK\


L..:Br::.:owse

. . . . . . . . . . . . . . . . . . . .. . . . . ......... .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . .. . . . .. ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

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. � ... ..-.- . ..... . ....... ��,.....

� ..,. ... ...... -. ...... ....�


......, .,.. ...,. ...._ ... ... _ .......,.. ....
""
!;2jRogstor .._.....
� .. .. .._.... .. .......
__ ....
_ .____
-..... .... .. .
..,
... ....
--
...... o-o•.._.....b'..,._,,.._
-- U*U<IIIU
Dn;{
....... \.elO.,..,_lO,..� ... .
��.,,,...,.,Dik
.
--

c:::
:::J:!ii!'o �

Ver510tl 5. 1. 10 < Bod< Next >

wwVLbdmpyblicatlpos.com Coding - Tips, Tricks & Fixes


Coding on Linux

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.

oc:a x - ttwr U•" t1 ty


4..
A • cd
�o liDS Feahrcs

Hyper-Thrc•• i•t Ttc�•oloty ll•••le4J Itt • elp


H
Qtick Power 0• SeU Test lb•"ell
First loot Device IUSI ·HDI I rtenu LeveI •
Seco•• loot Jevlce IHU· 81
Thir• loot Device ect Yoar loot
loot Other Device F irst loot levlee ice rrlorJty
loot Up floppy Sc
Security Option ltDJ·] . . .. . I I •

OS Select For DRA ZlriOO ..... I I


USI-fDD . ..I I..

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.

tlet.oorl iloot rrm 1111 Ao1'JC'l1811


-tt.r4 Drloc
...... . �1· .... Ices
tletorork iloot rr.. MD fii1'JC'l1811 12

<Inter Sehp>

BDM's i·Tech Special I Volume 27


Testing Linux Mint's Live Environment

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.


-

--- -·--
-

- -

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


Coding on Linux

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

BDM's i·Tech Special I Volume 27


Installing Linux Mint on a PC

• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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.

....flllllfll't<'f.... "" -......,,, ..._lit,,....'III'W-..1,......,...

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.

----·

wwVLbdmpyblicatlpos.cgm Coding · Tips, Tricks & Fixes


Coding on Linux

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•.
______ _,
...�- .......,..,.......

c..• 1Qo!A(.117 ========-:!.

BDM's i·Tech Special I Volume 27


Installing Linux Mint in VirtualBox

• •• • • • • • • • • • • • •••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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! ­ -

• l:l ­ ..,_ _,.... -·-

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
.......... ,.....

'--""""�...ft:of'IWII IS.)4QIJ �1'41.1,


"''•""''" ,_-_..,,_...#. ,.,. _
CIO
=o -
______

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 -

...,_-'tel•wwfCMQIIIM It• •rl'wcll...,


Unux Mtnt Cinnamon ..� ....... 1 ,... ,.. .-w ......
• -
s.a..._,......"'-'11 ... _..........
.., ...........
_,......_....._ ... ..�....-....--- n..........................� ...
....�............ .. ....WOol....
' ....,.,......,...,...,,.,..,...••t.trow.
..... n.......be.,..ctlld ·-..W"Wdtrte:
.. .
.
·
- ,..,,.,, ...,..,...,. .........,_
,
olf,but.,.,CWI..ciD..'tOnf/llfl'llltiCIIId\IWI;fll
--

....
.......,,.11.1-G"NMCCI
• ..(1.6tGI)

a-
---- � c-..
----�, -.... ....- - .., ........, ,...--,-
.,. -----------i

www.bdmpyblicat!oos,cgm Coding · Tips, Tricks & Fixes


Coding on Linux

�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
' .

the command line .

daVId�clavi
davi�clavid- F
ile Ed
it v
- search Te
riiW\oll p
Hel
File Edit View Search Terrrnnal Help

BDM's i·Tech Special I Volume 27


Getting Ready to Code in Linux

. . .. .. . .. . . . .. . .. . . . . . .. . . . . .. . . . . . . . .. . . . . . . . . . . .. . . . . .. . . . . . . . . . . . . . . . . . . . .. .. . . . . .. . . .. . .. . . .. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
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

d.lw'ld 'd.l-. l d-T l r l t pwd


/home/davld
fi,l'•'1d •d,1·.·ld- Tlrl t uname - a
Lin ux david-mint 4 . 4 . 9-53-generlc #74-Ubuntu SMP
d.n.·1d ·
d.J·
.,
;ld-rrl!lt 'I

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.
. . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . ..

Having access to the Terminal meansyou're


STEP 1 STEP 3 Therefore it's always a good idea to work in the
bypassing the GUI desktop method of working Terminal using a two-pronged approach. First, use
with the system. The Terminal is a far more powerful environment the desktop to make regular backups of the folders you're working
than the desktop, which has several safeguards in place in case you in when in the Terminal. This way, should anything go wrong, there's
accidentally delete all your work, such as Rubbish Bin to recover a quick and handy backup waiti ng for you.
deleted files. -- -------

--
- ·- -

--

. ................. .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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 .. ___,..

Edit View Search Terminal

ucsev -wn.<so. Cld �� dO . f.klx & LnJxStack _


....
..
�--�t l
taV •""'..................
...... . ..
...
..OL'W .. .... 0 .
,... � ....... ........... ,..
'; .7 .....
..
_ .........,....,"'..._
......., ... ...
DODes1loyerol DIIU·-01V

_,.,.,�··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.. •
...
. ...,..

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Coding on Linux

"'-rea ti n """"' Bas ts


Pa rt 1
Eventually, as you advance with Linux Mint, you'll want to start creating your own automated
tasks and programs. These are essentially scripts, Bash Shell scripts to be exact, and they
work in the same way as a DOS Batch file does, or any other programming language.

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

--

--�-

BDM's i·Tech Special I Volume 27


Creating Bash Scripts- Part 1

. . . . . . . . . . . . . . .. . . . . . . . . . . .. .. .. .. . . .. .. .. . .. .. .. . .. .. . . .. .. .. . .. . . . . . . . . . . . . .. . .. . ... .. . . . . . .. . .. . . .. . .. . . .. . . . . . . . . . . . . . .. . . .. . .. . . .. . .. . .. . . . . . . . . . . . . . . .

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

echo Hello World !


I
.. . .. . .. . .. . . .. . .. . .. . . . . .. . . . . . . . .. . .. . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . . . . . .. . . . . .. . .. . . . . .. . . . . . . . . . . . . . . .. . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . .

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 -

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Coding on Linux

"'-rea t i n """"' Bas ts


Pa rt 2
Previously we looked at creating your first Bash script, Hello World, and adding a
system variable. Now you can expand these and see what you can do when you start
to play around with creating your own unique variables.

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.

File Edit Voew Search TenTllnal Help


;] .I ; '
T
r '
0
• : '1 0 !
' : •

. / 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.

file Edo< VIew Search Tools Documents Help

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

BOM's i·Tech Special I Volume 27


Creating Bash Scripts- Part 2

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

File Edot Voew Search Tools Oocuments Help


File Ed1t View Search Term1nal Help

� 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

File EdJt Voew Search Termonal Help


n o o, .,..
.. greeungs.sh x
•ttbin/bash

echo ·n • wtlat ts your n e


read f 1 rs tna11
e
echo ·n you a d wtlat u y r surnam,e7
read surnatte
clear
echo Hello sfirstna� ssurna•e. how are you on this fine S(dat

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Coding on Linux

'- rea tin Bas ts


Part
In the previous pages we looked at some very basic Bash scripting, which involved
outputting text to the screen, getting a user's input, storing it and outputting that to
the screen; as well as including a system variable using the Date command. Now let's
combine what you've achieved so far and introduce Loops.

IF, THEN, ELSE


With most programming structures there will come a time where you need to loop through the commands you've entered to
create better functionality, and ultimately a better program.

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
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.

•greetings.sl File Ecit \ll@ w S..arch Ttrmnal Help


File Ecfn Voew Search Tools Oocum<!ntS Help

!l o o; ...

� *greetlngs.sh x

"/bin/bash

echo -n • , what ts your nam 7


read f1rstna1ne
echo ·n h! • you, nnd whnt ts your surnd c7 •
read surna11e
clear
if I $hrstname •n v �· I
••

.... . - .. � ._

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
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

echo -n ello, what 1s yo r na1ne?


r•ad f1rstna1te
echo ·n - a k you a d t 15 yo sur 1
read surna•e
clear
if I sflrstna11e 0 vid
•• I W. ( ssurna11e - 1
then echo 1114!, Sflrstnaee ssurname
else echo Hello $firstna11e ssurna11e, how are you on th1s f1ne

BDM"s I·Tech Special I Volume 27


Creating Bash Scripts- Part 3

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

� count.sn x 1'1 /bin/bash


"/bin/bash
until I scount ·gt 188 I ; do
coun -e
echo scount
let count•count•l
while I Scount ·lt 1&& );do done
echo scount I
let count•count +l

• • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

for co �· in {e.. lee}; do


echo Scount
111 •count.Sh x
let count-count+l
I'! /bin/bash
done
I for co in {8 .. 1888 . . 2);do
echo scount
let count-cou nt•l
done

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


Coding on Linux

"'- rea tin Bas ts


Pa rt4
You've encountered user interaction with your scripts, asking what the user's name
is and so on. You've also looked at creating loops within the script to either count or
simply do something several times. Let's combine and expand some more.

CHOICES AND LOOPS


Let's bring in an oth er command, CHOICE, along with some nested IF and ELSE statements. Start by creating a new script called
mychoice. sh.
• • • • • • • •• • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • •

STEP3 You can, of course, increase the number of choices


STEP 1 ill ,....... � ..... n.w-- .....
but you need to make sure that you match the
n � � - � x Q � � �
The mychoice.sh script number of choices to the number of IF statements. The script can
-�-·
is beginning to look a •t/\1•/MIIIi
quickly become a very busy screen to look at. This lengthy scrip t
lot more complex. What •• is another way of displaying a menu, this time with a fancy colour
we have here is a list .... scheme too.
....
of four choices, with ....
.... .
three possible options. t· lldlf • ,�-.,.,..
t"U-s( "' .,
• •)
.. 1tr• ·• """" a

The options: Mint, Is, ,...., ctt.otu


""'0{ 1-' "\.Stft
GUll(I( S. ,,..;&;}
tU�IJ

<M�()( Sf' "\t-PUI"I)


and Awesome will be U I t•Nll<e ·eoq l I J thH .....:I( ... "1"41•n I)
lllJlU) ( S. "'...1"1)
...
.... .
displayed if the user ,,..
-« )( ... .... .� 1)
....(
.. )( yo , .,
• , ..
-, )
it I tchotce •eq 2 ) ; �•• •uc cuM ;�"' �-�"-' •t •[tr. -\•l rr)•
presses the correct .che he• �)( IIUit
tw ••lrl h UM>41 1 U)l*
•h• S( • •
option key. If not, then

tt t S.theu-• "I s 1 ; ttw. -
echo Y o.. �lllj..a(ll'Ul I I

the menu will reappear, ehe


ec:l1e e b 1111@en 1 3
'lo\\10 \IUCII!lll 1111111
\..1 (LfMI Clllll llltiLL•fft v("""
.1....
111(... 1)
tOOt ()( IWIIIIII'UI U \
the fourth choice. eche '

u�ct(• jl't"" ec:-. ,.... , ,�.


JIO•tf "llllll UUtl,IIUT "1�11(1}
echo -o( r-t�
" ' ,
. -111 by bftl•vfii•U ,..,
u.., .
echo
echo n 1-
A.

e
oo e- • optlon 1, J o r S \t I
Ill ( )( tNt
I 1bt • 11"t • lt ...ll KM
• ltl h "loo\n '"'" I)
IHI)
'., 1• ,.._ ...
"I ()( IM t 1tl ��� 'll•l•tfl"l)
II Ill()( '""' 6 161
llf
1• '0\lk'l)
IIJ()( l� 1 lei $• 'II O.. tl ' l )
114()( f1IIJT • 261 , •t\11011 '1)
.
1'1\()( "'Ill 'f lei J• "fi ii(IIJI I)
IM It 261

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
_,.._,,_

. .. . .. , . . ... .. .... . .... . ........ . .... .. .. .. ... . . ... . . ... . . .. . ..

STEP4 You can use the


arrow keys and Enter
in the menu setup in t he script. Each
choice is an external command that
feeds back various information. Play
around with the commands and
choices, and see what you can come
up with. It's a bit beyond what we've
looked at but it gives a good idea of
what can be achieved.

BDM's i·Tech Special I Volume 27


Creating Bash Scripts- Part 4

CREATING A BACKUP TASK SCRIPT


One of the most well used examples of Bash scripting is the creation of a backup routine, one that automates the task as well
as ad ding some customisations along the way.
• ••••••••••••••••••••• • • • • • • • •• • • ••••••••••••••••••••••• • • • • • • • • • • • • • • ••••••••••••••••••• • • • • • • • • • • • •• • • • •• • • ••••• • • • • • • • • • • • •

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\..,

echo k t ' J ow ere tl g 1 (.tdir ·P Sdest)


....
tar tvh ldolrll/ S�r '-'ll\ h••• /llot.-llh..(lyp t91 i\•••
echo
dMf
fi ....

www,bdmoublicatjggs.com Coding ·Tips, Tricks & Fixes


Coding on Linux

'- rea tin Bas ts


Part
The backup script we looked at previously can be further amended to incorporate
choices, user-interaction with regards to where the backup file will be copied to and
so on. Automating tasks is one of the main benefits of Bash scripting, a simple script
can help you out in many ways.

EASY AUTOMATION AND HANDY SCRIPTS


Entering line after line of commands to retrieve system information, find a file or rename a batch of files? A script is a
better answer.
• • • • •• • • • • • • • • • • • • •• • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • •• • • • •• • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • •••

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 . ..

• -rr" tll'ld ustd ..110ry:


.c:tlo -e I 1 4l8 AHD o ,,..
,,....
-··

1 ·5yste• uptt_, �nd perform�<• load


ec:ho ·e I 1 4J••• • T Pt UPTII-tr A. LOAO ••• •• to 8
uptt.e
Mho • • •••••••••••••••••••••••••••••• • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •

# -users currfflt\y \oggtd in:


.-cho •e I 4 l HI U K • • \t 011 STEP4 Although there are simple Termina l commands to
.
...
.. .. h elp you look for a particular file or folder, it's often
• Top hv� pHIU!u"' bto<u>q u"toel Dy tt-t •r•tea more fun to create a scri pt to h elp you. Plus, you can use that script
echo ·e I 1.4S. •••• T I"' Y IN f'� [ .
. ...
,..
ps -•o "'-"·'<JW,<o• ··tOr-t•·\IM• I tt.N . ,.. • for other non·technical users. Create a new script called look4. sh,
.. ..
Kht ·e I ' el • entering the content from the screenshot below.

• • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••• • • • • • • • • • • • • •
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,

BDM's I·Tech Special I Volume 27


Creating Bash Scripts- Part 5

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • ••••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••

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

looklM for: '

soutput I t
e $output
. ,.
e No Match found
h

• •• • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Here's an interesting, fun kind of script using the


STEP6
app espeak. lnstall espeak with sudo apt-get
install espeak, then enter the text below into a new script called
s
p eak.sb. As you can see it's a rehash of the first greeting script
we ran. Only this time, it uses the variables in the espeak output.

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
·

·• albeit, a little basic. If you fancy playing a game, enter wget


ec:... •
http://brux
y.reg
net .cz/linyx/hoysenkg/ho ysenkg.sh,
·

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.
. . . . . . . . . . . . . . . . . . .. . ... ...... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •

BDM's i·Tech Special I Volume 27


Command Line Quick Reference

free sed

top deluser

grep

wwV£bdmpublicat!oos.cgm Coding · Tips, Tricks & Fixes


Coding on Linux

A-Zo Linux '-omman"""s


There are literally thousands of commands, so while this is not a complete A-Z, it does
contain many of the commands you will most likely need. You will probably find that
you end up using a smaller set of commands over and over again but having an overall
knowledge is still very useful.

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 . . ... .
.............................. . ............................................ .

di ff Display the differences grep Search file(s) for lines that


. ..................................... ............................................

awk Find and replace text


between two files match a given pattern
wi thin file(s)
. .. . . . . . . . . . . . . . . . . . . . . . . .
............ .. . ...... .. . . .
.... ... .. .... .................
di.rname Convert a full path name groups Print group names a user
to just a path IS In

B du Estimate file space usage QZlP


.

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
......... '............ '............................................ . . . . . . . . . . . ..

cron tab Schedule a command to

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

BDM's i-Tech Special I Volume 27


A-Z of Linux Commands

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) . . . . . . .. . . . . .
. .. .. . ... . . .. . .. . .. . . . . ... . . ... ... . . ... . . ... ... . . ..... ... ... . . .

umq Un iqui fy files


lprm Remove jobs from the rpm R emote Pa ckage Manager
print queue unit s Convert units from one
. . . . . . .. .
. .. . .. . . . .. . .. ... ..... . . . .. . . .. .
.. . . . . . ................ '" ....... . . . rsync Remote fi le co py
scale to another
(synchronise fi le trees)
Unpack shell archive scripts
M
unshar

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
. .. ......... .. .............. .................................. ...............

shutdown Shutdown or restart linux


a t a time vdir Verbosely lis t directory
sleep Delay for a specified time contents ('Is -1-b')
mount Mount a file system
sort Sort text fil es
. .. . . . . . . . . . . . . . . . . . . . . . . . . .................... ...................................

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 .. .
. .. ..

sum Print a checksum for a file we Print byte, word, a nd


nl Number lines and
line counts
write files symlink Make a new name for
a file wherei. s Report all known
nohup Run a command immune
instances of a command
to hangups sync Synchronise data on disk
with memory which Lo ca te a program file in
the user's path

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

. ..... . . .. ......... . . .. ...... ..


for printing
..................................... . . . ..
.. .. .... . tar Tape Archiver X E xe cute ut ili ty, pa ssi n g
pri.ntcap Printer capability database xargs
. . . . . . .
.... .. .... .... .. ....... . .................. ................... .. . . .
. .. .... .. te e Red irect o ut put to
constructed argument list(s)
pri.ntenv Pr int environ ment variables multi ple files . . . .
.. . .. . . . . . .. . . . . . . . . . . .. . . . . .
. . ............... .... .......................... ............... ................

. ... . .. ....... . .. ............... ............... ... .......... ............ . . .. ............ ....... ......... .... .. .... .. .... .... . . .. .... . ..

printf Format and print data t e st Evaluate a


. . ... ............ . . .
" ". . .
.. "."..........."........ ..".......... "........ " . conditional expression
y
Q
t ime Measure Program yes Print a string
Resource Use until i nterrupt ed
. .. ... .. . . . .. ... . . . . . . . . . . . .. . . . . . .
. .. . .... .. ... . .. . .. ... . .. . . .. . .. . .... .......... . . .
.... .... .. . . . . . .
. . .. .... .. . .. . ..
. . . . . . ... . . .. . . . ... .. ... .. . . . ... .. ... .. . .. . .. .

quota Displ ay disk usage to uch Change file timestamps


and limits
. . .. .. .
. .. . .. . .. . .. .. ... ... top List processes running o n
quotacheck Scan a file system for the system
disk usage
traceroute Trace Route to Host
quotactl Set disk quotas
tr Translate, squeeze and or
delete chara ct ers

R t s o rt Topological sort

ram Ram disk device

www,bdmoublicatjggs.com Coding -Tips, Tricks & Fixes


Working with Code

By now you can see that there's more


to coding than simply entering a
few lines into an IDE and expecting
a result. Working with code means
confo rming to proper layout, adhering
to strict operations and making
the code easy to understand and as
efficient as possible.

In this section we take a look at the


common coding mistakes with Python,
C++ and Linux scripting and how to
avoid them. Learn how to check your
code with checklists, where to get
help when you're stuck and how to
test your code online in a safe and
secure environment.

Learning to code is an on-going


occupation, where you discover new
techniques and ways of managing your
code from other developers. Being
able to recognise mistakes, fix them
and help others is part of becoming a
better coder.

• ••••••••••••••••••••••••••••••••••••••••••••••••••

82 Common Coding Mistakes

84 Beginner Python Mistakes

86 Beginner C++ Mistakes

88 Beginner Linux Scripting Mistakes

90 Code Che cklist

92 Where to Find Help with Code

94 Test Your Code Online

96 Python OS Module Error Codes

98 Python Errors

BDM's 1-Tech Special I Volume 27


Index

www,bdmoublkatjggs.com Coding ·Tips, Tricks & Fixes


'-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.

It would be wonderful to be able to Meaningful naming for variables is a must to eliminate


work like Neo from The Matrix movies. common coding mistakes. Having letters of the alphabet
Simply ask, yo ur operator loads it into is fine but what happens when the code states there's
your memory and you instantly know a problem with x variable. It's not too difficult to name
everything about the subject. Sadly variables lives, money, player1 and so on.
though, we can't do that. The first
major pitfall is someone trying to learn 1 var points • 1023;
2 var lives • 3;
too much, too qu ickl y. So take coding in
3 var totalTime • 45;
small pieces and take your time. 4 write("Pointa: "+points);
5 write(" Lives: "+lives);
6 write("Total Time: "+totalTime+" sees");
7 write("---------------------------·);
8 var totalScore • 0;
9 write("Your total Score is: "+totalScore)

Use comments. It's a simple concept but commenting on


your code saves so many problems when you next come to
look over it. Inserting comment lines helps you quickly sift
through the sections of code that are caus ing pro b lems;
also useful if you need to review an older piece of code.
White it's great to wake up one morning and decide to code
or10 ..,. L:
.
a classic text adventure, it's not always practical without a
earoet ...- 2 ;
i
good plan. Small snippets of code can be written without
--
o:

) 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

II (n' (-S)l 1e �be qrca�••� aultlple of 1 r �- - ..


.. --
II In the �le loop ahead, or1q v1ll move o
II 1ncreaen�• (1 el�nts of 2 b�es).
II end &Art• our barrier tor noc tal11ng out!
char eonst • const end • or1q • 2 • (n •
·-

c -
y ( -
·
)
II See �t we're al1qned tor
-
t1f Act SIZtOF LONG -8 '' \
..
vrlttino

detln,.d 1 c -
T

) l (
..
·-
)
Common Coding Mistakes

User input is often a paralysing mistake in code. For


example, when the user is supposed to enter a number for
their age and instead they enter it in letters. Often a user
can enter so much into an input that it overflows some
internal buffer, thus sending the code crashing. Watch those
user inputs and clearly state what's needed from them.
Always make a backup of your work, with a secondary
backup for any changes you've made. Mistakes can be
rectified if there's a good backup in place to revert to for
those times when something goes wrong. It's much easier
to start where you left off, rather than starting from the
beginning again.

You can easily spend days trying to fathom out a section


of code to achieve a given result and it's frustrating and
often time-wasting. While it's equally rewarding to solve the If you're writing code
problem yourself, often the Silme code is out there on the to deal with usernames
Internet somewhere. Don't try and re-invent the wheel. look and passwords, or other
to see if some else has done it first such sensitive data,
then ensure that the
data isn't m cleartext.
Learn how to create
a function to encrypt
sensitive data, prior to
feeding into a routine
that can transmit or
store it where someone
may be able to get to
view it.

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.

c.,t.e.· t•, rt • • • {t, I} r • {f, I)


-*1. C&, -,, l, •f • f-. hi• ,_,. I I - tlllf 11 .....\. b, '' 1 • 1 • c


..teo. h. rJ, • • tJ

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.
• • • • • • • • • • • • • • • • • ••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • •• • • • • • • • •••••••••••••••••••• • • • • • • • • • • • • • • •• • • • • • • •

To add to the conf usi on that most beginners


INDENTS, TABS AND SPACES Python uses precise
VERSIONS
already face when coming into programming, indentations when
Python has two live versions of its language available to download dis playing its code. The indents m ean that the code in that section
and use. There isPyt hon version 2.7.x and Python 3.6.x. The 3.6.x is a part of thepreviousstatement. and n ot something linked with
versio nis the most recent. and the one we' d recommend starting. another part of the code. Usefour spaces to create an indent, not
But, version 2.7.x code doesn't always work with 3.6.x code and the Tab key.
vice versa.

' -'- ....- t'�


f,...!- • �. tollal..�,..,_,._. . ·�·

• -�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

• • • •• • • • • • • • ••••••••••••••••••••••••••••••••• • • • • • • • • • • • •• • • • •• . . ... .. ...


.. .. .. ... .. .......'.................. ... ... ... .
.. .. ..

Every programmer has and does at some Again we mention commenting.


THE INTERNET COMMENTING
point go on the Internet and copy some It's a hugely important factor in
code to insert i n t o their own routines. There's nothing wrong with programming, even if y ou re the on yl one who is ever going to view
'

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

• 1�c�• ""'"""10.-"*'" hm• w..tilt All,.,c.,secOcp:•di'!lClftr.c-..:clft pyqa:e. 1D-.1t. ()


elM .....,....
�dblt.w--' l ,....,. .,. h•nUts olhscenario a:a1-nClock pyqa.me.time.Clock ()

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 =

H P"l't�"'C� WI...I•J •• ""'t..,,. >'6 IH'[I I

,..,.. 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

BDM's i·Tech Special I Volume 27


Beginner Python Mistakes

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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.

� "'"'""'·"' "'" 0 X ...... Bao;..a.:


__1n1� (e�l(,vor�) :
file [dl Shell Otbllf OpttOfll WINlow �
__

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

CS.f MftQiirllJII WOfttidCJ :


1f • ••1t.b1Ge_word t•:

'

• •••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••••

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.

rc. oon 3 6.2 �he


File Edrt Shell Debug Ophons \londow Help
:...._ ____
• • •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Pychon 3 . 6 . 2 (v3 . 6 . 2 : Sfd33bS, Jul 8 2017, 0�:14:3


Using the wrong operator is also a common
on wJ..n32 OPERATORS
mistake to make. When you're performing
Type "copyrioh�". •cred1�s· or "l1ceose ( ) " for mor
a comparison between two values, for example, you need to use
>>> L1ves•10
>>> 11ves•9
the equality operator (a double equals, ==). Using a single equal (=)
>>> pr1n� (Lives, 11ves) is an assignment operator that places a value to a variable (such as,
10 9 lives=1 0).
>>>

c - 10

d - 10

b == c tfal� becau�e 5 i� noc equal co lC


• • • • • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

c -- d tcrue be , -- .. 1'\

Everyone forgets to include that extra
BRACKETS
bracket they should have added to the end • • •••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••••

of the statement. Python relies on the routine having an equal


OPERATING SYSTEMS Writing code for multiple
amount of closed brackets to open brackets, so any errors in
platforms is difficult,
your code could be due to you forgetting to count your brackets;
esp eci ally when you start to utilise the external commands of the
including square brackets.
operating system. For example, if your code calls for the screen
to be cleared, then for Windows you would use c1s. Whereas for ,

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 '

pr�ne ( ' " Py�bon 3 File Hanager<<<<<<<<<<\n ' )


print ()

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


Working with Code

Be""' 1nne r ista


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.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

A common C++ Referencing the Standard Library is


UNDECLARED IDENTIFIERS STD NAMESPACE
mistake, and to be commo n for beginners throughout
honest a common mistake with most programming languages, is their code, but if you miss the std:: element of a statement, your
when you try and output a variable that doesn't exist. Displaying the code errors out when compiling. You can combat this by adding:
value of x on-screen is fine but not if you haven t told the compiler '

using namespace std;


what the value of x is to begin with.
Under the #include part and simply using cout, cin and so on from
then on.
File Edtt Vtew Search Tools Documents Help

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.

d�v1d�m1nt·m�t• • /Doc uments


File Edit Voew Search TermoNI Help
��·, l d · � t n � - � l. tr gee test l . epp -o tes t
int main ( ) 't�p/eeASzhtg.o: In funct1on �a1n·:

c, d;
test1. cpp: ( . text•6•2a) : undttf 1 �d reference n to std: : cout
{
·

s 1. cpp:( .text•6x2f): undef1ned reference


te t to · std: :ostream: :
a=l8;
int a, b,

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:

d= 8; c l ect 2 : error: ld returne 1 exlt �tdt u�


ol
:JdJld""Tln! -�.1tr
:JaJld"'Tlnt-!l',ltl'
q•• te�tl.cpp
d
test -o

std : : cout << a , b , c , d;

BDM's i·Tech Special I Volume 27


Beginner C++ Mistakes

• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Indeed the mistake of never The braces, or curly brackets, are


COMMENTS (AGAIN) TOO MANY BRACES
making any comments on beginning and ending markers
code is back once more. As we've previously bemoaned, the lack around blocks of code So for every ( you must have a}. Often it's
.

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" ; •

cout << 'What ' s the value of x? " ;


u .tcc -e�.,... ,.u 1 11 • �t t u .,.. , :\11' ,
• c1n >> x ;
o 111\t I · ., 1 t1 iiUI , ut) COIIt v!1 " '\n'l
cout << x ;
{
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
cout « \n\n " ;
cout << mystring;
QUOTES Missing Quotes is a common mistake to make, for
every level of user. Remember that quotes need
}
to encase strings and anything that's going to be outputted to the
screen or into a file, for example. Most compi lers errors are due to
missing QUotes in the code. • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

I N ITIALISE VARIABLES In C++ variables aren't


initialised to zero by default.

'
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: • •••••••••••••••••••••••••••• ••••••••••••••••••••••••••••••••••

#incl ude A.OUT A common mistake when compiling in Linux is


forgetting to name your C++ code post compiling.
i. f 1 i.nes
When you compile from the Terminal. you enter:
swit ch li. n es
Q++ cod e . cpp
If it sounds confusing do n't worry, the compiler lets you know where
you went wrong. This compiles the code in the file code.cpp and create an a.out File
that can be executed with ./a.out. However, if you already have code
in a.out then it's overwritte n. Use:
•lllode <lost,.._,
u�i,. �PK• stcl;
g++ code. cpp -o nameofprogram
tn()
!11t ..
( d�YJc:l m11t-m.ue -/Documents
l,t nu.ber;
File Edl! VIew Search Te,.,.,.l Help
COU't « •[nUl" M III
C..'f
l": �;
c:in )) �;

cout « 'You •nttrtd • posltht inttat"": "' « nu.ber « endl;


)

cout « "ThiS st•ttwf'\t 1s •lw.-.yr. uec:utM. ";


rtturn t;

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 Forgetting to add the #!/bin/bash. the


DOT SLASH Remember, Unux scripts do n t run when you
'

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.

*Hello.sh (-/Docu david@mint-mate -/Do

File Edrt V1ew Search Term1nal Help


File Edit View Search Tools Documents Help
david·, rr int - rrate [} _1 I (, I � '. . I·T '> Hello . s h

p 0=.. .. IHel lo . sh : command no t f ound


<JI I .

david,<lrrint -mate
'Hello �/orld'
D'1l•
J1 •
r�1·, s . /Hello . sh

� *Hello.sh x david,,omint -mate D·


1 1l·•·
J1 r
•�1·• s

# ! /bin/bashl • • ••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • •

WHITESPACES Most of us automatically continue


echo ' H e l lo World ! '' writing script code without entering
whitespaces after a variable but if you've been using other
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• programming languages then it can become habit to hit the space
bar frequently. In scrip ti ng there's no spaces on either side of the
The second most common issue for
,

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

File Ed•t View Search Terminal Help


david·'fTlnt ·rr.1te . " ,. ,
r > . /He t t o . sh . . ·
echo swrd

bash: ./Hetto . s h : Perm1ss1on den1ed cbvid


t'
"unt-m
at• "" IDoc.Urrntnu
dav1d�rr1nt ·n-ate [. ,,., s chmod +x Hello . s h Filo Edi< V
- S.O<dl ,...,... , ....
d;wid�rr1nt ·rr.1te r., ,.. ,
...· > . /Hetto . s h
Hello World•
·rr1nt ·rrate
d avid,
, [:-,.-I,.
JI ' 'I • ') I

BDM's I·Tech Special I Volume 27


Beginner Linux Scripting Mistakes

• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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 ·n s th syst� pa$sword'


target �thome{david/Music read · s pass

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
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

CHECK SPELLING It's all too easy to mistype a


command in the Terminal. When you • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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

� Edt: .ew Surch


V' Term
Nl H91p

IS "Hello.sh x

11 ! /bin/bash
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••• • • • • • • • • •

A popular mistake with most


MISSING OPERATORS
echo llo Wortd • " Bash scripters is missing out
vital operators in their code. Missing quotations marks in an echo
ehco 1s 1s t he best scr1pt eve r • • command, or square brackets when using loop, and even flags for
external commands can have undesired results when you execute
the script. Best to check through your code before running it.
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

While frustrating to the coder it's always


WRONG OS ,

amusing to see someone who's written Flit Edit V�VW S-earch Tool� Dotl,l'flotnb �lp

a script and inserted a Microsoft command instead of a Linux


com mand. Clearing the screen is popular, where in Linux you use • •Hf:l kJ..s
h )(
clear, someone who's got their DOS head on uses cls. • 1 / bb/ b
uh

., "Hello.sh (-!Documents}
clear

echo t Tho .... ..


File Ed•t View Search Tools Documents Help sleep 2

•<�••
•<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

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Working with Code

ist

u nless you're writing

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.

Recent research by the Software Engineering In sti tute suggested


that programmers can make between fifteen to twenty mistakes
in their code, and these are experienced coders. The rest of us
probably make significantly more as we earn our coding stripes
and build our skills.

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.

Don't forget also, to continually add to the last part of the


checklist, theMistakes check. Being able to see your past mistakes
helps you form a m uscle memory that gets rid of them before the
actual checks begin.

• BDM's i·Tech Special I Volume 27


Code Checklist

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
_
, _ __ _ _

- - ... -·-· . - ......·- i'liM h,,._ !fifo�� 11 '


I " w 11111'11 "'""�'' l•�ol ln!!�ly ''"'ll•d I'll11 wfok h h�� h • 11il•• """' "
K(;Ioto
fl<l lont fil>ll..tJ,."" pmg�ml"')
. ..- ....
...... ..._,..... �..
--··
.. ""'· ... .. .
�---·- . ·- -.. - -
......
I> 0 1 "Cll "'
'ooi • I

J �o
)lflhl
n,._ ...�1��
lt
\ �
,
,... ,,
• ._,,.,,�,..,,.

* ·
� . ...
-•....-. .. - ..

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • •••••••••••••••••••• ••••••••••••••••••• •••••••••••••••••••••••

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 ' - .. --...--..--- -·-
---·-
-·-·-.....-.....
-...---·---·

BDM's i·Tech Special I Volume 27


Where to Find Help with Code

• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

CODEPROJECT With articles, discussions, source


OZONE OZone offers the user a wide range of help on
code and an excellent community, topics such as AI, Big Data, Cloud, Databases,
CodeProject is certainly worth bookmarking and paying daily visits Java, lOT and much more. There are guides, Zones, where you can
to. It covers virtually every programming language you can think of, get specific help and information and tons of tips for upcoming
and questions are quickly answered in a professional manner: developers. You can find it at: w
ww.dzo ne.
c om / .
www.www.codeproject.com/.
. •

- - -
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 -· - .... .

..,_ • 81.comp langJumc,..e.. ....


....
"I
.......
f1 ,..,. •
-
..._ ., ,
.. ......,.. n_.l'
BIJrt �-
- ...

.. .... ·-

. ,_
ARE YOU A..
� �...,__.,'-'Ol&.ll Uot_...8il (1)
Old•.,. •'"'""'' �� B ...... ......* �et� •-
ICOIIriO .......,.....
"...... .....,.,
. • lllt'-1.10� f'i

B &tNP tfMCII i I

W•lll10"-,.,..C..M"'-I ""liMNfrplurl WS�llnlaAlii_,.._._ (2)


B &!.,... ..,_..,.. I

1M*fl!!f- (1)
B "' ""' .
Tech Ne
rd lncltpenden1 Sofrwllt V�
or S t anup/ SUB

Co. lht... ot!O<Ittillotl...tttttloltljl


ll llll lllllll-*' 1'1
B Or · � � - .. .
• �
... .. IlCht.
'*
""'•lhlll
......Oot!htlhlll• t"l
B l)y'"-"" l!lld ,.
ll

-- ---··- ...........����-_....._.. .,�,

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •

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/.

-- ' \ I;" ·--

1
: - �p?
:!!A
' w.r,.. IN'altable
When homework geb - NHd last mMute
tough, get tougtMr "� 24n
-·•.-.....0
� 1>11....•'!1101.•

wwVLbdmpyblicatloos,com Coding · Tips, Tricks & Fixes


Working with Code

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""

,:,
:�::
= -
:==
-=========:::!.
.. ••

. . . .... ..
. .. ... . . . . . . .. . ' . . . . . . . . . . . . . . .... . . ... ... ...
. . . .. . . .. • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • •

For those of you looking to JS Fiddle is another excellent online resource


WRITE PHP ONLINE JS FIDDLE
learn PHP for future web where you can experiment and test using
development, Write PHP Online is a great resource to bookmark. HTML, JavaScript and CSS, and see the output from the code you re '

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 _..,_,_

e-- n -... --------

--

-·-

-
-- - ·-u.-w •

----

BDM's i·Tech Special I Volume 27


Test Your Code Online

. . . . . . . . . . . . . . . . . . . . . . . . . . .. .. .. .. . . .. .. .. . .. .. .. . .. .. . . .. .. .. . .. . . . . . . . . . . . . .. . .. . . . . .. . . . . . .. . .. . . .. . .. . . .. ............. .. . . .. . .. . . .. . .. . . . . . . . . . . . . . . . . .

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.....
_ .. • •

·-- -
------ - ., .... . ! ,.... . - ..... ....... •

. . . . . . . . . . . . . .. . . . . . . . . . . . . . . . .. . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . .. . . .. . . . .. . . . . . . . . . . . . . . . . . . ..

IDEONE.COM This online code editor and compiler


JSBEEB Purely For fun, those of you old enough to recall
supports over 60 different programming the heady days of the '80s when the C64, ZX
languages for you to test and debug. There's Linux Bash, C++, Spectrum and other marvellous 8·bit computers ruled will certainly
Python and Python 3, Java, JavaScript and many more to try out. warm to jsbeeb. ln the 80s British classrooms where filled with BBC
' ,

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 -
·­ - -
- -
... -
·­ -
- - �
- -

wwVLbdmpyblicatlpos.com i s, Tricks & Fixes


Coding · Tp
Working with Code

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 , __ , .. , ... .... lJ ,. ..,


-·· -·-
II«: · -�
·
.... ....,.,...,... -v.._u• ., �� �·.., -• ....,_,_ · ·--·--......- -- -"'
..
... --
_,__ hii-UJ
�- -· •- .u-·

.,....
..._
. .-..
..-
_-
.�....
....-. ..
.
.... . . .._.,
- - .. ..
......
.. ., ..
··-··- ..·- -· - - -
-- - . ...,_ .. ---
• -
.,._ -- -
- ��1 t·- -. "'
..
' 1).

• . .... - .... ·- -· - -"-

,... ·- h
.....-.,. ,_ -- 1.. ..
,. c u.
.
,.. •. ,
..

.._ .....
,_ , _.
.- __
....
,._, ••h.,.,_,Ke
... ....... cw 1�. Cta

' .... t ·=-- c:.A


.. - lh -···

,.......-. . .....
___ ... . ,_ ..J. )0

• •• o ..,. oil- ., ,, - -•«•

• .... t"" -..1 ., HI:..,....,. ••u--.•• ..,., t'>t ..,,t.


,,... ......u(..-... ,.
,., .:o. (t.,.-o.lott ·

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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) »>

hle ·�n•••·· hne 1. 1ft ....,1••


pirnt(...,lH)
"-tfrron � 'pu-nt• ll net ,.. ..,..
>>> I
,

BDM's i·Tech Special I Volume 27


Python Errors

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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 ... .
_ _ __

ll if character in rtference.keys O: ·- .... _.._ _ _


14 stack.append(character) ____..__

- ·--- ---- - --
,
.........
. .......-
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

Tht poblcm""""• twt c-t•


athlt1tw-1<oJlCh�e� lf'lt1UdlngonnyourN1tf
dc>l'fs'fllaf'lltobt�(like K�,..oHrupcind51/U�Iall h:WOUld bemuc:n better d -
....
'j'OUf"�blo<'llonlyCMChlmt�,I('t'lltf'POO"')'DUewpen..,.., let...ot:hersDubt»e �
•notftWII
Ar�oc..Mf�..comi!Wt'lbon)'(Mil cOOt­

• In �one200, you 1\tYt rP'ItJconsrrumon

fOf Jette.- 1'1 r-...(l.,.(cloo�tn -Nil))


>f pi Y "...a"�"
•• '"""'•"-'4�"4(htt...)
_.;.... .,,.,,.,.(hlt•"l ph��....u
..

You'rl!looplncO¥ffth@ lnd@xVMIIIblt. buratJo us1ncme1m etemeoc It would be betterto

for l«o.,l•ttcr in -r.te(cllou.,_-•)1


H piloyl"l"I;��'" h•UI',.I
••

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " . . . . . . . . . " . . . . . . . . . . . . . . . . . . . . .. . .......................... ................. "..................

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.

file £.• FQm<ll flun QpbotiS '1t'nOowt tttfp


..or-d-L.npUt( ern•r • fwr•lett•r �rd
)
.. rd tftl
t
l
_l h- ltn(IIIO fd)
1r ..ord_ .,th - .c:
.
pnnt (word. ,.., J•tttr .,,, wr11 .,.._ •)
....:
prtnt (.ord, •ls not a fOUr 1ffter IIIOfd •)

0
expected .., indented block
r=-
-

[ II« J

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


Programming with the FUZE

The Raspberry Pi is the powerhouse


for many excellent projects. However,
one project in particular stands head
and shoulders above the rest, the
FUZE Project. FUZE is a learning
environment for the Raspberry Pi
that's amazingly accessible and gets
students, teachers and enthusiasts
coding and experimenting with the
Raspberry Pi quickly and easily.

Used in hundreds of schools across


the UK, the FUZE is the perfect
combination of Pi potential,
imagination, engineering and
education, all presented in a cleverly
designed retro-themed keyboard
case. More importantly, the FUZE
also comes with its own programming
language, FUZE BASIC. With FUZE
BASIC you're able to create simple
routines, games, complex algorithms
and even interact with robots and
other electronics.

• ••••••••••••••••••••••••••••••••••••••••••••••••••

102 Introducing the FUZE Project

104 Setting Up the FUZE

106 Getting Started with FUZE BASIC

108 Coding with FUZE BASIC- Part 1

110 Coding with FUZE BASIC- Part 2

112 Coding with FUZE BASIC - Part 3

114 Using a Breadboard

116 Using the FUZE 10 Board


118 Using a Robot Arm with FUZE BASIC

120 FUZE BASIC Examples - Part 1

122 FUZE BASIC Examples - Part 2

BDM's i·Tech Special I Volume 27


Index

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


Programming with the FUZE

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.

••• • • •• • • • •• • • • • •• • • • ••• • • •• • • • ••• • • • ••• • • • • ••• •• •• • • • • • • • • •••• • • •• • • • •• • • • •• • • • • •


• •

• • •
: The FUZE Workstation itself. styled on •



• Inside the FUZE workstation is •


: a BBC Micro Model B from the '80s, • • the beating heart of the project. a


• • • •
• •
: constructed with a metal body and • • • • • •• • • • • ••• • • • • • • • • •••••••
..
Raspberry Pi 3. Jt's connected to the •

• • • •

: high quality keyboard complete with • • FUZE 10 board via the 40-pin GPIO pins •

• • • • •
• • • •
• •
: 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. •
• •
• •
• • • • • • • • • • • • • •• • • • • • • • • • • • •••••• •••••• • • • • • ••••••••••••
•••••• • • • • • • • • • • • • • • • • • •• • •

BDM's I·Tech Special I Volume 27


Introducing the FUZE Project

• • • • • •• • • • • ••• • ••• •• •••• •• •• •• • •••• • • • • • • • • • • • •



• •

: You also receive an electronics kit as part of •




: the FUZE workstation, to helpyou get started •
• •

: on some of the projects the FUZE is designed


: to support. Within the kit you can find 24 •



; coloured LEOs, 1 seven-segment LED, 1 light •


: dependant resistor, 8 micro switches, 30 mixed :


• •

: specification resistors. 20 jumper cables and 60 :


• •
: jumper wires. •


• • ••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •



• • • • •• • •• • • • • • •• • •• • • • •••• •

• •• • • • ••• •• • •• • • • •• ••• ••• •• ••• •• ••• •• • • •• •• •



• •
• •
• Complimenting the electronics project kit, •
• •


the FUZE team also bundles an 84Q-socket •

• •


solderless breadboard which you can use •

• •


to wire up interesting projects and use •

• •
• FUZE BASIC together with the Raspberry •
• •

• Pi and the FUZE 10 board to control the •

• •


components from the electronics kit. In •

• •


case you're wondering why it's called a •

• •
• breadboard, it's because in the early days •
• •
• •
• of electronics users would use a bread •
• •


board For the base of their 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' •

; logo mouse mat. •





• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••

• •• ••• ••• ••• • •••••• •••••• •••• •••• • ••• • •• •• • •



• •

�,#
• •
,. • The kit comes with two ring-bound books •
• •

o ",)
.i' 1J;



containing project ideas for the electronics •

� • kit and a programmer's reference guide •

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. •


• • ••••••••••••••••••••••••• ••••••••••••••

wwVLbdmpyblicat!oos.com Coding - Tips, Tricks & Fixes


Programming 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.

LIGHT THE FUZE


Getting the FUZE up and running is as simple as plugging in a standard desktop computer; but it's always worth running
through the process for those who don't know what to do.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Before you power up your FUZE, make sure that the


STEP 3 For now, use the Ethern et port, LAN cable, for
STEP 1
provided SD card is inserted into the SD card slot the FUZE's connection to the home network and
on the rear 10 backplate of the FUZE workstation. The chances are ultimately the outside world. You can set up the Wi·Fi but i t's always
the SD card is already inserted but depending on how the FUZE was easier to establish a wired connection first if possible. Connect the
packaged, it may be in the electronics kit box. Ethernet cable to rear 10 backplate of the FUZE.

. . . . .... . . .. . . .... . . . . . . .. .. ... . . . . . . . . . . . . . . . . ... . . ... . . ... . . . . . . ... . . .. . . ... . . . .. . . . . . .. .. . . . . . . . . . . . . . . . . ... . . ... . . ... . . .
' '

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.


- -
-


• •

BDM's i·Tech Special I Volume 27


Setting Up the FUZE

• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••

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.

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •

The First thing to notice is that it's significantly


STEP 7 STEP 10 To ensure you're running the latest software and
different to that of the standard Raspberry Pi programs, click on the F start button, followed
Raspbian interface. The launch panel and buttons are located along by Accessories > Terminal. In the Terminal enter: sudo opt-get
the bottom of the screen, as with a Windows-type setup, with a update && sudo opt-get upgrade and accept any changes
couple of icons on the desktop itself. and updates the system has to offer. This will update all your
installed software and system files.

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Programming with the FUZE

�e ttin ta rte ""-'- wit


F ZE BA I
FUZE BASIC is a marvellous programming language to begin learning to code with. It
greatly mimics the '80s BASIC versions from the 8-bit machines of the time, such as
the Commodore 64, ZX Spectrum and BBC Micro.

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:-____
----
....-----
... .. .. ... . . .. _ _ - · . -�
�----­
.

--- ----...,.._... ------·


--- -
--
- -- ·� -..�-...-
---
-
-
-- - -----
-
..

--

..._ ---·--- .......

--·-· .. .... ... ... - - .


---·-·-·-..··
.

.... _, . .. . ··- · ·� ·-··


-··
- ·
•"'-'Oi l O• I tt• •

--....---···--- .... ...


. ..... �
.. ______
_.._____.._
- ---....·---- ___............
...,. ....
. ..
--..,.--.-·--ra---
..,..

BDM's i·Tech Special I Volume 27


Getting Started with FUZE BASIC

• • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••

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

the numbers you've entered will now be displayed on the screen,


10 cls
in this case the number 60. Try more numbers and even different
20 input "What is your nome? " , n$
mathematical symbols.
30 print
40 print "Hello ; n$ "

Enter run to execute the cod e.

• • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • • • •

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
• • •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

STEP9 Dissecting the previous code we have the command


,

to clear the screen [CLS], then the Input command


asking for user input and storing the input as the variable n$. The
Print at line 30 puts a blank line on the screen, whilst the Print
command at line 40 displays the message Hello and the contents
of the variable n$.
• • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

STEP ? If you're old enough to recall BASIC from the early


days of com puting you'll no doubt remember that
,

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. • • • • • • • • • • • • • • • • • • • • • • • ••••••• ••••••••••••• • • • • • • • • • • ••••••••••

Press the Escape key to exit it.


STEP 10 There s a lot you can do in Immedia te Mode;
'

however to unleash the full potential of FUZE


,

BASIC you're best working in the Program Editor. To enter the


Progra m Editor type in the command new to clear any programs
already stored in memory and press the F2 key. As you can see,
Program Editor looks significantly different to Immediate Mode.

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Programming with the FUZE

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.

LET THERE BE VARIABLES


We've already looked at assigning some variables in the previous tutorial so let's extend that and see what else we can do
with them .

• • •• • • ••• •• • • •••• • ••• •• • • • •• • •• •• •• • •• • • ••• • • •• • • • • •• • • ••• • • ••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • •

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 .

• • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

What if we wanted to change the value of a


STEP 4
variable? Enter this listing:
• • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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

BDM"s i·Tech Special I Volume 27


Coding with FUZE BASI C - Part 1

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • •••••••••••••••••••••••••••••••••••• ••••••••••••••••••••••••••

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" "

Data "Thu rsday" , "Friday", "Saturday"


• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Data "Sunday "
Dim DaysOfWeek$(7)
STEP 6 Variables can do more than store numbers:
For DayNo = 1 TO 7 loop
Input "Hello, what is your first name? Read DaysOfWeek$(DayNo)
«1 f$ Repeat
Print For DayNo = 1 TO 7 loop
Input "Thanks, and what is your surname? " , s$ Print "Day of the week number " ; DayNo ;
Cls Print " i.s " ; Days0fWeek$(DayNo)
Print "Hello " ,· fS· ss· " How ore you
J " "'
· J Repeat
today?" End
End

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

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

STEP 7 Conditional statements allow you to make your


program do different things dependi ng on the user • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

input. For example:


STEP 10 The code from Step 9 is beginning to look quite
cls complex, using the Data command to store
Input "Enter your nome: " , nome$ constant data, creating a variable called DaysOFWeek using the Dim
If name$="Dave" t hen command and assigning it an indexed dimension (7). The code then
Print "I am sorry " ; name$ Reads the stored Data, assigns it a variable dimension from 1 to 7
Pri.nt "I am afraid I can't do that" and prints the result.
Else
Pri.nt "That is not a problem " ; nome$
Endi.f
End

Save as 'HAL' and Run.

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Programming with the FUZE

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.

"PALE BULBOUS EYES STARE AT YOU ... "

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."

BDM's i·Tech Special I Volume 27


Coding with FUZE BASIC- Part 2

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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.

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

You can now Save the code, call it Adventure (or


STEP 9
something) and Run it from the menu. Whilst it s not
, '

the most elegant code you will ever see, it brings in many different
elements and shows you what can be done with FUZE BASIC.

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Let's finish this routine off with:


STEP 6
Endi.f
Endif
Goto 29
Print "Sorry, you are dead . End of game. Press
Spacebar to start agai.n."
Whi.le inkey <> 32 cycle
Repeat
Goto 1

This closes the If statements then goes to line 29 (If you pressed the
,

Green button) to continue the game, skipping the end of


game routine.

• •••••••••••••••••••••••••••••••••••••• ••••••••••••••••••••••••
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Before you continue with the adventure, and map


From line 25 we start the end of game routine as STEP 1 0
STEP 7 the fate of our relucta nt space hero, we're going
stated on line 15, goto 25. This only works if the
to improve our code with some graphics. FUZE BASIC has some
variable lives equals 0; the player's lives have run out. It prints a
great graphi cal commands at its disposal. along with some other
sorry you are dead' message and asks to press the Spacebar to start
useful and interesting extras.
'

the game all over again from line 1, the goto 1 part.

www,bdmoublicatjggs.com Coding ·Tips, Tricks & Fixes


Programming with the FUZE

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)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . ... .... .
. .. .... . . . . . . . . .
... . .. . . .. . .

You may need to source your own images foryour


STEP 4
game. In our example, we found an image of red
and green buttons and copied to the /Desktop/fuze-basic/extras/
images folder. Now we need to add it to our code from line 15:

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.

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Continuing, we can use images of the interior of the


STEP 5
ISS if the Green button is pressed. Download the
image, put it in the images folder, name it ISS.png and call it from
the code whilst hidesprite hides the previous image.

Hidesprite (spriteindex)
U;date
Print "The door to space station opens . . "
ISS$�"ISS. png"
Loadsprite (ISS$, spriteindex, 0)
Plotsprite (spriteindex, 200, 200, 0)

BDM"s i·Tech Special I Volume 27


Coding with FUZE BASIC- Part 3

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • •• • • • • • • • • • • • . ................. .. . . . . .. . . . . . . . . .. . . . .. . . . . .. . . . . . . . . . . . . . . . . . . . . .

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 '

with FUZE Basic.

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • •

Addition ally we can add an image for the End of


STEP 7
Game routine and insert the code from line 39:

Print "Sorry, you are dead."


GameoverS="gameover.png"
loadsprite (gameoverS, spriteindex , 0)
Plotsprite (spriteindex, 200, 200, 0)
While inkey <> 32 cycle
Repeat
Hidesprite (spriteindex)
Goto 1

. . .... .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . . .

Here's a recap of the images we've used for the


STEP 10
graphics in our adventure game. The FUZE BASIC
manual comes with countless more commands to make better
use of the system, so read through it and expand on what you've
learned here.
• • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Once more the code has now expanded and as such


STEP S ,

you need to ensure that any reference to another


line is updated to reflect the new numbering; especially lines 24
and 38, which call either End of Game routine or continue the game
if the Green Button has been pressed.

"'
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 ..-.- ..
""
·-·
�'· ,...._ .. » ·�'·

1!-i._,•• ,,.. ,,.. ..�,


'"
prl"t ..,_ ...... t• ..,. ,..,vlf '" - •1'1•1 ....... ..,_. u.. .,.
, .,....
f
.,..,_ ,..........

1....,.11.. (tlo.ot._, .-u•-· 11


....... ,. ,.. ,....� . ..... - ... . ,
r...,. -
, � .. •• � ...,.,_ . ,. 1·- ,.f .,.,... •- .,.. .,. ��t.'t • ,., ,..,....,..
.,. �,....
, ..,._....._.,.. .-.
h't h-11-·1
fi"'1!"' ..,., ,..., _.... ce.. ti�l-- - ...... ""' - - •
.,..V.t ..,.,. - ..... • 1hft, • lto91 1•'1
11 1..1...... ... .... "

"""'' "Pnu l:h � b r, ..- •


..... �.,. - Jl UCJ•
.._.,
... u
oiM
N....l . te f...UU ....I
-h
,...,t •n. .,.. w .. iN••• •' 1hf ..,.... ·�u.. ..-. u•.•r •• ,w. •
s:st-
T "1. .·
ss•.,. clss
t . _u.o-... 01
-J1• (...H....
....., h
l
*· M, ••
_,
plet •

...
....
.u
,
,.-In! -,...-, ,... •• 4oM
.
-'....... h· ...--.-
- •a-- ...
·'"""· ,..., .,
,.,.,.,.,..Jt.. c ....-u••••· 100 1110, .,
,.. l nt lrof ef� Ptf'�1 �...._. .. tiiOI'I ... i.. •
....� tl'lf'l 0 Jl �)..
"

·-
...
._ _ ·_ ...
-
--·
--

www,bdmoublkatjgps1rom Coding -Tips, Tricks & Fixes


Programming with the FUZE

a B rea"'"' .....,oa r"'"'


A great way to learn circuits is to use a breadboard. You can use a breadboard with
FUZE BASIC, or Scratch and Python, to control LEOs and other simple circuits. Here
we'll show you how a breadboard works.

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.

BDM's i·Tech Special I Volume 27


Using a Breadboard

• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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.

RECREATING THIS IN A BREADBOARD


Wrapping wires and circuits around each other isn't going to be much fun, especially when you're trying to figure out how
something works. That's what a breadboard is for: the holes enable you to connect one item to another.
• • •••••••••••••••••••••••••• ••••••••••••••••••••• •••••••••••••• • •• • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Let's now recreate this simple LED circuit on a This is


STEP 1 STEP 3
breadboard. With the positive and negative cables the part
from the battery connected to the top of the power rails, take a red where most people mess
connector and slot one end into a hole on the red line, and the other up. Take the other leg
end into hole A1. of the LED and connect
it to hole D2. This is the
next row down. If you
connected it to another
hole on line 1, such
as D1, it would be the
equivalent of touching
both LED legs together.
• • •••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••••

Now take your resistor and place it next to the LED


STEP 4
leg in slot E2 (also on the second line). If you look at
the photo from Step 6, this is the equivalent of 2 (the part where the
LED and resistor are connected. lnsert the other end of the resistor in
a hole on the negative power rail and your LED will light up.
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • •

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.

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


Programming with the FUZE

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.
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Remove the 9V battery and battery clip if it is The pins


STEP 1 STEP 3
still connected to the breadboard, and slide the marked
breadboard into the top of the FUZE so the wires and LED are near the "GPIO" and numbered
10 board. Let's take a closer look at what the 10 board has to offer. 1·7 are more interesting.
These can be turned
on or off from inside
programs, or at the
command line. When
turned on they provide
3.3V, and when off they
provide nothing. These
On/Off switches can
be used to activate and
deactivate components
you attach to the
Raspberry Pi.
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Start by connecting a blue cable to the pin marked


STEP 4
GND in the Supply section on the top right of the
FUZE 10 board. Connect the other end to the leftmost hole in the
blu e rail, now running alon g the top of the breadboard. Connect
• • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •
a red cable to the socket marked 3.3V on the supply section in
If you look closely at the 10 board you'll see a section the bottom right of the 10 board. Connect the other end of the
STEP 2
of pins marked "RPI Header . These match the pins " cable to first hole in the red rail running along the bottom of the
that are on your Raspberry Pi. On the right side of the board are a breadboard The LED will come on
. .

number of output sockets. The PWM and


supply sockets provide steady power
(3.3V or s.ov as marked).

BDM's i·Tech Special I Volume 27


Using the FUZE 10 Board

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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"

Press F3 to run the program.

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • •

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".

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Programming with the FUZE

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).

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Once the arm begins to rotate clockwise it will get


STEP 4
to the limit of its range and start clicking. When it
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . ..

starts this quickly enter the command:


Start by plugging the robot arm into one of
STEP 2 ArmBody (0)
the spare USB po rts on the back of the FUZE
workstation. Ensure that the arm has its batteries correctly in place This will stop the arm from moving.
and that its power switch is On. Now open FUZE BASIC and remain
in the Immediate Mode.

BDM"s i·Tech Special I Volume 27


Using a Robot Arm with FUZE BASIC

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

STEP S Now enter: Press F2, and type in the following:


STEP S
ArmBody (-1) PROC ResetArm
PROC Displayinstructions
This will start moving the arm anti<lockwise. Again, when it starts to
End
click enter the command:
DEF PROC ResetArm
ArmBody (0) ArmBody (0)
ArmShoulder (0)
To stop it from moving.
ArmElbow (0)
ArmWrist (0)
ArmGripper (0)
ArmLight (0)
ENDPROC
DEF PROC Displayinstructions
CLS
FONTSCALE (2,2)
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • Ink Red
=

Print "I, Robot"


STEP 6 The other commands to make the arm move are:
Ink White
=

ArmShoulder (x) - where x can be 1, -1 HVTAB (0,2)


or 0
This is the start of the program, resetti ng the arm and preparing the
ArmElbow (x) - where x can be 1, -1 or 0
on-screen display.
ArmWrist (x) - where x can be 1, -1 or 0
ArmGripper (x) - where x can be 1 -1, or 0
ArmLight (x) - where x can be 1 or 0
Note: you can press the up arrow key to re-enter the previously
typed commands so you can quickly stop the arm's movement
,

when it reaches its limit.

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

Now to expand the program to control the arm:


STEP 9
Print "Press : "
Print
Print "1 or 2 for Body Left & Right"
Print "3 or 4 for Shoulder Up & Down"
Print "5 or 6 for Elbow Up & Down"
• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Print "7 or 8 for Wri.st Up & Down"
Let's create a program allowing you to move the arm Pri.nt "9 or 0 for Gripper Open & Close"
STEP 7
around freely. There are some new commands here: Print "Enter to turn the Li.ght On or Off"
PROC and DEF PROC, that enables BASIC to jump to a PROCedure, Ink =Red
another part of the program then back with ENDPROC. FONTSCALE
, Pri.nt "Spacebar to stop all movement and turn off
determines the size of the on-screen print display and HVTAB is an X the li. ght . "
a nd Y coordinate system to print on-screen. ENDPROC

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

STEP 10 Now we need to process the user input. There's


a lot here but type the content as shown in the
screenshot. Save the code and Run; you can now control the robot
arm using the number keys 1 to 0, the Enter key for the light and
Spacebar to reset everything .

wwVLbdmpyblicatlpos.com Coding - Tips, Tricks & Fixes


Programming with the FUZE

FUZE BA I Exam .......,. es


Pa rt 1
FUZE BASIC has an impressive Following of coding experts and enthusiasts who have
selflessly provided their code For others to learn From and use. These stalwarts of
FUZE BASIC have Forged some amazingly detailed examples, which we'll take a look
at over the coming pages.

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.

• ••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••

I n the Program Editor click the Load button and


SHOOTER
browse through the folder /fuze-basic/Demos
• • ••••••••••••••••••••••••••••• ••••••••••••••••••••••••••••••••
until you find Shooter.fuze. With the program listing loaded, scroll
CARDIOIDS Simon Plouffe is a Mathematician who, back down to line 247 and change the "player2.png" entry to "Player2.
in the 70s, created some incredible cardiod png" -adding a capital P. This is a basic side-scrolli n g shooter and
images by dividing a circle into prime parts and drawing lines based you'vejust fixed an error in the code.
on mathematical spaced points on the circumference. Anyway,
enter this and be amazed:

BDM's I·Tech Special I Volume 27


FUZE BASIC Examples- Part 1

• • • • • • • • • •• • • • •••••••••••••••••••••• • • • • • • • • • • ••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

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!

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Programming with the FUZE

FUZE BA I Exa m......, es


Pa rt 2
Continuing from the previous pages, here are ten more excellent examples of what
can be done with FUZE BASIC. Take what you want from the code, alter it and insert it
into your own routines to fine-tune your program.

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.
• • • • • • • • • • • • •••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • •••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • •

Amiga owners will have fond memories Scankeyboard.fuze is an extremely


AMIGABALL KEYBOARD INPUT
of their futuristic computer back in the hand y bit of code to load up. It's a
late '80s and early '90s. The Amiga was a pretty impressive home simple program that will display the key pressed on the keyboard,
computer, even by today's standards and its iconic Bouncing Ball which is a great resource when it comes to creating keyboard
routine will forever be remembered by those who grew up with inte ractions with the user and the program such as a game. Just ,

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.

. · · - .. .

••

• • • • • • • • ••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • ••• • • • • • • • • • • • •••••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • •

Whilst an analogue clock on the


BBC MIC RO::B IT This code snippet will look for and
ANALOGUE CLOCK
screen, complete with second detect any at tached BBC mi cro:bit or
hand, may not sound too interesting, ther e's a s ur pr is ing amount of Arduino compatible devices that you've attached to the FUZE 10 or
useful code within this particular routine. Clock.fuze is in the Demos Raspbe rr y Pi GPIO pins. It's incredibly handy for helping you cr eate
folder and once loaded up you can strip all manner of handy code the code behind your h ardware project.
snippets from it.
d<tVicu •DETfClOEVIClS
CLS
IF drm.*l • FALSE fH(N
PRINT "No o..;c.. lound'
END
(lS(
PRINT ct.�cea; "'6fovl<f'S found•
PRINT
FOR 10 • 0 TO dovo<"LOOP
IF DfVKfTYP� ID) · 1 THEN
PRINT -sec mJ(rO:btl �0•"'; 10
ENDIF
IF DEVICETYP[(ID)• 2 THEN
PRINT "Ardu•no 01comp.t•.,.edtviCt 10-"· 10
ENDIF
REPUoT
END

BDM's i·Tech Special l Volume 27


FUZE BASIC Examples- Part 2

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

This little snippet If you want to incorporate mouse


FONTS MO USE CONTROL
of code, although pointer and button use in your
simple, will display some of the code, then this will certainly help you out. It's a fairly simple bit of
available font sizes of FUZE BASIC. code but remarkably effectv
i e and it can easily be inserted into your
The maximum size is 20, so alter as own programs.
you wish:
1CLS
CLS 2 LOCICMOUSIO (TRUE)
3 I«>USEON
FOR si ze = 1 TO 7 LOOP 4 COLOUR - VIUT£
5 RECT (100, 100, 150, 50, TRUE)
INK = RND(30) 6 :INIC. BLACK
7 PAPER -:IT[
FONTSIZE( size ) 8

PLOTTEXT ("C 1 1ck Me", 105, 125);


PRINT "Hello" 9 UPOATE
10 clicked - I'ALSE
REPEAT 11 LOOP
12 GnMOUSE (x, y, z)
END 13 XI' Z <> 0 THEN
14 XI'
15 XI' ((x
> 100 ANO X < 250) TltEN
y
> 100 ANO y < 150) THEN

16 c11 eked - TRUE


• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 17 ENO:II'
18 E NO:II'
If you're working with a Raspberry Pi 19 E NO:II'
R P I SENSEHAT 20 REPEAT UNTIL cl1cked
Sense HAT, then th e following code will 21 SETMOUSE (QtYOTtf / 2, GltUGHT 2)
return the current value of the HAT's accelerometer: 22 LOCICMOUSE(FALSE)
23 ENlli

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

19 H:JNT �H.t.T'S (O)


20 :r• .. ORAHGl.
21 PUHTAT co, S): "a�o�uon suus"
22 f-CNt but 0 TO HIJlo'IVTJOirri!ao (0) 1 LOOP
2J

INC 81U GffTGIIti:£N


24 PRJNTAT (bUt ), 6); "f": but


25 lNIC. • Y£LLOW
26 P1UNTAT (but ), 7); <.HBVTTOfol (0, b�t)
27 ll(P[AT
28 INK • ORANC:.t;
"Anal09
0)
(
29 PIUNlAl (0, 9): Statts"
30 FOR ax .. 0 TO �Ale�� • 1 LOOP
31 IHIC. BJUGUTGRllN

32 f'MJNlAI (AX • &. 10); "f"; U


]) INK • Y{LLOW
J.4 PRINTAT (ax • &. U): (iifA.lCI!I (0, U)
l5 lt(P(AT

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

40 ntNTAT (li.U ) , l4); •1"; t'


It
•1 Ullt • Y£LLC*
• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 4l P1UNTAT (ii.Oit ) , lS): C'"STttA.T (0, .at)

4} Rr"Pf:AT
44 UPMT[
And this code will return the value of 4 S aLKAT
HATCOMP A SS 46 P'tllNT
the SenseHAT's compass: 4 7 (�

CLS • • ••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••••

LOOP Finally, if you're after something a


REACTION TIMER
PRINT "Sense Compass X=".' SENSECOMPASSX little competitive with your family,
PRINT "Sense Compass Y-".' SENSECOMPASSY then load up reaction.fuze from the Demos folder. When run, this code
PRINT "Sense Compass Z-"·
- ' SENSECOMPASSZ will test your reaction time by hitting the Spacebar when indicated. see
REPEAT how fast you can get it, and see if you can hack the code.
END

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

www,bdmoublkatjggs.com Coding ·Tips, Tricks & Fixes


To help you on your way to becoming
a better coder, we've included some
great Python code examples for you
to type in, use and expand for your
own programming projects. There's
number guessing, random number and
password generators, scrolling text
examples, an on-screen digital clock,
the beginning ofa text adventure, a
classic game of Hangman and much,
much more for you to snip and use.

The only limitation here on is your own


imagination but we've also added a
handy glossary of terms in case you
get stuck along the way. Wherever
you go next in coding, whether it's on
to learning more languages,creating
amazing apps and games or helping
out and teaching new coders to avoid
the pitfalls you made, the next steps
are upon you.

Keep on coding and keep on learning.

• • • • • • • • • • • • • • •••••••••••••••••••• • • • • • • • • • • • • • • • • •

126 Python File Manager


128 Number Guessing Game
130 Polygon Circles
131 Random Number Generator
132 Basic GUI File Browser
134 Vertically Scrolling Text
136 Text Adventure Script
138 Python Digital Clock
140 Hangman Game Script
142 Where Next?
144 Common Coding Terms
FILEMAN.PY

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())
- -·

input('Press Enter .. .' )


...
- ... .....
-·-
·-- ·­
fila.closa()
.-
· --· �
__
'--
·-··
....
&
._
-.-
. . .
·-·-­
,._,__
-·--
de£ Write() :
path=input("Enter the path of file to write or create:")
i f os.path. iafile(path):

print('Rebuildinq the existing file')
else:
print('Craatinq the new file')
text=input("Enter te>tt:")
fi.le=open(path,"w")
file . w rit e(text)

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')

a.tr.nt ,.. and •·n• is;� S..," 17:41.11 10t7


def Dirlist():
I a.... ttw opt'*",.,....,:
path=input("Enter the Directory path to display:")
sortlist=sortad(os.listdir(path))
i=O
while (i<len(sortlist)):
print(sortlist[i)+'\n')
i+=l

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

if os.path.is1ile(patb)-True: S.List fll es in a directory


print('File Found') 6.Check flle existence
else: ?.Move a flle
print('File not found') 8.Copy a fll e
if fp==2: 9.Create a directory
path=input("Enter the directory path:") lO.Delete a directory
os.path.isdir(path) ll.Open a program
if os.path.isdir(path)=False: 12.Exit
1
I
print('Directory Found')

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: • •

displays the current time and date.


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.
• •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


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

number between 1 and 30.')


while guessesUsed < 5:
guess=int(input('Guess the number within 5 guesses.. .'))
Ol'HHSUsd • •
......u ... .. ...... �' ) guessesUsed = guessesUsed + 1
...,... ,. .. , .,.. ,,_,J.ftt( l , 10)
pt't.nt( ''" .. ..... • J\. "'''*&ftl ., • ,-� ...,.... t .-d ..... , if gue ss < number:
�c':
.,..U ...lnt (&."JllUt( 1"- ,.,..., •llhlft S llM'S'IoH )) print('Too low, try again.')
JI.IUM� psM• sus.M • 1
..-ss c ,. ,.,.. : if guess number:
>
prun( T• IM tr7 ,._.,_ )
..,...,.,.,...,: print('Too high, try again.')
pnnt( ,.. ft� HJ �Lft )
f p$5 - ,...:..., if guess = number:
...
if CUHt ... ....:...,. brealt
...,.,..us.4 • su(.,.,....., ...
pnnt( • ..... • You cun•M COHK1h 1ft • e.tnMSU'5M • if guess number:
==

&f CUHS I• �; guessesUsed = str(guessesUsed)


IUIIb•r SV (�I')
print( Well Name + '1 You guessed

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 •

F.QN!'I�t Bun .Qptk>nt W!n<IO'M: t:ltlp

Python 3....2 OCt It


[GC.C 4.51.11 on
Type "copyr&.aht'", •cr•ditl" or '"ltctnso()'" for 110rt tnforaatton. guessesused • 0
""" ••·••••·••••···•·••••••••••••••• �START •••••••••••••••••••••••••••••••• ,.... .. :LJ'1)Ut( �llo1 \.h,at u y' jr .,.., )
»> f'Uibtor • r�.r.ndint(l. JO)
Ht!llol \��twit 15 your NM? O•v�d pnnt( Grnt.l"P • .,_. • I .,. ttunln"l of • ,..._b•r Nt���t•n 1 •ncl 30. •)
Grutinp. o.nid, I•• thlnki..rc of 1 n�.Mtr ben..� t and �­ �1 �S5eSUS� < 5:
Gutss ttM ,..ber •uth&.n S IU"Itt• . .le guessnnt(lnput( Gil tl 1"1..-.l "UI'Itn S IUf'SMS . ))
"

Too h.t.-,. try •'-"· guess.sused • cutsstsUs.d • 1


Cit.MSS tt. �I' WlthlR 5 ..,...Q'I.,,20 auess < ,.,..._,:
Too hl£'t, try 9ln. pnnt( lo t ti'L )
ttM ,..,.,
... •utt'lrn 5 """"'·. ,15 autPSS > nl.lllber :
done. Ontdl YCM.I aunMCt corrt<t.ly 1n) psses. pnnt( ·a.11 t•r ap1n . •)
a pss- .......,,:

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•

• •••••••••••••••••••••••••••••••• REStART ••••••••••••••••••••••••••••••••


• >» Luck•O
• ErdJr.ou•rM6ofl.r•ndint(l, IS)
• Yuor Qharacter's •�•ts •r• II follows:
• (R&random.r�int(l, 20)
• Luck•r¥1doa. r¥Kimt(1. 10)

• Endurenct: -4 prtnt(*Yoor �r.ct•r 1 st•t• lrt • • tollONS:\n·)

• COIIb•t lllo�tu'C 5 print( \n�nduran<•: Endurance)

• prin t('"C�tt.t ll:•tln ·, (lit)


• LUCk: e
• >>> •••••••••••••••••••••••••••••••• �START •••••••••••••••••••••••••••••••• Loc� ·, Luc:k)
prlnt( ..
• »>

• vuor ch.iracter•s st•ts .,. •• foll�:



• Endur.-.ce: l
• COIIIbott �tU'C 20

• ludc: '
• �>> ••••• ••••••····••
••• •••••••••••• RESTART ••••�•••••••••••••••••••••••••••
• >»

• Yuor �r.ct•r's st•ts ''' IS f•ll...



• EndurMte: 12
• c.ea.t a.t� 1•
• luO: t

• >»

• •
• •
• •

• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

www,bdmoublicatjggs.com Coding ·Tips, Tricks & Fixes


Po y'-'lo n
POLYGONCIRCLES.PY

There's lots of Mathematics used here along with some


in tricate coo rdi nate m a nipulation with the Turtle module.
Enter the code and execute it to see how it turns out.

from turtle import*


from math import sin, cos, pi
r 200
inc=2*pi/100
t O;n=l. S J
for i in range (100) : J
xl=r*sin(t); yl�r*cos(t)
x2=r*sin(t+n) ;y2=r*cos(t+n)
penup(); goto(xl yl ) ,

pendown();goto(x2,y2)
t+=inc

• • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • ••••••• •


Graphical Enhancements •




There are several ways in which you can improve this code to make For example: •

i t more interesting. You can insert colours, perhaps a different •




colou r for every line. You can display a message inside the circle for i in range(lOO): •


and have the Turtle draw around it. Let your imagination run wild xl=r*sin(t); yl•r*coa(t) •

on this one. x2=r*sin(t+n);y2zr*coa(t+n) •




speed('fastest') •


penup(); goto(xl,yl)
Turtle's graphics can take a while to map out and draw, d epending •

pendown();goto(x2,y2) •
on how big and how intricate an image it is you re designing. Whilst •
t+=inc
'


the effect can be qu ite stunning, it is limited by the amount of •

time it takes to display an image. Therefore it's worth seeing if the •



This will run through the code at the 'fastest' speed possible for •

function turtle.speed() will quicken thing s up. •



the T urtl e It certainly makes a difference and is worth con sideri ng
. •


if you're drawing Turtle images for games or presentations. •
Turtle.speed() comes in various values: •




slo ,.. st •

• •
slow •

normal file fd¢ f.Qrmat Sun llJ>Uon5 �ndows l:jelp •





fast I � tu r tle t • •

fastest fr� •ath 1 r� sin. cos. p1 •

r•200 •

tnc•2•p11100 •

t•O:n•1.5 •
You can experiment with the various speeds by adding the function to 1 " r;tnge( 100): •

in the for loop,just b efore the penup line. xl•r•stn(t): yl•r•cos(t) •



x2•r• sln(t• n);y2•r •cos(t+ n) •

spt'ed( f •t t ) •

penup(); goto(xl.yl) •

pendown():goto(x2.y2) •

t••inc •













• • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••• • • • • • • • • • •
Polygon Circles/Random Number Generator

Ran"-"om Num...,er '-'enera tor


User input and the ability to RNDNUMGEN.PY
manipulate that input are important It might be simple but this little piece of code will ask the user
elements with any programming for two sets of numbers, a star t and a finish. The code will then
pluck out a random number between the two sets and display it.
language. It's what separates a good
program from a great program, one from random i mport *

that allows the user to interact and print("\n>»>»>»>Random Number Generator<<«<<<<<<\n")

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

: While an easy code to follow, it could be more interesting if you


For example, the code could be edited to this:


: 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

: through a Tkinter window or even the Ticker window that's



print("\n>»»»»>Random Turtle I11149e«««««\n")
: available on Page 128.
• nmbl=int(input("Enter the start numbe�:: "))


nmb2�int(input("Enter the aeeond number: " ) )
: Furthermore, the core of the code can be used in a text adven t ure
• nmb3�int(input("Enter the third number: "))
: game, where the character fights s omething and their health ,
nmb4=int(input("Enter the fourth number: " ) )

: along with the enemy's, is reduced by a random number. This


; can be mixed with the previous code From Page 90's Number
: Guessing Game, where we defined the stats For the adventure turtle.forward(nmbl)

: game's character. turtle.left(90)



• turtle.forward(nmb2)

: 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



• •

• ••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

www,bdmoublkatjggs.rom Coding -Tips, Tricks & Fixes


Basic Ul Fie Browser
Fl LEB ROWS ER.PY

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.

from tkinter import Tk

from idlelib.TreeWidqet import ScrolledCanvas,


FileTreeitem, TreaNoda
import os

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 •-

sc.frame.pack(expand=l, fill="both", side= "left") J


·-

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)

from tkintar import • file.add command(labal 'Opan', command • Opanl'ile)


from tkintar import ttk fila. add command(labal = 'lxit', command =


from tkintar.f!ladialoq import aakopanfilename lambda: axit())

root = Tk( ) menu.add caacada(label • 'l'ila', -nu • fila)

daf Opanl'ila( ) : root.mainloop()


name aakopanfilaname(initialdir="C : /",

filatypaa =(("Text File", �•.txt"),(�All


Fi laaH,N*.*")),
It's not quite the same but this code allows you to open files
Utla "Chooaa a fila."
in your system via the familiar Windows Explorer. It's worth

)
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:)

w... open. (n.AJM:, ' '1 oa.ri1•:


prlnt(UeeFlle.read())
t'XCept:
print ("H f� � r ned")

TJ.tlc - root.tltlc( • s. • �rr.•r ")


label • ttlr:.La.bel(l'OOt., teat. •"F1.1e C .•,foreground•" l",tont•("Helvetl.c:4", 16))
label ,Pf!CIC(I

.eAU • MenU(l'OOt)
root.coatlQcunu.-.enu)

tue.•Ckl_ ,... ni!CI(l&bt:l • •ep.a•, c::c-And • Open1'1le)


hle.add_c:o.a.nd(l•t>.-1 - •E•It", c-.nd- aa.J.t())

root u.l.Al.oop()

l.ft: 11 Cot 21

• •
• .......................... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . .. . . .. .. ............................. . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . .. . . .
.

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


Text
EPICSCROLL.PY

We've used the poem Cimmeria by Robert E. Howard for the


code's scrolling text, along with a dramatic black background
and red text. We think you'll agree, it's quite epic.

import pygame as pg
from pygama.locals import *

p g .init ()

text list = "'

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.

Vista on vista marching, hills on hills,


Slope beyond slope, each dark with sullen trees,
Our gaunt land lay. So when a man climbed up
A rugged peak and gazed, his shaded eye
Saw but the endless vista - hill on hill,
Slope beyond slope, each hooded like its brothers.

It was a gloomy land that seemed to hold


All winds and clouds and dreams that shun the sun,
With bare boughs rattling in t he lonesome winds,
And the dark woodlands brooding over all,
Not even lightened by the rare dim sun
Which made sq u at shadows out of men; they called it
Cimmeria, land of Darkness and deep Night.

It was so long ago and far away


I have forgot the very name men called me.
The axe and flint-t
ipped spear are like a dream,
And hunts and wars are shadows. I recall
Only the stillness of that sombre land;
The clouds that piled forever on the hills,
The dimness of the everlasting woods.
Cimmeria, land of Darkness and the Night.

Oh, soul of mine, born out of shadowed hills,


To clouds and winds and ghost.s that shun the sun,

How many deaths shall serve to break at last


This heritage which wraps me in the grey
Apparel of ghosts? I search my hea.rt and find
ria , land of Darkness and the Night!
C imme

'". split( '\n')


Vertically Scrolling Text

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()

file £dlt FQt"mal Bun .Qp llont Won dows l:ltl p


def make_ text(aelf,mesaage):
font = pg.font.SysFont('Arial', self.size) ' " . spht{ '\n' )
te xt = font.r
ender(message,True,self.color) cl Crechts:
ract = text.get rect(centar = (se lf.s rect. IPf _ n _(Stlf, SCrttf\_rtct, 1st):
i i1;
self.srect • scree�rec1
enterx, aelf.arect.centery +
c s elf.buff _ centery) ) tlf. lst • 1 st
s
••If.an:t • 1$
return taxt,ract self.color • (215.0.0)
stlf.buff_centery • stlf.srect.ht�t/2 • S
stlf.buff_lants a 50
.
stlf t�tr • 0.0
def m ake surfaces (self): sdf.6el1y • o
self·••ke_surfACtl()
self.text = []
for i , line in enumerate(self.lst) :
t Nkt_ttllt(..lf...lutf):
1 = self.make text(lina) font • Pl.font.Sy,�ont( • s.lt.s�) •

tellt - font.r�r(.. s...- . ,141f.color)


l[l) .y += i*self.buff linea rtct • ttX,.Ift_rtct(ctnttr • (telf.sr.ct.c�ttrx, 5tlt.srect.c�ttry •

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,....

if pg.time.get _ t icks()-self. ti.me r > self.delay: self.te•t._,.nd(l)


self.timer = pg.time.get _ ticks()
41 updatt(ltlf);
for text , rect in salf.taxt:
r.
Pl-t�.,et._UCb( )•Mif.tUMr )> Mlf.dt'lay:
self.tu.er • pe.tu.e. t.ttcks()
rect.y -• 1 tut. rtct nl .tut:
r.-c:t.y ·• l

def rendtr(stlf, surf):


def render(self, surf): tt11t, rtct Mlf.tt•t:
surf.bltt(ttllt. rect)
for text, rect in self.text:
screen • pc.dtspl•y.st\Jiode((IOO.IOO))
surf.blit(text, rect) scree"-rect • scr.en.cet_rtct()
clOQk • Pl· t�. Clock ( )
runntnc-
screen = pg.display.set _ moda((800,600)) cred • credtts(scret�rtct. ttllt_last)

screen _ rect = scraen.get _ rectO


• rurwunt:
clock = pg.tima.Clock() ennt ar pe.tvent ..t(): .
1 event. t1pe QUIT:
running=Trua
••

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

screen.fill ( (0, 0, 0))


cred.updata()
cred.render(screen)
P9.display.update()
clock. tick(60)

•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• •





• 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.

• ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

wwVLbdmpyblicat!oos.com Coding · Tips, Tricks & Fixes


Text A ve ntu re t
ADVENTURE.PY

The Adventure game uses just the time module to begin


with, creating pauses between print functions. There's a help
system in place to expand upon, as well as the story itself.

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)

print('" You find yourself at a small inn. There's


little gold in your purse but your sword is sharp,
- _ , ,.... .
_ ..... ,.... . .. . _
,.. _.. .. _, ... ... . ..... ... ..._. and you're ready for adventure.
- - ..· - - - --·

_. ...,.,._ ... . ...... 1# ....---
. ... -·
With you are three other customers.
·�···
�...,.
...-c .. ,_ ___ 1
't A ragged looking man, and a pair of dangerous
.........,
looking guards.'")
.
.....
. . ....
f*ll'llC ......_
.,...
....
..
. -.
... ..-a I

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 . . . ) "

print("\n" *200) time.sleep(S)


print('"You walk up to the dangerous looking guards exit()
and greet them.
The guards look up from their drinks and

snarl at you. if name ==" main
"What do you want, barbarian?" One guard reaches start()
for the hilt of his sword. . . '" )

••••••••• •••••••••••••• • • • • • • • • • •••••••••••••••••••••••• • • • • • • ••••••••••••••••••••••••• • • • • • • • • • • • • •••••••••••••••••••••••• •••• •

: 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)

: behind improving the chance of a better hit based on your or your


prtnt( t� find yeu¥self •t • ..all tnn. T�re·s ltttlt ..ld 1ft ,our purse

.,.. ,... ,.
. ,...., ,., .achlftt\lf't
: opponent's combat skills. current health, strength and endurance. lbut )'eur ....,,. ... stt.p
.r
• 1t1 y..., .,, tftrH other c�t-.rs
You could create a game of dice in the inn, to see if you win or lose .... l"' .,...
l ..,... ,._,, of .,_.,...,. l.ok... .,. ,
• . •" )
: " r 1

; 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.










• • • • • • • • • • • ••••••••••••••••••• •••••••••••••••••••• •••••••••••••••••••• •••••••••••••••••••• ••••••••••• • • • • • • • • • • • • • • • • • • • • • • • • • • •

www,bdmoublkatjggs.rom Coding ·Tips, Tricks & Fixes


ita
DIGCLOCK.PY

This is a surprisingly handy little script and one that we've


used in the past instead of relying on a watch or even the
clock in the system tray of the operating system.

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

F1le Ed1t Tab


s Help

D �I

d•t t1c:k(tllnol• ' ) :


• ;et the current t�• ,,.. t�o P<
tliM2 • tlao.strftla.t( '\tl 'Ooi ,, )
tU.e2 1 · u.. ,:
t�•' 9 u..2
cloek.conrta( ton•t*2>

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

filo �d< ._ � lll><


•ont � t!O'P
.. ....
.. dunt.,. tk


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 •

• root.title( 'Stop Watch' )


•• quick glance to where we've placed it on the screen.
• root.reaizabla(Trua, Falaa)

• root.grid _ columnconfiqura(O, vaight=l)
• The Tkinter box
• can be moved around without affecting the time,
• padding • dict(padxa5, pa dy•5)
•• maximised or closed by the user at will. We haven't given the
• widget = StopWatch(root, **padding)
• Tkinter clock window a title, so you can add to that easily enough
•• viclqat.grid(aticky•tltintar.NSEW, **padding)
• by snipping the code from other examples in this book. root.mainloop()


••
: Another area of improvement is to include this code when daf in it (aalf, maatar-Nona, cnf={}, **kv):
• padding • dict(padxakv.pop( 'padx', 5), pady=kv.
: Windows or Linux starts, so it automatically pops up on the
• pop('pady'. 5))
• desktop. See also, if you're able to improve its functionality by

• super(). _ _ init _ _ (maatar , cnf, **kv)
• including different time zones: Rome, Paris, London, New York,
• self.grid _ c olumnconfiqura(l, vaig ht,l)
• Moscow and so on.

• self.gr id rov c o nfiqura(l, vaigh�l)
self. total • 0
self. _ _ label • tltinter.Labal(self,

tka.nt.r text-'Total Time:')


....
self. ti- • tkintar.StrinqVar(self, '0.000000')
c St�tch(tklnter .Fr...): self. _ _ display = tltinter. Label(aelf,
tc:1•111Mthod TotIInone 2 259363 textvariab
la=salf. time)
aatn(cls) !
tktnter.HoDefaulf�t() Stop s
elf. button • tkintar.Button(aelf,
roo t • tktnter. lk()
p a r. )
root . tnle( .to text=' Start', co����nct-aalf.
��& click)
root. rniuble( r , f • )
root.ar&d_colunnconflture(O, �s;ht•1)
self. labal.grid(rov•O, column=O,
paddi�l • dtct(pad�·S. pady•S) sticky-tkintar.Z, **padding)
wld&•t - StopW�tch(root. ••paddtnl>
• self. diaplay.grid(rov•O, column•l,
•• wtd&et.•rtd(St1Cky•tk1nttr.HSEW. ••padd1ng)
root.•atnloop()
sticky=tkinter.EW, **padding)
6f _tnu_(nlf, •nttr•l t, tnf•(), ..kw):
paddln& • dict(pad�·kw.pop( 'p�d• ' , 5). p&dy•kw.PQP('pAdy' , 5)> self. button .grid(rov•l, column•O,
super( )._anit_(muttr, cnf, ..kw)
stlt.aridlco1umncontaaurt(1, �atnt•1) columnapan=2,sticky•tkinter.NSEW, *�dding)
ltlf.aridLrowconfigurt( l , �i�t·1)
self. total • 0
__

stlf._libtl • tktnttr.Labtl(stlf. teKt•'Tot•l l�: · ) daf click(aalf):


self ._ll.lflt • tkinttr .s t rlncvar< u lf . ' 0 . (1(
oouo· >
stlf. __
dtspl•y • t kin t tr. L.O.l(st lf , tt•t�arlablt•�elf. t�•> __
if self. _ _ button [ 'text'] == 'Start':
stlf. button • tktnttr.tutton(stlr, tt•t• Start
__ co-.and•stlf. tllCk)
• __

self. l�l .arad(row•O. colUMn•O, sticky•tklnttr.E. ..paddtna>


__ self. _ _ button['taxt'] • 'Stop'
stlf. dispt-,.artd(row-0. colunn•1, sticky•tklnttr.EW. ••padding)
__

stlf,_Jbutton.ar-d(row-1. colu.n•O. colu.nspan•2, self. _ _ start = tillla .clock()


stacky•tktnttr.NSEW. ••padd�>
sel
f. r • salf.a
counte fte
r i
dla(Hlf. -update)
_. _chc.k(self):
self._buttonI t J ••
else:
elf. _
s button! J • p·
stU._Jtart • tutt.clock()
self. button['taxt') = 'Start'
stlr._counttr • stlf .af ttr_•dlt(..lf. __ updatt) aelf.after cancal(salf. counter)
.
self.�tton( J • Jt•t
self.af•er_c�tlC�tlf. __ cownter)
daf __ update(aalf) :
n
ov = t�.cl oclt()

: Another example, expanding on the original code, could be a cliff = nov - salt. start

: digital stopwatch. For that you could use the following: se lf. atart • nov
• •
• self. total +a cliff •
• •
• self . total))
••
u-.••t( '(: .6f)' .f onaa t (aalf.
• tltintar •
•• ter •
tiJM self. - coun • Hlf.after - idla(self. - - update) ••

• •
• •

• i f name == ' main ' .
. ••
• class StopWatch(tltintar. Frame): •
• StopWatch.main() •
•• •
• •
• •
• • • • • •• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • •

wwVLbdmpyblicatlpos.com Coding · Tips, Tricks & Fixes


Ha n ""'' ma n t
HANGMAN.PY

We've made a Hangman game board (the gallows) out of


characters that can be displayed in the IDLE Shell, along with
a huge bank of words to randomly choose from.

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

luggage responsible ambassador circumstance


====-' ' ',
"' congratulate frequent'.split()
re turn bank[random.randi.nt(O, lan(bank)))
+---+

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)

daf guass(salf,latter) : print ('\nGoodbya!\n')


if latter in salf.word and latter not in self.
guessed_ letters: if name == \\ a
min "·
.

salf.guessed _ letters.append(letter) main()


elif letter not in self.word and letter not 1n
self.missed letters: •••••••••••••••••••••••••• • • • • • • • • • • • • • • • • • • • • • • •••••••••••••• •

• •
self .missed latters.append(letter) • •

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 •

: gallows being drawn with each incorrect guess of the word. •



def hangman _ over(self): • •

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 •
• •

def hangman von(salf): : things differently). •



• •
if ' ' not in self. hide word(): • •


return True •

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 •
• •

: returns an error if the user enters a nu mber or character. You •



def hide _ word(self) : • •

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;
'

rtn += ' ' : or something celebratory if you win.


else: •


rtn += letter •

return rtn •

, ..
.
_. .. ..
. ... --
... ...
-
= = .,__
- _ ___ .....
• ..
.�·-:.... .. _........
-

�- ..
·- -

• _
..,. ........
_
def print _ game _ status(salf): •


print (board[len(self.missad _ letters) )) •

print ('Word: ' + self. hide _ word()) •




print ('Letters Missed: ',) •

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

: you c.an Find a text document with over 466,000 words.


bank = 'ability about above absolute accessible •

: 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 •

handsome refrigerator opposite socks interested


strawberry backgammon anniversary confused def rand.word():
with open ("/home/pi/Dovnloada/vords. txt",
dangerous entertainment exhausted impossible
overweight temperature vacation scissors bank•£ . readlines()

accommodation appointment decrease development • return bank[random. randint(O,len(bank)))


• •
• •
earthquake environment brand environment necessary •

• • • • • • • • • • • • • • • • • • • • • • • • • • • •••••••••••••••••••••••• • • • • • • • • • • • •

wwVLbdmpubllcat!oos.com Coding · Tips, Tr icks & Fixes


.I v
" I
I
II 'II II rl cl I H cr '
' '' II 'II .'
I ') (
\
"
' 6
1 I'
� I I \•.>( r
.1 '! (J 'I /\ ,
· I'I E I
)i I
'' II I.I ')
I .J
''' ,' (
. ' I
.
.
. I ,
" ;I I! \
/
" Il ' '

</> u T r •
I I .
"' ( \
l
Code Project Examples . . '

-'� f [ L '' rJ '' '.


.
.

.
,
' 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
--·-

..

····� � .
.
- .

···'·

....,. ·- Wilt! ciOIPfOOIMW!*ttc:b.llllfdol


... ·-· -
Cf UATI! M (' O il"' T • , .

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• • • • • • • • • • • • • • • • • • • • • • ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

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.

</> u T 1..• I I.•. (./ \.



Code Project Examples '
'I
f. [ L ,I !J '
I
.
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.

: (Boolean operators) to limit, : CONSTANT


A j broaden or define your search.
! BUG
j A variable that never changes its
! value. Example: the PI constant
F
ALGORITHM ! has the value 3.14. FLASK
j A mistake in the program. A
A process or set of rules to be A micro web application
j point of error that causes the
followed in calculations or other
problem solving operations,
especially by a computer.
j
j
program to stop, or behave
differently than expected.
ID
.
framework written in Python and
based on the Werkzeug toolkit
and Jinja2 template engine.
j BRACKETS j DATA STRUCTURES
ANGULAR.JS j A data structure is a method of FRAMEWORK
j Characters often used to
Angular.js is an open source web j organisation ofdata in a computer A Framework is often a layered
j surround text. The different
application framework
j types of brackets are: Parenthesis, ! so that it can be used efficiently. structure indicating what kind of
maintained by Google. programs can or should be built
j Curly Brackets, Angle Brackets j DEPLOYMENT
j and Square Brackets.
and how they would interrelate.
APACHE j Software deployment is all of the
Apache is an open source j activities that make a software FULL STACK
Unix-based Web server. It was
created by the Apache Software
IC j system available For use.

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.

144 BDM's I·Tech Sp ecial I Volume 27 I r- I I• •I II :I


I.l
f1
J
I I; cI J . .I Il
I·� -
(
\ 1l I
. '
II
.
\
•' II II i ·;I -� 1j II I
)

1
'I Il {.: l ,)
I ', I II' I
I
' I, I II ·n '
"
. .' ()
•.,
n
• I
I!
.
(.. !.J I 'I i
.I II �
·� ;
.
I ,• I' .
ll ' '
" ,

'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
.
,

. SDK is a bundle of software tools


relational database management � based on the concepts of objects' '

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
,

� known as attributes; and code, in . platform or framework.


INTEGRATED LBYL � the form of procedures, often
DEVELOPER Look before you leap. This SUBROUTINE
�. known as methods.
ENVIRONMENT (IDE) coding style explidtly tests for . A function or other portion of
An Integrated Development pre--conditions before making � OBJECT RELATIONAL code that can be run anywhere
� MAPPER (ORM) . within a program.
Environment is a basic editor and calls or lookups.
code interpreter which allows
LINUX � ORM is a programming SYNTAX
.
you to work with a specific
Linux is a unix-like computer
� technique for converting data Programming languages arejust
coding language. The Python IDE : between incompatible type like human languages, they have
operating system assembled .
is known as IDLE. : systems in object-oriented their own 'syntax' or rules to
under the model of free and
INTERPRETED open source software ..
[ programming languages. .
describe how statements should
.. be written.

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. .
,

perform an action for every item divides a given software application .


� For example a variable may add
in a list you would 'iterate over
' into three interconnected parts.
� something to itself for each WRITE
that list. Each time it is repeated To send output data values to an
is one iteration.
MYSQL .�
iteration of a code loop.
external destination, usually to a
MySQL is an open source [ RUNTIME file. Can also refer to sending
relational database management � The time during which a program data over a network.
system (RDBMS).
� is actively running.
Black Dog Media

To continue learning more about your tech visit us at:

www.bdmpubl ications.com

· -· - · -·-

G
aGuilaX
debooky

• Print & digital editions


• Featuring the very latest updates
• Step-by-step tutorials and guides
• Created by BDM experts
Check out our latest titles today!

PLUS

Special Deals and


Bonus Content
Sign up to our monthly newsletter
and get the latest updates, offers
and news from BDM. We are here
to help you Master Your Tech!
Python Power
The world of programming is vast, with dozens of
programming languages available for multiple different
platforms. It's confusing at the best of times, so we're
.� BDM's i-Tech Special here to help you find your way through the labyri nthine
coding universe .

• Python is one of the most popular programming


languages available today. It's used throughout the
Internet, in businesses and education. It's easy to learn
and powerful to use and has a vast knowledge base
online for you to dip into for inspiration or help.
We look at how you get started with Python and what

Tips, Tricks & Fixes


you need to begin to become a Python programmer.

(++
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

You might also like