Unit 1
Unit 1
Unit 1
o Desktop Applications
Python Introduction o Console-based Applications
Python is a general purpose, dynamic, high level, and o Mobile Applications
interpreted programming language. It supports Object o Software Development
Oriented programming approach to develop applications. o Artificial Intelligence
It is simple and easy to learn and provides lots of high- o Web Applications
level data structures. o Enterprise Applications
o 3D CAD Applications
Python's syntax and dynamic typing with its interpreted o Machine Learning
nature make it an ideal language for scripting and rapid o Computer Vision or Image Processing
application development. Applications.
o Speech Recognitions
Python supports multiple programming pattern,
including object-oriented, imperative, and functional or Programming cycle for python
procedural programming styles.
Python's development cycle is dramatically shorter than
Python is not intended to work in a particular area, such that of traditional tools. In Python, there are no compile
as web programming. That is why it is known or link steps -- Python programs simply import modules
as multipurpose programming language because it can be at runtime and use the objects they contain.
used with web, enterprise, 3D CAD, etc.
time. This makes debugging easy and thus suitable for Python is used to build Business applications like ERP
beginners. and e-commerce systems. Tryton is a high level
4) Cross-platform Language- Python can run equally on application platform.
different platforms such as Windows, Linux, Unix and 6) Console Based Application
Macintosh etc. So, we can say that Python is a portable We can use Python to develop console based applications.
language. For example: IPython.
5) Free and Open Source- Python language is freely 7) Audio or Video based Applications
available at official web address. The source-code is also Python is awesome to perform multiple tasks and can be
available. Therefore it is open source. used to develop multimedia applications. Some of real
6) Object-Oriented Language- Python supports object applications are: Tim Player, cplay etc.
oriented language and concepts of classes and objects 8) 3D CAD Applications
come into existence. To create CAD application Fandango is a real application
7) Extensible- It implies that other languages such as which provides full features of CAD.
C/C++ can be used to compile the code and thus it can be 9) Enterprise Applications
used further in our python code. Python can be used to create applications which can be
8) Large Standard Library- Python has a large and broad used within an Enterprise or an Organization. Some real
library and prvides rich set of module and functions for time applications are: OpenErp, Tryton, Picalo etc.
rapid application development. 10) Applications for Images
9) GUI Programming Support- Graphical user Using Python several applications can be developed for
interfaces can be developed using Python. image. Applications developed are: VPython, Gogh,
10) Integrated- It can be easily integrated with languages imgSeek etc.
like C, C++, JAVA etc.
Python Applications Installation on Windows
Visit the link https://www.python.org/downloads/ to download the
1) Web Applications latest release of Python.
We can use Python to develop web applications. It
provides libraries to handle internet protocols such as Step - 1: Select the Python's version to download.
HTML and XML, JSON, Email processing, request,
beautiful Soup, Feed parser etc. It also provides
Frameworks such as Django, Pyramid, Flask etc to design
and develop web based applications. Some important
developments are: Python Wiki Engines, Pocoo, Python
Blog Software etc.
2) Desktop GUI Applications
Python provides Tk GUI library to develop user interface
in python based application. Some other useful toolkits
wxWidgets, Kivy, pyqt that are useable on several
platforms. The Kivy is popular for writing multi touch
applications.
3) Software Development
Python is helpful for software development process. It
works as a support language and can be used for build
Step - 2: Click on the Install Now
control and management, testing etc.
4) Scientific and Numeric Double-click the executable file, which is downloaded; the
Python is popular and widely used in scientific and following window will open. Select Customize installation and
numeric computing. Some useful library and package are proceed. Click on the Add Path check box, it will set the Python
SciPy, Pandas, IPython etc. SciPy is group of packages of path automatically.
engineering, science and mathematics.
5) Business Applications
Jupyter Notebook.
Eclipse + PyDev + LiClipse.
GNU Emacs.
PayCharm is a cross-platform IDE used for Python
programming. It is one of the best Python IDE editor that
can be used on Windows, macOS, and Linux. This
software contains API that can be used by the developers
to write their own Python plugins so that they can extend
the basic functionalities.
^ (binary xor) The resulting bit will be 1 if both the Identity Operators
bits are different; otherwise, the The identity operators are used to decide whether an element
resulting bit will be 0. certain class or type.
~ (negation) It calculates the negation of each bit Operator Description
of the operand, i.e., if the bit is 0, the
resulting bit will be 1 and vice versa.
is It is evaluated to be true if the
<< (left shift) The left operand value is moved left reference
by the number of bits present in the present at both sides point to the
right operand. same object.
>> (right shift) The left operand is moved right by is not It is evaluated to be true if the
the number of bits present in the reference
right operand. present at both sides do not point
to the same object.
Logical Operators
The logical operators are used primarily in the expression Operator Precedence
evaluation to make a decision. Python supports the following The precedence of the operators is essential to find out since it
logical operators. enables us to know which operator should be evaluated first. The
precedence table of the operators in Python is given below.
Operat Description
or Operator Description
and If both the expression are true, then the ** The exponent operator is given priority
condition will be true. If a and b are the over all the others used in the
two expressions, a → true, b → true => a expression.
and b → true.
~+- The negation, unary plus, and minus.
or If one of the expressions is true, then the
condition will be true. If a and b are the * / % // The multiplication, divide, modules,
two expressions, a → true, b → false => a reminder, and floor division.
or b → true.
+- Binary plus, and minus
not If an expression a is true, then not (a)
will be false and vice versa. >> << Left shift. and right shift
in not in Membership operators the in-built functions for type conversion, along with their
descriptions.
not or and Logical operators
Function Description
The process of converting a Python data type into another float(y) It converts y to a floating-point number.
data type is known as type conversion. There are mainly
complex(real It creates a complex number.
two types of type conversion methods in Python, namely,
[imag])
implicit type conversion and explicit type conversion.
In this module, we will go through the following topics: str(y) It converts y to a string.
So, without any further delay, let’s get started. dict(y) It creates a dictionary and y should be a
sequence of (key, value) tuples.
Implicit Type Conversion in Python
ord(y) It converts a character into an integer.
In Python, when the data type conversion takes place
during compilation or during the run time, then it’s hex(y) It converts an integer to a hexadecimal string.
called an implicit data type conversion. Python handles
oct(y) It converts an integer to an octal string
the implicit data type conversion, so we don’t have to
explicitly convert the data type into another data type. We Now that we know the in-built functions provided by
have seen various examples of this kind of data type Python that are used for explicit type conversion, let’s see
conversion throughout the tutorial. the syntax for explicit type conversion:
Python Interpreter is able to convert the data type of the (required_data type)(expression)
resultant variable or not.
Explicit type conversion in Python.
Ex. a = 5
# adding string and integer data types using explicit type
b = 5.5 conversion
sum = a + b a = 100
print (sum) b = “200”
print (type (sum)) #type() is used to display the data type result1 = a + b
of a variable
b = int(b)
Output: result2 = a + b
10.5
print(result2)
<class ‘float’>
Output:
Explicit Type Conversion in Python Traceback (most recent call last):
Explicit type conversion is also known as type casting. File “”, line 1, in
Explicit type conversion takes place when the TypeError: unsupported operand type(s) for +: ‘int’ and
programmer clearly and explicitly defines the same in the ‘str’
program. For explicit type conversion, there are some in-
300
built Python functions. Following table contains some of
Output:
Python Data Types
The type of a <class 'int'>
A variable can hold different types of values. For example, a The type of b <class 'float'>
person's name must be stored as a string whereas its id must be The type of c <class 'complex'>
stored as an integer. c is complex number: True
Python provides various standard data types that define the Python supports three types of numeric data.
storage method on each of them. The data types defined in
Python are given below. 1. Int - Integer value can be any length such as integers
10, 2, 29, -20, -150 etc. Its value belongs to int
1. Numbers 2. Float - Float is used to store floating-point numbers
2. Sequence Type like 1.9, 9.902, 15.2, etc.
3. Boolean 3. Complex - A complex number contains an ordered
4. Set pair, i.e., x + iy where x and y denote the real and
imaginary parts, respectively. The complex numbers
5. Dictionary like 2.14j, 2.0 + 2.3j, etc.
Example - 1
1. str = "string using double quotes"
2. print(str)
3. s = '''''A multiline
4. string'''
5. print(s)
Output:
string using double quotes
A multiline
string
Consider the following example of string handling.
Example - 2
1. str1 = 'hello javatpoint' #string str1
1. a=5
2. str2 = ' how are you' #string str2
2. print("The type of a", type(a))
3. print (str1[0:2]) #printing first two character using slice operat
3. b = 40.5
or
4. print("The type of b", type(b))
4. print (str1[4]) #printing 4th character of the string
5. c = 1+3j
5. print (str1*2) #printing the string twice
6. print("The type of c", type(c))
6. print (str1 + str2) #printing the concatenation of str1 and str2
7. print(" c is a complex number", isinstance(1+3j,complex))
Check equality ==
For example, the following code will get all the number within 10
and put them in a list.
>>> [x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Dictionary comprehension
This is the same as list comprehension but will use curly braces:
{ k, v for k in iterable }
For example, the following code will get all the numbers within 5
as the keys and will keep the corresponding squares of those
numbers as the values.
>>> {x:x**2 for x in range(5)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Generator expression
The syntax for generator expression is shown below:
( compute(var) for var in iterable )
Conditional Expressions
You can use the following construct for one-liner conditions:
true_value if Condition else false_value
Example:
>>> x = "1" if True else "2"
>>> x
'1'