Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

PYTHON-PROGRAMMING-topic-1

Uploaded by

mariyarovic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

PYTHON-PROGRAMMING-topic-1

Uploaded by

mariyarovic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

PYTHON

PROGRAMMING
A BRIEF HISTORY OF PYTHON

Python was developed by


Python is derived from many
Guido van Rossum in the Python is copyrighted. Like
other languages, including
late eighties and early Perl, Python source code is
ABC, Modula-3, C, C++,
nineties at the National now available under the
Algol-68, SmallTalk,
Research Institute for GNU General Public License
and Unix shell and
Mathematics and Computer (GPL).
other scripting languages.
Science in the Netherlands.
For many uninitiated people, the word Python is related to a species
of snake. Rossum though attributes the choice of the name Python to
a popular comedy series Monty Python's Flying Circus on BBC.

Being the principal architect of Python, the developer community


conferred upon him the title of Benevolent Dictator for Life (BDFL).

However, in 2018, Rossum relinquished the title. Thereafter, the


development and distribution of the reference implementation of
Python is handled by a nonprofit organization Python Software
Foundation.
THE EVOLUTION OF PYTHON

Working at CWI 02.1991 Python 0.9.0 The first version, Python


(Centrum Wiskunde & • February 1991 was a historic 0.9.0, was inspired by the
Informatica) at that time, date. Guido van Rossum ABC language van
published the source code of
van Rossum discussed the Python interpreter to Rossum was
the future of Python with alt.source, a Usenet group for implementing at CWI at
his manager. They open-source code. Thus began that time. Its main goal
the dynamic story of Python,
eventually agreed to and open sourcing helped
was to take the best from
publish it open source. Python succeed. ABC and fix the rest.
01.1994 Python 1.0
• Another milestone in the history of Python came with the release of its second
version in January 1994. A separate Usenet group was created for this purpose.
Additional functionalities were implemented with this version:

Functional programming tools (lambda, map, filter, and reduce).

Support for complex numbers.

Functions with keyword arguments.


10.2000 Python 2.0
• With this version, Python started to build toward its
future form as a reliable language with a comfortable
development experience. Important features
introduced in this version included the following:
• List comprehension.
• Cycle-detecting garbage collector.
• Support for Unicode.
• Unification of data types and classes.
12.2008 Python 3.0


10.2020 Python 3.9
The rapid growth of Python's
popularity began in 2010. Soon,
Python was competing with the most
popular languages like Java and
JavaScript.

Other features have been implemented


in this current version of Python:
• Dictionary merge and mpdate operators.
• New removeprefix() and removesuffix() string
methods.
• Built-in generic types.
FEATURES OF PYTHON
Easy to Learn
This is one of the most important reasons for the popularity of
Python. Python has a limited set of keywords. Its features such as
simple syntax, usage of indentation to avoid clutter of curly brackets and
dynamic typing that doesn't necessitate prior declaration of variable help
a beginner to learn Python quickly and easily.

Dynamically Typed
Python is a dynamically typed programming language. In Python, you
don't need to specify the variable time at the time of the variable declaration.
The types are specified at the runtime based on the assigned value due to its
dynamically typed feature.
Interpreter Based
Instructions in any programming languages must be translated into
machine code for the processor to execute them. Programming languages
are either compiler based, or interpreter based.

Interactive
Standard Python distribution comes with an interactive shell that works on
the principle of REPL (Read – Evaluate – Print – Loop). The shell presents a
Python prompt >>>. You can type any valid Python expression and press
Enter. Python interpreter immediately returns the response, and the prompt
comes back to read the next expression.
Multi-Paradigm
Python is a completely object-oriented language. Everything in a Python program
is an object. However, Python conveniently encapsulates its object orientation to
be used as an imperative or procedural language – such as C. Python also
provides certain functionality that resembles functional programming.

Standard Library
Even though it has a very few keywords (only Thirty-Five), Python software is
distributed with a standard library made of large number of modules and
packages. Thus, Python has out of box support for programming needs such as
serialization, data compression, internet data handling, and many more. Python
is known for its batteries included approach.
SOME OF THE PYTHON'S POPULAR MODULES ARE:



Tkinter
Tkinter is a Python binding to the Tk GUI toolkit. It is the
standard Python interface to the Tk GUI toolkit and is
Python's de facto standard GUI. Tkinter is included with
standard Linux, Microsoft Windows and macOS installs of
Python.
The name Tkinter comes from Tk interface. Tkinter was
written by Steen Lumholt and Guido van Rossum, then
later revised by Fredrik Lundh.
Math
The math module is a built-in module in Python that is used for
performing mathematical operations. This module provides
various built-in methods for performing different mathematical
tasks.
Open Source and Cross Platform
Python is a cross-platform language. Pre-compiled binaries are
available for use on various operating system platforms such
as Windows, Linux, Mac OS, Android OS. The reference
implementation of Python is called CPython and is written in C. You
can download the source code and compile it for your OS platform.

GUI Applications
Python's standard distribution has an excellent graphics library
called TKinter. It is a Python port for the vastly popular GUI toolkit
called TCL/Tk. You can build attractive user-friendly GUI applications
in Python. GUI toolkits are generally written in C/C++. Many of them
have been ported to Python. Examples
are PyQt, WxWidgets, PySimpleGUI etc.
Database Connectivity
Almost any type of database can be used as a backend with the Python
application. DB-API is a set of specifications for database driver software to let
Python communicate with a relational database. With many third-party libraries,
Python can also work with NoSQL databases such as MongoDB.

Extensible
The term extensibility implies the ability to add new features or modify existing
features. As stated earlier, CPython (which is Python's reference implementation)
is written in C. Hence one can easily write modules/libraries in C and incorporate
them in the standard library. There are other implementations of Python such as
Jython (written in Java) and IPython (written in C#). Hence, it is possible to write
and merge new functionality in these implementations with Java and C#
respectively.
Active Developer Community
As a result of Python's popularity and open-source nature, many Python
developers often interact with online forums and conferences. Python
Software Foundation also has a significant member base, involved in the
organization's mission to "Promote, Protect, and Advance the Python
Programming Language"

Python has another big list of good features:


• It supports functional and structured programming methods as well as OOP.
• It can be used as a scripting language or can be compiled to bytecode for
building large applications.
• It provides very high-level dynamic data types and supports dynamic type
checking.
• It supports automatic garbage collection.
• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
ADVANTAGES AND DISADVANTAGES OF PYTHON
ADVANTAGES OF PYTHON PROGRAMMING

Free and Open-Source


Python is a free and open-source programming language, and
one of the key benefits of Python programming is its accessibility
and versatility. It is considered one of the main advantages of the
Python programming language and that’s why it has become more
popular. The OSI-approved open-source license under which
Python is developed allows it to be used and distributed freely,
including for commercial purposes. It will lower your maintenance
costs.
Easy to Learn
Python is simple to learn, even for beginners. It is a high-level dynamic
programming language with English-like syntax. These factors contribute to
the developers’ ease of learning and adoption. When compared to Java and
C, Python can accomplish the same task with fewer lines of code. Python’s
principles allow for faster execution when compared to other languages
due to its ease of understanding.

Vast Libraries Support


Python includes a large library that the user can access. There are many
advantages of high-level language use for development. Python’s standard
library is large and contains almost every function imaginable. This is due to
a large and enthusiastic membership, as well as corporate support. Users
do not use external libraries when working with Python.
Greater Productivity
Python is a very efficient programming language. Python’s simplicity feature
allows developers to concentrate on resolving problems with the language.
Python users save time by learning the syntax and behavior of the programming
language rather than doing more work. It is one of the main advantages of Python
over other languages and that’s why many developers prefer to use Python for
development.
Interpreted Language
Python is an interpreted language, which means that its source code is
converted into bytecode before being executed by the Python virtual machine.
There are many advantages and disadvantages of machine language and Python
is considered one of the most appropriate programming languages for machine
learning. Python differs from major compiled languages such as C and C + + in
that Python code does not need to be built and linked in the same way that code
for these languages does.
Portability
Python is built to be portable. Its programmes are compatible with any
modern computer operating system. Python script is interpreted because of
its high-level nature, so it can be written for further interpretation on Linux,
Windows, Mac OS, and UNIX without requiring changes. Python programmes
also enable the creation of portable graphical user interfaces.

Dynamically Typed
Python is a dynamically typed programming language. It is unaware of the
variable’s type until the code is executed. As a result, declaring is pointless. It
saves the value in some memory location and then binds the variable name to
that memory container. And makes the container’s contents accessible via
that variable name. As a result, the data type is irrelevant. Because it will learn
the type of the value at run-time.
Disadvantages Of Python Programming
Poor Memory Efficiency
Python uses a significant amount of memory. It is one of the main
limitations of Python and that’s why some developers don’t use it. Python’s
adaptability to various types of data causes it to consume a lot of memory. If
the user wants to optimize memory usage, Python is not a good choice for
memory-intensive tasks.
Slow Speed
When it comes to speed, Python is slower than Java or C. Python is an
interpreted, dynamically typed language. Because Python is an interpreted
language, each line of code must be carefully organized and read before
execution. This takes even longer and results in a slow execution process.
Python’s dynamic structure slows things down even more because extra work
must be done while the code is executed. As a result, Python becomes a
secondary option when rapid acceleration is required.
Database Access
Python simplifies programming. However, when it interacts with the
database, it encounters several complications. When compared to well-
known technologies such as JDBC and ODBC, Python suffers from the
disadvantage of being underdeveloped and rudimentary when it comes
to interaction with the database and data access layer. As a result, Python
is less popular among large corporations that require easy interaction with
complex legacy data.

Weak in Mobile Computing


Python is an excellent server-side programming language because it is powerful
on both server and desktop platforms. Python, on the other hand, is relatively
delicate and unsuitable for mobile development. Because Python is memory
inefficient and requires a significant amount of processing power, it does not
have many built-in mobile applications. Carbonnelle is an example of a pre-
installed Python program.
Runtime Errors
Python users raised several issues with the language’s
design. Runtime error in Python is considered one of the main
disadvantages of Python programming languages. Because
Python is a dynamically typed language, the data type of any
variable can change at any time. As a result, it should be tested
more frequently, and errors in the language are visible during
runtime.
Why Python Is Better Than Other Languages?

 Python, as previously stated, is a programming language that is


dynamic, high-level, interpreted, and designed for general use.
Hope that you understand the advantages and disadvantages of
high-level language to use Python for programming.
 Python’s architectural structure prioritizes code readability and
makes extensive use of indentation. Unlike other programming
languages such as Java and C, Python requires fewer steps.
 The object-oriented approach and design of Python allow
programmers to write logical and clear code for both small and
large-scale applications.
General Editors and IDEs with
Python Support
Eclipse + PyDev
Available for Linux,
Windows, and macOS,
Eclipse is the de facto open-
source IDE
for Java development. It has
a rich marketplace of
extensions and add-ons,
which makes Eclipse useful
for a wide range of
development activities.
Sublime Text
Written by a Google
engineer with a dream for a
better text editor, Sublime
Text is an extremely popular
code editor. Supported on all
platforms, Sublime Text has
built-in support for Python
code editing and a rich set of
extensions (called packages)
that extend the syntax and
editing features.
GNU Emacs
Back before the iPhone vs
Android war, before the Linux vs
Windows war, even before the
PC vs Mac war, there was
the Editor War, with GNU
Emacs as one of the
combatants. Billed as “the
extensible, customizable, self-
documenting, real-time display
editor,” GNU Emacs has been
around almost as long as UNIX
and has a fervent following.
Vi / Vim
On the other side of the Text Editor
War stands Vi (also known as Vim).
Included by default on almost
every UNIX system and macOS, Vi
has an equally fervent following.
Vi and Vim are modal editors,
separating the viewing of a file from
the editing of a file. Vim includes
many improvements on the original
Vi, including an extensibility model
and in-place code building. Vim
Scripts are available for various
Python development tasks.
Visual Studio
Built by Microsoft and not to be confused with Visual Studio
Code, Visual Studio is a full-featured IDE, in many ways comparable
to Eclipse. Built for Windows and macOS only, VS comes in both
free (Community) and paid (Professional and Enterprise) versions.
Visual Studio enables development for a variety of platforms and
comes with its own marketplace for extensions.
Python-Specific Editors and IDEs

PyCharm
One of the best (and only) full-
featured, dedicated IDEs for
Python is PyCharm. Available in
both paid (Professional) and free
open-source (Community)
editions, PyCharm installs quickly
and easily on Windows, macOS,
and Linux platforms.
Spyder
Spyder is an open-source Python
IDE that’s optimized for data science
workflows. Spyder comes included
with the Anaconda package manager
distribution, so depending on your
setup, you may already have it
installed on your machine.
What’s interesting about Spyder is
that its target audience is data
scientists using Python. You’ll notice
this throughout. For example, Spyder
integrates well with common Python
data science libraries
like SciPy, NumPy, and Matplotlib.
Thonny
A more recent addition to
the Python IDE family, Thonny is
billed as an IDE for beginners.
Written and maintained by
the Institute of Computer
Science at the University of
Tartu in Estonia, Thonny is
available for all major
platforms, with installation
instructions on the site.
Python Standard and Implementations
 Python is an object-oriented programming language and has evolved a lot
over the past few years and has gained tremendous popularity among
developers, students and data enthusiasts.
 We can use any name to create and use all kinds of objects in python
project since it’s an object-oriented programming language.
 There arises a lot of effort in deciding meaningful class names, method
names, variables, etc. even if it differs from developers to developers.
 Naming conventions then become the rules of thumb for naming the
objects, class, variables or methods that we create.
General
• Avoid names that are too general or wordy. Maintain a good
balance between the two.
• Bad: user_list, dict_to_store_defns_of_a_word, swapNums,
moveInts.
• Good: user_info, word_definitions, swap_numbers,
move_integers.
• Avoid naming things like “X”, “Y” or “Z”. Capitalize all letters of
an abbreviation when using camelcase.
• Avoid names starting with digits. Combination of lowercase
letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an
underscore (_) can be used.
Name a Package in python
•It is important to use only lowercase while naming a package in
python.
•Use an underscore to join multiple words.
•However, it is preferable to keep a short name. (Stick into one-
word name)

Module name
•Use lowercase for module names.
•Use an underscore to join multiple words to generate a
meaningful module name.
•Stick to 1-word module names.
Class name in Python
• Class name in python should follow naming scheme called
CapWords convention.
• Python has its built-in classes, however, are typically
lowercase.
• Exception classes should always end in “Error”.

Global Variables
• Global variables should be in lowercase
• Use an underscore to join words in a global variable.
Good Instance variable name
• Instance variable names should be in lowercase.
• Use an underscore to join multiple words in an instance variable.
• Non-public instance variable name should begin with a single
underscore.
• Use two consecutive underscores at the beginning of an instance
variable.

Method name in Python


• Use only lowercase in method names.
• An underscore should separate words in a method name.
• Non-public method name should begin with a single underscore.
• Use two consecutive underscores at the beginning of a method name,
if it needs to be mangled.
Method Arguments
• Every instance method should have ‘self’ as its first argument.
• Class methods should have their first argument named ‘cls’.

Functions
• Function names should be in lower case.
• Use an underscore to join words in function name.

Constants in Python
• Only use upper case for constant names.
• Use an underscore as a separator to join multiple words to form a long
constant name.
How Python Programs Are Executed

• Python was designed for readability and


has some similarities to the English
language with influence from
mathematics.
• Python uses new lines to complete a
command, as opposed to other
programming languages which often use
semicolons or parentheses.
• Python relies on indentation, using
whitespace, to define scope, such as the
scope of loops, functions and classes.
Other programming languages often use
curly brackets for this purpose.
The execution of the Python program involves 2 Steps:
• Compilation
• Interpreter
Compilation
▪ The program is converted into byte code. Byte code is a fixed set of
instructions that represent arithmetic, comparison, memory
operations, etc.
▪ It can run on any operating system and hardware. The byte code
instructions are created in the .pyc file. The .pyc file is not explicitly
created as Python handles it internally but it can be viewed with the
following command:
 -m and py_compile represent module and module name
respectively.

 This module is responsible to generate .pyc file. The


compiler creates a directory named __pycache__ where it
stores the first.cpython-38.pyc file.
Interpreter
 The next step involves converting the byte code (.pyc file)
into machine code. This step is necessary as the computer can
understand only machine code (binary code). Python Virtual
Machine (PVM) first understands the operating system and
processor in the computer and then converts it into machine
code. Further, these machine code instructions are executed
by processor and the results are displayed.
Python Programming Terminology

You might also like