Featured Post

SQL Interview Success: Unlocking the Top 5 Frequently Asked Queries

Image
 Here are the five top commonly asked SQL queries in the interviews. These you can expect in Data Analyst, or, Data Engineer interviews. Top SQL Queries for Interviews 01. Joins The commonly asked question pertains to providing two tables, determining the number of rows that will return on various join types, and the resultant. Table1 -------- id ---- 1 1 2 3 Table2 -------- id ---- 1 3 1 NULL Output ------- Inner join --------------- 5 rows will return The result will be: =============== 1  1 1   1 1   1 1    1 3    3 02. Substring and Concat Here, we need to write an SQL query to make the upper case of the first letter and the small case of the remaining letter. Table1 ------ ename ===== raJu venKat kRIshna Solution: ========== SELECT CONCAT(UPPER(SUBSTRING(name, 1, 1)), LOWER(SUBSTRING(name, 2))) AS capitalized_name FROM Table1; 03. Case statement SQL Query ========= SELECT Code1, Code2,      CASE         WHEN Code1 = 'A' AND Code2 = 'AA' THEN "A" | "A

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

Explained Ideal Structure of Python Class

How to Check Kafka Available Brokers