Posts

Sequences in Python

In Python, sequences are ordered collections of items, where each item is accessible by its position (index) in the collection. Sequences support various operations including indexing, slicing, and iteration. The most common sequence types in Python are: Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable ordered sequence of items - they can be changed. Elements can be reassigned or removed, and new elements can be inserted. Tuples are like lists, but they are immutable ordered sequence of items - they can't be changed. Strings are a special type of sequence that can only store characters, and they are immutable. Sequence Operations Indexing Accessing an element by its position. Example: my_list = [10, 20, 30, 40, 50] printtems(my_list[0])  # Output: 10 print(my_list[-1])  # Output: 50 (last element) Slicing Accessing a sub sequence by specifying a start and end index. my_list = [10, 20, 30, 40, 50] print(my_list[1:4])  # Ou

49.Example Programs using Classes

create class Arith to do arithmetic operation.It contains a member function read() to read the two numbers and add() method to find the sum. You can add more methods to the class to incorporate more functionality. class Arith:      def read(self):           self.x=int(input("enter first number.."))           self.y=int(input("enter second number..."))      def add(self):           print("sum=",self.x+self.y) #creating an object A=Arith() #calling the methods A.read() A.add() create a class Rectangle .A constructor is used to initialize the object values. Member function area() to compute the area of the rectangle ( university question) class Rectangle:     def __init__(self,length=0,breadth=0):         self.length=length         self.breadth=breadth     def area(self):         print("area=",self.length*self.breadth) R1=Rectangle(10,20) R1.area() R2=Rectangle(12,13) R2.area() Create a class car with attributes model, year and price and a method c

48. Abstract classes in Python

An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation. While we are designing large functional units we use an abstract class. When we want to provide a common interface for different implementations of a component, we use an abstract class. Why use Abstract Base Classes : By defining an abstract base class, you can define a common Application Program Interface(API) for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins, but can also help you when working in a large team or with a large code-base where keeping all classes in your mind is difficult or not possible. How Abstract Base classes

47.Inheritance, Polymorphism and Operator overloading in Python

Image
Structuring Classes with Inheritance and Polymorphism Object-based programming involves the use of objects, classes, and methods to solve problems. Object-oriented programming requires the programmer to master the following additional  concepts: 1. Data encapsulation. Restricting the manipulation of an object’s state by external users to a set of method calls.Encapsulation restricts access to an object’s data to users of the methods of its class.This helps to prevent in discriminant changes to an object’s data. 2. Inheritance. Allowing a class to automatically reuse and extend the code of similar but more general classes.Inheritance allows one class to pick up the attributes and behavior of another class for free. The subclass may also extend its parent class by adding data and/or methods or modifying the same methods. Inheritance is a major means of reusing code 3. Polymorphism . Allowing several different classes to use the same general method names. Polymorphism allows methods in

46.Classes and Objects in Python- Accessors and mutators

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are  classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes(properties) and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object.Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. The cl

45.Date and Time in Python

Python has a module named datetime to work with dates and times. Commonly used classes in the datetime module are: date Class time Class datetime Class timedelta Class date class >>>import datetime  >>>d = datetime.date(2019, 4, 13)  >>>print(d) 2019-04-13 We can use today() method defined in the date class to get a date object containing the current local date. >>> import datetime  >>> datetime_object = datetime.date.today() >>> print("current date=",datetime_object) 2020-09-11 Get date from a timestamp We can also create date objects from a timestamp. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. You can convert a timestamp to date using fromtimestamp() method.   >>>from datetime import date  >>> date.fromtimestamp(1583562786)  print("Date =", timestamp)  When you run this, the output will be:Date =2020-03-07  Print today's year, month an