Exercise1 Basics Python Interactive Mode and Indentation

EXERCISE - 1 BASICS

A) Running instructions in Interactive interpreter and a Python Script




B) Write a program to purposefully raise Indentation Error and Correct it

1 A) AIM: Running instructions in Interactive interpreter and a Python Script

INTRODUCTIONS TO PYTHON

Python is a high-level programming language designed to be easy to read and simple to implement. It is open source, which means it is free to use, even for commercial applications. Python can run on Mac, Windows, and UNIX systems and has also been ported to Java and .NET virtual machines.

Scripts written in Python (.PY files) can be parsed and run immediately. They can also be saved as a compiled programs (.PYC files), which are often used as programming modules that can be referenced by other Python programs.

Python Interactive Mode:

Python has two basic modes: normal and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole.

Interactive mode is a good way to play around and try variations on syntax.

On Windows, bring up the command prompt and type "python", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu. IDLE is a GUI which includes both an interactive mode and options to edit and run files.

When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:


Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> 


Once the Python interpreter is started, you can issue any command at the command prompt ">>>". 
The first real command we will use is the print command. We will create the mandatory “Hello World” statement:
>>> l = [“A”,42,78,”Just a String”]
>>>for character in l:
...     print(character)
...
A
42
78
Just a String
>>>
The problem is that Python requires formatting of blocks like this with indentation. So to correct that error in the above code, I would simply do:


>>>print(“Hello World”)
Hello World
>>>

We will show how the interactive prompt deals with multiline statements like for loops.


2 B) AIM:Write a program to purposefully raise Indentation Error and Correct it.

INTRODUCTION TO INDENTATION:

Python does not use braces({}) to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example,

if True:
print(“True”)
else:
print(“False”)



if True:
print(“True”)
else:
     print(“False”)

No comments:

Post a Comment

Total Pageviews