Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Python Made Simple: Learn Python programming in easy steps with examples
Python Made Simple: Learn Python programming in easy steps with examples
Python Made Simple: Learn Python programming in easy steps with examples
Ebook704 pages5 hours

Python Made Simple: Learn Python programming in easy steps with examples

Rating: 5 out of 5 stars

5/5

()

Read preview

About this ebook

In the last few years, python gained popularity and became the first choice of the students, teachers as well as professionals. It is being used in different fields such as education, software development, website development and also in various advanced research. In the field of education it allows students to learn the programming language in an easier and efficient manner. In the information technology field it can be used as a language for creating softwares as well as for web developments. It can be integrated with different platforms like Django. In research, Python programming can be used in simulation or it can be used for machine learning techniques.
The primary goal of this text is to create a pedagogically sound and accessible textbook that emphasises on core concepts of Python programming. The book contains lots of practical examples to show the working of a particular code construct. The book can be very helpful in order to learn the basic and advance concepts of python programming.
In the beginning of the book the focus is on the basic concepts related to core python programming starting from the installation phase of python interpreter to building the concepts for the reader towards python programming. Then the book moves towards the concept of different statements and programming conditions that python programming can handle in an easier manner. It then moves to the concepts related to object oriented programming and at last the reader will get to know about the database connectivity with the python program
LanguageEnglish
Release dateJul 30, 2019
ISBN9789389328080
Python Made Simple: Learn Python programming in easy steps with examples

Related to Python Made Simple

Related ebooks

Programming For You

View More

Related articles

Reviews for Python Made Simple

Rating: 5 out of 5 stars
5/5

1 rating0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Python Made Simple - Rydhm Beri

    CHAPTER 1

    Introduction to Python

    Introduction

    Computer systems are necessary in almost all fields nowadays. They are used in different fields and help create smart devices such as smart TVs, smart phones, smart home systems, driverless cars, and so on. These systems are gaining popularity by their features and working, as these systems reduce the efforts of human beings. Computers are used in different working environments and have become necessary tools for operations of different organizations. For example, fund managers use computers for analytical trading, architects use computers to make and check building structures, researchers use computers to recreate substantial investigations in material science and science, grocery stores use computers to plan and the calendar used in most homes helps plan the conveyance time and schedules.

    The computer coordinates with the hardware and software. Hardware refers to physical segments; for example, chips, keyboard, and monitors, while software refers to operating systems and application programs; for example, Office, Firefox, and so on. The software works on hardware to give helpful functionalities to clients; for example, word processing, web perusing, and gaming. Then again, the hardware gives fundamental support to the software by offering a lot of set of instructions, which the software joins to accomplish the complex functionality.

    Writing computer programs is the design of software utilizing a set of essential instructions. Programming language abstract hardware instructions into basic statements, which are easier to understand and handle. Using a programming language, you can compose complex software, accomplishing custom fitted functionalities by a blend of fundamental statements. This is a definitive method for using a computer and is essential while existing software neglects to fulfill the prerequisite; for example, the need of a computer to solve an equations or to simulate specific experiments in an engineering discipline.

    Structure

    Relation between programming languages

    Computational problem solving

    Introduction to Python

    History of the Python programming language

    Features of Python

    Execution of a Python program

    Memory management in Python

    Debugging

    Objective

    Understanding the core concept of programming in Python

    Understanding how Python programming can be used to solve computation problem solving

    Knowing about the history and features offered by the Python programming language

    Understanding the concept of memory management in Python

    Knowing about the process of debugging in Python

    Understanding how to run and execute Python programs

    Relation between programming languages

    Though each programming language is different, they can be related along some dimensions. These dimensions are shown in the following figure:

    Figure 1.1: Programming language relation

    Low-level versus high-level language

    A low-level programming language manages the computer hardware segments and constraints. It provides little or no abstraction from the computer’s instruction set architecture commands or capacities in a programming language map closely to the processor instructions. The basic role of the low-level language is to work, manage and control the computing hardware components. Program and applications written in low-level languages are directly executed on the computing equipment with no translation or interpretation. A program written in a low-level language tends to relatively non-portable, primarily in view of the close connection between the language and the hardware architecture. Some of the low-level languages are machine and assembly languages; these are termed as the first generation and second generation languages, respectively.

    A high-level language is a programming language intended to improve computer-programming languages. A high-level language is any programming language that empowers advancement of a program in a substantially more user-friendly programming context and is commonly autonomous of the computer’s hardware architecture. A high-level language has a higher level of abstraction from the computer and concentrates more on the programming logic rather than the underlying hardware components such as memory addressing register utilization. To execute the code of a high-level language on the computer system, you need a convertor that changes the high-level language code to the low-level language code (as the computer system only understands a low-level language). With the assistance of high-level languages, you can write the applications that are compact over the various platforms and is autonomous of any architecture. Some of the examples of high-level languages are C++, Java, Python, and so on.

    General-purpose language versus domain-specific language

    The general-purpose programming language is used to write applications in numerous application domains. On the other side, a general-purpose programming language is restricted to use for a specific type of a computer system or specialized application. In many ways, a general-purpose language only has this status because it does not include language constructs designed to be used within a specific application domain. The general-purpose language is used to solve a variety of problems in an easy and efficient manner. The examples of general-purpose programming languages are C, C++, Java, C#, Python, and so on.

    A domain-specific language is a specialized computer language designed for a specific task. In other words, a domain-specific language is a computer programming language used to solve a specific type of problem. A domain-specific language can also be defined as the language targeted towards making certain kind of things possible but does not do everything that other languages might. A domain-specific language is created specifically to solve problems in a particular domain and is not intended to be able to solve problems outside of it. It may be developed to meet the needs of a particular platform, system, toolset, software problem, industry, or business challenge that cannot be effectively addressed using mainstream languages. There is a wide variety of DSLs, ranging from widely used languages for common domains, such as HTML for web pages, to languages used by only one or a few pieces of software such as Lisp, Prolog, and so on.

    Compiled languages versus interpreted languages

    The compiled languages are computer-programming languages in which the translator known as the compiler is used to translate the code written in some programming language to the native code or machine language code so that the converted code can easily be executed by the computer system. The programming code is termed as source code, while the native or machine code is termed object code. The source code may be written in some high-level language and native code or machine code is low-level language code. By native code, we mean that this code requires some processing before directly executing it on the computer system while the machine language code is directly executed by the machine without the requirement of any processing. The processing required by the native code may be further converted to the machine code or maybe some linking task. Most of the compilers nowadays convert the programming code into the native code to provide the platform independence functionality to the code. The compiler converts the source code to the target code by scanning the whole program line by line and after that creates an object code file of a low-level language that is used by another processor for further processing, or it is executed many times without repeating the compilation process. Some of the examples of compiled languages are C, C++, C#, Objective-C, Scala, and so on. Compiled programs run faster as compared to interpreted programs.

    The interpreted languages are computer-programming languages in which the translator known as an interpreter is used to translate the code written in some programming language to the machine language code so that the converted code can easily be executed by the computer system. Unlike compiled languages, an interpreted language’s translation does not happen beforehand. The translation occurs at the same time as the program is being executed. Interpreted languages have major advantages such as they are portable, which means they can run on different operating systems and platforms. Because they are translated on the spot, they are going to be optimized for the system on which they are being run. This means there are no middle steps, less memory space is required for conversion of the object code, and there is no need to worry about platform-specific code. The interpreter converts the source code into the machine code each time you run the program, one line at a time. It starts interpreting each instruction immediately on execution, which means that the resulting program runs slower than a compiled program. Interpreted languages are especially helpful for reviewing, running, and testing an application’s functionality during the development because they are able to execute high-level programs immediately and generate helpful error reports. They allow programmers to make small systematic changes during the development process, incrementally, which complement a step-by-step process for adding and then testing smaller sections of an application. Some of the examples of interpreted languages are Java, PHP, Perl, Python, and so on.

    Computational problem solving

    Computational problem solving is a step by step procedure used to solve the real-world problems using the computer system. In other words, computational problem solving is a process in which some theoretical and experimental aspects are used to create a computer-based model that might be able to solve the particular problem. After creation, these models might be used to simulate the behavior of objects and relate the results to the objects that exist in the real world. The process of computational problem solving is most often applied in cases where the physical experiment is difficult, expensive or impossible. In the process of computational problem solving, the problem to be solved is given as the input to the system, and in return, the results are provided by the computer systems, which satisfy some properties. The field of algorithm studies the method of solving a computational problem efficiently. Before solving the problem with the help of computers, the generalized algorithm is created.

    Process of solving computational problems

    The process of solving a computational problem solving does not begin with writing a computer program. In fact, a lot of creative work must be done before the first line of a program can be written. It is a process with programming being only one of the steps. Before a program is written, a design for the program must be developed. Before a design can be developed, the problem to be solved must be well understood. Once it is written, the program must be thoroughly tested. Therefore, the process of computational problem solving consists of series of steps to be performed to solve a particular problem using computer systems. The following diagram shows the steps involved in the process of computational problem solving:

    Figure 1.2: Process of computational problem solving

    Problem solving

    The problem analysis can be defined as a process of understanding the problem and the users need to relate to the problem. It is also considered a process of proposing a solution to that problem. A problem analysis explores a problem to enable the developer to see all the problems so that it can prescribe practical solutions for solving it. Problem analysis is a set of analytic tasks intended to increase the designer’s understanding of an unbalanced solution for the sake of designing a change to the circumstances that will have better equalization.

    It is important to note that:

    Designing is not performed at this stage.

    Solutions to the problems are not found in this stage.

    The current situation is analyzed and other situations that can be preferred need to be mentioned.

    The main aim of problem analysis is to gain a better understanding of the problem before the development of the solution begins. Problem analysis is mainly done in the following sections.

    Understanding the problem

    The first step is to examine the problem carefully. Once the problem is clearly understood, the fundamental computational issues for solving the problem can be determined. A single problem may have many different solutions, but they will all have something in common. Therefore, you must try to analyze which program will be required to do the particular task in an optimized manner.

    For instance, in the event that we were approached to compose a calculator program, we could pick a wide range of ways for the user to enter calculations such as entering equations, pressing buttons or even writing them on the screen. But if the software cannot add up correctly then it will not solve the problem. Subsequently, our initial couple of prerequisites must be as follows:

    The user can enter sums (we do not care how they do this).

    The program will then evaluate those sums correctly and display the result for the user.

    We also need to decide what sort of sums our calculator will be required to evaluate. Again, we have a fair amount of choice. We could be ambitious and ask it to solve simultaneous equations or complex expressions. Therefore, the third requirement is as follows:

    The calculator must be able to evaluate sums made up of two whole numbers (integer operands) and one addition (+), subtraction (-), multiplication (*) or division (/) sign (operator).

    Specification

    The second step is to then look at the list of requirements and decide exactly what your solution should do to fulfill them. Besides clearly understanding a computational problem, you must know what constitutes a solution. For some problems, there is only one solution. For others, there may be a number (or infinite number) of solutions. Here, the main aim is to decide the best solution to solve the problem. The best solution is the one with the shortest number of steps.

    For instance, a simple calculator allows the user to input some numbers, perform only basic arithmetic operations, and print the obtained results on the screen. We must note down the details of all these operations in this phase.

    Therefore, we need to decide which methods we need to write the basic operations. We need to note down all the other functionalities a calculator program can perform, which are as follows:

    When the software of a calculator starts, it may show a welcome message on the screen and some simple instructions or manual instructions to use the calculator software.

    The software program then displays the prompt sign ([number]>) and the user needs to input the first number followed by the Enter key.

    The software program displays the prompt sign and the user needs to input the type of operator that needs to be applied to the numbers, which is then followed by the Enter key.

    The software again shows the prompt symbol and asks the user to input the second number.

    When the user presses the enter key, the calculator calculates the results and displays the results on the screen.

    Program design

    After deciding the steps required to solve the problem and specification of steps that needs to be taken to solve the problem, you need to decide how to transform the created specification into the working model. The working model can be designed by writing a program, which is a sequence of steps that describes the operation to be performed by the computer. Before writing a computer program, you need to write down the design of the program. A design phase describes the high-level description of the steps to be performed by the computer. A design of the program can be written in a layman’s language or can be written as a pseudo code or flow chart. Once the design of a program is written, it becomes easier to convert the design in a program using any programming language.

    Once the design part of the program is completed, you will have an idea of the specification of the program that meets your requirements and you will come to know whether this design is able to solve your problem or not.

    Program coding and implementation

    The next phase of the problem solving is the change of a detailed sequence of activities into a language that the computer can understand. Here, each step is changed to its comparable instructions or instructions in the coding languages that have been decided for the implantation. Design decisions give general details of the data representation and the algorithmic methodologies for solving a problem. The details, however, do not indicate which programming language to utilize, or how to execute the program. That is a choice for the implementation phase.

    In this stage, the code is written to express the solution in a programming language. You will translate the logic from the flowchart or pseudo code or some other tool to a programming language. The right utilization of the language is required at the initial step. At this point, your coded program must be keyed; probably, utilizing a terminal or computer in a structure the computer can understand. Since we are programming in Python, the implementation needs to be expressed in a syntactically correct and appropriate way, using the instructions and features available in Python. To get your program to work, you need to pursue the rules of the syntax of the language you are utilizing.

    Program testing

    A few specialists demand that a well-structured program can be written effectively. In fact, they state that there are numerous approaches to demonstrate that a program is right. However, the imperfections of the world are still with us, so most programmers get used to the idea that their recently written programs are likely have a couple of errors. This is a bit discouraging at first since programmers tend to be precise, careful, detail-oriented people who take pride in their work. Still, there are many opportunities to introduce mistakes into programs, and you just as those who have gone before you, will probably find several of them. Eventually, after coding the program, you must prepare to test it on the computer. This step involves the following phases:

    Desk checking: Desk checking is a process in which the programmer reviews the code to check the login of the program to ensure it is working properly and error free. Desk checking works like proofreading which can sometimes be avoided by the programmer as it is easier to run the code on the computer to detect errors instead of checking it manually. Desk checking or the program code detects several errors sand reduces future work to remove and detect errors in a program. Different organizations perform this phase with different programmers that read and detect errors and provide suggestions in a collaborative manner.

    Translating: A translator program scans the code written in a programming language and checks for the syntax errors, returns the error messages to the programmer, and converts the code written in a human understandable form to a machine understandable form. The translators produce descriptive error messages and detect syntax errors. For instance, in the C programming language, if someone writes the expression N=2 *(I+J))- that have two closing parentheses will return you the message as, UNMATCHED PARENTHESES. Several translators display different wordings of error messages. The translators used in programming languages are compilers or interpreters. A compiler is a translator that scans the whole program and if it is found error free, converts the whole program into the object code file that can be run by the computer a number of times without reconversion. An interpreter is a program that scans and executes the code line by line, and if the code found error free, it converts the line to machine language, which in turn is executed by the computer.

    Debugging: Debugging is a process used to detect the logical errors in the program. A debugger is a special type of module that is capable to detect errors and not correct errors. In the debugging phase, a programmer runs the program using some test data and executes the program in a partial manner. Once the program is found to be error free, parts of the program will then be executed as whole. The casual way of debugging is to apply break points in the program so that we get the output partially, and if we find the output of one part of the program in the correct manner, only then the other part of the program starts the execution.

    Introduction to Python

    Python is a general-purpose programming language that can be used effectively to build any kind of program that does not require direct access to the computer hardware. Python is a very powerful, cross-platform programming language that can run on different platforms. Python is a high-level programming language that is processed by the Python interpreter to produce results. Python includes a comprehensive standard library of test modules that can be easily incorporated into your own programs. Due to its simplicity and elegance, it gained popularity among programmers quickly and was adopted by large companies such as Google Inc. and compared with other programming languages such as Java and C++.

    However, Python does have several advantages over many other languages. It is a relatively simple language that is easy to learn. Because Python is designed to be interpreted, it can provide the kind of feedback that is especially helpful to novice programmers. Python is a programming language that also supports scripting, making it suitable for rapid application development. Python comes with a powerful and easy-to-use Graphical User Interface (GUI) toolkit that makes the task of developing GUI applications in Python quite easy. Python is freely available to use.

    The Python language was created by Guido Van Rossum in the late eighties and mid-nineties at the National Research Institute for Mathematics and Computer Science in Netherlands. Python is derived from numerous different languages, including C, C++, the UNIX shell, and other programming languages. Today, Python is kept by a core development team at the institute, despite the fact that Guido Van Rossum still holds a crucial job in coordinating its progress.

    Comparison of Python with other languages

    Python is derived from various popular languages like C and C++. Today, Python is the trendiest language and is the first choice of most programmers. There are several reasons why Python is the preferable choice of the programmers over other popular languages like C++, Java, and so on. The following figure shows the difference between Python and other programming languages:

    Figure 1.3: Difference between Python and other programming languages

    The preceding figure shows the comparison between Python, Java, and C++, the three popular languages. Among these, C++ was invented much earlier. Mastering in C++ requires knowledge of computer hardware, as some of its instructions are closest to the hardware instructions. Java was invented decades after C++. Python is one-step further in abstracting away hardware details, offering a conceptually very simple system for statement execution. As a result, Python programs are easier to learn but Python is slower to run.

    In terms of functionality, Python is as powerful as Java and C++. IT supports almost all capabilities, typically all hardware supports. In addition, libraries provided by Python and open-source contributors enrich the power of Python, making it the most convenient choice for many applications like text processing, scientific computations, and so on.

    Problem solving skills are as important as knowledge of programming languages and computer programming. It is, therefore, important to learn common approaches to typical problems when learning computer programming. Python supports virtually all programming paradigms that Java and C++ support, and even more, making it more convenient to apply flexible problem-solving techniques using the Python programming language.

    History of the Python programming language

    Python is a high-level programming language developed by Guido Van Rossum in 1989 at Centrum Wiskunde and Informatica (CWI) in Netherlands. It was eventually released for public distribution in 1991. Python was conceptualized in the late 1980s. At that time, Van Rossum was a researcher with a considerable language design experience with the interpreted language ABC, also developed at CWI, but he was unsatisfied with its ability to be developed into something more. He then decided to design a simple scripting language that possessed some of ABC’s better properties but without its problems. He created a simple virtual machine, a simple parser and a simple runtime and created different versions of ABC parts.

    He created a basic syntax that used indentation for statement grouping instead of curly braces or begin-end blocks and developed a small number of powerful data types such as a hash table (or dictionary, as we call it), a list, strings, and numbers. So what about the name Python? Most people think about snakes and even the logo depicts two snakes, but the origin of the name has its root in British humor. Guido van Rossum, the creator of Python, wrote about the origin of the name of his programming language in 1996. In December 1989, he was looking for a ‘hobby’ programming project that would keep him occupied during the week around Christmas. When he completed his project, he chose Python as a working title for the project from his favorite comedy group Monty Python’s Flying Circus.

    Development stages of Python

    Guido Van Rossum published the first version of the Python code version 0.9.0 in February 1991. This release includes numerous features like exception handling, functions, core data types, etc. It was an object-oriented programming language and had a module system (taken from Modula-3 language).

    Python version 1.0 was released in January 1994. The major new features included in this release were the functional programming tools lambda, map, filter, and reduce, which Guido Van Rossum never liked.

    Six and a half years later in October 2000, Python 2.0 was introduced. This release included list comprehensions, a full garbage collector, and it supported Unicode. Python 2.7 was the last release of 2.x series. In 2014, it was announced that Python 2.7 would be supported until 2020.

    In December 2008, the next major release of Python, Python 3.0 (also known as Python 3000 and Py3K) was released. Python 3 was not backward compatible. The latest release of Python is Python 3.6.

    Features of Python

    Python offers a list of features that makes it the first choice of almost all programmers in large IT companies. This section entitles the features provided by the Python programming language.

    Simple: Python is a compact and simple programming language. The syntax of the Python language is more similar to the English language. Python inherits the pseudo code nature, which is its greatest strength. This nature allows the programmer to concentrate on the creation of a solution to the problem rather than concentrate on the syntax of the language. In Python, programs are easy to write and execute as it omits some cumbersome, poorly understandable and confusing features of other programming languages such as C, C++, etc. These features include the exclusion of main functions, pointers, exclusion of header files, etc.

    Easy to learn: Python is extremely easy to get started with. Python is an easy-to-learn programming language and provides a stepping-stone into the world of programming. Python is easy as well as a powerful programming language that acts as the backbone for the different large IT companies like Google, Facebook, Yahoo, IBM, and so on. Python has a few keywords, simple structure, and clearly defined syntax that makes it easily understandable for beginners. Programmers familiar with traditional languages will find all the familiar constructs such as loop, conditional statements, arrays, and so on.

    Free and open-source: Python is developed under an OSI approved open source licence, making it freely usable and distributable, even for commercial use. Python is an example of FLOSS (free/libre and open-source software). In simple terms, the Python software can be freely distributed and anyone can read its source code, make changes to it, and use the pieces in new free programs. FLOSS is based on the concept of community, which shares knowledge.

    High-level language: Python is a high-level programming language. It provides abstraction to the user from the machine’s internal architecture. In other words, when you write a program in Python, you do not need to bother about the low-level details such as managing memory used by the programs. Python provides the programmer some crucial building blocks and minimizes the development time as well as the code size, resulting in a more readable code.

    Portable: Python is an interpreted language, which makes it platform independent language. A Python program has been ported to many different platforms such as Windows, Unix, Linux, Macintosh, and so on without making any changes to the code. This makes the Python programming language idle to solve any problem that ranges from low to high difficulty levels. The portability is successful if the Python program does not include some system dependent features.

    GUI programming: Python supports GUI applications that can be made and ported to numerous system calls, libraries and windows frameworks; for example, Windows MFC, Macintosh and the X Window system of UNIX.

    Integrated: Python programs can be easily integrated with different other programming languages like C, C++, Java, and so on. This means that a programmer can take advantage of the work already done in another language while using Python. The integration procedure allows different programmers with different specializations work on a single application simultaneously. By integrating Python with other programming languages, the programmers can use the strength of other languages such as the speed of C or C++ while enjoying the ease of development features of the Python programming language.

    Scalable: Python allows programmers to create large applications and even programmers can easily increase the functionality of the existing software by adding some modules to it. Python allows the programmers to grow their projects and add some other existing or new elements to the project or reuse their code to other projects. Python encourages the clean code design, high-level

    Enjoying the preview?
    Page 1 of 1