Basics of Python
Basics of Python
Python Programming
ing Problem Solving Approach
Reema Thareja
2
Features of Python
• Simple • Embeddable
• Easy to Learn • Extensive
• Versatile • Easy maintenance
• Free and Open Source • Secure
• High-level Language • Robust
• Interactive • Multi-threaded
• Portable • Garbage Collection
• Object Oriented
• Interpreted
• Dynamic
• Extensible 3
Limitations of Python
Parallel processing can be done in Python but not as elegantly as done in some other
languages (like JavaScript and Go Lang).
• Being an interpreted language, Python is slow as compared to C/C++. Python is not a
very good choice for those developing a high-graphic 3d game that takes up a lot of CPU.
• As compared to other languages, Python is evolving continuously and there is little
substantial documentation available for the language.
• As of now, there are few users of Python as compared to those using C, C++ or Java.
• It lacks true multiprocessor support.
• It has very limited commercial support point.
• Python is slower than C or C++ when it comes to computation heavy tasks and desktop
applications. 4
• It is difficult to pack up a big Python application into a single executable file. This
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS
RESERVED.
Applications of Python
• Embedded scripting language: Python is used as an embedded scripting language for
various testing/ building/ deployment/ monitoring frameworks, scientific apps, and
quick scripts.
• 3D Software: 3D software like Maya uses Python for automating small user tasks, or
for doing more complex integration such as talking to databases and asset
management systems.
• Web development: Python is an easily extensible language that provides good
integration with database and other web standards.
GUI-based desktop applications: Simple syntax, modular architecture, rich text
processing tools and the ability to work on multiple operating systems makes Python a
preferred choice for developing desktop-based applications.
• Image processing and graphic design applications: Python is used to make 2D
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS
RESERVED.
5
Applications of Python
• Scientific and computational applications: Features like high speed, productivity and
availability of tools, such as Scientific Python and Numeric Python, have made Python a
preferred language to perform computation and processing of scientific data. 3D
modeling software, such as FreeCAD, and finite element method software, like Abaqus,
are coded in Python.
Games: Python has various modules, libraries, and platforms that support development
of games. Games like Civilization-IV, Disney's Toontown Online, Vega Strike, etc. are
coded using Python.
• Enterprise and business applications: Simple and reliable syntax, modules and
libraries, extensibility, scalability together make Python a suitable coding language for
customizing larger applications. For example, Reddit which was originally written 6 in
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS
Common Lips, was rewritten in Python in 2005. A large part RESERVED.
of Youtube code is also
Writing and Executing First Python Program
8
Literal Constants
Strings
A string is a group of characters.
• Using Single Quotes ('): For example, a string can be written as 'HELLO'.
• Using Double Quotes ("): Strings in double quotes are exactly same as those in single
quotes. Therefore, 'HELLO' is same as "HELLO".
• Using Triple Quotes (''' '''): You can specify multi-line strings using triple quotes. You
can use as many single quotes and double quotes as you want in a string within triple
quotes.
Exampl
es:
Exampl
e:
10
Exampl
e:
11
Exampl
e:
13
14
15
Exampl
e:
16
17
18
19
21
22
Identity Operators
is Operator: Returns true if operands or values on both sides of the operator point to
the same object and false otherwise. For example, if a is b returns 1, if id(a) is same as
24
id(b). © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS
RESERVED.
is not Operator: Returns true if operands or values on both sides of the operator does
Expressions
An expression is any legal combination of symbols (like variables, constants and
operators) that represents a value. In Python, an expression must have at least one
operand (variable or constant) and can have one or more operators. On evaluating an
expression, we get a value. Operand is the value on which operator is applied.
Constant Expressions: One that involves only constants. Example: 8 + 9 – 2
Integral Expressions: One that produces an integer result after evaluating the
expression. Example:
a = 10
• Floating Point Expressions: One that produces floating point results. Example: a * b / 2
• Relational Expressions: One that returns either true or false value. Example: c = a>b
• Logical Expressions: One that combines two or more relational expressions and returns
25
a value as True or False. Example: a>b && y! = 0 © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS
RESERVED.
• Bitwise Expressions: One that manipulates data at bit level. Example: x = y&z
Operations on Strings
Exampl
es:
26
27
28
29
30
31
32