0% found this document useful (0 votes)
9 views

python notes

Python is a versatile, high-level programming language known for its simplicity and readability, making it ideal for beginners. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming, and is widely used in various fields such as data science, web development, and artificial intelligence. Python's extensive libraries and frameworks enhance its functionality, while its open-source nature encourages community contributions.

Uploaded by

ketandhadve95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

python notes

Python is a versatile, high-level programming language known for its simplicity and readability, making it ideal for beginners. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming, and is widely used in various fields such as data science, web development, and artificial intelligence. Python's extensive libraries and frameworks enhance its functionality, while its open-source nature encourages community contributions.

Uploaded by

ketandhadve95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

What is Python ?

Python is a general-purpose, dynamically typed, high-level, compiled and


interpreted, garbage-collected, and purely object-oriented programming
language that supports procedural, object-oriented, and functional
programming.

Features of Python:
Python provides many useful features which make it popular and valuable
from the other programming languages. It supports object-oriented
programming, procedural programming approaches and provides dynamic
memory allocation. We have listed below a few essential features.
1) Easy to Learn and Use
Python is easy to learn as compared to other programming languages. Its
syntax is straightforward and much the same as the English language.
There is no use of the semicolon or curly-bracket, the indentation defines
the code block. It is the recommended programming language for
beginners.
2) Expressive Language
Python can perform complex tasks using a few lines of code. A simple
example, the hello world program you simply type print("Hello World"). It
will take only one line to execute, while Java or C takes multiple lines.
3) Interpreted Language
Python is an interpreted language; it means the Python program is
executed one line at a time. The advantage of being interpreted language,
it makes debugging easy and portable.
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. It enables programmers to develop the software for several
competing platforms by writing a program only once.
5) Free and Open Source
Python is freely available for everyone. It is freely available on its official
website www.python.org. It has a large community across the world that is
dedicatedly working towards make new python modules and functions.
Anyone can contribute to the Python community. The open-source means,
"Anyone can download its source code without paying any penny."
6) Object-Oriented Language
Python supports object-oriented language and concepts of classes and
objects come into existence. It supports inheritance, polymorphism, and
encapsulation, etc. The object-oriented procedure helps to programmer to
write reusable code and develop applications in less code.
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. It converts the
program into byte code, and any platform can use that byte code.
8) Large Standard Library
It provides a vast range of libraries for the various fields such as machine
learning, web developer, and also for the scripting. There are various
machine learning libraries, such as Tensor flow, Pandas, Numpy, Keras,
and Pytorch, etc. Django, flask, pyramids are the popular framework for
Python web development.
9) GUI Programming Support
Graphical User Interface is used for the developing Desktop application.
PyQT5, Tkinter, Kivy are the libraries which are used for developing the
web application.
10) Integrated
It can be easily integrated with languages like C, C++, and JAVA, etc.
Python runs code line by line like C,C++ Java. It makes easy to debug the
code.
11. Embeddable
The code of the other programming language can use in the Python
source code. We can use Python source code in another programming
language as well. It can embed other language into our code.
12. Dynamic Memory Allocation
In Python, we don't need to specify the data-type of the variable. When
we assign some value to the variable, it automatically allocates the
memory to the variable at run time. Suppose we are assigned integer
value 15 to x, then we don't need to write int x = 15. Just write x = 15.

Python Basic Syntax


There is no use of curly braces or semicolons in Python programming
language. It is an English-like language. But Python uses indentation to
define a block of code. Indentation is nothing but adding whitespace
before the statement when it is needed.
For example -
1. def func():
2. statement 1
3. statement 2
4. …………………
5. …………………
6. statement N
In the above example, the statements that are the same level to the right
belong to the function. Generally, we can use four whitespaces to define
indentation.
Instead of Semicolon as used in other languages, Python ends its
statements with a NewLine character.
Python is a case-sensitive language, which means that uppercase and
lowercase letters are treated differently. For example, 'name' and 'Name'
are two different variables in Python.

Indentation and Comment in Python


Indentation is the most significant concept of the Python programming
language. Improper use of indentation will end up "IndentationError" in
our code.
Indentation is nothing but adding whitespaces before the statement when
it is needed. Without indentation Python doesn't know which statement to
be executed to next. Indentation also defines which statements belong to
which block. If there is no indentation or improper indentation, it will
display "IndentationError" and interrupt our code.

Python indentation defines the particular group of statements belongs to


the particular block. The programming languages such as C, C+
+, java use the curly braces {} to define code blocks.

History of Python
Python was created by Guido van Rossum. In the late 1980s, Guido
van Rossum, a Dutch programmer, began working on Python while at the
Centrum Wiskunde & Informatica (CWI) in the Netherlands. He wanted to
create a successor to the ABC programming language that would be
easy to read and efficient.
In February 1991, the first public version of Python, version 0.9.0,
was released. This marked the official birth of Python as an open-
source project. The language was named after the British comedy series
"Monty Python's Flying Circus".
Python development has gone through several stages. In January 1994,
Python 1.0 was released as a usable and stable programming
language. This version included many of the features that are still
present in Python today.
From the 1990s to the 2000s, Python gained popularity for its
simplicity, readability, and versatility. In October 2000, Python 2.0 was
released. Python 2.0 introduced list comprehensions, garbage collection,
and support for Unicode.
In December 2008, Python 3.0 was released. Python 3.0 introduced
several backward-incompatible changes to improve code readability and
maintainability.
Throughout 2010s, Python's popularity increased, particularly in fields
like data science, machine learning, and web development. Its rich
ecosystem of libraries and frameworks made it a favourite among
developers.
The Python Software Foundation (PSF) was established in 2001 to
promote, protect, and advance the Python programming language and its
community.

Where is Python used?


ython is a general-purpose, popular programming language, and it is used
in almost every technical field. The various areas of Python use are given
below.
o Data Science: Data Science is a vast field, and Python is an
important language for this field because of its simplicity, ease of
use, and availability of powerful data analysis and visualization
libraries like NumPy, Pandas, and Matplotlib.
o Desktop Applications: PyQt and Tkinter are useful libraries that
can be used in GUI - Graphical User Interface-based Desktop
Applications. There are better languages for this field, but it can be
used with other languages for making Applications.
o Console-based Applications: Python is also commonly used to
create command-line or console-based applications because of its
ease of use and support for advanced features such as input/output
redirection and piping.
o Mobile Applications: While Python is not commonly used for
creating mobile applications, it can still be combined with
frameworks like Kivy or BeeWare to create cross-platform mobile
applications.
o Software Development: Python is considered one of the best
software-making languages. Python is easily compatible with both
from Small Scale to Large Scale software.
o Artificial Intelligence: AI is an emerging Technology, and Python is
a perfect language for artificial intelligence and machine learning
because of the availability of powerful libraries such
as TensorFlow, Keras, and PyTorch.
o Web Applications: Python is commonly used in web development
on the backend with frameworks like Django and Flask and on the
front end with tools like JavaScript HTML and CSS.
o Enterprise Applications: Python can be used to develop large-
scale enterprise applications with features such as distributed
computing, networking, and parallel processing.
o 3D CAD Applications: Python can be used for 3D computer-aided
design (CAD) applications through libraries such as Blender.
o Machine Learning: Python is widely used for machine learning due
to its simplicity, ease of use, and availability of powerful machine
learning libraries.
o Computer Vision or Image Processing Applications: Python
can be used for computer vision and image processing applications
through powerful libraries such as OpenCV and Scikit-image.
o Speech Recognition: Python can be used for speech recognition
applications through libraries such
as SpeechRecognition and PyAudio.
o Scientific computing: Libraries like NumPy, SciPy,
and Pandas provide advanced numerical computing capabilities for
tasks like data analysis, machine learning, and more.
o Education: Python's easy-to-learn syntax and availability of many
resources make it an ideal language for teaching programming to
beginners.
o Testing: Python is used for writing automated tests, providing
frameworks like unit tests and pytest that help write test cases and
generate reports.
o Gaming: Python has libraries like Pygame, which provide a platform
for developing games using Python.
o IoT: Python is used in IoT for developing scripts and applications for
devices like Raspberry Pi, Arduino, and others.
o Networking: Python is used in networking for developing scripts
and applications for network automation, monitoring, and
management.
o DevOps: Python is widely used in DevOps for automation and
scripting of infrastructure management, configuration management,
and deployment processes.
o Finance: Python has libraries like Pandas, Scikit-learn,
and Statsmodels for financial modeling and analysis.
o Audio and Music: Python has libraries like Pyaudio, which is used
for audio processing, synthesis, and analysis, and Music21, which is
used for music analysis and generation.
o Writing scripts: Python is used for writing utility scripts to
automate tasks like file operations, web scraping, and data
processing.

Python Popular Frameworks and Libraries


Python has wide range of libraries and frameworks widely used in various
fields such as machine learning, artificial intelligence, web applications,
etc. We define some popular frameworks and libraries of Python as
follows.
o Web development (Server-
side) - Django Flask, Pyramid, CherryPy
o GUIs based applications - Tkinter, PyGTK, PyQt, PyJs, etc.
o Machine Learning - TensorFlow, PyTorch, Scikit-
learn, Matplotlib, Scipy, etc.
o Mathematics - NumPy, Pandas, etc.
o BeautifulSoup: a library for web scraping and parsing HTML and
XML
o Requests: a library for making HTTP requests
o SQLAlchemy: a library for working with SQL databases
o Kivy: a framework for building multi-touch applications
o Pygame: a library for game development
o Pytest: a testing framework for Python Django
o REST framework: a toolkit for building RESTful APIs
o FastAPI: a modern, fast web framework for building APIs
o Streamlit: a library for building interactive web apps for machine
learning and data science
o NLTK: a library for natural language processing.

Python print() Function


Python print() function is used to display output to the console or terminal.
It allows us to display text, variables and other data in a human readable
format.
Syntax:
print(object(s), sep=separator, end=end, file=file, flush=flush)
It takes one or more arguments separated by comma(,) and adds a
'newline' at the end by default.

Parameters:
o bject(s) - As many as you want data to display, will first converted
into string and printed to the console.
o sep - Separates the objects by a separator passed, default value = "
".
o end - Ends a line with a newline character
o file - a file object with write method, default value = sys.stdout

Example:
1. # Displaying a string
2. print("Hello, World!")
3.
4. # Displaying multiple values
5. name = "Aman"
6. age = 21
7. print("Name:", name, "Age:", age)
8.
9. # Printing variables and literals
10. x=5
11. y=7
12. print("x =", x, "y =", y, "Sum =", x + y)
13.
14. # Printing with formatting
15. percentage = 85.75
16. print("Score: {:.2f}%".format(percentage))
Output:
Hello, World!
Name: Aman Age: 21
X = 5 y = 7 Sum = 12
Score: 85.75%

In Python, statements that are the same level to the right belong to the
same block. We can use four whitespaces to define indentation. Let's see
the following lines of code.
Example -
1. list1 = [1, 2, 3, 4, 5]
2. for i in list1:
3. print(i)
4. if i==4:
5. break
6. print("End of for loop")
Output:
1
2
3
4
End of for loop

Comments in Python
Comments are essential for defining the code and help us and other to
understand the code. By looking the comment, we can easily understand
the intention of every line that we have written in code. We can also find
the error very easily, fix them, and use in other applications.
In Python, we can apply comments using the # hash character. The
Python interpreter entirely ignores the lines followed by a hash character.
A good programmer always uses the comments to make code under
stable. Let's see the following example of a comment.
name = "Thomas" # Assigning string value to the name variable

Types of Comment
Python provides the facility to write comments in two ways- single line
comment and multi-line comment.
Single-Line Comment - Single-Line comment starts with the hash #
character followed by text for further explanation.
# defining the marks of a student
Marks = 90

Multi-Line Comments - Python doesn't have explicit support for multi-


line comments but we can use hash # character to the multiple lines. For
example -
1. # we are defining for loop
2. # To iterate the given list.
3. # run this code.
We can also use another way.
"""
This is an example
Of multi-line comment
Using triple-quotes
"""

Python Variables
A variable is the name given to a memory location. A value-holding
Python variable is also known as an identifier.
Variable names must begin with a letter or an underscore, but they
can be a group of both letters and digits.
The name of the variable should be written in lowercase. Both Rahul
and rahul are distinct variables

Identifier Naming
Identifiers are things like variables. An Identifier is utilized to
recognize the literals utilized in the program. The standards to name
an identifier are given underneath.
o The variable's first character must be an underscore or alphabet (_).
o Every one of the characters with the exception of the main person
might be a letter set of lower-case(a-z), capitalized (A-Z), highlight,
or digit (0-9).
o White space and special characters (!, @, #, %, etc.) are not allowed
in the identifier name. ^, &, *).
o Identifier name should not be like any watchword characterized in
the language.
o Names of identifiers are case-sensitive; for instance, my name, and
MyName isn't something very similar.
o Examples of valid identifiers: a123, _n, n_9, etc.
o Examples of invalid identifiers: 1a, n%4, n 9, etc.

Declaring Variable and Assigning Values


o Python doesn't tie us to pronounce a variable prior to involving it in
the application. It permits us to make a variable at the necessary
time.
o In Python, we don't have to explicitly declare variables. The variable
is declared automatically whenever a value is added to it.
o The equal (=) operator is utilized to assign worth to a variable.

Assigning single value to multiple variables


Eg:
1. x=y=z=50
2. print(x)
3. print(y)
4. print(z)
Assigning multiple values to multiple variables:
Eg:
1. a,b,c=5,10,15
2. print a
3. print b
4. print c

Python Variable Types


There are two types of variables in Python - Local variable and Global
variable. Let's understand the following variables.

Local Variable
The variables that are declared within the function and have scope within
the function are known as local variables. Let's examine the following
illustration.
Example -
1. # Declaring a function
2. def add():
3. # Defining local variables. They has scope only within a function
4. a = 20
5. b = 30
6. c=a+b
7. print("The sum is:", c)
8.
9. # Calling a function
10. add()

Global Variables
Global variables can be utilized all through the program, and its
extension is in the whole program. Global variables can be used
inside or outside the function.
By default, a variable declared outside of the function serves as the
global variable. Python gives the worldwide catchphrase to utilize
worldwide variable inside the capability. The function treats it as a
local variable if we don't use the global keyword. Let's examine the
following illustration.
Example -
1. # Declare a variable and initialize it
2. x = 101
3.
4. # Global variable in function
5. def mainFunction():
6. # printing a global variable
7. global x
8. print(x)
9. # modifying a global variable
10. x = 'Welcome To Javatpoint'
11. print(x)
12.
13. mainFunction()
14. print(x)

You might also like