You are on page 1of 8

List Slicing

The most popular method used by programmers to handle effective problems is list slicing,
which is standard practice in Python. Think about a Python list. You must slice the list in order
to access a range of its elements. Using colon(:), a basic slicing operator, is one method to
accomplish this. This operator allows the user to define the step, the start and stop points for
the slicing. List slicing takes an existing list and creates a new one from it.

To get a slice of a list, you write an expres sion in the following general format:

list_name[start : end]

Inheritance
Inheritance is a fundamental idea in object-oriented programming (OOP) languages. By deriving
a class from another class, you can use this process to build a hierarchy of classes that share a
set of attributes and methods. One class's capacity to obtain or inherit properties from another
class is known as inheritance.

Advantages of inheriting are.

The ability to inherit properties from one class, or base class, to another, or derived class, is
known as inheritance. The following are some advantages of inheritance in Python:

 It does a good job of capturing real connections.


 It offers the possibility to reuse code. It is not necessary for us to write the same code
repeatedly. Furthermore, it enables us to expand a class's functionality without
changing it.
 Because it is transitive, all of class B's subclasses will inevitably inherit from class A if
class B inherits from another class A.
 An easy-to-understand model structure is provided by inheritance.
 An inheritance results in less money needed for development and maintenance.

Python Inheritance Syntax


Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
init_ Method
An instance function in Python called __init__ initializes a newly generated object. It accepts
the object as the first parameter and then further arguments.

The method initially accepts the object (self) as an argument, then any other parameters that
must be supplied to it. Double underscores surround the name __init__, signifying that it is a
unique Python method.

After the object is formed by the __new__ method, the __init__ method is invoked. It initializes
the object attributes using the values supplied as arguments.

You should use __init__ when you need to initialize the object. For example, you might want
to use __init__ to:

 Set the object’s attributes.


 Call the object’s superclass’ __init__ method.
 Perform other initialization tasks.

How Does the __init__ Method Work?


The object-oriented Python counterpart of the C++ constructor is the __init__ method.
Whenever an object is created from a class, the __init__ function is called. There is no other use
for the __init__ method other than allowing the class to initialize the object's attributes. Only in
class does it get used.

Encapsulation
One of the fundamental concepts in object-oriented programming (OOP) is encapsulation. It defines the
concept of data wrapping and the methods that work on data within a single unit. This restricts direct
access to variables and methods and can prevent data from being accidentally modified. An object's
variable can only be changed by an object's method to prevent accidental changes. These variables are
referred to as private variables.
Python provides three types of access modifiers private, public, and protected:

 Public Member: Accessible anywhere from otside oclass.


 Private Member: Accessible within the class
 Protected Member: Accessible within the class and its sub-classes

Encapsulation Example:

Assume that we run a business that offers professionals, engineers, and students courses. This
company is divided into several divisions, such as operations, finance, accounts, sales, etc.
Therefore, an employee in the accounts department will not be able to directly access the sales
records for 2022 if they need them.

The member of the sales section team must grant permission for the account section employee
to access. The sales data is therefore concealed from other departments. Similarly, the
company's financials are hidden from other sections and only available to the finance data. The
data related to operations, marketing, sales, finance, and accounting is concealed from other
sections.

List Operations
A list is a type of data structuring that enables the storage of characters or integers in an index
order beginning with 0. The operations that can be carried out on the data within a list data
structure are known as list operations. In Python programming, some of the fundamental list
operations include: concatenate(), count(), multiply(), sort(), index(), clear(), slice, reverse(),
min() & max(), insert(), append(), remove(), pop(), slice, etc.

List operations consist of the following:

 Accessing elements within a list through indexing and slicing.


 Expanding and appending: Including items in a list.
 Elements can be eliminated by value or index.
 Sorting and reversing: Modifying the constituents' order.
 List comprehensions: A condensed approach to list creation.
Tkinter Application
The built-in Python module Tkinter is used to create graphical user interfaces (GUIs). Due to its
ease of use and simplicity, it is one of the most popular modules for Python GUI application
development. Since Python already includes the Tkinter module, you don't need to worry about
installing it separately. It provides the Tk GUI toolkit with an object-oriented interface.

To create a tkinter Python app:

 Importing the module – tkinter


 Create the main window (container)
 Add any number of widgets to the main window
 Apply the event Trigger on the widgets.

Here are some common use cases for Tkinter:

Creating windows and dialog boxes: You can use Tkinter to make user-interactive
windows and dialog boxes for your program. These can be used to show options to the
user, collect input, or display information.
Creating a desktop application's graphical user interface (GUI): Tkinter can be used to
create a desktop application's GUI, which includes buttons, menus, and other interactive
elements.
Adding a graphical user interface (GUI) to a command-line program: Tkinter can be used
to add a GUI to a command-line program, facilitating user interaction and argument
input.
Making custom widgets: Tkinter lets you make your own custom widgets in addition to a
number of pre-built ones, like text boxes, buttons, and labels.
GUI prototyping: Before committing to a final implementation, you can test and refine
various design concepts using Tkinter, which makes it easy to quickly prototype a GUI.
SQLite3 Database Scripting
With the many features that databases provide, managing vast volumes of data via the internet
and high-volume input and output over standard files like text files is made simple. The query
language SQL is widely used in databases. MySQL is used by many websites. A "light" version of
SQL that operates over syntax very similar to SQL is called SQLite. SQLite is a public-domain,
fully featured, embedded, highly reliable, and self-contained SQL database engine. On the
internet, this database engine is the most popular. Since version 2.5, the Python package has
included a library called sqlite3 that allows access to SQLite databases for use with Python.

SQLite has the following features:

1. Serverless
2. Self-Contained
3. Zero-Configuration
4. Transactional
5. Single-Database

Connecting to the Database:

The connect() method can be used to establish a connection to a SQLite database by providing
the name of the database to be accessed as a parameter. That database will be created if it
doesn't already exist.

sqliteConnection = sqlite3.connect('sql.db')

However, what happens if you wish to run some queries after the connection has been
established? To do that, we need to create a cursor on the connection instance by calling the
cursor() method, which will then run our SQL queries.

cursor = sqliteConnection.cursor()

print('DB Init')

The execute() method on the cursor object can be used to call the SQL query after it has been
written as a string. The SQLite Version Number, in this case, can then be obtained from the
server by using the fetchall() method.

query = 'SQL query;'

cursor.execute(query)
result = cursor.fetchall()

print('SQLite Version is {}'.format(result))

Python Classes
A class is an object creation template, or prototype, that is defined by the user. Classes offer a
way to group functionality and data together. A new object type is created when a new class is
created, enabling the creation of new instances of that type. For the purpose of preserving its
state, each class instance may have attributes attached to it. Additionally, class instances may
have methods for changing their state that are specified by their class.

The need to creating a class and an object example:

Suppose your goal was to keep tabs on the quantity of dogs with varying characteristics, such as
age and breed. If a list is utilized, the breed of the dog could be the first element and its age
could be the second. In the event that there are one hundred distinct dogs, how would you
determine which element belongs to which? What if you wanted to give these dogs additional
characteristics? This is disorganized, and classes are exactly what are needed.

Syntax: Class Definition

class ClassName:

# Statement

Syntax: Object Definition

obj = ClassName()

print(obj.atrr)

Some points on Python class:

Classes are created by keyword class.

Attributes are the variables that belong to a class.

Attributes are always public and can be accessed using the dot (.) operator. Eg.: My
class.Myattribute
An object of a Python class

An instance of a class is called an object. An instance is a copy of the class with actual values,
whereas a class is similar to a blueprint. It is now a real dog, a seven-year-old pug of the breed,
rather than just an idea. Even though you have a lot of dogs and can create a lot of different
instances, you wouldn't know what information is needed if you didn't have the class to guide
you.

An object is composed of:

State: An object's attributes serve as a representation of it. It also reflects an object's


characteristics.

Behavior: It is embodied in an object's methods. It also shows how an object reacts to other
objects.

Identity: It provides a special name.

Subclass Definitions
A subclass in Python is a class that receives inheritance from another class, known as the superclass or
base class, for its attributes and methods. By using subclassing, you can reuse an existing class's code
and functionality to create a customized version of it. This fundamental idea of object-oriented
programming (OOP) encourages the reuse and maintainability of code.

A class (also known as a superclass, base class, parent class) can be specialized into a subclass, derived
class, or child class.

A subclass is connected to its superclasses through an is-a relationship and inherits their methods and
instance data. For instance, an instance of P is an instance of Q and also (by transitivity) an instance of S
if subclass P inherits from superclass Q and subclass Q inherits from superclass S. The data and methods
of Q and S are inherited by an instance of P.
Using subclasses has several advantages:

Reuse of code: Through inheritance, a subclass can reuse methods that already exist in a superclass.

Specialization: In a subclass you can add new methods to handle cases that the superclass does not
handle. You can also add new data items that the superclass does not need.

Change in action: A subclass can override a method that it inherits from a superclass by defining a
method of the same signature as that in the superclass. When you override a method, you might make
only a few minor changes or completely change what the method does.

Defining a subclass instance method

The methods of a superclass are passed down to a subclass. Any instance method that the subclass
inherits can be overridden in its definition by designating an instance method that has the same
signature as the inherited method. Additionally, you can define new methods required by the subclass.

A subclass instance method has the same syntax and structure as a class instance method. In the
PROCEDURE DIVISION of the OBJECT paragraph of the subclass definition, define the instance methods
of the subclass.

Example: defining a subclass (with methods:

The instance method definitions for the CheckingAccount subclass of the Account class are displayed in
the example that follows.

To obtain the check data, the processCheck method calls the Check class's JavaTM instance methods
getAmount and getPayee. To credit the payee and debit the payer of the check, it calls the instance
methods for credit and debit that were inherited from the Account class.

The print instance method specified in the Account class is superseded by the print method. It shows the
check fee in addition to the account status by calling the overridden print method. The subclass defines
the instance data item CheckFee.

You might also like