python notes
python notes
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.
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.
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
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.
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)