Basic 1:

Class + init + call to use + Inheritance:

class Person:
	def __init__(self, firstName, lastName, idNumber):
		self.firstName = firstName
		self.lastName = lastName
		self.idNumber = idNumber
	def printPerson(self):
		print("Name:", self.lastName + ",", self.firstName)
		print("ID:", self.idNumber)
class Student(Person):
    def __init__(self, firstName, lastName, idNumber, scores):
        self.firstName = str(firstName)
        self.lastName = str(lastName)
        self.idNumber = int(idNumber)
        self.scores = scores
        self.average = 0

    #   Class Constructor
    #   
    #   Parameters:
    #   firstName - A string denoting the Person's first name.
    #   lastName - A string denoting the Person's last name.
    #   id - An integer denoting the Person's ID number.
    #   scores - An array of integers denoting the Person's test scores.
    #
    # Write your constructor here
    def calculate(self):
        for i in self.scores:
            self.average = self.average + i
        self.average = float(self.average/len(self.scores))
        if (self.average<=100)and(self.average>=90):
            result = "O"
            return result
        elif (self.average<90)and(self.average>=80):
            result = "E"
            return result
        elif (self.average<80)and(self.average>=70):
            result = "A"
            return result
        elif (self.average<70)and(self.average>=55):
            result = "P"
            return result
        elif (self.average<55)and(self.average>=40):
            result = "D"
            return result
        else:
            result = "T"
            return result
    #   Function Name: calculate
    #   Return: A character denoting the grade.
    #
    # Write your function here

line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())

Note: understand the self

Basic 2:

Python Abstract Classes

from abc import ABCMeta, abstractmethod
class Book(object, metaclass=ABCMeta):
    def __init__(self,title,author):
        self.title=title
        self.author=author   
    @abstractmethod
    def display(): pass

#Write MyBook class
class MyBook(Book):
    def __init__(self,title,author,price):
        self.title=title
        self.author=author  
        self.price=price
    def display(self):
        print("Title:", self.title)
        print("Author:", self.author)
        print("Price:", self.price)

title=input()
author=input()
price=int(input())
new_novel=MyBook(title,author,price)
new_novel.display()

Basic 3:

Scope

class Difference:
    def __init__(self, a):
        self.__elements = a
        self.__elements.sort()
        self.maximumDifference = 0
    def computeDifference(self):
        n = len(a)
        if n>=2:
            self.maximumDifference = abs(a[n-1]-a[0])

Basic 3:

Linked list:

def insert(self,head,data):
    #Complete this method
        newNode = Node(data)
        if head == None:
            head = newNode
        else:
            current = head
            while current.next != None:
                current = current.next
            current.next = newNode
        return head
  • Create newNode
  • Check if head None / not None
  • Go to next Node
  • insert newNode

Basic 4:

Exception:

try:
    print(int(S))
except:
    print("Bad String")

Basic 5:

More Exception:

#Write your code here
class Calculator:
    def power(self,n,p):
        if n<0 or p<0:
            raise Exception("n and p should be non-negative")
        else:
            x = n**p
            return x



myCalculator=Calculator()
T=int(input())
for i in range(T):
    n,p = map(int, input().split())
    try:
        ans=myCalculator.power(n,p)
        print(ans)
    except Exception as e:
        print(e)   

Basic 6:

Stack and Queue version array, just append and pop the right index

class Solution:
    # Write your code here
    def __init__(self):
        self.stack = []
        self.queue = []
    def pushCharacter(self, s):
        return self.stack.append(str(s))
    def popCharacter(self):
        l = len(self.stack)
        return self.stack.pop(l-1)
    def enqueueCharacter(self, s):
        return self.queue.append(str(s))
    def dequeueCharacter(self):
        return self.queue.pop(0)