Loading...
Menu

Python: A Beginners’ Guide to Python Programming to automate the boring tasks

Table of Contents

Introduction to Python 4

Why is Python So Popular? 6

Outstanding Features of Python 7

Installing Python on Various Platforms 13

Installing Python on Microsoft Windows 13

Running Few Programs on Your New Install 17

Installing Python on Linux 21

Installing Python on Mac OS X 22

Your First Steps towards Learning Python 25

Using the Interpreter Prompt 25

Using an Editor in Python 27

Using the Source File 36

Python Basics for Beginners 40

Comments 41

Literal Constants 42

Quotes 43

The Format Method 45

Escape Sequences 47

A Program to Practice 49

Various Operators & Expressions Used in Python 54

Operators in Python 55

Operator Precedence Table for Python 65

Expressions 67

Control Flow Statements in Python 72

The If Statement 73

The While Statement 79

The Recursive Function in Python 83

Functions 85

Files & Storing Persistently in Python 88

Storing Persistently in Python 92

cPickle 96

Sample Programs for Practice 98

Exercise for You 108

Conclusion 110

h1<>{color:#2e74b5;}.

Introduction to Python

At some point in time or the other, a lot of people get the urge to start programming. However, most of them never make a move towards this field because computer programming sounds scary. Well, you should know that it’s a lot easier than it sounds. All it takes is the correct selection of programming languages to begin your journey as a computer programmer. Programming languages are made to make human life easier. These languages make programs that increase the overall productivity, communication, and efficiency of work. Of the many programming languages, Python is an all-time favorite. This is because Python is one of those rare languages that is both simple, and powerful. Python has, indeed, everything that you require to make a new program. If you should see someone writing a program in Python, you would be surprised at how easy it is to find the solution for a problem in that language.

Python is defined as “a powerful, yet simple programming language. Python consists of high-level data structures. Besides, it also has an effective procedure for object-oriented programming. Python’s neat syntax, along with its interpreted nature and dynamic typing makes it the perfect programming language for quick application development.”

In simple terms, python is an open source, high-level programming language. It was developed by Guido van Rossum in the 1980s and is presently administered by Python Software Foundation.

Why is Python So Popular?

As I indicated earlier, programming languages are created to make human life better. However, many languages become obsolete over time when they fail to keep pace with human needs and expectations as technology changes. Python has proven its worth over the years in both business and industrial use. Unlike other languages, it did not go obsolete, but in fact, has increased its use with time. Python is broadly used in making web applications, GUIs (Graphical user interface), games, etc., and writing and reading the codes in the program are as simple as reading regular English sentences. Programs written in Python are required to be processed before running, as they are not written in machine readable language. Once you learn the basics of Python (as you are a beginner), you will find it easy to move on to the advanced features that the program offers to programmers. Below are some features of Python that make it a favorite.

Outstanding Features of Python

Python is simple

Python is a simple and minimalistic programming language. It is problem-oriented rather than focused on machine language. That is, the coding language is written in simple English, giving the user enough time to focus on the problem and find a solution, rather than worry about the mistakes that he could make while writing the program.

Python is easy to learn

Python has the simplest syntax that you can find in any programming language; therefore, even a beginner can learn it easily in a short time.

Python is free and open sourced

Python is the best example of Free/Libre and Open Source Software (FLOSS). This is its strength as FLOSS gives the Python community the opportunity to make changes to the program, to contribute to its development, to distribute it, to make use of it in creating new software, and to read its source. The Python community works constantly without any worry about legal issues to ensure that it gets better over time.

Python is portable

Because it is an open source software, Python has been ported to many platforms, thus making it the most portable programming language available. All your Python programs can work on any of the following platforms without the need for changes to the Python file. You can use it on GNU/Linux, Windows, Macintosh, FreeBSD, OS/2, AROS, Amiga, Solaris, AS/400, OS/390, BeOS, z/OS, QNX, Palm OS, VMS, Acorn RISC OS, Psion, VxWorks, Sharp Zaurus, PlayStation, PocketPC and Windows CE.

We will discuss how you can install Python on various platforms in the next chapter of this book.

Python is embeddable

The user can easily embed Python with C or C++ programs.

Python is both procedure oriented and object oriented

When using Python, a user can choose to use it as a procedure oriented language or object oriented language. In a procedure oriented language, the program is built around functions or procedures that are reusable pieces of software. While in object oriented language, the program is built around objects which combines the functionality and data. When compared to vast languages like Java or C++, Python is the simplest object oriented program available today.

Can you now understand why you should learn Python as your first programming language? If yes, then let’s continue to the next chapter.

Installing Python on Various Platforms

As mentioned earlier, Python is available for various platforms because of it being an open source software. Let’s discuss how you can install it on your system. Select your Operating System (OS) below and follow the steps to install it.

Installing Python on Microsoft Windows

The first step in installing Python on Microsoft Windows is to download the software. You can download it from Python’s official website at the link below:

https://www.python.org/downloads/windows/

*
h3<>. Once you click on the version that you want to install, you will go to the next page where you can see all the recent changes and features that were added to it. Scroll down on that page to reach the Files section.

*
h3<>. From the Files section, you can download Python. Ensure that you select the correct Operating System for your download.

*
p<>{color:#000;}. Once the download is complete, open the downloaded file and follow the on-screen instructions.

*
h3<>. Once you are on the Customize screen, scroll down to find the “Add Python.exe to Path”, there you will see a red ‘x’. Click on this icon and select the “Will be installed on local hard drive” option, then click “Next.”

*
h3<>. Now the command prompt will start which will download and install Python on your machine.

*
p<>{color:#000;}. After the installation is complete, you can exit the setup by clicking on the ‘finish’ button.

*
p<>{color:#000;}. The next step is adding Python to system path variables. If you downloaded version 3 for Python, this step is not required. However, if you installed Python version 2, you will have to complete this step. Doing this step ensures that Python can work perfectly and can run all its scripts on the Windows machine without creating any conflict with your operating system.

*
h3<>. Start by opening your Start menu on your machine and then typing the word ‘Environment’. Then select ‘Edit the system environment variables.’

*
h3<>. After that, system properties will open in front of you in a new window. At the bottom of this window, you will see a button named ‘Environment Variables…’ proceed by clicking on this button.

*
h3<>. Once the window with Environment Variables opens, you will see the bottom section named as ‘System Variables’. Create the new variable by clicking on ‘New’.

*
p<>{color:#000;}. Now, a new window will open with options to enter the name and variable value. In the name box, enter the name of your script, and in the value slot, enter the code. The code is given below –‘C:\Python27\;C:\Python27\Scripts;

*
h3<>. The above code may change depending on the Python version that you installed. Now save all the changes and you are done.

Running Few Programs on Your New Install

In the previous section, you learned how to install Python on your Microsoft Windows machine. Now, it is time to test your Python installation by running some basic programs in it for a start. Let’s start with opening Python on your Windows machine.

*
h3<>. To open Python, click on Start and type “python”. Two or three options will appear. Select “IDLE (PythonGUI)”.

*
h3<>. Once Python starts, you can test its functioning by using a simple Print directive as explained in this step. This directive which print whatever you type on your screen as it is. Begin by typing your first directive similar to the one in the below image, you can change the green line to whatever you want.

*
p<>{color:#000;}. If you installed Python 3 on your machine, then the symbol >>> must have caught your attention already. This symbol indicates that you can perform simple mathematics in Python without needing any sort of directive. Try doing some of the simple mathematics questions like the ones shown in this image -

*
h3<>. If you could perform these simple operations in Python, then it is time to congratulate yourself. Yes, you have finally installed your programming language on your Microsoft Windows machine.

Now, let’s look at other operating systems available and learn how you can install Python on them.

Installing Python on Linux

You might not know this but for GNU or Linux users, Python comes already installed by default. To check if you have Python already installed, simply open the terminal application on your Linux machine, or press Alt+F2 from your keyboard and then enter gnome-terminal. Now type the following command to continue checking for Python and which version is installed:

$ python –V

It may give you a result like this –

Python 2.7.6

Installing Python on Mac OS X

Just like GNU or Linux, Mac OS X also comes with Python already installed by default.

*
p<>{color:#000;}. To check this, simply press the Command and Space keys together from your keyboard to open the Spotlight search.

*
h3<>. Then type ‘Terminal’ and press the Enter key.

*
h3<>. Now, run Python and verify your install to ensure that there are no errors.

So, that’s it for installing Python on your machine. From now on in this book, it will be taken for granted that you have Python already installed on your system. That being the case, why don’t we get used to Python by learning few basics about it in the next chapter?

***

Visit: http://www.Shakespir.com/books/view/676430 to purchase this book to continue reading. Show the author you appreciate their work!


Python: A Beginners’ Guide to Python Programming to automate the boring tasks

At some point of the time or the other, every persons wishes to make a computer program. However, most of them never make a move towards this feel because computer programming sounds scary. In fact, computer programming isn’t scary at all. All it takes is a correct selection of programming language to begin your journey as a computer programmer. Programming languages are made to make the human life better than before. These languages help in making programs which increase the overall productivity, communication, and efficiency of the work. Out of so many programming languages to choose from, python is one of the most loved programming languages among computer geeks. This is because python is one of those rare languages which is both simple, and powerful. Python has indeed, everything that you may require to make a new program. If you will see someone writing a program in python, you will be really surprised to see how easy it is in python to find the solution for a problem. In simple words, python is an open source, a high-level programming language developed by Guido van Rossum in 1980s. This language is presently administered by Python Software Foundation. Python has proven its worth in all these years for both business and industrial use. Unlike other languages, it didn’t go obsolete and in fact, with time, the use of Python has increased. Python is broadly used in making web applications, GUIs (Graphic user interface), games, etc. and writing and reading the codes in python is as simple as reading regular English sentences. The program written in python is required to be processed before running as they are not written in machine readable language. Once you will learn the basics of python (as you are a beginner), you will find it really easy to move on towards the advanced features that python offers to programmers. Below are some salient features of Python which make it one of the most lovable languages among the computer programmers.

  • Author: supershake
  • Published: 2016-10-28 20:05:10
  • Words: 9021
Python: A Beginners’ Guide to Python Programming to automate the boring tasks Python: A Beginners’ Guide to Python Programming to automate the boring tasks