Posts

Python inheritance

Image
Inheritance is one of the OOP(Object Oriented Programming) Concept. It involves a parent and child class. A class is created and its member functions are defined. This class is called base class or parent class. When you want a copy of base class, you can inherit it   and create a child class. A child class is simply a replica of base class. 1)Here, a base class is created with name " customer" class customer:   def __init__(cust, fname, lname):     cust.firstname = fname     cust.lastname = lname      def printfullname(cust):     print(cust.firstname, cust.lastname) #Use the customer class to create an object, and then execute the printfullname method: x = customer("jeev", "anand") x.printfullname()   2) A child class “Regularcustomer” is created. #Creating a child class Regularcustomer class Regularcustomer(customer):   pass # pass denotes this class doesnot have any d...

Object oriented concepts -Classes and objects

Image
 Everything is an object in real world. In object-oriented programming, basic term is classes and objects. Class is a collection of objects and methods. Each class has some properties and functionalities. For eg, A car is a class. Name of the car is like the classname. Brand of the car, color and size are the properties of the class. Running is the functionality of a car. It is the method. Let us create a class in python. Here, the code begins… Syntax:   class classname:                    Variablename Eg: class SampleClass:                S = 10 Next, an object is created. The object accesses the Sum variable and it gets printed.         Ob = SampleClass()         Print(Ob.sum) Add this code in a file “SampleClass.py”. While execu...

Python modules

Image
 Modules… From the name itself, we know, a module is a set of statements which can perform a functionality. It has properties, functions and methods. Modules can be of two types ·        Built in modules ·        User defined modules Built in modules: These are the modules available in the python library. The declaration and definitions are included in the python itself. To know about the built in modules, just type help(‘modules’) in the python command prompt.It list outs all the modules in the library.   User defined module: A sample module in python should have an extension “.py”. Let us create a python module “mul.py” which multiplies the two numbers given “mul.py” def pro(a, b):   return(a*b) import mul mul.pro(2 ,3) while executing this, the values are multiplied and returned. If you want to print the value, you can use print().  

Python for beginners - Lambda function in python

Image
Python has some interesting function called “Lambda”. In this function, the user can have any number of arguments in one expression. The syntax be like, Lambda arguments: expression For example, Multiply the variable by 2 and return the result. The code will be given below.. y = lambda b: b * 2 print(y(3)) Next, using 2 arguments in lambda function. It has 2 arguments x, y. we add the values and return it. a = lambda x, y : x + y print (a(7,8)) while executing this, you get the below output. When you use 3 arguments in lambda function, it look like below program. Let d as the returning value. x,y,z are three variables. The function is add these 3 variables. d = lambda x, y, z : x + y + z print(d(3,4,5)) The output is Lambda function can be used in many ways. It can be defined once, use it various places. The function may be called in many times. But, it can be used in short period of time.

Python for beginners - Python Tuples

Image
  Tuple Tuple is one of the inbuilt datatypes in python for storing data. The data is stored in ordered way and it cannot be changeable. The syntax for creating tuple is Tuplename=(“data1”,”data2”,”data3”) Example is given below. mytuple=(“carrot”,”beetroot”,”beans”) print(mytuple) While executing this code,the output is look like below. ('carrot', 'beetroot', 'beans') Features: Tuple has some features. Tuples are indexed. The index value starts from 0 to n values. Once created, it cannot be changed. Sometimes, same values can be duplicated.   Next,the datatypes like int,Boolean and strings are given below. Da.py #This is a tuple which contains integer values. Nutuple=(1,2,3,4,5)   #This is a tuple which contains Boolean values. Botuple=(True,False,True) #This is a tuple which contains string values. Strtuple=(“happy”,”days”,”today”) print(Nutuple) print(Botuple) print(Strtuple) while executing this program, th...

Python for beginners- Dictionary Operations

 Dictionary is one of the type of collections in python. It combines variety of data items under a common name. Syntax be like,  Name={               "Membername1":"Value",               "Membername2":"Value",                ........................................                   "Membernamen":"Value"              } An example is given below... Eg: Creating a dictionary named "sunglass"  sunglass = {                       "brand": "Suntrack"                       "model": "Wideangle"                       "Year " : "2023"                      } To print the...

How to add a value, remove a value and emptying a list???

 List has many operations to perform. The basic operations like creating a list, changing a value, displaying a list, searching an element in a list and finding length of a variable is explained in part 1. https://www.blogger.com/blog/post/edit/5251857942821229/7649420483850529537 In part 2, the remaining operations are listed below. Adding a value Removing a value Empty a list Usage of constructor  The operations are explained below.   =>Adding a value:                 This can be achieved by two ways. First one is adding a value in the end of the list. Second one  deals with adding in a specified index.     Method1:Adding a value in the end of the list:      colorlist=["Red", "Green", "Yellow" ]      colorlist.append( "Orange")      print(colorlist)     W hile executing this code, you get the following output.       [ 'Red', 'Green',...