PYTHON APPLICATION PROGRAMMING Module 1 Book Python for
PYTHON APPLICATION PROGRAMMING Module – 1 Book: “Python for Everybody: Exploring Data Using Python 3”, Charles R. Severance Presented By Dr. Vandana Jha
Introduction to Python Programming 2 Dr. Vandana Jha, Associate Professor, Dept. of CSE
What is � Python is a clear and powerful object-oriented Python? programming language, comparable to Perl, Ruby, Scheme, or Java. 3 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Histor y �Python is a high-level, interpreted scripting language developed in the late 1980 s by Guido van Rossum at the National Research Institute for Mathematics and Computer Science in the Netherlands. �The initial version was published at the alt. sources newsgroup in 1991, and version 1. 0 was released in 1994. 4 Dr. Vandana Jha, Associate Professor, Dept. of CSE
�Python 2. 0 was released in 2000, and the 2. x 5 versions were the prevalent releases until December 2008. �At that time, the development team made the decision to release version 3. 0, which contained a few relatively small but significant changes that were not backward compatible with the 2. x versions. �Python 2 and 3 are very similar, and some features of Python 3 have been backported to Python 2. But in general, they remain not quite compatible. Dr. Vandana Jha, Associate Professor, Dept. of CSE (Source: https: //realpython. com/python-
�The name Python, by the way, derives not from the snake, but from the British comedy troupe Monty Python’s Flying Circus, of which Guido was, and presumably still is, a fan. 6 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Some of Python's notable � Uses an elegant syntax, making the programs you write easier to features: read. � Is an easy-to-use language that makes it simple to get your program working. � Comes with a large standard library that supports many common programming tasks such as connecting to web servers, searching text with regular expressions, reading and modifying files. � Python's interactive mode makes it easy to test short snippets of code. There's also a bundled development environment called IDLE. � Is easily extended by adding new modules implemented in a compiled language such as C or C++. 7 Dr. Vandana Jha, Associate Professor, Dept. of CSE (Source: https: //wiki. python. org/moin/Beginners. Guide/Overview)
�Can also be embedded into an application to provide a programmable interface. �Runs anywhere, including Mac OS X, Windows, Linux, and Unix, with unofficial builds also available for Android and i. OS. �Is free software in two senses. It doesn't cost anything to download or use Python, or to include it in your application. �Python can also be freely modified and re-distributed, because while the language is copyrighted it's available under an open source license. 8 Dr. Vandana Jha, Associate Professor, (Source: Dept. of CSE
Some programming-language features: � A variety of basic data types are available: numbers (floating point, complex, and unlimited-length long integers), strings (both ASCII and Unicode), lists, and dictionaries. � Python supports object-oriented programming with classes and multiple inheritance. � Code can be grouped into modules and packages. � The language supports raising and catching exceptions, resulting in cleaner error handling. � Data types are strongly and dynamically typed. Mixing incompatible types (e. g. attempting to add a string and a number) causes an exception to be raised, so errors are caught sooner. � Python contains advanced programming features such as generators and list comprehensions. � Python's automatic memory management frees you. Dr. Vandana from. Jha, Associate having to manually allocate and free Professor, 9 memory in your code. Dept. of CSE
Module �Chapter 1: Why 1: should you learn to write programs �Chapter 2: Variables, expressions and statements �Chapter 3: Conditional execution �Chapter 4: Functions 10 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Chapter 1: Why should you learn to write � 1. 1 Creativity andprograms motivation � 1. 2 Computer hardware 11 architecture � 1. 3 Understanding programming � 1. 4 Words and sentences � 1. 5 Conversing with Python � 1. 6 Terminology: interpreter and compiler � 1. 7 Writing a program � 1. 8 What is a program? � 1. 9 The building blocks of programs Dr. Vandana Jha, Associate Professor, Dept. of CSE � 1. 10 What could possibly go
Computer hardware architecture 12 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Terminology: Interpreter and Compiler �Python is a high-level language intended to be relatively straightforward for humans to read and write and for computers to read and process. The actual hardware inside the Central Processing Unit (CPU) does not understand any of these high-level languages. �The CPU understands a language called machine language. �Since machine language is tied to the computer hardware, machine language is not portable across different types of hardware. 13 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Translat �Allows or programmers to write in high-level languages like Python or Java. Script and convert the programs to machine language for actual execution by the CPU. �These programming language translators fall into two general categories: (1) interpreters and (2) compilers. �Programs written in high-level languages can be moved between different computers by using a different interpreter on the new machine or recompiling the code to create a machine language version of the program for the new machine 14 Dr. Vandana Jha, Associate Professor, Dept. of CSE
1) � An. Interpreter: interpreter reads the source code of the program as written by the programmer, parses the source code, and interprets the instructions on the fly. � Python is an interpreter and when we are running Python interactively, we can type a line of Python (a sentence) and Python processes it immediately and is ready for us to type another line of Python. >>> x = 6 >>> print(x) 6 >>> y = x * 7 >>> print(y) 42 � Even though we are typing these commands into Python one line at a time, Python is treating them as an ordered sequence of statements with later statements able to retrieve data created in earlier statements. We are writing our first simple paragraph with Dr. Vandana Jha, Associate Professor, 15 four sentences in a logical and meaningful order. It is the nature of Dept. of CSE an interpreter to be able to have an interactive conversation as
2) �Compilers needs to be handed the entire program in a Compiler: file, and then it runs a process to translate the high-level source code into machine language and then the compiler puts the resulting machine language into a file for later execution. �If you have a Windows system, often these executable machine language programs have a suffix of “. exe” or “. dll” which stand for “executable” and “dynamic link library” respectively. In Linux and Macintosh, there is no suffix that uniquely marks a file as executable. 16 Dr. Vandana Jha, Associate Professor, Dept. of CSE
�The Python interpreter is written in a high-level language called “C”. �So Python is a program itself and it is compiled into machine code. When you installed Python on your computer (or the vendor installed it), you copied a machine-code copy of the translated Python program onto your system. In Windows, the executable machine code for Python itself is likely in a file with a name like: C: Python 35python. exe 17 � Click Jha, Associate Professor, Dr. Vandana Dept. of CSE
General types of �Syntax error: means that you errors have violated the “grammar” rules of Python. �Logic errors: A logic error is when your program has good syntax but there is a mistake in the order of the statements or perhaps a mistake in how the statements relate to one another �Semantic errors: A semantic error is when your description of the steps to take is syntactically perfect and in the right order, but there is simply a mistake in the program. The program is perfectly correct but it does not do what you intended for it to do. 18 Dr. Vandana Jha, Associate Professor, Dept. of CSE
The building blocks of programs �Input: Get data from the “outside world”. This might be reading data from a file, or even some kind of sensor like a microphone or GPS. In our initial programs, our input will come from the user typing data on the keyboard. �Output: Display the results of the program on a screen or store them in a file or perhaps write them to a device like a speaker to play music or speak text. �Sequential execution: Perform statements one after another in the order they are encountered in the script. �Conditional execution: Check for certain conditions and then execute or skip a sequence of statements. �Repeated execution: Perform some set of statements Dr. Vandana usually Jha, Associatewith Professor, some variation. 19 repeatedly, Dept. of CSE
Writing a program �Script: When we want to write a program, we use a text editor to write the Python instructions into a file, which is called a script. �By convention, Python scripts have names that end with. py �To execute the script, you have to tell the Python interpreter the name of the file. 20 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Installing the Python �Before you can converse with Python, you must first install the Python software on your computer and learn how to start Python on your computer. �https: //www. python. org/downloads/ �https: //www. anaconda. com/distribution/ 21 Dr. Vandana Jha, Associate Professor, Dept. of CSE
21 22 Dr. Vandana Jha, Associate Professor, Dept. of CSE Tejaswini H, Assistant Professor, Dept.
23 Dr. Vandana Jha, Associate Professor, Dept. of CSE Tejaswini H, Assistant Professor, Dept. of CSE
Python �Python is most Shell commonly translated by use of an interpreter. �Thus python provides the very useful ability to execute in interactive mode. �The window that provides this interaction is referred to as the python shell. �Although working in the python shell is convenient, the entered code is not saved. 24 Dr. Vandana Jha, Associate Professor, Dept. of CSE Tejaswini H, Assistant Professor, Dept. of CSE
Python Shell 25 Dr. Vandana Jha, Associate Professor, Dept. of CSE
IDL � EIntegrated Development Environment is a bundle set of software tools for program development. �This typically includes �Editor: for creating and modifying programs �Translator: for executing programs �Program debugger: takes control of the execution of a program to aid in finding program errors 26 Dr. Vandana Jha, Associate Professor, Dept. of CSE
IDL E 27 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Anaconda Navigator 28 Dr. Vandana Jha, Associate Professor, Dept. of CSE
�At some point, you will be in a terminal or command window and you will type python or python 3 and the Python interpreter will start executing in interactive mode and appear somewhat as follows: 29 Dr. Vandana Jha, Associate Professor, Dept. of CSE
�The >>> (chevron) prompt is the Python interpreter’s way of asking you, “What do you want me to do next? ” Python is ready to have a conversation with you. >>> quit() �The proper way to say “good-bye” to Python is to enter quit() at the interactive chevron >>> prompt. 30 Dr. Vandana Jha, Associate Professor, Dept. of CSE
�In a Unix or Windows command window, you would type python hello. py as follows: csev$ cat hello. py print('Hello world!') csev$ python hello. py Hello world! csev$ 31 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Conversing with �print() Python 32 Dr. Vandana Jha, Associate Professor, Dept. of CSE
print DEMO 33 Dr. Vandana Jha, Associate Professor, Dept. of CSE
34 Dr. Vandana Jha, Associate Professor, Dept. of CSE
35 Dr. Vandana Jha, Associate Professor, Dept. of CSE
The Python �The Python Standard Library is a collection of built-in modules, each providing specific functionalities beyond what is included in the core part of Python. �In order to utilize the capabilities of a given module in a specific program, an import statement is used. 36 Dr. Vandana Jha, Associate Professor, Dept. of CSE
Keywords in python import keyword. kwlist 37 Dr. Vandana Jha, Associate Professor, Dept. of CSE
False assert class elif for import nonlocal raise with 38 None async continue else from in not return yield Dr. Vandana Jha, Associate Professor, Dept. of CSE True and await def except global is or try s break del finally if lambda pass while a
Commen ts # is used for commenting Example: #print(hello) 39 Dr. Vandana Jha, Associate Professor, Dept. of CSE
References Text Books: • Charles R. Severance, “Python for Everybody: Exploring Data Using Python 3”, 1 st Edition, Create. Space Independent Publishing Platform, 2016. (Chapters 1 – 13, 15). • Allen B. Downey, "Think Python: How to Think Like a Computer Scientist”, 2 nd. Edition, Green Tea Press, 2015. Reference Books: • Charles Dierbach, "Introduction to Computer Science Using Python", 1 st Edition, Wiley India Pvt Ltd. ISBN-13: 978 -8126556014. • Mark Lutz, “Programming Python”, 4 th Edition, O’Reilly Media, 2011. ISBN-13: 978 -9350232873. Class Notes Question Bank NPTEL or other Videos: https: //onlinecourses. nptel. ac. in/noc 20_cs 36/ Dr. Vandana Jha, Department of CSE, KSSEM 40
- Slides: 40