Popular Posts

Monday, September 14, 2020

Python Beginner: Class inheritance

 Hello, Developer! 😄


Today, we will learn the "Inheritance". The "Inheritance" function allows you to use other class functions without codes copy. If you inherit other classes' properties, you can improve programming productivity through this function.


Class Inheritance Form

class Sample_class_name(Inheritance parent class): 

    def additional_property # You can add additional properties on parents' all properties

    def override_property # If you want to override parent properties, you can redefine property with the same property name


I showed all exercise codes and execution result as following.

Chef.py

class Chef:

def make_chicken(self):
print("The chef makdes a chicken.")

def make_salad(self):
print("The chef makes a salad.")

def make_special_dish(self):
print("The chef makes bbq ribs.")

ChineseChef.py

class ChineseChef: # ChineseChef can do everything which Chef can do

def make_chicken(self):
print("The chef makdes a chicken.")

def make_salad(self):
print("The chef makes a salad.")

def make_special_dish(self):
print("The chef makes soup noodle.")

def make_fried_rice(self):
print("The chef makes fried rice.")

InheritanceChineseChef.py

from Chef import Chef

class IchineseChef(Chef):

def make_special_dish(self):
print("The Chef makes a orange chicken.")


def make_fried_rice(self):
print("The Chef makes a fried rice.")

Inheritance.py

from Chef import Chef
from ChineseChef import ChineseChef
from InheritanceChineseChef import IchineseChef

myChef = Chef()
myChef.make_special_dish()

myChineseChef = ChineseChef()
myChineseChef.make_special_dish()

ImyChineseChef = IchineseChef()
ImyChineseChef.make_fried_rice()

Execution Result

Can you see the same result on your PC? Congratulations! Now you can utilize class inheritance function in the Python.


Alright, let's see in the next post, Python developer! 😊

Python Beginner: Object Functions

 Hello, Developer! 😄


Today, we will learn "Object Functions". Object Functions enable you to call the functions in the object and modify the values in the function in the object.


I showed full exercise codes and executed result as follow.

Student2

class Student2:
def __init__(self, name, major, gpa):
self.name = name
self.major = major
self.gpa = gpa

def on_honor_roll(self):
if self.gpa >= 3.5:
return True
else:
return False

Object Functions

from Student2 import Student2

student1 = Student2("Oscar", "Accounting", 3.1)
student2 = Student2("Phyllis", "Business", 3.8)

print(student1.on_honor_roll())
print(student2.on_honor_roll())

Execution Result

Can you see the same result on your PC? Congratulations! Now you can handle the function in the object.


Thank you for reading! See you again at the next post! 😊

Python Beginner: Building a Multiple Choice Quiz

 Hello, Developer! 😄


Today, we will make a "Multiple Choice Quiz" program. This program displays questions with multiple-choice. If the user input the correct answer, the program will count the correct answer number and display another questions until there is no more questions in the question array.

I showed all Multiple Choice Quiz program codes and execution results as follows.

Question.py

class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer

Building a Multiple Choice Quiz.py

from Question import Question

question_prompts = [
"What Color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n",
"What Color are bananas?\n(a) Teal/n\a(b) Magenta\n(c) Yellow\n\n",
"What Color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n"
]

questions = [
Question(question_prompts[0], "a"),
Question(question_prompts[1], "c"),
Question(question_prompts[2], "b"),
]

def run_test(questions):
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
print("You got " + str(score) + "/" + str(len(questions)) + "correct")

run_test(questions)

Execution Result

Can you see the same result at your PC? Congratulations! Now you can use object, function, loop, input and output functions in the Python.


Let's see you again at the next post, Python developer! 😊

 



Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...