Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

UNIT-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

BCA-III

Introduction to Python Programming Language

UNIT-1
History and Origin of Python Language
• Python is a widely used general-purpose, high-level programming language. It was
initially designed by Guido van Rossum in 1991 and developed by Python
Software Foundation. It was mainly developed for emphasis on code readability,
and its syntax allows programmers to express concepts in fewer lines of code.
• Who invented Python?
• In the late 1980s, history was about to be written. It was that time when working on
Python started. Soon after that, Guido Van Rossum began doing its application-
based work in December of 1989 at Centrum Wiskunde & Informatica (CWI)
which is situated in the Netherlands. It was started as a hobby project because he
was looking for an interesting project to keep him occupied during Christmas.
• The programming language in which Python is said to have succeeded is ABC
Programming Language, which had interfacing with the Amoeba Operating
System and had the feature of exception handling. He had already helped create
ABC earlier in his career and had seen some issues with ABC but liked most of the
features. After that what he did was very clever. He had taken the syntax of ABC,
and some of its good features. It came with a lot of complaints too, so he fixed
those issues completely and created a good scripting language that had removed all
the flaws.
Features
• Easy to code and maintain
Many people call it developer-friendly because it is quite easy to learn. Furthermore, we can learn to write its
code very easily in a few hours or days. Moreover, this is possible because it has only a few keywords, a simple
structure, and a clearly defined syntax.
• Open source and free
It is an open-source language which means that anyone can download it, use it, and share it. Moreover, it is free
of cost.
• Object-oriented
It supports object-oriented programming language features. For example, the concept of object and classes,
encapsulation, inheritance, etc.
• Supports GUI programming
Python has support for creating various GUI applications. Furthermore, these applications can work in many
system software and libraries. Besides, it has modules like PyQt5, PyQt4, wxPython, or Tk through which we
can make graphical user interfaces. Among them, PyQt5 is the most popular for making graphical apps.
• Extensible in nature
It is extensible in nature which means that we can use python code in other languages. For example C, C++ also
can compile that code in C or C++.
• Portable and Platform independent
It is portable in nature. Moreover, we can run the python code in any environment like windows, Linux, mac, etc
without changing it. Besides, if we write the code in one OS and run it on the other it is totally valid.
• Integrated Language
We can easily integrate it with other languages such as C, C++, etc. Hence, it is an integrated language.
• Huge Standard Library
There are a very large number of libraries in python. These libraries contain predefined modules
and functions for certain tasks. Hence, it becomes easy fr the programmer to develop the code
since he does not have to do all the things by himself. Moreover, the library is portable and cross-
platform compatible.
• Interpreted Language
It uses an interpreter for converting the source code into machine code. This means that we
execute the python code line by line. Hence, it becomes easy to debug the error and solve it.
• Dynamically Typed
This means that we do not have to define the type of the variable. The interpreter decides it itself
at the run time. Hence, the name dynamically typed.
• Scalable
It provides good support for large programs.
• Some additional features besides these are as follows:
It supports both object-oriented and procedure-oriented programming structure.
We can use it as a scripting language for large applications.
It contains automatic garbage collection.
It performs dynamic type checking.
Case sensitive in nature.
Useful for developing web applications.
it uses indentation for program structuring. Unlike other languages that use braces for the
distinction of blocks.
Limitations
• Performance: Python is generally slower than compiled languages like C or C++ due to its
interpreted nature.
• Mobile Development: Python is not commonly used for mobile app development, which is
dominated by Swift (iOS) and Kotlin (Android).
• Memory Consumption: Python can consume more memory than other languages, making it
less suitable for memory-constrained environments.
• Global Interpreter Lock (GIL): The GIL allows only one thread to execute at a time in
CPython, which can limit performance in CPU-bound multithreaded applications.
• Dynamic Typing: While flexible, dynamic typing can lead to runtime errors that are hard to
debug and may affect performance.
• Limited Support for GUI Development: Python’s GUI frameworks (like Tkinter and PyQt)
are less robust compared to dedicated frameworks in other languages.
• Fewer Libraries for Certain Domains: While Python has extensive libraries, some
specialized domains (like game development) have less support compared to languages like
C# or C++.
• Not Ideal for Low-Level Programming: Python is not suitable for low-level programming
tasks, such as writing operating systems or device drivers.
• Weak in Mobile Computing: Less support and fewer libraries compared to languages
tailored for mobile development.
• Deployment Challenges: Packaging and distributing Python applications can be more
complex compared to statically linked languages.
Major Applications of Python,
• 1. Web Development
It is one of the most astonishing applications of Python. This is because Python comes up with a
wide range of frameworks like Django, Flask, Bottle, and a lot more that provide ease to
developers. Furthermore, Python has inbuilt libraries and tools which make the web development
process completely effortless. The use of Python for web development also offers:
• Amazing visualization
• Convenience in development
• Enhanced security
• Fast development process
• 2. Machine Learning and Artificial Intelligence
Machine Learning and Artificial Intelligence are the hottest subjects right now. Python along with
its inbuilt libraries and tools facilitate the development of AI and ML algorithms. Further, it offers
simple, concise, and readable code which makes it easier for developers to write complex
algorithms and provide a versatile flow. Some of the inbuilt libraries and tools that enhance AI and
ML processes are:
• Numpy for complex data analysis
• Keras for Machine learning
• SciPy for technical computing
• Seaborn for data visualization
• 3. Data Science
Data science involves data collection, data sorting, data analysis, and data visualization.
Python provides amazing functionality to tackle statistics and complex mathematical
calculations. The presence of in-built libraries provides convenience to data science
professionals. Some of the popular libraries that provide ease in the data science process are
TensorFlow, Pandas, and Socket Learning. These libraries provide an ecosystem for fine-
tuning data models, data preprocessing, and performing complex data analysis.
• 4. Game Development
With the rapidly growing gaming industry, Python has proved to be an exceptional option for
game development. Popular games like Pirates of the Caribbean, Bridge Commander, and
Battlefield 2 use Python programming for a wide range of functionalities and add-ons. The
presence of popular 2D and 3D gaming libraries like pygame, panda3D, and Cocos2D makes
the game development process completely effortless.
• 5. CAD Applications
CAD refers to computer-aided design; it is the process of creating 3D and 2D models
digitally. This application has replaced manual drift and is used by architects, product
designers, and construction managers to design products with extremely high
consistency. Python is embedded with amazing applications like Blender, FreeCAD,
open cascade, and a lot more to efficiently design products. These provide enhanced
features like technical drawing, dynamic system development, recordings, file export,
and import.
• 6. Desktop GUI
Python is an interactive programming language that helps developers to create
GUIs easily and efficiently. It has a huge list of inbuilt tools for Python usage
are PyQT, kivy, wxWidgets, and many other libraries like them to build a fully
functional GUI in an extremely secure and efficient manner.
• 7. Web Scraping Application
Web scraping is an automated process used to extract information from
websites in an easier and faster way. The information is used by researchers,
organizations, and analysts for a wide variety of tasks. Python has a wide
range of features that make it suitable for web scraping some of them are:
• A concise syntax enhances readability and saves you time.
• A wide range of libraries and tools like pandas, matplotlib, and Selenium
makes the web scraping process easy and efficient.
• Easy to use and understand
How to Install Python: A Step-by-Step Guide

• Step 1: Check Your System


• Before you begin, check if Python is already installed
on your computer. Open your command prompt
(Windows) or terminal (macOS and Linux) and type the
following command:


• python --version
• If Python is installed, it will display the version number
(e.g., Python 3.9.1). If it's not installed, move on to the
next steps.
• Step 2: Download Python
• Visit the official Python website at python.org. You'll find the latest Python
version available for download.
• Step 3: Choose the Right Version
• Python has two major versions: Python 2 and Python 3. Python 2 is no
longer supported, so it's recommended to download Python 3. Choose the
latest version of Python 3, which is displayed prominently on the website.
• Step 4: Download the Installer
• Click on the download link to get the Python installer for your operating
system (Windows, macOS, or Linux). For Windows, you'll see two options:
32-bit and 64-bit. Choose the one that matches your system.
• Step 5: Run the Installer
• After downloading, locate the installer file and run it. Follow the
installation wizard's instructions. Be sure to check the box that says "Add
Python X.X to PATH" during installation (replace "X.X" with the version
number you downloaded). This is essential for easy command-line access.
• Step 6: Verify the Installation
• Once the installation is complete, open your
command prompt or terminal again and run:

• python --version
• You should see the installed Python version.
• Step 7: Install a Code Editor (Optional)
• While Python includes the IDLE development environment, many
developers prefer using code editors like Visual Studio Code, PyCharm, or
Jupyter Notebook for a more robust coding experience. Download and
install a code editor of your choice.
• Step 8: Write Your First Python Code
• Open your code editor and create a new Python file with the ".py"
extension. For example, "hello.py.“

• print("Hello, World!")
• Save the file and run it from your code editor. You'll see "Hello, World!"
printed to the console.
Setting Up Path and Environment Variables
Windows

• Windows
• Add Python to PATH Manually (if not done during installation):
– Right-click on This PC or My Computer and select Properties.
– Click on Advanced system settings.
– Click on Environment Variables.
– In the System variables section, find and select the Path variable, then
click Edit.
– Click New and add the path to your Python installation (e.g.,
C:\Python39, C:\Python39\Scripts).
– Click OK to close all dialog boxes.
• Running Python
• Open a Command Line Interface:
– Windows: Open Command Prompt or PowerShell.
– macOS/Linux: Open Terminal.
• Run Python:
– To start the Python interpreter, type:
– You should see a prompt indicating you are in the Python interactive
shell.
• Run a Python Script:
– Create a Python file (e.g., script.py) using a text editor:

– Run the script by navigating to the directory where the file is


saved and typing:
First Python Program
• a= int(input("Enter First Number : "));
• b= int(input("Enter Second Number : "));
• c=a/b;
• print("Div=",c);
Python provides us the two ways to run a program:
• Using Interactive interpreter prompt
• Using a script file

Interactive interpreter prompt


• Python provides us the feature to execute the Python statement
one by one at the interactive prompt. It is preferable in the case
where we are concerned about the output of each line of our
Python program.
• To open the interactive mode, open the terminal (or command
prompt) and type python .It will open the command prompt where
we can execute the Python statement and check their impact on
the console.
Using a script file
• Interpreter prompt is good to run the individual
statements of the code. However, we cannot
write the code every-time on the terminal.
• We need to write our code into a file which can
be executed later. For this purpose, open an
editor like notepad, create a file named first.py
(Python used .py extension) and write the code
in it.
Statements and Syntax
Comments ( # )
• Python comment statements begin with the pound sign or hash
symbol (#).
• A comment can begin anywhere on a line. All characters following
the # to the end of the line are ignored by the interpreter
Continuation ( \ )
• Python statements are, in general, delimited by NEWLINEs,
meaning one statement per line.
• Single statements can be broken up into multiple lines by use of the
backslash.
• if (a == 1) and \
(b == 0):
Print(“msg”)
There are two exceptions where lines can be continued without backslashes.
• A single statement can take up more than one line when enclosing
operators are used, i.e., parentheses, square brackets, or braces, and when
NEWLINEs are contained in strings enclosed in triple quotes.
• # display a string with triple quotes
print '''hi there, this is a long message for you
that goes over multiple lines... you will find
out soon that triple quotes in Python allows
this kind of fun! it is like a day on the beach!'''
• # set some variables
go_surf, get_a_tan_while, boat_size, toll_money = (1,
'windsurfing', 40.0, -2.00)
Multiple Statement Groups as Suites ( : )
• Groups of individual statements making up a
single code block are called “suites” in Python.
• Compound or complex statements, such as if,
while, def, and class, are those that require a
header line and a suite.
• Header lines begin the statement (with the
keyword) and terminate with a colon ( : ) and are
followed by one or more lines that make up the
suite.
Suites Delimited via Indentation
• Python employs indentation as a means of delimiting blocks of
code. Code at inner levels are indented via spaces or tabs.
• Indentation requires exact indentation; in other words, all the lines
of code in a suite must be indented at the exact same level (e.g.,
same number of spaces).
• if (a == 1) and (b == 0):
c= a+b
Print(“msg”)

Multiple Statements on a Single Line ( ; )


• The semicolon ( ; ) allows multiple statements on a single line given
that neither statement starts a new code block
• import sys; x = 5; sys.stdout.write(x + '\n')
Python differences from other
languages
Syntax and Readability
• Python: Emphasizes readability and simplicity, using indentation to define code
blocks.
• Others (e.g., C, Java): Use braces {} for code blocks, often leading to more complex
syntax.
Dynamic vs. Static Typing
• Python: Dynamically typed; variable types are determined at runtime.
• Others (e.g., Java, C++): Statically typed; variable types must be declared.
Memory Management
• Python: Automatic memory management through garbage collection.
• Others (e.g., C): Requires manual memory management, leading to more
complexity and potential errors.
Multi-Paradigm Support
• Python: Supports multiple programming paradigms (procedural, object-oriented,
functional).
• Others: Some languages are more rigid in their paradigm (e.g., Java is primarily
object-oriented).
Standard Library and Ecosystem
• Python: Comes with a vast standard library and a rich ecosystem of third-party packages
(e.g., NumPy, Pandas).
• Others: May have smaller standard libraries or rely more heavily on external libraries.
Community and Support
• Python: Has a large and active community, providing extensive resources and support.
• Others: Community size varies; some languages have less active support.
Use Cases
• Python: Widely used in data science, web development, automation, and AI.
• Others: Languages like C/C++ are often used for system programming, while Java is popular
in enterprise applications.
Error Handling
• Python: Uses exceptions for error handling, which can make debugging easier.
• Others: Error handling mechanisms vary (e.g., C uses return codes).
Development Speed
• Python: Generally allows for faster development due to its simplicity and rich libraries.
• Others: Languages like Java or C may require more boilerplate code, slowing development.
Performance
• Python: Slower than compiled languages (like C or Go) due to its interpreted nature.
• Others: Compiled languages typically offer better performance.
Keywords
• Every language contains words and a set of rules that would
make a sentence meaningful. Similarly, in Python
programming language, there are a set of predefined
words, called Keywords which along with Identifiers will
form meaningful sentences when used
together. Python keywords cannot be used as the names
of variables, functions, and classes.
• In this article, we will learn about Python keywords and
how to use them to perform some tasks.
• Python Keywords
• Keywords in Python are reserved words that can not be
used as a variable name, function name, or any other
identifier.
List of 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
async elif if or yield
https://www.javatpoint.com/python-keywords

You might also like