Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

1695214219-4.2 Essentials of Python Programming

Uploaded by

umasaipriya462
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

1695214219-4.2 Essentials of Python Programming

Uploaded by

umasaipriya462
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Essentials of Python Programming Language

Essentials of Python Programming Language


Essentials of Python Programming Language

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

Why do we need to learn Python?

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.

Source: OIP.AQ5rAzGvicbIAudCjnmlyQHaHa (266×266) (bing.com)


Essentials of Python Programming Language

Python Interfaces

• IDLE – a cross-platform Python development


environment

• Anaconda is a Python distribution focused on data driven


projects.

• PythonWin – a Windows only interface to Python

• Python Shell – running 'python' from the Command Line


opens this interactive shell

Source: OIP.AQ5rAzGvicbIAudCjnmlyQHaHa (266×266) (bing.com)


Essentials of Python Programming Language

IDLE - Development Environment


IDLE helps you program in Python by:
• color-coding your program code
• debugging
• auto-indent
• interactive shell
Essentials of Python Programming Language

Print Statement
For output we use statements of the form

print <expression>

Semantics
Value of expression is computed
This value is displayed

Several expressions can be printed – separate them by commas


Essentials of Python Programming Language

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

Numeric Data Types


• int
This type is for whole numbers, positive or negative. Examples: 23, -1756

• float
This type is for numbers with possible fraction parts. Examples: 23.0, -14.561
Essentials of Python Programming Language

More than just printing


• Python is an object oriented language

• Practically everything can be treated as an object


“hello world” is a string

• Strings, as objects, have methods that return the result of a function on the string
Essentials of Python Programming Language

Lab 33: Installation of Python and Hello World Program in Python


Essentials of Python Programming Language

Data Structures in Python


Lists (mutable sets of strings)
var = [] # create list
var = [‘one’, 2, ‘three’, ‘banana’]

Tuples (immutable sets)


var = (‘one’, 2, ‘three’, ‘banana’)

Dictionaries (associative arrays or ‘hashes’)


var = {} # create dictionary
var = {‘lat’: 40.20547, ‘lon’: -74.76322}
var[‘lat’] = 40.2054
Each has its own set of methods

Source: OIP.AQ5rAzGvicbIAudCjnmlyQHaHa (266×266) (bing.com)


Essentials of Python Programming Language

Lists
• Think of a list as a stack of cards, on which your
information is written

• The information stays in the order you place it in until


you modify that order

• Methods return a string or subset of the list or modify


the list to add or remove components
• Written as var[index], index refers to order within set
(think card number, starting at 0)

• You can step through lists as part of a loop

Source: OIP.puQxxCnV_sa5ageMV--A-wHaEj (351×234) (bing.com)


Essentials of Python Programming Language

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

• Removing from the List


var[n] = [] #empties contents of card, but preserves order
var.remove(n) #removes card at n
var.pop(n) #removes n and returns its value
Essentials of Python Programming Language

Tuples
Like a list, tuples are iterable arrays of objects
Tuples are immutable -
once created, unchangeable

To add or remove items, you must redeclare


Example uses of tuples
County Names
Land Use Codes
Ordered set of functions

Source: https://blog.finxter.com/the-ultimate-guide-to-python-tuples/
Essentials of Python Programming Language

Dictionaries
Dictionaries are sets of key & value pairs

Allows you to identify values by a descriptive name


instead of order in a list

Keys are unordered unless explicitly sorted

Keys are unique:


var[‘item’] = “apple”
var[‘item’] = “banana”

print var[‘item’] prints just banana

Source: OIP.QxN93ABT-n49TvtCYqYmnQHaEK (387×234) (bing.com)


Essentials of Python Programming Language

Lab 34: Data Structures in Python


Essentials of Python Programming Language

Indentation and Blocks


• Python uses whitespace and indents to denote blocks of code
• Lines of code that begin a block end in a colon:
• Lines within the code block are indented at the same level
• To end a code block, remove the indentation
• You'll want blocks of code that run only when certain conditions are met
Essentials of Python Programming Language

Conditional Branching
if and else
if variable == condition:
#do something based on v == c
else:
#do something based on v != c

elif allows for additional branching

if condition:
…............
elif another condition:
….............
else: #none of the above
….............
Essentials of Python Programming Language

Looping with For


• For allows you to loop over a block of code a set
number of times
For is great for manipulating lists:

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

Lab 35: Control Statements in Python


Essentials of Python Programming Language

Python OOPs Concepts


Like other general-purpose programming languages, Python is also an object-oriented language since its
beginning. It allows us to develop applications using an Object-Oriented approach. In Python, we can easily
create and use classes and objects.
Major principles of object-oriented programming
system are given below:
• Class
• Object
• Method
• Inheritance
• Polymorphism
• Data Abstraction
• Encapsulation

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

Lab 36: OOPS concepts in Python


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

Data Abstraction in Python


• Data abstraction and encapsulation both are often
used as synonyms. Both are nearly synonyms
because data abstraction is achieved through
encapsulation.
• Abstraction is used to hide internal details and show
only functionalities. Abstracting something means to
give names to things so that the name captures the
core of what a function or a whole program does.

Source: 8N1b4sGzdTgAAAABJRU5ErkJggg== (300×168)


Essentials of Python Programming Language

Lab 37: Advanced OOPS concepts in Python


Essentials of Python Programming Language

Integration of Database Technologies with Python


Essentials of Python Programming Language

Python Modules for Database Connection


• In Python, we can use the following modules to communicate with MySQL

• MySQL Connector Python


• PyMySQL
• MySQLDB
• MySqlClient
• OurSQL

• 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

Python with Databases


• Python supports relational database systems.

• It is very easy to migrate and port database


application interfaces (compatible database APIs).

• Python also supports Data Definition Language


(DDL), Data Manipulation Language (DML) and Data
Query Statements.

Source: maxresdefault.jpg (1280×720) (ytimg.com)


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

Establishing a Database Connection


connect() method
Takes 4 parameters –
1. host
2. user
3. password
4. database
Essentials of Python Programming Language

Creating Database Tables in Python


• cursor() - used to execute SQL statements
• execute() - used to compile SQL statements
• fetchall() - fetches all the rows from the last executed statement

Source: py-db-connection-edureka.png (979×411)


Essentials of Python Programming Language

Creating Database Tables in Python

Output -
Essentials of Python Programming Language

Database INSERT in Python


INSERT INTO table_name (column_names) VALUES (data)

Output -
Essentials of Python Programming Language

Inserting Multiple rows in Python

Output -
Essentials of Python Programming Language

SELECT statement in Python


SELECT column_names FROM table_name

Output -
Essentials of Python Programming Language

DELETE statement in Python


DELETE FROM table_name WHERE
condition

Output -
Essentials of Python Programming Language

UPDATE Statement in Python


UPDATE table_name SET column_name = new_value WHERE condition

Output -
Essentials of Python Programming Language

DROP Statement in Python

Output -

© Edunet Foundation. All rights reserved.


Essentials of Python Programming Language

Lab 38: Database Connectivity in Python


Essentials of Python Programming Language

Introduction to Python Popular Frameworks


Python is one of the most popular and effective
programming languages that contain vast libraries and
frameworks for almost every technical domain. Python
frameworks automate the implementation of several tasks
and give developers a structure for application
development. Each framework comes with its own
collection of modules or packages that significantly
reduce development time.
Essentials of Python Programming Language

Python Frameworks for ML and DL


• TensorFlow
• Keras
• PyTorch
• Scikit-Learn

Source: 1588602415d0xfhuoXiu.png (960×480) (hackr.io)


Essentials of Python Programming Language

Python Frameworks Web Development


Python frameworks available in the market for web
development. Depending on the functionality and key
features they provide to the user, below are the
frameworks available for web development.

• Django
• Flask

Source: Python-Frameworks-for-Web-Development.jpg (1068×601) (tekkiwebsolutions.com)


Essentials of Python Programming Language

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's indentation-based syntax enforces code readability.

• 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?

a) Iterates through a sequence


b) Defines a loop
c) Executes a block of code conditionally
d) Prints a value

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!

You might also like