1695214219-4.2 Essentials of Python Programming
1695214219-4.2 Essentials of Python Programming
Disclaimer
The content is curated from online/offline resources and used for educational purpose only
Essentials of Python Programming Language
Learning Objectives
You will learn in this lesson:
• Python Programming
• How to write first program
• Perform basic operations
• How to read and write files in Python
• Database connectivity
Essentials of Python Programming Language
Source: https://jobaxle.com/blog_detail/career-opportunity-in-python/24
Essentials of Python Programming Language
Python
• Python uses an interpreter. Not only can we write
complete programs, but we can also work with the
interpreter in a statement-by-statement mode
enabling us to experiment quite easily.
• Python is especially good for our purposes in that it
does not have a lot of “overhead” before getting
started.
• It is easy to jump in and experiment with Python in
an interactive fashion.
Python Interfaces
Print Statement
For output we use statements of the form
print <expression>
Semantics
Value of expression is computed
This value is displayed
Example of Python
Hello World
print ('hello world')
Prints hello world to standard out
Open IDLE and try it out yourself
Follow along using IDLE
Essentials of Python Programming Language
• float
This type is for numbers with possible fraction parts. Examples: 23.0, -14.561
Essentials of Python Programming Language
• Strings, as objects, have methods that return the result of a function on the string
Essentials of Python Programming Language
Lists
• Think of a list as a stack of cards, on which your
information is written
Lists Methods
• Adding to the List
var[n] = object #replaces nth value with object
var.append(object) #adds object to the end of the list
Tuples
Like a list, tuples are iterable arrays of objects
Tuples are immutable -
once created, unchangeable
Source: https://blog.finxter.com/the-ultimate-guide-to-python-tuples/
Essentials of Python Programming Language
Dictionaries
Dictionaries are sets of key & value pairs
Conditional Branching
if and else
if variable == condition:
#do something based on v == c
else:
#do something based on v != c
if condition:
…............
elif another condition:
….............
else: #none of the above
….............
Essentials of Python Programming Language
Example:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
Results:
• cat 3
• window 6
• defenestrate 12
Essentials of Python Programming Language
Source: https://svitla.com/blog/design-object-oriented-code-python
Essentials of Python Programming Language
Class
The class can be defined as a collection of objects. It is a logical entity that has some specific attributes
and methods. For example: if you have an employee class, then it should contain an attribute and
method, i.e. an email id, name, age, salary, etc.
Essentials of Python Programming Language
Object
• The object is an entity that has state and behavior.
It may be any real-world object like the mouse,
keyboard, chair, table, pen, etc.
• Everything in Python is an object, and almost
everything has attributes and methods. All functions
have a built-in attribute __doc__, which returns the
docstring defined in the function source code.
Essentials of Python Programming Language
Method
The method is a function that is associated with an object. In Python, a method is not unique to class
instances. Any object type can have methods.
Essentials of Python Programming Language
Python Inheritance
• Inheritance allows us to define a class that inherits
all the methods and properties from another class.
• Parent class is the class being inherited from, also
called base class.
• Child class is the class that inherits from another
class, also called derived class.
Source: https://www.scaler.com/topics/python/inheritance-in-python/
Essentials of Python Programming Language
Python Polymorphism
The word "polymorphism" means "many forms", and in programming it refers to
methods/functions/operators with the same name that can be executed on many objects or classes.
Source: https://www.scaler.com/topics/python/polymorphism-in-python/
Essentials of Python Programming Language
Encapsulation in Python
Encapsulation is a mechanism of wrapping the data
(variables) and code acting on the data (methods)
together as a single unit. In encapsulation, the
variables of a class will be hidden from other classes,
and can be accessed only through the methods of
their current class.
Source: https://www.boardinfinity.com/blog/understanding-encapsulation-in-python/
Essentials of Python Programming Language
• Above all interfaces or modules adhere to Python Database API Specification v2.0 (PEP 249) i.e.,
the syntax, method, and way of accessing the database are the same in all.
Essentials of Python Programming Language
Procedure
Source: https://yuiltripathee.medium.com/connect-your-database-in-python-code-works-almost-everywhere-21637b311bb4
Essentials of Python Programming Language
Output -
Essentials of Python Programming Language
Output -
Essentials of Python Programming Language
Output -
Essentials of Python Programming Language
Output -
Essentials of Python Programming Language
Output -
Essentials of Python Programming Language
Output -
Essentials of Python Programming Language
Output -
• Django
• Flask
Summary
• Python is a high-level, versatile programming language known for its readability and simplicity.
• Python supports both procedural and object-oriented programming paradigms and has a rich
ecosystem of libraries.
• Python is widely used for web development, data analysis, artificial intelligence, and automation due to
its extensive standard library and active community support.
Essentials of Python Programming Language
Quiz
1. What is Python primarily known for?
a) Speed optimization
b) Complex syntax
c) Readable and clean syntax
d) Low-level memory management
Answer: c
Readable and clean syntax
Essentials of Python Programming Language
Quiz
2. Which of the following is used to define a block of
code in Python?
a) Parentheses ()
b) Brackets []
c) Indentation
d) Semicolons ;
Answer: c
Indentation
Essentials of Python Programming Language
Quiz
3. What does the if statement do in Python?
Answer: c
Executes a block of code conditionally
Essentials of Python Programming Language
Quiz
4. Which Python data type is ordered, mutable, and
allows duplicate elements?
a) Set
b) List
c) Tuple
d) Dictionary
Answer: b
List
Essentials of Python Programming Language
References
• https://docs.python.org/3/library/
• https://www.tutorialspoint.com/numpy
• https://towardsdatascience.com/
• https://pynative.com/
• https://medium.com/javarevisited/top-5-python-frameworks-for-web-development-e034ebe85574
• https://www.projectpro.io/article/machine-learning-frameworks/509
• https://www.javatpoint.com/python-oops-concepts
• https://www.shiksha.com/online-courses/articles/abstraction-in-python/
• https://www.boardinfinity.com/blog/understanding-encapsulation-in-python/
Essentials of Python Programming Language
Thank you!