Featured Post

Python map() and lambda() Use Cases and Examples

Image
 In Python, map() and lambda functions are often used together for functional programming. Here are some examples to illustrate how they work. Python map and lambda top use cases 1. Using map() with lambda The map() function applies a given function to all items in an iterable (like a list) and returns a map object (which can be converted to a list). Example: Doubling Numbers numbers = [ 1 , 2 , 3 , 4 , 5 ] doubled = list ( map ( lambda x: x * 2 , numbers)) print (doubled) # Output: [2, 4, 6, 8, 10] 2. Using map() to Convert Data Types Example: Converting Strings to Integers string_numbers = [ "1" , "2" , "3" , "4" , "5" ] integers = list ( map ( lambda x: int (x), string_numbers)) print (integers) # Output: [1, 2, 3, 4, 5] 3. Using map() with Multiple Iterables You can also use map() with more than one iterable. The lambda function can take multiple arguments. Example: Adding Two Lists Element-wise list1 = [ 1 , 2 , 3 ]

How to Write Class Object in Ubuntu Python

Python supports object-oriented programming, which makes python powerful. Creating class in Ubuntu python explained. You can use it in different ways by assigning it to an object. Doing all these are explained in the below steps.
 

Here is a logic you can create a script with Class and Objects


Writing Class in Python in 3 Steps


  1. Python code
  2. Write the code in script
  3. Execute script

Writing Class in Python


Below sample code my give indentation errors. However, I have corrected the code in the actual script.


class Employee:
"""Base class"""
empCount = 0


def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1


# can also be written as Employee.empCount = Employee.empCount + 1
def displayEmployee(self): # function is defined here
print "Name : ", self.name, ", Salary: ", self.salary


# "emp1 is the first object of Employee class"
emp1 = Employee("Akhil", 2000)


# "emp2 is the second object of Employee class"
emp2 = Employee("Suresh", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print ("Total Employee %d" % Employee.empCount)

Python Script


You need to use vim command to create a script in Ubuntu.  You can do any Linux operating system for practice.


$ vim poly.py


Python Class and Object in a Script


Logic:
 

In python,' init' is a mandatory method with 'self' you need to give. The arguments 'name' and 'salary' are optional to you. Here I used two objects of Employee - emp1 and emp2. The Employee is the base class.


These objects used the "displayEmployee" method. According to the print definition, you got the below output details.


The last print has two characters. One is %d and %. The %d, pads a blank. If you want more, you can write %2d, %3d, and so on.

Executing script


From Python Console, get the .py module using the "import" command. It runs as and when the import completes.

  • The command to import is - >>> import poly.py
  • When I imported, it displayed the messages as expected.

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Python placeholder '_' Perfect Way to Use it