Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore 1. Introduction to Python

1. Introduction to Python

Published by Teamlease Edtech Ltd (Amita Chitroda), 2022-04-29 11:31:55

Description: 1. Introduction to Python

Search

Read the Text Version

Course On Python Basics PYTHON BASICS Page 1 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics UNIT - 1: INTRODUCTION TO PYTHON Structure 1.0 Learning Objectives 1.1 Introduction to Python Language 1.2 History of Python 1.3 Advantages of Python 1.4 Features of Python 1.5 Versions of Python 1.6 Applications of Python 1.7 Python 3 1.8 Python IDE (PyCharm) 1.9 Namespace 1.10 Keywords 1.11 Summary 1.12 References Page 2 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics 1.0 Learning Objectives After studying this unit, you will be able to: • Understand Python Programming, history, advantages, features, and versions • Understand Python 3 • Identify the use of Python IDE • Explain Namespace and Keywords of Python Programming 1.1 Introduction to Python Language Python is an interpreted as well as an object-oriented and high-level programming language with flexible semantics. It also has high-level built-in data structures, combined with flexible typing and flexible binding which makes it very convenient for use as a scripting language and for software development using the Rapid Application Development process. Python is a very simple and easy to learn programming language. Its syntax highlights readability, therefore, reduces the cost of maintaining programs. Python supports modules and packages, which helps in reusing the code and also provides modularity. Python has an interpreter and the huge standard library is available in source or binary form open-source for all majority platforms which can be freely distributed and used. Programmers most of the time prefer Python because it provides increased productivity and ease of use. The edit-test-debug cycle becomes incredibly fast since there is no compilation step involved Page 3 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics when working with Python. Debugging Python programs is very easy as bugs or bad input will never cause a segmentation fault. Instead, an exception is raised when the interpreter discovers an error. The interpreter will print a stack trace when the program doesn't catch the exception. A source-level debugger will permit inspection of local and global variables, assessment of arbitrary expressions, setting breakpoints, walking through the code a line at a time, and so on. The debugger is written in Python only which is testifying to Python's contemplative power. On the other hand, the fast edit-test-debug cycle makes this simple approach very impactful most of the time the quickest way to debug a program is to add a few print statements to the source. 1.2 History of Python Guido van Rossum Python was apprehended in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands as a Page 4 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics beneficiary of the ABC programming language, which was inspired by SETL. Python was developed in a way to make it capable of exception handling and interfacing with the Amoeba operating system. Its application began in December 1989. Van Rossum accepted sole responsibility for the project, as the lead developer, until 12 July 2018, when he announced his \"permanent vacation\" from his responsibilities as Python's \"benevolent dictator for life\", a name the Python community gave upon him to reproduce his long-term commitment as the project's chief decision-maker. In January 2019, active Python core developers elected a five-member Steering Council to lead the project. 1.3 Advantages of Python There are many advantages of Python, some are mentioned below: • Python has a huge presence of third-party modules to use • Python has extensive support for its libraries such as NumPy for numerical calculations, Pandas for data analytics, and so on • Python is an open-source and community-developed programming language that makes it easier for programmers • Python is very versatile as well as easy to read, learn and write even for beginners • Python has a user-friendly data structure • Python is a high-level language • Python is a dynamically typed language which means there is no need to mention data type based on the value assigned, it takes data type automatically • Python is an object-oriented language Page 5 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics • Python is extremely portable and interactive to code • Python is ideal for developing prototypes as python provides more functionality with lesser coding as compared to other languages • Python is a highly efficient programming language which means python’s clean object-oriented structure provides greater process control, and the language is armed with outstanding text processing and integration abilities, as well as its own unit testing framework, which makes it even more effective • Python opens a lot of Opportunities in (IoT)Internet of Things to development • Python is an interpreted language which means that the implementation of this language executes instructions directly without compiling a program into machine language • Python is a platform-independent language and can be used with any Operating System (OS) 1.4 Features of Python Some of the key features of Python are as follows: 1. Easy to code: Python is a high-level and user-friendly programming language. Python is a very easy-to-learn language in comparison to other languages such as C, C#, JavaScript, Java, and so on. It is very easy to type, modify and execute the code in python language and anyone can learn python basics in a short span of time. Python is also a developer-friendly language. Page 6 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics 2. Free and Open Source: Python language is freely available to download for anyone at their official website. As python is open-source it means that source code is also available to the public. So, you can download it, use it, and share it for free. 3. Object-Oriented Language: One of the core features of python is that it is an Object-Oriented programming language. Python supports object-oriented language and concepts such as classes, objects encapsulation, and so on. 4. GUI Programming Support: Python also supports Graphical User interfaces which can be made using a module such as PyQt5, PyQt4, wxPython, or Tk. PyQt5 is the most preferred option for creating graphical apps with Python. 5. High-Level Language: Python is a high-level language which means that when we write programs or code in python, we do not need to identify or keep in mind the system architecture, and also, we do not need to manage the memory since python does that for us. 6. Extensible feature: Python is an Extensible language which means we can use some Python code in C or C++ language and also, we can compile that code in C/C++ language. Page 7 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics 7. Python is a Portable language: Python language is also a portable language which means it is independent of any platform. For example, if we have python code for windows and if we want to run this code on other platforms such as Linux, Unix, and Mac then we do not need to change it, we can run this code on any platform. 8. Python is an integrated language: Python is also an integrated language since we can without any hassle integrate python with other languages like C, C++, and so on. 9. Interpreted Language: Python is an Interpreted Language since Python code is performed line by line at a time. Unlike other languages C, C++, or Java, there is no need to compile python code this makes it very easy to debug the code. The source code of python is converted into an instant form called bytecode. 10. Huge Standard Library Python has a huge standard library that provides an abundant set of modules and functions so that there is no need to write your own code for every single thing from scratch. There are many libraries available in python such as regular expressions, internet data handling, numeric and mathematics, web browsers, and so on. 11. Dynamically Typed Language: Python is a dynamically-typed language. That means the type of any variable such as int, double, long, etc, in a python program is decided at Page 8 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics run time, not in advance. With the use of this feature, we don’t need to specify the type of variable every time we declare it. 1.5 Versions of Python Page 9 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Version 1.X Python released version 1.0 in January 1994. The important new features included in this release were the functional programming tools lambda, map, filter and reduce. Van Rossum stated that \"Python acquired lambda, reduce(), filter() and map(), courtesy of a Lisp hacker who missed them and submitted working patches\". The last version released while Van Rossum was at CWI was Python 1.2. In 1995, Van Rossum sustained his work on Python at the Corporation for National Research Initiatives (CNRI) in Reston, Virginia from where he released quite a lot of versions. By version 1.4, Python had developed many new features. Prominent among these are the Modula-3 inspired keyword arguments (which are also similar to Common Lisp's keyword arguments) and built-in support for complex numbers. Also comprised is a basic form of data hiding by name mangling, however, this is easily bypassed. During Van Rossum's stay at CNRI, he launched the Computer Programming for Everybody (CP4E) initiative with an intention to make programming more available to more people, with a basic \"knowledge\" in programming languages, similar to the basic English knowledge and mathematics skills required by most employers. Python aided a dominant role in this initiative because of its attention to clean syntax, it was by now suitable, and CP4E's goals bore resemblances to its predecessor, ABC. The project was backed by DARPA. As of 2007, the CP4E project is not active, and though Python efforts to be easily Page 10 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics learnable and not too complex in its syntax and semantics, outreach to non-programmers is not an active concern. Version 2.X Python 2.0 was released in October 2000 and introduced list comprehensions, a feature lent from functional programming languages like SETL and Haskell. Python's syntax for this concept is very similar to Haskell's, except for Haskell's preference for punctuation characters and Python's preference for alphabetic keywords. Python 2.0 also introduced a garbage collector which was capable of collecting reference cycles. Python 2.1 was similar to Python 1.6.1, and also Python 2.0. Its license was renamed Python Software Foundation License. All code, documentation, and specifications added, from the time of Python 2.1's alpha release, are owned by the Python Software Foundation (PSF), a non-profit organization formed in 2001 which was modeled after the Apache Software Foundation. The new release included a change to the language requirement to support nested scopes, like other statically scoped languages. The feature was turned off by default, and not required, until Python 2.2. Python 2.2 was released in December 2001. A major advancement was the unification of Python's types (types written in C) and classes (types written in Python) into just one hierarchy. This single amalgamation made Python's object model completely and consistently object- oriented. Also, Icon-inspired generators were added. Page 11 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Python 2.5 was released in September 2006. It was introduced with a statement, which has a code block within a context manager (for example, acquiring a lock before the block of code is run and releasing the lock afterward, or opening a file and then closing it), permitting Resource Acquisition Is Initialization (RAII) like behavior and substituting a common try/finally idiom. Python 2.6 was released to accord with Python 3.0, and comprised some features from that release, as well as a new \"warnings\" mode that notified the use of features that were detached in Python 3.0. Likewise, Python 2.7 accorded with and included features from Python 3.1, which was released on June 26, 2009. Parallel 2.x and 3.x releases then stopped, and Python 2.7 was the last release in the 2.x series. In November 2014, the announcement was made that Python 2.7 would be supported until 2020, but users were motivated to move to Python 3 as soon as possible. Python 2.7 support ended on January 1, 2020, along with the code freeze of the 2.7 development division. A final release, 2.7.18, happened on April 20, 2020, and comprised fixes for critical bugs and release blockers. This indicates that Python 2 has reached the end of its useful life. Version 3.X On December 3, 2008, Python 3.0 (sometimes known as \"Python 3000\" or \"Py3K\") was released. It was created to address fundamental design issues in the language; the essential changes could not be made while maintaining full backward compatibility with the 2.x series, necessitating Page 12 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics the creation of a new major version number. \"Reduce feature duplication by deleting obsolete ways of doing things,\" was Python 3's guiding idea. Python 3.0 was built on the same principles as previous versions. However, because Python has gathered new and redundant ways to program the same task, Python 3.0 focused on reducing redundant constructs and modules, in accordance with the principle \"There should be one— and preferably only one—obvious way to do it.\" Python 3.0, on the other hand, remained a multi-paradigm language. Programmers could still use object-oriented, structured, and functional programming paradigms, among others, but the details were supposed to be more evident in Python 3.0 than they were in Python 2.x. 1.6 Applications of Python • Python language is used in GUI-based desktop applications • Python programming is used in graphic designing, image processing applications, Games, and Scientific as well as Computational Applications • Python programming language is also used by Web frameworks and applications • Enterprise and Business applications have benefited from the implementation of python • Python's implementation in Operating Systems is one lesser-known application of python • Python is used in education systems to automate many day-to-day activities Page 13 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics • Python is used in databases for analytics and for generating insights • Python can enhance overall programming language Development • Python’s simple syntax makes prototyping quicker • Python is used for developing the software using software development models such as Rapid Application Development 1.7 Python 3 Python 3.0 was released on December 3rd, 2008. Python 3.0 (a.k.a. \"Python 3000\" or \"Py3k\") is a new version of the language that is incompatible with the 2.x line of releases. The language is mostly the same, but many details, especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Also, the standard library has been reorganized in a few prominent places. Page 14 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Here are the steps to Download and Install Python 3. Step 1 − Select Version of Python to Install Python comes in a variety of versions, each with its own syntax and way of doing things. We must select the version that we want to use or require. Python 2 and Python 3 are available in various versions. Page 15 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Step 2 − Download Python Executable Installer Navigate to the Download for Windows section of the official Python website (www.python.org) in your web browser. When you select download, a list of available executable installers with varied operating system requirements will appear. Select the installer that best suits your system's operating system and download it. Let's say we go with the Windows installer (64 bits). The download size is less than 30MB. Page 16 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Step 3 − Run Executable Installer We have downloaded the Python 3.10.4 Windows 64-bit installer for demonstration. Run the installer. Make sure that you have selected both the checkboxes at the bottom and then click Install New. On clicking the Install Now, the installation process will start. The installation process will take a few minutes to complete and once the installation is successful, the following is shown. Page 17 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Step 4 − Verify Python is installed on Windows To ensure that Python is successfully installed on your system. Follow these steps − • Open the command prompt. • Type ‘python’ and press enter. Page 18 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics • The version of the python which you have installed will be displayed if the python is successfully installed on your windows. Step 5 − Verify Pip was installed Pip is a useful package management system for Python software packages. Thus, ensure that you have it installed. To verify if pip was installed, follow these steps − • Open the command prompt. • Enter pip –V to check if pip was installed. • The following output appears if pip is installed successfully Thus, the installation of Python 3 is completed. Page 19 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics 1.8 Python IDE (PyCharm) An IDE (or Integrated Development Environment) is a software development programme. IDEs, as the name suggests, combine a number of tools that are specifically built for software development. • An editor that is intended to handle code (with syntax highlighting and auto-completion feature) • Build, execution, and debugging tools • Some form of source control Most integrated development environments (IDEs) support a wide range of programming languages and offer a wealth of additional capabilities. As a result, they may be huge and require some time to download and install. To utilize them correctly, you may also require specialized understanding. A specialized code editor, on the other hand, can be as simple as a text editor, complete with syntax highlighting and code formatting. The majority of competent code editors can also operate a debugger and run code. Source control systems are also used by the finest ones. A decent dedicated code editor is typically smaller and faster than an IDE, but it has less features. PyCharm is a computer programming integrated development environment (IDE) that focuses on the Python programming language. JetBrains, a Czech enterprise, created it (formerly known as IntelliJ). It includes code analysis, a graphical debugger, a combined unit tester, Page 20 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics VCS integration, and Django and Anaconda support for web development and data science, independently. PyCharm is available in Windows, Mac OS X, and Linux versions. There is a Community Edition that is provided under the Apache License, a Professional Edition with more features that are released under a subscription-funded proprietary license, and an educational edition. Here are the steps to install PyCharm IDE. Step 1) Download the PyCharm IDE from the given link https://www.jetbrains.com/pycharm/download/ or you can search for ‘PyCharm’ in your search engine Page 21 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Step 2) Once the download is completed run the executable file to install PyCharm on your computer. Step 3) On the next screen, change the installation path if required. Click “Next Page 22 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Step 4) On the next screen, you can create a desktop shortcut if you want and click on “Next”. Step 5) Choose the start menu folder. Keep selected JetBrains and click on “Install”. Page 23 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Step 6) Wait for the installation to finish. Step 7) Once the installation is finished, you should receive a message on your screen that PyCharm is installed. If you want to go forward and run it, click the “Run PyCharm Community Edition” box first and click “Finish”. Page 24 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Step 8) After you click on “Finish,” the following screen will appear Page 25 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics 1.9 Namespace In Python, a namespace is a system in which each object has a unique name. A variable or a method might be an object. In the form of a Python dictionary, Python retains its own namespace. Let's take a look at a directory-file system in a computer as an example. Naturally, a file with the same name might be found in different folders. However, by supplying the absolute path of the file, one may be led to it whenever one wants. A namespace serves the same purpose as a surname in real life. There may be more than one \"Susan\" in the class; however, if you specifically ask for \"Susan Park\" or \"Susan Clark\" (with surname), there would only be one (for an instance, don't assume that both first and surname are the same for numerous students). Similarly, based on the namespace, the Python interpreter recognizes what particular function or variable one is pointing to in the code. As a result, the word's division provides a bit more information. Its Name (a name that serves as a unique identification) + Space (which talks about something related to scope). A name might be any Python function or variable, and space is determined by the position from where the variable or method is being accessed. Page 26 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics Types of namespaces: The Python interpreter is known as a pure Python interpreter when it operates without any user-defined modules, methods, or classes. Because they are part of the built-in namespace, several functions, such as print() and id(), are always available. When a user creates a module, a global namespace is created, and when local functions are added, a local namespace is created. The built-in namespace contains the global namespace, while the global namespace contains the local namespace. Page 27 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics The lifetime of a namespace: A namespace's lifespan is determined by the scope of objects; if the scope of an object expires, the namespace's lifetime expires as well. As a result, items in the inner namespace cannot be accessed from the outer namespace. 1.10 Keywords Python keywords are unique reserved words with defined meanings and functions that can only be used for those functions. You will not have to import these keywords into your code since they're always available. The built-in functions & types in Python are not the same as the keywords. Built-in functions & types are always accessible, although they aren't as limited in their use as keywords. As of Python 3.10, there are thirty-five keywords in Python. False await else import pass None break except in raise True class finally is return and continue for lambda try as def from nonlocal while assert del global not with Page 28 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics 1.11 Summary • Python is a dynamically semantic, interpreted, object-oriented high- level programming language. • The Python interpreter and its substantial standard library are free to download and distribute in source or binary form for all major platforms. • To run a python program, Python 3 and Python IDE is necessary to install. 2.5 Glossary • Object-oriented: It is a programming paradigm that organizes software design around data, instead of functions and logic. • Integrated Development Environment (IDE): It's application development software that integrates many developer tools together into a single graphical user interface. • Internet of Things (IoT): It is a network of physical things that have sensors, software, as well as other technologies built in them for the purpose of communicating to and sharing information between devices and systems through the internet. Page 29 of 30 All Rights Reserved. Vol. TLE001/03-2022

Course On Python Basics 1.12 References • https://www.python.org/about/ • https://wiki.python.org/moin/BeginnersGuide • https://docs.python.org/3/ • https://www.jetbrains.com/pycharm/learn/ • https://docs.python.org/3/tutorial/classes.html#python-scopes-and- namespaces Page 30 of 30 All Rights Reserved. Vol. TLE001/03-2022


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook