Anna GE 8151 - DATA, EXPRESSIONS, STATEMENTS

Unformatted text preview:

1 Unit 2 Data expressions Statements UNIT II DATA EXPRESSIONS STATEMENTS Python interpreter and interactive mode values and types int float boolean string and list variables expressions statements tuple assignment precedence of operators comments Modules and functions function definition and use flow of execution parameters and arguments Illustrative programs exchange the values of two variables circulate the values of n variables distance between two points 1 Introduction to python Python is a general purpose interpreted interactive object oriented and high level programming language It was created by Guido van Rossum during 1985 1990 Python got its name from cid 498 Monty Python cid 495 s flying circus cid 499 Python was released in the year 2000 Python is interpreted Python is processed at runtime by the interpreter You do not need to compile your program before executing it Python is Interactive You can actually sit at a Python prompt and interact with the interpreter directly to write your programs Python is Object Oriented Python supports Object Oriented style or technique of programming that encapsulates code within objects Python is a Beginner s Language Python is a great language for the beginner level programmers and supports the development of a wide range of applications 1 1 Python Features Easy to learn Python is clearly defined and easily readable The structure of the program is very simple It uses few keywords Easy to maintain Python s source code is fairly easy to maintain Portable Python can run on a wide variety of hardware platforms and has the same interface on all platforms Interpreted Python is processed at runtime by the interpreter So there is no need to compile a program before executing it You can simply run the program Extensible Programmers can embed python within their C C Java script ActiveX etc Free and Open Source Anyone can freely distribute it read the source code and edit it High Level Language When writing programs programmers concentrate on solutions of the current problem no need to worry about the low level details Scalable Python provides a better structure and support for large programs than shell scripting 1 2 Applications Bit Torrent file sharing Google search engine youtube Intel Cisco HP IBM i Robot NASA Facebook Drop box 2 Unit 2 Data expressions Statements 1 3 Python interpreter Interpreter To execute a program in a high level language by translating it one line at a time Compiler To translate a program written in a high level language into a low level language all at once in preparation for later execution Compiler Interpreter Compiler Takes Entire program as input Interpreter Takes Single instruction as input Intermediate Object Code is Generated No Intermediate Object Code is Generated Conditional Control Statements are Executes faster Conditional Control Statements are Executes slower Memory Requirement is More Since Object Code is Generated Memory Requirement is Less Program need not be compiled every time Every time higher level program is converted into lower level program Errors are displayed after entire program is checked Errors are displayed for every instruction interpreted if any Example C Compiler Example PYTHON 1 4 Modes of python interpreter Python Interpreter is a program that reads and executes Python code It uses 2 modes of Execution 1 Interactive mode 2 Script mode Interactive mode Interactive Mode as the name suggests allows us to interact with OS When we type Python statement interpreter displays the result s immediately Advantages Python in interactive mode is good enough to learn experiment or explore Working in interactive mode is convenient for beginners and for testing small pieces of code Drawback We cannot save the statements and have to retype all the statements once again to re run them In interactive mode you type Python programs and the interpreter displays the result Script mode In script mode we type python program in a file and then use interpreter to execute the content of the file Scripts can be saved to disk for future use Python scripts have the extension py meaning that the filename ends with py Save the code with filename py and run the interpreter in script mode to execute the script 3 Unit 2 Data expressions Statements Interactive mode Script mode A way of using the Python interpreter by typing commands and expressions at the prompt A way of using the Python interpreter to read and execute statements in a script Cant save and edit the code Can save and edit the code If we want to experiment with the code we can use interactive mode If we are very clear about the code we can use script mode we cannot save the statements for further use and we have to retype all the statements to re run them we can save the statements for further use and we no need to retype all the statements to re run them We can see the results immediately We cant see the code immediately Integrated Development Learning Environment IDLE Is a graphical user interface which is completely written in Python It is bundled with the default implementation of the python language and also comes with optional part of the Python packaging Features of IDLE Multi window text editor with syntax highlighting Auto completion with smart indentation Python shell to display output with syntax highlighting 2 VALUES AND DATA TYPES Values Value can be any letter number or string Eg Values are 2 42 0 and Hello World These values belong to different datatypes Data type Every value in Python has a data type It is a set of values and the allowable operations on those values Python has four standard data types 4 Unit 2 Data expressions Statements Numbers Number data type stores Numerical Values This data type is immutable i e values items cannot be changed Python supports following number types 1 integers 2 floating point numbers 3 Complex numbers 2 1 integer Integers are the whole numbers consisting of ve or ve sign Example a 10 a eval input cid 498 enter a value cid 499 a int input cid 498 enter a value cid 499 2 2 Float it has decimal part and fractional part Example a 3 15 a eval input cid 498 enter a value cid 499 a float input cid 498 ente a value cid 499 2 3 Complex It is a combination of real and imaginary part Example 2 5j a bi Sequence A sequence is an ordered collection of items indexed by positive integers It is a combination of mutable value can be changed and immutable values cannot be changed data types There are three types of


View Full Document

Anna GE 8151 - DATA, EXPRESSIONS, STATEMENTS

Download DATA, EXPRESSIONS, STATEMENTS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view DATA, EXPRESSIONS, STATEMENTS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view DATA, EXPRESSIONS, STATEMENTS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?