Python Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
The present book is a book on teaching Python programming that has two volumes, the first volume includes theories of this language and the second volume includes exercises of this language. This book uses Android software, the link of which is placed below https://play.google.com/store/apps/details?id=com.apkzube.learnpython Python Tutorial Python tutorial provides basic and advanced concepts of Python. Our Python tutorial is designed for beginners and professionals.Python is a simple, easy to learn, powerful, high level and object-oriented programming language. Python is an interpreted scripting language also. Guido Van Rossum is known as the founder of python programming. Our Python tutorial includes all topics of Python Programming such as installation, control statements, Strings, Lists, Tuples, Dictionary, Modules, Exceptions, Date and Time, File I/O, Programs, etc. There are also given Python interview questions to help you better understand the Python Programming. • Prerequisite - Before learning Python, you must have the basic knowledge of programming concepts. • Audience - Our Python tutorial is designed to help beginners and professionals. • Problem - We assure that you will not find any problem in this Python tutorial. But if there is any mistake, please post the problem in contact on [email protected] Python Introduction 1. Python is a general purpose, dynamic, high level and interpreted programming language. It supports Object Oriented programming approach to develop applications. It is simple and easy to learn and provides lots of high-level data structures. 2. Python is easy to learn yet powerful and versatile scripting language which makes it attractive for Application Development. 3. Python's syntax and dynamic typing with its interpreted nature, makes it an ideal language for scripting and rapid application development. 4. Python supports multiple programming pattern, including object oriented, imperative and functional or procedural programming styles. 5. Python is not intended to work on special area such as web programming. That is why it is known as multipurpose because it can be used with web, enterprise, 3D CAD etc. 6. We don't need to use data types to declare variable because it is dynamically typed so we can write a=10 to assign an integer value in an integer variable. 7. Python makes the development and debugging fast because there is no compilation step included in python development and edit-test-debug cycle is very fast. Python 2 vs. Python 3 In most of the programming languages, whenever a new version releases, it supports the features and syntax of the existing version of the language, therefore, it is easier for the projects to switch in the newer version. However, in the case of Python, the two versions Python 2 and Python 3 are very much different from each other. A list of differences between Python 2 and Python 3 are given below: • In Python 2 uses print as a statement and used as print "something" to print some string on the console. On the other hand, Python 3 uses print as a function and used as print("something") to print something on the console. • Python 2 uses the functionraw_input() to accept the user's input. It returns the string representing the value, which is typed by the user. To convert it into the integer, we need to use the int() function in Python. On the other hand, Python 3 uses input() function which automatically interpreted the type of input entered by the user. However, we can cast this value to any type by using primitive functions (int(), str(), etc.). • In Python 2, the implicit string type is ASCII, whereas, in Python 3, the implicit string type is Unicode • Python 3 doesn't contain thexrange() function of Python 2. The xrange() is the variant of range()function which returns a xrange object that works similar to Java iterator. The range() returns a list for example the function range(0,3) contains 0, 1, 2. • There is also a small change made in Exception handling in Python 3. It defines a keyword as which is necessary to be used. We will discuss it in Exception handling section of Python programming tutorial. Our Online Python Compiler You do not need to install Python on your computer to follow this tutorial. However, we recommend you to run Python programs in Online Python Compilersection. Python Features Python provides lots of features that are listed below. 1. Easy to Learn and Use - Python is easy to learn and use. It is developer-friendly and high level programming language. 2. Expressive Language - Python language is more expressive means that it is more understandable and readable. 3. UNIQUE STYLES - Python is an interpreted language i.e. interpreter executes the code line by line at a time. This makes debugging easy and thus suitable for beginners. 4. Cross-platform Language - Python can run equally on different platforms such as Windows, Linux, Unix and Macintosh etc. So, we can say that Python is a portable language. 5. Free and Open Source - Python language is freely available at offical web address (https://www.python.org/).The source-code is also available. Therefore it is open source. 6. Object-Oriented Language -Python supports object oriented language and concepts of classes and objects come into existence. 7. Extensible - It implies that other languages such as C/C++ can be used to compile the code and thus it can be used further in our python code. 8. Large Standard Library - Python has a large and broad library and prvides rich set of module and functions for rapid application development. 9. GUI Programming Support - Graphical user interfaces can be developed using Python. 10. Integrated - It can be easily integrated with languages like C, C++, JAVA etc. Python History 1 Python laid its foundation in the late 1980s. 2 The implementation of Python was started in the December 1989 by Guido Van Rossum at CWI in Netherland. 3 In February 1991, van Rossum published the code (labeled version 0.9.0) to alt.sources. 4 In 1994, Python 1.0 was released with new features like: lambda, map, filter, and reduce. 5 Python 2.0 added new features like: list comprehensions, garbage collection system. 6 On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to rectify fundamental flaw of the language. 7 ABC programming language is said to be the predecessor of Python language which was capable of Exception Handling and interfacing with Amoeba Operating System. 8 Python is influenced by following programming languages: (1) ABC language. (2) ABC language. Python Version Lets start off by looking at Python history. Python was developed at a time when many other dynamic and open-sourceprogramming languages like Tcl, Perl, Ruby etc. were also being actively developed and gaining popularity. 1 Python 1.0 In January 1994, the first version of Python 1.0 was released. This version 1 includes the major new features like the functional programming tools filter, reduce, map, and lambda etc. 2 Python 2.0 After Six and a half years later, Python 2.0 was introduced in October 2000. In this release, a full garbage collector, list comprehensions were included, and it also supports Unicode. 3 Python 3.0 then after 8 years, the next major release was made. This release wasPython 3.0 also known as”Py3K” or “Python 3000”. The major changes in Python 3.0 are: • In this version, Print is a Python function • Instead of lists, in this version, we have Views and iterators. • In this version, we have more simplified rules for ordering comparisons. For example, we cannot sort a heterogeneous list, because each element of a Python List must be comparable to other elements. • In this python version, int. long is also an int as there is only one integer type. • In this python version, when we divide two integers it resultant returns is a float instead of an integer. We can use “//” to have the “old” behavior. • In this python version, Instead of Unicode Vs. 8-bit we have Text Vs. Data • The one drawback of Python 3.0 is that it is not backward compatiblewith Python 2.x. Python Latest version: The Python latest version is 3.7.0. This stable version was released on 27 June. The current latest (as of Winter 2019) is Python 3.7.2. The Python 3.7 version is aimed at making complex tasks simple, and It is currently in production release. The improvements to Python 3.7 version include: • Data classes that reduce boilerplate when working with data in classes. • A “development mode” for the interpreter. • A new built-in for triggering the debugger. • UTF-8 mode that uses UTF-8encoding by default in the environment. • A potentially backward-incompatible change involving the handling of exceptions in generators. • Nanosecond-resolution time objects . Python Applications Area Python is known for its general purpose nature that makes it applicable in almost each domain of software development. Python as a whole can be used in any sphere of development. Here, we are specifing applications areas where python can be applied. 1. Web Applications - We can use Python to develop web applications. It provides libraries to handle internet protocols such as HTML and XML, JSON, Email processing, request, beautifulSoup, Feedparser etc. It also provides Frameworks such as Django, Pyramid, Flask etc to design and delelop web based applications. Some important developments are: PythonWikiEngines, Pocoo, PythonBlogSoftware etc. 2. AI & Machine Learning - Python has Prebuilt Libraries like Numpy for scientific computation, Scipy for advanced computing and Pybrain for machine learning (Python Machine Learning) making it one of the best languages For AI. 3. Desktop GUI Applications - Python provides Tk GUI library to develop user interface in python based application.