Share E-Book

Linux Complete Manual - 29th Edition 2026 (Papercut Limited)(Z-Library)

Author

Rating 3.5 AI initial rating

Log in to rate

Linux
Language English

No Description

Format PDF
Size 18.7 MB
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
100% INDEPENDENT LINUX complete manual Learn essential new coding skills
Page 2
Save a Huge 50% Off ALL Digital Manuals!* As a token of our thanks for your support, bring you this unmissable offer! Not only can you learn new skills and master your tech, but you can now SAVE 50% off all of our coding, photography and consumer tech digital manuals! To take advantage of this amazing offer visit: www.pclpublications.com/product-category/digital/ Make your selection(s) and then simply use the following exclusive code at checkout to get your purchase at half price: SAVE50 *Please note this exclusive single use per user discount code requires sign up for activation. Discount cannot be applied against discounted items, print titles or used with an additional discount code.
Page 3
M a s te r yo u r te c h @ w w w. p c l p u b l i c a t i o n s .c o m Linux is everywhere. It powers the Internet as the main operating system behind the Web’s servers, it powers spacecraft, it’s the operating system for the fastest supercomputers in the world and it’s used in smart TVs and mobile devices. Why? Because it’s ultra stable, lightning fast and completely free of charge. However, there’s more to Linux than simply being a free to use operating system. Its unique configuration allows the user to customise and personalise the OS into any form they wish. A Linux user can change their OS look and feel from one day to the next, install thousands of freely available apps and programs and take back control of their computer. With this book, you too can become a part of the open community of Linux users. The tutorials within these pages will help you get to grips with Linux, show you how it works, what you can do with it and how you can code with it to take your Linux experience to even greater heights. Discover Linux. Discover freedom.
Page 4
Contents 8 Why Python? 10 How to Set Up Python in Linux 12 Starting Python for the First Time 14 Your First Code 16 Saving and Executing Your Code 18 Executing Code from the Terminal 20 Did you Know…Space Invaders 22 Numbers and Expressions 24 Using Comments 26 Working with Variables 28 User Input 30 Creating Functions 32 Conditions and Loops 34 Python Modules 36 Did you Know…Debugging 40 Why C++? 42 Your First C++ Program 44 Structure of a C++ Program 46 Compile and Execute 48 Did you Know…Virus! 50 Using Comments 52 Variables 54 Data Types 56 Strings 58 C++ Maths 60 User Interaction 62 Did you Know…The Hobbit 64 Common Coding Mistakes 4 Contents Python on Linux6 C++ on Linux38 www.pclpublications.com Linux Coding and Programming Tricks & Tips
Page 5
5 Contents www.pclpublications.com © 2021 Papercut Limited All rights reserved. No part of this publication may be reproduced in any form, stored in a retrieval system or integrated into any other publication, database or commercial programs without the express written permission of the publisher. Under no circumstances should this publication and its contents be resold, loaned out or used in any form by way of trade without the publisher’s written permission. While we pride ourselves on the quality of the information we provide, Papercut Limited reserves the right not to be held responsible for any mistakes or inaccuracies found within the text of this publication. Due to the nature of the tech industry, the publisher cannot guarantee that all apps and software will work on every version of device. It remains the purchaser’s sole responsibility to determine the suitability of this book and its content for whatever purpose. Any app images reproduced on the front and back cover are solely for design purposes and are not representative of content. We advise all potential buyers to check listing prior to purchase for confirmation of actual content. All editorial opinion herein is that of the reviewer - as an individual - and is not representative of the publisher or any of its affiliates. Therefore the publisher holds no responsibility in regard to editorial opinion and content. This is an independent publication and as such does not necessarily reflect the views or opinions of the producers of apps or products contained within. This publication is 100% unoicial. All copyrights, trademarks and registered trademarks for the respective companies are acknowledged. Relevant graphic imagery reproduced with courtesy of brands and products. Additional images contained within this publication are reproduced under licence from Shutterstock. Prices, international availability, ratings, titles and content are subject to change. All information was correct at time of publication. Some content may have been previously published in other volumes or titles. Papercut Limited Registered in England & Wales No: 4308513 Linux For Beginners – th Edition ISBN: 978-1-912847-76-1 Published by: Papercut Limited Digital distribution by: Readly AB, Zinio, Magzter, Cafeyn, PocketMags “There’s more to Linux than simply being a free to use operating system. Its 8th Edition
Page 6
“Python is an experiment in how much freedom programmers need. Too much freedom and nobody can read another’s code; too little and expressiveness is endangered.” – Guido van Rossum (Developer of the Python programming language) Python on Linux 6 www.pclpublications.com
Page 7
Python is a fantastic programming language, combining ease of use with a generous helping of power to allow the user to create both minor utilities or performance-heavy computational tasks. However, there’s more to Python than simply being another programming language. It has a vibrant and lively community behind it that shares knowledge, code and project ideas, as well as bug fixes for future releases. We’ve used a Raspberry Pi for this section of the book, as it’s Linux based and one of the best coding platforms available. The Pi’s features and functions make it the perfect Python programming partner, so let’s get started. Python on Linux 7www.pclpublications.com
Page 8
Python on Linux 8 There are many different programming languages available for the modern computer, and some still available for older 8 and 16-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? Why Python? Ever since the earliest home computers were available, enthusiasts, users and professionals have toiled away until the wee hours, slaving over an overheating heap of circuitry to create something akin to magic. These pioneers of programming carved their way into a new frontier, forging small routines that enabled the letter ‘A’ to scroll across the screen. It may not sound terribly exciting to a generation that’s used to ultra high-definition graphics and open world, multi- player online gaming. However, forty-something years ago it was blindingly brilliant. Naturally these bedroom coders helped form the foundations for every piece of digital technology we use today. Some went on to become chief developers for top software companies, whereas others pushed the available hardware to its limits and founded the billion pound gaming empire that continually amazes us. PYTHON POWER Regardless of whether you use an Android device, iOS device, PC, Mac, Linux, Smart TV, games console, MP3 player, GPS device built-in to a car, set-top box or a thousand other connected and ‘smart’ appliances, behind them all is programming. All those aforementioned digital devices need instructions to tell them what to do, and allow them to be interacted with. These instructions form the programming core of the device and that core can be built using a variety of programming languages. The languages in use today differ depending on the situation, the platform, the device’s use and how the device will interact with its www.pclpublications.com
Page 9
Why Python? 9 environment or users. Operating systems, such as Windows, macOS and such are usually a combination of C++, C#, assembly and some form of visual-based language. Games generally use C++ whilst web pages can use a plethora of available languages such as HTML, Java, Python and so on. More general-purpose programming is used to create programs, apps, software or whatever else you want to call them. They’re widely used across all hardware platforms and suit virtually every conceivable application. Some operate faster than others and some are easier to learn and use than others. Python is one such general- purpose language. Python is what’s known as a High-Level Language, in that it ‘talks’ to the hardware and operating system using a variety of arrays, variables, objects, arithmetic, subroutines, loops and countless more interactions. Whilst it’s not as streamlined as a Low-Level Language, which can deal directly with memory addresses, call stacks and registers, its benefit is that it’s universally accessible and easy to learn. Python was created over twenty six years ago and has evolved to become an ideal beginner’s language for learning how to program a computer. It’s perfect for the hobbyist, enthusiast, student, teacher and those who simply need to create their own unique interaction between either themselves or a piece of external hardware and the computer itself. Python is free to download, install and use and is available for Linux, Windows, macOS, MS-DOS, OS/2, BeOS, IBM i-series machines, and even RISC OS. It has been voted one of the top five programming languages in the world and is continually evolving ahead of the hardware and Internet development curve. So to answer the question: why python? Simply put, it’s free, easy to learn, exceptionally powerful, universally accepted, effective and a superb learning and educational tool. Java is a powerful language that’s used in web pages, set-top boxes, TVs and even cars. BASIC was once the starter language that early 8-bit home computer users learned. Python is a more modern take on BASIC, it’s easy to learn and makes for an ideal beginner’s programming language. www.pclpublications.com
Page 10
Python on Linux While the Raspberry Pi’s operating system contains the latest, stable version of Python, other Linux distros don’t come with Python 3 preinstalled. If you’re not going down the Pi route, then here’s how to check and install Python for Linux. How to Set Up Python in Linux 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’re sticking with Linux Mint for this particular tutorial. PYTHON PENGUIN First you need to ascertain which version of Python is currently installed in your Linux system. To begin with, drop into a Terminal session from your distro’s menu or hit the Ctrl+Alt+T keys. In our case we have both Python 2 and 3 installed; as long as Python 3.x.x is installed, then the code in our tutorials will work. It’s always worth checking to see if the distro has been updated with the latest versions, so enter: sudo apt-get update && sudo apt-get upgrade to update the system. Next enter python --version into the Terminal screen. You should have the output relating to version 2.x of Python in the display. By default, most Linux distros come with both Python 2 and 3, as there’s plenty of code out there still available for Python 2. Now enter: python3 --version. Once the update and upgrade completes, enter: python3 --version again to see if Python 3.x is updated or even installed; as long as you have Python 3.x, you’re running the most recent major version. The numbers after the 3. indicate patches and further updates. Often they’re unnecessary but can contain vital new elements. STEP 1 STEP 3 STEP 2 STEP 4 10 www.pclpublications.com
Page 11
How to Set Up Python in Linux However, if you want the latest, cutting edge version, you need to build Python from source. Start by entering these commands into the Terminal: sudo apt-get install build-essential checkinstall sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev In the Terminal, go the Downloads folder by entering cd Downloads/. Then unzip the contents of the downloaded Python source code with: tar -xvf Python- 3.Y.Y.tar.xz replacing the Y’s with the version numbers you’ve downloaded. Now enter the newly unzipped folder with cd Python-3.Y.Y/. Open up your Linux web browser and go to the Python download page: www.python.org/ downloads. Click on Downloads followed by the button under the Python Source window. This opens a download dialogue box; choose a location and start the download process. Within the Python folder, enter: ./configure sudo make altinstall This could take a while depending on the speed of your computer. Once finished, enter: python3.7 --version to check the installed latest version. You now have Python 3.7 installed, alongside older Python 3.x.x and Python 2. You also need PIP (Pip Installs Packages) which is a tool to help you install more modules and extras. Enter: sudo apt-get install python3-pip When PIP is installed, check for the latest update with: 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. For the GUI IDLE, you need to enter the following command into the Terminal: sudo apt-get install idle3 The IDLE can then be started with the command idle3. Note that IDLE runs a different version from the one you installed from source. STEP 5 STEP 7 STEP 6 STEP 8 STEP 10 STEP 9 11www.pclpublications.com
Page 12
Python on Linux If you’re using the new Raspberry Pi together with its latest release of the Pi OS, then you will need to manually install the Python IDLE. This is due to the Pi team removing the core Python IDLE in favour of their own coding text editor. Starting Python for the First Time For those using the Pi4 and new Raspbian, drop into a Terminal and enter: sudo apt-get install idle3. Older versions of Raspbian already have the official Python IDLE pre-installed. STARTING PYTHON With the Raspbian desktop loaded, click on the Menu button followed by Programming > Python 3 (IDLE). This opens the Python 3 Shell. Windows and Mac users can find the Python 3 IDLE Shell from within the Windows Start button menu and via Finder. The Shell is where you can enter code and see the responses and output of code you’ve programmed into Python. This is a kind of sandbox, where you’re able to try out some simple code and processes. STEP 1 STEP 2 For example, in the Shell enter: 2+2 After pressing Enter, the next line displays the answer: 4. Basically, Python has taken the ‘code’ and produced the relevant output. The Python Shell acts very much like a calculator, since code is basically a series of mathematical interactions with the system. Integers, which are the infinite sequence of whole numbers can easily be added, subtracted, multiplied and so on. STEP 3 STEP 4 12 www.pclpublications.com
Page 13
Starting Python for the First Time While that’s very interesting, it’s not particularly exciting. Instead, try this: print(“Hello everyone!”) Just enter it into the IDLE as you’ve done in the previous steps. You may have noticed the colour coding within the Python IDLE. The colours represent different elements of Python code. They are: Black – Data and Variables Green – Strings Purple – Functions Orange – Commands Blue – User Functions Dark Red – Comments Light Red – Error Messages This is a little more like it, since you’ve just produced your first bit of code. The Print command is fairly self-explanatory, it prints things. Python 3 requires the brackets as well as quote marks in order to output content to the screen, in this case the ‘Hello everyone!’ bit. The Python IDLE is a configurable environment. If you don’t like the way the colours are represented, then you can always change them via Options > Configure IDLE and clicking on the Highlighting tab. However, we don’t recommend that, as you won’t be seeing the same as our screenshots. The Python IDLE is a power interface and one that’s actually been written in Python using one of the available GUI toolkits. If you want to know the many ins and outs of the Shell, we recommend you take a few moments to view www.docs.python.org/3/library/idle.html, which details many of the IDLE’s features. Just like most programs available, regardless of the operating system, there are numerous shortcut keys available. We don’t have room for them all here but within the Options > Configure IDLE and under the Keys tab, you can see a list of the current bindings. STEP 5 STEP 7 STEP 6 STEP 8 STEP 10 STEP 9 13www.pclpublications.com
Page 14
Python on Linux Your First Code Essentially, you’ve already written your first piece of code with the ‘print(“Hello everyone!”)’ function from the previous tutorial. However, let’s expand that and look at entering your code and playing around with some other Python examples. With most languages, computer or human, it’s all about remembering and applying the right words to the right situation. You’re not born knowing these words, so you need to learn them. PLAYING WITH PYTHON If you’ve closed Python 3 IDLE, reopen it in whichever operating system version you prefer. In the Shell, enter the familiar following: print(“Hello”) You can see that instead of the number 4, the output is the 2+2 you asked to be printed to the screen. The quotation marks are defining what’s being outputted to the IDLE Shell; to print the total of 2+2 you need to remove the quotes: print(2+2) Just as predicted, the word Hello appears in the Shell as blue text, indicating output from a string. It’s fairly straightforward and doesn’t require too much explanation. Now try: print(“2+2”) You can continue as such, printing 2+2, 464+2343 and so on to the Shell. An easier way is to use a variable, which is something we will cover in more depth later. For now, enter: a=2 b=2 STEP 1 STEP 3 STEP 2 STEP 4 14 www.pclpublications.com
Page 15
Your First Code What you have done here is assign the letters a and b two values: 2 and 2. These are now variables, which can be called upon by Python to output, add, subtract, divide and so on for as long as their numbers stay the same. Try this: print(a) print(b) You can play around with different kinds of variables and the Print function. For example, you could assign variables for someone’s name: name=”David” print(name) The output of the last step displays the current values of both a and b individually, as you’ve asked them to be printed separately. If you want to add them up, you can use the following: print(a+b) This code simply takes the values of a and b, adds them together and outputs the result. Now let’s add a surname: surname=”Hayward” print(surname) You now have two variables containing a first name and a surname and you can print them independently. In Python 3 you can separate the two variables with a space using a comma: print(name, surname) Alternatively, you can add the space yourself: print(name+” “+surname) The use of the comma is much neater, as you can see. Congratulations, you’ve just taken your first steps into the wide world of Python. If we were to apply the same routine as before, using the + symbol, the name wouldn’t appear correctly in the output in the Shell. Try it: print(name+surname) You need a space between the two, defining them as two separate values and not something you mathematically play around with. STEP 5 STEP 7 STEP 6 STEP 8 STEP 10 STEP 9 15www.pclpublications.com
Page 16
Python on Linux While working in the IDLE Shell is perfectly fine for small code snippets, it’s not designed for entering longer program listings. In this section you’re going to be introduced to the IDLE Editor, where you will be working from now on. Saving and Executing Your Code You will eventually reach a point where you have to move on from inputting single lines of code into the Shell. Instead, the IDLE Editor will allow you to save and execute your Python code. EDITING CODE First, open the Python IDLE Shell and when it’s up, click on File > New File. This will open a new window with Untitled as its name. This is the Python IDLE Editor and within it you can enter the code needed to create your future programs. You can see that the same colour coding is in place in the IDLE Editor as it is in the Shell, enabling you to better understand what’s going on with your code. However, to execute the code you need to first save it. Press F5 and you get a Save…Check box open. The IDLE Editor is, for all intents and purposes, a simple text editor with Python features, colour coding and so on; much in the same vein as Sublime. You enter code as you would within the Shell, so taking an example from the previous tutorial, enter: print(“Hello everyone!”) Click on the OK button in the Save box and select a destination where you’ll save all your Python code. The destination can be a dedicated folder called Python or you can just dump it wherever you like. Remember to keep a tidy drive though, to help you out in the future. STEP 1 STEP 3 STEP 2 STEP 4 16 www.pclpublications.com
Page 17
Saving and Executing Your Code Enter a name for your code, ‘print hello’ for example, and click on the Save button. Once the Python code is saved it’s executed and the output will be detailed in the IDLE Shell. In this case, the words ‘Hello everyone!’. If you open the file location of the saved Python code, you can see that it ends in a .py extension. This is the default Python file name. Any code you create will be whatever.py and any code downloaded from the many Internet Python resource sites will be .py. Just ensure that the code is written for Python 3. This is how the vast majority of your Python code will be conducted. Enter it into the Editor, hit F5, save the code and look at the output in the Shell. Sometimes things will differ, depending on whether you’ve requested a separate window, but essentially that’s the process. It’s the process we will use throughout this book, unless otherwise stated. Let’s extend the code and enter a few examples from the previous tutorial: a=2 b=2 name=”David” surname=”Hayward” print(name, surname) print (a+b) If you press F5 now you’ll be asked to save the file, again, as it’s been modified from before. Now create a new file. Close the Editor, and open a new instance (File > New File from the Shell). Enter the following and save it as hello.py: a=”Python” b=”is” c=”cool!” print(a, b, c) You will use this code in the next tutorial. If you click the OK button, the file will be overwritten with the new code entries, and executed, with the output in the Shell. It’s not a problem with just these few lines but if you were to edit a larger file, overwriting can become an issue. Instead, use File > Save As from within the Editor to create a backup. STEP 5 STEP 7 STEP 6 STEP 8 STEP 10 STEP 9 17www.pclpublications.com
Page 18
Python on Linux Although we’re working from the GUI IDLE throughout this book, it’s worth taking a look at Python’s command line handling. We already know there’s a command line version of Python but it’s also used to execute code. Executing Code from the Terminal Using the code we created in the previous tutorial, the one we named hello.py, let’s see how you can run code that was made in the GUI at the command line level. COMMAND THE CODE Python, in Linux, comes with two possible ways of executing code via the command line. One of the ways is with Python 2, whilst the other uses the Python 3 libraries and so on. First though, drop into the command line or Terminal on your operating system. Now you’re at the command line we can start Python. For Python 3 you need to enter the command python3 and press Enter. This will put you into the command line version of the Shell, with the familiar three right- facing arrows as the cursor (>>>). Just as before, we’re using a Raspberry Pi: Windows users will need to click the Start button and search for CMD, then click the Command Line returned search; and macOS users can get access to their command line by clicking Go > Utilities > Terminal. From here you’re able to enter the code you’ve looked at previously, such as: a=2 print(a) You can see that it works exactly the same. STEP 1 STEP 3 STEP 2 STEP 4 18 www.pclpublications.com
Page 19
Executing Code from the Terminal Now enter: exit() to leave the command line Python session and return you back to the command prompt. Enter the folder where you saved the code from the previous tutorial and list the available files within; hopefully you should see the hello.py file. Naturally, since this is Python 3 code, using the syntax and layout that’s unique to Python 3, it only works when you use the python3 command. If you like, try the same with Python 2 by entering: python hello.py From within the same folder as the code you’re going to run, enter the following into the command line: python3 hello.py This will execute the code we created, which to remind you is: a=”Python” b=”is” c=”cool!” print(a, b, c) The result of running Python 3 code from the Python 2 command line is quite obvious. Whilst it doesn’t error out in any way, due to the differences between the way Python 3 handles the Print command over Python 2, the result isn’t as we expected. Using Sublime for the moment, open the hello.py file. Save the hello.py file and drop back to the command line. Now execute the newly saved code with: python3 hello.py The result will be the original Python is cool! statement, together with the added input command asking you for your name, and displaying it in the command window. Since Sublime Text isn’t available for the Raspberry Pi, you’re going to temporarily leave the Pi for the moment and use Sublime as an example that you don’t necessarily need to use the Python IDLE. With the hello.py file open, alter it to include the following: name=input(“What is your name? “) print(“Hello,”, name) STEP 5 STEP 7 STEP 6 STEP 8 STEP 10 STEP 9 19www.pclpublications.com
Page 20
Space Invaders 20 Python on Linux www.bdmpublications.com.pclpublications.com
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List