Python Fundamentals – Getting Started with Python

Python Fundamentals – Getting Started with Python

WHAT IS A PROGRAM?
A program is a sequence of instructions given to the computer. A programming language is something like a code for writing down the instructions that a computer will follow. In fact, programmers often refer to their programs as computer code, and the process of writing an algorithm in a programming language is called coding. Python is one example of a programming language. Other high-level languages are C, C++, Perl, Java etc. In a high-level language like Python, the addition of two numbers can be expressed more naturally and it is easier for us to understand. But we have already studied that computer understands only the machine language also called low level language. So, we need some way to translate the high-level language into the machine language that the computer can execute. There are two ways to do this: a high-level language can either be compiled or interpreted.
A compiler reads the program written in a high-level language and translates it into an equivalent program in the machine lan¬guage of some computer. The high-level program is called source code, and the resulting machine code (object code) is a program that the computer can directly execute. Once a program is compiled we can execute it repeatedly without further translation. (See Figure 9.1).
network-automation-python-fundamentals-getting-started-with-python-1
An interpreter is a program that translates one statement of a high-level language pro¬gram in to machine codes and executes it and then translates and executes the next statement of the program. In this way, it proceeds further till all the statements of the program are translated and executed one after the other. Figure 9.2 shows the structure of interpreter
network-automation-python-fundamentals-getting-started-with-python-2
Python is considered an interpreted language because :

  1. Python programs are executed by interpreter not by compiler.
  2. A program written in a compiled language like C or C++ is converted from the source language, i.e., C or C++ into a binary code i.e. Os and Is) using a compiler. When you run the program, the linker/loader software copies the program from hard disk to memory and starts running it.
    Python, on the other hand, does not need compilation to binary. You just run the program directly from the source code.

FEATURES OF PYTHON LANGUAGE
Python is a high-level, interpreted, interactive and object oriented-scripting language.
Python was designed to be highly readable which uses English keywords frequently
where as other languages use punctuation and it has fewer syntacticalconstructions
than other languages.

  1. Python is Interpreted: This means that it is processed at runtime by the interpreter and you do not need to compile your program before executing it. Interpreted languages run directly from source code that humans generate (whereas pro¬grams written in compiled languages, like C++, must be translated to machine code before they can run).
  2. Python is Interactive: This means that you can actually works with Python prompt and interact with the interpreter directly to write your programs. Python is considered an interpreted language because Python programs executed by an interpreter.
  3. Python is Object-oriented: Python supports procedure-oriented programming as well as object-oriented programming. In procedure-oriented languages, the pro¬gram is built around procedures or functions which are nothing but reusable pieces of programs. In object-oriented languages, the program is built around objects which combine data and functionality. This means that Python supports Object-oriented style or technique of programming that encapsulates code within objects.
  4. Python is Beginner’s Language: Python is a great language for the beginner programmers as the language is interpreted, hence gives immediate results. It also supports the development of a wide range of applications, from simple text processing to WWW (World Wide Web) browsers to games.
    (e) Python is complete Language: When you install Python, you need not download and install additional libraries. All types of required functionality is available through various modules of Python standard library. Thus, it is also called – Python follows “Batteries Included” Philoshopy

HISTORY OF PYTHON
Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Nether¬lands. The language was released in 1991. Python is derived from many other lan¬guages, including ABC, Modula-3, C, C++, Algol-68, SmallTalk, and UNIX shell and other scripting languages. Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public License (GPL).

Characteristics of Python Language
Python has following characteristics:

  1. Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined syntax. So, it is easy to learn and use.
  2. Easy-to-read: Python code is much more clearly defined and visibly to the eyes. So, Program written in Python are easily readable and understandable.
  3.  Easy-to-maintain: Python’s success is that its source code is fairly easy-to- maintain. It can be used as a scripting language or can be compiled to byte-code for building large applications.
  4.  A wide standard library: One of Python’s greatest strengths is the bulk of the library with add on modules is present and is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.
  5.  Interactive Mode: Support for an interactive mode in which you can enter results from a terminal right to the language, allowing interactive testing and debugging of code.
  6. Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms. So, it is called platform independent programming language. Python runs on Windows, Linux/Unix, Mac OS X, and has been ported to the Java and .NET virtual machines also.
    Since Python can rim equally well on variety of platforms such as Windows, Linux/UNIX, Machintosh, Supercomputer, smart phones so, Python is also called a true cross-platform language.
  7. Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient.
  8. Databases: Python provides interfaces to all major commercial databases available today.
  9. GUI Programming: Python supports Graphical User Interface or GUI applications that can be created and ported to many system calls, libraries, and windows systems, such as Windows MFC (Microsoft Foundation Class(es), Macintosh, and the X Window system of UNIX.
  10. Scalable: Python provides a better structure and support for large programs than shell scripting.
  11. Free and Open Source : Python is an example of a FLOSS (Free/Libre and Open Source Software). In simple terms, you can freely distribute copies of this soft¬ware, read its source code, make changes to it, and use pieces of it in new free programs.
  12.  Variety of Usage and Applications : Python is general purpose programming language which can be used for both scientific and non-scientific programming. This language is used by companies in real revenue generating products, such as operation of Google search engine, youtube. Intel, Cisco, HP (Hewlett-Packard), IBM (International Business Machines) use Python for hardware testing. NASA (National Aeronautics and Space Administration) and other use Python for their scientific programming task. It is also used in Raoid Prototyping, Database applications, Game development, Web applications, System Administrations etc.

HOW TO DOWNLOAD AND INSTALL PYTHON?
Python distribution is available for a wide variety of platforms. Python runs on Windows, Linux/UNIX, MAC OS X, and has been ported to the Java and .NET virtual machines also with no special hardware requirement. The most up-to-date and current source code, binaries, documentation, news, etc., can be downloaded from the official Website of Python: http://www.python.org/
To install Python on Windows machine, Open a Web browser and go to http://www.python.org/download
Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you are going to install. We will be working with Python 2.7.
Choose the required format/platform for download. Since we are working in Windows environment, so we choose Windows x86 MSI or Microsoft Installer.
Run the downloaded file by double-clicking it in Windows Explorer. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the installation is finished.

HOW TO START PYTHON?
After installation is successfully completed, we can start Python in three different ways. These are :

  1.  Interactive Interpreter
  2. Script from the Command-line
  3.  Integrated Development Environment (IDLE)

Interactive Interpreter
You can enter python and start coding right away in the interactive interpreter by start¬ing it from the command line. In Intractive mode, you type Python command in front of Python command prompt »>, the Python executes the given command there, and then gives the output. For example if you type 2+3 in front of Python prompt, it will display result 5.

To start Python’s interactive mode, follow these steps:
(a) If you’re using Windows, open the Command Prompt window and Type Python and press Enter key. If you directly open Command Prompt Window, your screen will look similar to as shown in Figure 9.3. Or Click on Start Button —» All Programs —» ActiveState ActivePython 2.7 (32 bit) -> Python Package Manag- er(PyPM). Type Python and your screen will look similar to shown in Figure 9.4. and 9.5.

network-automation-python-fundamentals-getting-started-with-python-3
network-automation-python-fundamentals-getting-started-with-python-4
network-automation-python-fundamentals-getting-started-with-python-5
When Python’s interactive mode starts up, it tells you what version is running, the date the version was released, and a few hints about what to do next. Then it displays the Python prompt: ››› also known as primary prompt indicating that Python is ready to take commands from the user. When you use interactive mode, Python processes each line of code you type as soon as you press Enter key.
The Python interpreter can be used like a calculator. If you just want to do simple calculations, you can type numbers and operators and press Enter key to see the result, as in the example shown in Figure 9.6. The ›››, is the prompt the interpreter uses to indicate that it is ready. If you type 2 + 2, the interpreter displays result 4.

network-automation-python-fundamentals-getting-started-with-python-6

In Programming languages, a complete command is called a statement. In Python you can also print statements. Type the following text to the right of Python prompt and Press Enter Key.

It will  display the result as shown in Fig 9.7
network-automation-python-fundamentals-getting-started-with-python-7

Quitting Interactive Mode
To exit Python’s interactive mode, press Ctrl-Z. If you type quit(), Python reminds you what to do, like this:

‘Use quit() or Ctrl-Z plus Return to exit.’
When you exit interactive mode, you return to the command prompt of your Terminal window or Command Prompt window.

Script from the Command-line 
Interactive mode does not save commands entered by the user. But if you want to save all the commands in the form of program file and want to see all output line together then script mode of Python is used. A program stored in a file (usually one that will be interpreted) is called script. This file is saved on a disk so that it can be used over and over again. A way of using the Python interpreter to read and execute statements in a script is called script mode. Because Python’s interactive mode doesn’t save any of your work when you quit, you may want to store your important work in text files. Text files that contain Python code are called scripts (if they are whole programs) or modules (if they contain chunks of code meant to be imported into other programs). Both scripts and modules end with the suffix .py.
You can write multiline programs or parts of programs in interactive mode. However, note that interactive mode doesn’t let you save your work. When you write multiline statements, the … (continuation prompt) will be displayed. This … (continuation prompt) is also called secondary prompt indicating that interpreter is waiting for addi¬tional input to complete the current statement. If you type a function definition in inter¬active mode, the interpreter prints ellipses (…) to let you know that the definition is not complete. A function definition specifies the name of a new function and the sequence of statements that execute when the function is called. To end the function, you have to enter an empty line.

Python allows us put a sequence of statements together to create a brand-new command called function. The first line tells Python that we are defining a new function called goldenlines(). The following lines are indented to show that they are part of the golden- lines() function. The blank line (obtained by hitting the <Enter> key twice) lets Python know that the definition is finished, and did not cause anything to happen.
The function is invoked by typing its name.

›››is the default Python prompt of the interactive shell. Often seen for code examples which can be executed interactively in the interpreter.
…is the default Python prompt of the interactive shell when entering code for an indented code block or within a pair of matching left and right delimiters (parentheses, square brackets or curly braces).

Integrated Development Environment (IDLE)
You can run Python from a Graphical User Interface (GUI) environment as well. All you need is a GUI application on your system that supports Python. A special type of program known as a Programming environment is specifically designed to help programmers to write programs and include features, such as automatic indenting, colour, highlighting, and interactive development.
When you write multiline statements, IDLE does not display the … continuation prompt. IDLE automatically indents lines for you.
IDLE uses colored syntax to highlight your code. By default, built-in functions are purple, strings are green, and language keywords (like print, if, else) are orange. Any results produced are in blue. The code you type is colourized to make it easier to distinguish one part of a statement from another.

Creating and Saving program in IDLE
When you open IDLE, you see a window called Python Shell, as shown in Figure 9.8. The Python Shell window runs in interactive mode; it displays the Python prompt, »>. Typing statements and programs in the Python Shell Interactive mode in IDLE works much the same as interactive mode in a shell window.
IDLE includes a text editor for opening, editing, and creating modules and scripts. Let us create and save program in text editor. To do so, follow the following instructions:

(a) Click on Start button —>All Programs —> Active State Active Python 2.7 —>
IDLE(Python GUI). It will display Figure as shown in Figure 9.8.
network-automation-python-fundamentals-getting-started-with-python-8

(b) To open a new text editing window, choose File —> New Window as shown in Figure 9.9.
network-automation-python-fundamentals-getting-started-with-python-9

In this new window type the commands you want to save in the form of program or script as shown in Figure 9.10.
network-automation-python-fundamentals-getting-started-with-python-10
(c) Use File —> Save or File —» File Save As option to save the file. Note that all Python programs are saved with .py extension.

Opening and Running Program in IDLE
(a) Click on File -> Open option to open the saved program, module or script.
(b) To run a program, module or script, make sure it is open in the text editor, and choose Run —» Run Module or Press F5 key. See Figure 9.11.
network-automation-python-fundamentals-getting-started-with-python-11

The Python Shell restarts (clearing any names that it was storing from the previous session) and Python executes the code. Since we created function, so we have to call the function by typing golclenlines() at the prompt a s shown in Figure 9.12.
network-automation-python-fundamentals-getting-started-with-python-12
Work effectively with IDLE
IDLE has lots of features, but you need to know about only a few of them.

TAB Completion
Start to type in some code, and then press the TAB key. IDLE will offer suggestions to help you complete your statement.
Type pr and then TAB at the »> prompt to see IDLE’s list of command completion suggestions.

Recall Code Statements
Press Alt-P to recall the previous code statement entered into IDLE or press Alt-N to move to the next code statement (assuming there is one). Both key combinations can be used to cycle rapidly through all of the code you have entered into IDLE, re-executing any code statements as needed.

Edit Recalled Code
Once you recall your code statement, you can edit it and move around the statement using the arrow keys. It’s possible to edit any statement that you have previously entered, even code statements that span multiple lines.

Adjust IDLE’s Preferences
IDLE’s preferences dialog lets you adjust its default behavior to your tastes. There are four tabs settings to adjust. You can control font and tab behavior, the colours used to syntax highlight, the behavior of certain key combinations, and IDLE’s start-up settings.

PYTHON CHARACTER SET
Character set is a set of valid characters that a language can recognize. There are two character sets in Python language. These are:

  1. Source characters
  2. Execution characters/Escape sequences

Source Characters
Using source characters, source text is created. Following is the list of source characters:
Alphabets         A to Z, a to z and _ (underscore)
Numbers          0 to 9
Special characters    +-*&^/^˜%=!|(){}[]?’,:;/#blank”

Execution Characters/Escape Sequences
These characters are interpreted at the time of execution. The values of execution char-acters are implementation-defined.
The characters on the keyboard can be printed or displayed by pressing the key. But some characters, such as line feed, form feed, tab, etc., cannot be printed or displayed directly. Python provides the mechanism to get such characters that are invisible or dif¬ficult through execution characters. Execution characters and escape sequences are used inter-changeably.
Escape codes are embedded inside string literals and start with a backslash character (\). They are used to embed characters that are either unprintable or have a special syntactic meaning to Python that you want to suppress. Each of the escape sequences shall pro¬duce a unique implementation-defined value which can be stored in a single character. These escape sequences are represented by a backslash (\) followed by a character. Though there are two or more characters in an escape sequence, they are considered as a single character. Some of them are shown in Table 9.1
network-automation-python-fundamentals-getting-started-with-python-13

Many other characters, after backslash (\), in output statements will result in display of the character itself. For example, the execution of \k will result in displaying of k.

IDENTIFIERS
An identifier is a unique name that enables you to identify something. Identifiers are used to label variables, functions, classes, objects, and modules. They begin with either a letter or an underscore, and they can contain letters, underscores, or digits. This implies that a single identifier cannot contain any space. They cannot contain punctuation marks. Python is a case-sensitive programming language. Thus AGE, Age and age are three different identifiers in Python. Some legal identifiers are x, age, marks2, Total_Marks, etc.

LINES AND INDENTATION
In Python, unlike a compiled language, such as C, line breaks are significant, and the end of a program statement is defined by hard return. There are no braces to indicate blocks of code for class and function definitions or flow control. Program blocks are defined by a combination of statements and program blocks, are delimited by use of indentation. A block is a group of statements that can be executed if a condition is true (conditional statements), or executed several times (loops), and so on. A block is created by indenting a part of your code; that is, putting spaces in front of it.
The number of spaces in the indentation is changeable, but all statements within the block must be indented the same amount. For example,

Thus, in Python all the continous lines indented with similar number of spaces would form a block.
There are two simple syntax rules to remember about code blocks:

  1. The code on the first line of a code block always ends with a colon (:).
  2.  The code underneath the first line of a code block is indented.

Python programmers almost always use four spaces for the first level of indentation, eight spaces for the next level (a code block nested inside another code block), and so on. The indentation must be the same for every line of a block. You get an Indentation Error message (syntax error) if you use, for example, four spaces on one line of a block and three spaces on another line in the same block.

MULTI-LINE STATEMENTS
Statements in Python typically end with a new line, i.e., at the end of the line. Sometimes it is good to break a long statement into small pieces. Python does, however, allow the use of the line continuation character (\) at the end of a line to denote that the line should continue. It is possible to have a single statement span multiple lines. You could do this for artistic reasons or simply because the line is too long to read on one screen. To do this, simply put a space and a backslash at the end of the line. Here are a few examples,

Statements contained within the [], {}, or () brackets do not need to use the line continu-ation character. For example,

QUOTATION IN PYTHON
The quotation marks in the program mark the beginning and end of the text to be dis-played, they don’t appear in the result. Python accepts single (‘), double (“) and triple (“‘ or “””) quotes to denote string literals, as long as the same type of quote starts and ends the string. The triple quotes can be used to span the string across multiple lines. For example, all the following are legal:

The use of triple quoted strings, makes it possible for you to continue a string on a new line, and to preserve the line break in the string.

COMMENTS IN PYTHON
In programming, a comment is text that you insert into the program that is for docu-mentation purpose. Comments provide a quick and easy form of documentation. They are ignored by the compiler or interpreter and are intended to explain what you are doing.
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the physical line (A physical line is what you see as a line when you write a program) end are part of the comment, and the Python interpreter ignores them.
For example,

In Python, use a triple quote for multiple-line comments. When you use a triple quote without assigning it to a variable, everything between the triple quotes is considered a comment.
Python has a special feature: if the beginning of a program is just a quoted string, that string is taken to be the program’s documentation string. Multiline comment can be written as follows:

Python Style Rules and Conventions
Following table summarizes the list of some of the Python style rules and conventions.

network-automation-python-fundamentals-getting-started-with-python-14
network-automation-python-fundamentals-getting-started-with-python-15

ERROR MESSAGES
When Python detects an error in your code, it will display an error message, which indicates that you have made a mistake and what the cause of the error might be. Two basic categories of error, messages will occur. These are:

  1.  Syntax error or compilation error messages
  2. Run-time error messages

Syntax Error or Compilation Error Messages
These error and messages are detected and displayed by the Python interpreter as it attempts to translate the program and recognizes as illegal when first reading it. This prevents execution of your code. If a statement has a syntax error, it cannot be translated and the program will not be executed. For example, you get this error message when you try to use an equals sign (=) to do an arithmetic calculation:

The messages differ depending on the type of error but usually include these types of information. For a syntax error, a caret (A) points to the problem character (or to the place where the interpreter first figured out there was a problem).
As you will write and execute the programs, you will get more familiar with this type of error messages.

Run-time Error Messages
Run-time errors are detected by the computer and are displayed during the execution of a program. A run-time error occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. When a run-time error occurs, the computer stops executing the program and prints a message which helps in detect¬ing the error in the program. Such a message is known as run-time error message and it helps in diagnosing the mistake in the program at the time of execution. Some error messages include information about the conditions in which the error occurred (this is called
traceback information). The message you get when you try to divide a number by zero as in following example:

In Python, indentation is built into the interpreter. Code that’s part of a block must be indented, or else you get an error message. This is one of the features that makes Python especially readable.
When Python detects nothing illegal, but you do not get the results you desire is called logical error. These errors are the hardest to trace down.

 

Solved Exercise

Question 1. 
What is a program?
Answer:.
A program is a sequence of instructions given to the computer.

Question 1. 
Distinguish between compiler and interpreter.
Answer:.
The differences between compiler and interpreter are as follows:

  1. A compiler reads the program written in a high-level language and translates it into an equivalent program in the machine language of some computer.
    An interpreter is a program that translates one statement of a high-level language program in to machine codes and executes it and then translates and executes the next statement of the program.
  2. A program written in a compiled language like C or C++ is converted from the source language i.e. C or C++ into a binary code i.e. Os and Is) using a compiler. When you run the program, the linker/loader software copies the program from hard disk to memory and starts running it.
    Python, on the other hand, does not need compilation to binary. You just run the program directly from the source code.
    Python programs are executed by interpreter not by compiler.
    (c)Interpreted languages run more slowly because the translation takes place line by line, but development and debugging is faster.

Question 3. 
What is Python?
Answer:.
Python is a high-level, interpreted, interactive and object oriented-scripting language. Python was designed to be highly readable which uses English keywords frequently where as other languages use punctuation and it has fewer syntactical constructions than other languages.

Question 4. 
Discuss some characteristics of Python Language.
Answer:.
Python has following features:

  1. Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined syntax. So, it is easy to learn and use.
  2.  Easy-to-read: Python code is much more clearly defined and visible to the eyes. So, Program written in Python are easily readable and understandable.
  3.  Interactive Mode: Support for an interactive mode in which you can enter results from a terminal right to the language, allowing interactive testing and
    debugging of code.
  4.  Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms. So, it is called platform independent programming language.

Question 5. 
What do you mean by the statement ‘Python is Interactive’?
Answer:.
This statement means that you can actually work at a Python prompt and interact with the interpreter directly to write your programs. Python is considered an inter¬preted language because Python programs executed by an interpreter.

Question 6. 
Is Python is case-sensitive?
Answer:.
Python is a case-sensitive programming language. Thus AGE, Age and age are three different identifiers in Python. Some legal identifiers are x, age, marks2, Total_Marks etc.

Question 7. 
What are comments in a Python program?
Answer:.
In programming, a comment is text that you insert into the program that is for docu-mentation purpose.
A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the physical line (A physical line is what you see as a line when you write a program) end are part of the comment, and the Python interpreter ignores them.

Question 8. 
What is multi-line statement?
Answer:.
Statements in Python typically end with a new line i.e. at the end of the line. Some¬times it is good to break a long statement into small pieces. Python does, however, allow the use of the line continuation character (\) at the end of a line to denote that the line should continue.

Question 9. 
Why indentation is important in Python?
Answer:.
Python programmers almost always use four spaces for the first level of indentation, eight spaces for the next level (a code block nested inside another code block), and so on. The indentation must be the same for every line of a block. You get an Indentation Error message (syntax error) if you use, for example, four spaces on one line of a block and three spaces on another line in the same block.

Question 10. 
Why is Python called interpreted language?
Answer:.
A program written in a compiled language like C or C++ is converted from the source language, i.e., C or C++ into a language that is spoken by your computer (binary code i.e., Os and Is) using a compiler with various flags and options. When you run the program, the linker/loader software copies the program from hard disk to memory and starts running it.
Python, on the other hand, does not need compilation to binary. You just run the program directly from the source code. Internally, Python converts the source code into an intermediate form called bytecodes and then translates this into the native language of your computer and then rims it. All this, actually, makes using Python much easier since you don’t have to worry about compiling the program, making sure that the proper libraries are linked and loaded, etc. This also makes your Python programs much more portable.

Question 11. 
What is the difference between a keyword and an identifier?
Answer:.
Keyword is a special word that has a special meaning and purpose. Keywords are reserved and are a few. For example, if, for, print etc., are keywords.
Identifier is the user-defined name given to a part of a program viz. Variable, object, function etc. Identifiers are not reserved. These are defined by the user and they can have letters, digits and a symbol underscore. They must begin with either a letter or underscore. For instance, _chk, chess, trial, etc., are valid identifiers.

Question 12. 
What are the rules for naming a variable?
Answer:.
A variable is a symbol that represents a storage location in the computer’s memory. The information that is stored in that location is called the value of variable.
The following are the rules to name a variable.

  1.  It consists of any sequence of letters, digits and underscores(_). No other spe¬cial characters are allowed.
  2.  The first character should be a letter or an underscore. It can be followed by letters, digits or underscore.
  3.  Upper and lower case letters are distinguishable. Though upper case letters are allowed, usually Python variables are written in lower case only. Thus Num, NUM and num are three different variables.
  4.  Reserved words (keywords) cannot be used as names of identifiers/variables.

Question 13. 
Python is free and open source. Comment.
Answer:.
Python is an example of a FLOSS (Free/Libro and Open Source Software). In simple terms, you can freely distribute copies of this software, read its source code, make changes to it, and use pieces of it in new free programs. FLOSS is based on the concept of a community which shares knowledge. This is one of the reasons why Python is so good – it has been created and is constantly improved by a community who just want to see a better Python.

Question 14. 
Why Python is considered an ideal language for scripting and rapid application development?
Answer:.
Python is an easy to learn, powerful programming language. It has efficient high- level data structures and a simple but effective approach to object-oriented pro¬gramming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

Question 15. 
‘Python is a true cross-platform language’. Comment.
Answer:.
Python can run equally well on variety of platforms such as Windows, Linux/UNIX, Machintosh, Supercomputer, smart phones. So, Python is a portable language and a true cross-platform language.

Question 16. 
Differentiate between run time error and syntax error.
Answer:.
A run time error is an error that causes abnormal program termination during execu¬tion. For example, (division by zero) dividing a number by zero.
A syntax error are most common errors and much easier to correct them other errors because they usually produce a helpful error message. For example, undefined symbols, statement missing, etc.

Question 17. 
The only prompt used by the Python interactive system is the one consisting of three right-angle brackets (›››, True or False?
Answer:.
False. The Python interactive system also uses a secondary prompt consisting of three periods (…).

Question 18. 
Write one advantage of using triple quoted strings.
Answer:.
The use of triple quoted strings, makes it possible for you to continue a string on a new line, and to preserve the line break in the string.

Question 19. 
What is a script?
Answer:.
A script is a text file containing the programming statements that comprise a Python program.

Question 20. 
Indentation in Python is used for cosmetic purposes only. True or False?
Answer:.
False. Indentation is used in Python to determine the grouping of statements, which causes it to be critical to the success of a program.

Multiple choice questions

Question 1. 
Feature of Python program:
(a) Easy-to-learn and read
(b) Interactive Mode
(c) Free and Open Source
(d) All of the above
Answer: (d)

Question 2. .
Indicate which of the following is not true about an interpreter?
(a) Interpreter generates an object program from the source program.
(b) Interpreter is a kind of translator.
(c) Interpreter analysis each source statement every time it is to be executed.
(d) None of the above
Answer: (d)

About the author

James Palmer

Leave a Comment