Sunday, October 29, 2023

CHAPTER 7 LEARN CLASS IN PYTHON

 LEARN PYTHON THROUGH SERIES OF ROBOT'S ACTIVITIES 

A user-defined class serves as a template or prototype from which objects can be built. Classes offer a way to group functionality and data. A new class produces a new type of object, enabling the creation of new instances of that type. Each instance of a class may have characteristics connected to it to preserve its state. Class instances may also contain methods for changing their state that is defined by their class. Sana is working as a professor in a college. Sana had ordered the robot named Tana, to monitor students of her college to improve the educational methods, that is to say,

how well the students have learned the content of each subject by conducting a series of tests or exams on each student.

 

Sana gives the marks obtained by the student to the robot, Tana, and similarly gives the completed question paper to the robot. At present questions are asked from 5 subjects of 20 marks each from each subject.

 

The function of this task is to assess whether the content of all 5 subjects is well understood by the students based on how much each student has scored in these 5 subjects.

To do this, the robot needs information about how each student wrote the answers to the related questions in the 5 subjects,

and the robot has to calculate the percentage of the student's score in each subject and convert it into a percentage and submit it to the professor.

The job of Tana is to get the given data, and marks of the students and, at the same time take the marks of the students in each subject separately and convert it into percentages and get the learning percentage of each subject.

Program 1:

[In]Class Student():

    def __init__(self, name, roll, gen):

        self.name=name

        self.roll=roll

        self.gen=gen

    def show(self):

        print("The student details are: Name :",  self.name, ", Roll No: ", self.roll, ", Gender: ", self.gen)

#This comment prints the student details.

[In]class Student():

    def __init__(self, name, roll, gen):

        self.name=name

        self.roll=roll

        self.gen=gen

    def show(self):

        print("The student details are: Name :",  self.name, ", Roll No: ", self.roll, ", Gender: ", self.gen)

stu1=Student('raj',123,"male")

stu1.show()

[Out]The student details are: Name : raj , Roll No:  123 , Gender:  male

[In]class Science(Student):

    def __init__(self, name, roll, gen):

        super().__init__(name, roll, gen)

        self.marks1=0

        self.marks2=0

    def test1marks(self, score1):

        self.score1=score1

        self.marks1 = self.marks1+ self.score1

        [In]print("The test 1 marks updated", self.marks1)

    def test2marks(self, score2):

        self.score2=score2

        self.marks2 = self.marks2+ self.score2

        [In]print("The test 2 marks updated", self.marks2)

[In]stu1=Science('raji',123,'female')

[In]stu1.test1marks(100)

[Out]The test 1 marks updated 100

[In]stu1.test2marks(10)

[Out]The test 2 marks updated 10

Program 2:

In the second example, A shopkeeper has to calculate the total price for 3 items bought by a customer. The 3 items are moong, oil, and vegetables. The program is very simple to make a class program that contains different functions such as _init_, and price. The output of this program executes the price function with the prices of all the groceries. This is available in the Ingredients class. The values of prices are passed to the Ingredients class.

[In] class Ingredients:

    def __init__(self, m,o,c):

        self.m=m

        self.o=o

        self.c=c

    def price(self):

        tot= self.m + self.o + self.c

        print("The total price is Rs.", tot)

p=Incredients(45,34,4)

p.price()

​[Out] The total price is Rs.  83

The next program explains the functions of a class with more than one function.

[In]class Ingredients:

def unitprice(self,moong,oil,veg):

        self.moong=moong self.oil = oil

        self.veg = veg

        print("The unitprice of moong is Rs",self.moong)

        print("The unitprice of oil is Rs",self.oil)

        print("The unitprice of veg is Rs",self.veg)

def quantity(self,moonquan=float,oilquan=float, vegquan=float):

        self.moonquan=moonquan

        self.oilquan = oilquan

        self.vegquan = vegquan

        print("The quantity of moong is ",self.moonquan,"kg")

        print("The quantity of oil is ",self.oilquan,"kg")

        print("The quantity of veg is ",self.vegquan,"kg")

def price(self,m=float,o=float,v=float,tot=float):

        m=self.moong*self.moonquan

        o=self.oil*self.oilquan

        v=self.veg*self.vegquan

        self.m =m

        self.o =o

        self.v =v

        tot= self.m+self.o+self.v

        print("The total price of the groceries is Rs. ",tot)

p=Incredients()

p.unitprice(1,4,4)

p.quantity(0.5,1,4)

p.price()

[Out] The unitprice of moong is Rs 1

The unitprice of oil is Rs 4

The unitprice of veg is Rs 4

The quantity of moong is  0.5 kg

The quantity of oil is  1 kg

The quantity of veg is  4 kg

The total price of the groceries is Rs.  20.5

 

 

 

CHAPTER 6 LEARN FUNCTIONS

 LEARN PYTHON THROUGH SERIES OF ROBOT'S ACTIVITIES 

To repeat a particular action, reducing the generation of codes each time, and reusing codes generated once, is called operations in computer language. Activities are an event in which an action is initiated and completed within a specified time frame and this event is completed through specified procedures. For example, activities such as going to the store, returning from the store, writing an essay, and cooking can be considered. A shopping activity consists of various small tasks. Decide what to buy, decide how much of each item to buy, decide how much money you need to have, learn about giving money and buying the balance, get it all into a bucket, and take it home again.

This example shows how a robot can complete household chores promptly as an operation. Similarly, other operations may have several smaller tasks and conditions attached to them, such that when a robot is given a command, the commands are executed one after the other, and an operation is completed.

 

Ironing, Washing, Playing, Walking, Brushing, Gardening, Reading, Shopping, Cleaning the home, Cooking, Cleaning the kitchen, and singing.

Ironing function

    [In]def iron():

       dress_type=input("get the type of dress   ")

        dress_typelist=dress_type.split()

        print(dress_typelist)

        for x in dress_typelist:

            if x == "cotton":

           print("Maintain temperature at 60 deg")

                print("iron is over")

            elif x== "velvet":

            print("Maintain temperature at 30 deg")

                    print("iron is over")

            else:

                print("select correct cloth")

    [In]iron()

    [Out]get the type of dress   velvet

    ['velvet']

    [Out]Maintain temperature at 30 deg

    [Out] iron is over

    #Running the function again.

    [In]iron()

    [In]get the type of dress   cotton

    ['cotton']

    [Out]Maintain temperature at 60 deg

    [Out]iron is over

        #Running the function again.

 [In]iron()

    [In]get the type of dress   polyester

    ['polyester']

    [In]select correct cloth

Washing function

    #washing job

    def wash():

no_ofcloth=int(input("enter the no of dresses  "))

        while no_ofcloth<6:

            if no_ofcloth<3:

                waterlow=int(input("get the low water level   "))

                print("motor on. finish wash")

            else:

                waterhigh=int(input("get the high water level   "))

                print("motor on. finish wash")

            break

        else:

            print("get the low no of dresses")

    #Running the function.

    [In]wash()

    [In]enter the no of dresses  2

    [In]get the low water level   4

    [In]motor on. finish wash

    #Running the function again.

    [In]wash()

    [In]enter the no of dresses  5

    [In]get the high water level   5

    [In]motor on. finish wash

    #Running the function again.

    [In]wash()

    [In]enter the no of dresses  7

    [In]get the low no of dresses

    Autonomous Car Driving

         The following decisions were made before the car has to be moved from one place to another place. There were 4 sensors located beside the wheels of the car. If any sensor input is 0, no obstacle is assumed, False condition. If any sensor detects an obstacle, then the sensor input will be 1, True.

[In] def drive1():

         fls=str(input("front left sensor input   "))

         frs=str(input("front right sensor input   "))

    def forwbreak():

           print("stop the forward vehicle, apply brake")

    def forwacce():

         print("forward acceleration on")

    def forright():

        print("steer right")

    def forleft():

        print("steer left")

    def revbreak():

        print("stop the reversed vehicle, apply brake")

    def revacce():

        print("steer 4")

    if fls == "1" and frs == "1":

            forwbreak()

    elif frs=="1" and fls=="0":

            forleft()

    elif fls=="1" and frs=="0":

            forright()

    elif fls == "0" and frs == "0":

            forwacce()

    return

#When the function runs, based on the decision the car will be driven.

[In]drive1()

    front left sensor input   1

    front right sensor input   0

    steer right

[In] drive1()

    front left sensor input   1

    front right sensor input   1

    stop the forward vehicle, apply the brake

[In] drive1()

    front left sensor input   0

    front right sensor input   1

    steer left

[In]drive1()

    front left sensor input   0

    front right sensor input   0

    forward acceleration on

   #This example shown below is the function drive(), that is using 2 sensors. These 2 sensors will get the input from the user. As per user input, the decisions will be made. 

def drive():

        get_sendata=input(" the sensor data    ")

        sendata = get_sendata.split(",")

        print(sendata)

        a=type(sendata)

        b=len(sendata)

        print(a)

        print(b)

        a1=sendata[0]

        b2=sendata[1]

        print(a1)

        print(b2)

        if a1=="0" and b2=="1":

            print("turn left")

        elif a1=="1" and b2=="0":

            print("turn right")

        elif a1=="0" and b2=="0":

            print("accelerate")

        elif a1=="1" and b2=="1":

            print("stop the car")

        return

    When program runs

[In]drive()

    [Out] the sensor data    0,0

    ['0', '0']

    <class 'list'>

    [Out] 2

    [Out] 0

    [Out] 0

    Accelerate

[In]drive()

 

 

[Out] the sensor data    1,1

    ['1', '1']

    <class 'list'>

    [Out] 2

    [Out] 1

    [Out] 1

    [Out] stop the car

Practice Problem

If 4 sensors are fitted in a car, then the following program will be used.

[In]def drive():

         get_sendata=input(" the sensor data    ")

         sendata = get_sendata.split(",")

         print(sendata)

    #a1=front left , b2= front right, c3=rear right,     

        d4=rear left sensors.

        a=type(sendata)

    b=len(sendata)

    print(a)

    print(b)

    a1=sendata[0]

    b2=sendata[1]

    c3=sendata[2]

    d4=sendata[3]

    print(a1)

    print(b2)

    print(c3)

    print(d4)

    if ((a1=="0") and (c3=="0") and (d4=="0") and (b2=="1")):

        print("turn left and accelerate")

    elif ((b2=="1") and (c3=="1") and (d4=="1") and (a1=="0")):

        print("turn right and accelerate")

    elif a1=="0" and b2=="0":

        print("accelerate")

    elif c3=="1" and d4=="1":

        print("stop reverse the car")

    elif ((a1=="1") and (b2=="1") and (c3=="1") and (d4=="0")):

        print("reverse the car towards rear right")

    elif ((a1=="1") and (b2=="1") and (d4=="1") and (c3=="0")):

        print("reverse the car towards rear left")

    return

[In] drive()

[Out] the sensor data    0,0,0,1

['0', '0', '0', '1'] # list created.

<class 'list'>

4 # length of list elements, 4 elements.

0   # sensor data1

0   # sensor data2

0   # sensor data3

1   # sensor data4

accelerate

 

CHAPTER 5 LOOP STRUCTURES

 

LEARN PYTHON THROUGH SERIES OF ROBOT'S ACTIVITIES 

In this chapter, the loop structures are explained with suitable examples. This example shows how Johnny, who is a robot, and what tasks he has to complete each day according to the requirement.

[In]list_duty = ['cleaning', 'cooking', 'arranging','ironing', 'washing', 'singing','playing','breakfast','lunch','dinner']

set_time=["morning","noon","evening", "night"]

for w in list_duty:

    a=input("get the time   ")

    if a == "noon" :

# if and only if, the chosen time is noon, the following duties are to be called. 

        a1=input("call the the followings  ")

        print("the duties are  ", a1)

        continue

    else:

        print("get the time again")

# The next program will select the time and duties simultaneously. 

[In]set_time=["morning","noon","evening", "night"]

[In]list_duty = ['cleaning', 'cooking', 'arranging','ironing', 'washing', 'singing', 'playing','breakfast','lunch','dinner']

[In]for w in list_duty:

    a=input("get the time   ")

    if a in set_time:

        a1=input("call the the followings  ")

        if a1 in list_duty:

           print("the duties are  ", a1)

           print("the duties are over")

        else:

            print("get the correct duty and time")

            continue

    else:

        print("get the correct time again")

[Out]get the time   noon

[Out]call the following _________

[Out]get the correct duty and time

Workout Problem 1:

[In]set_time=["morning","noon","evening", "night"]

[In]list_duty = ['cleaning','wash']

for w in list_duty:

    a=input("get the time   ")

    if a in set_time:

        a1=input("call the the followings  ")

        if a1 in list_duty:

           print("the duties are  ", a1)

           print("the duties are over")

        else:

            print("get the correct duty and time")

            continue

    else:

        print("get the correct time again")

[Out]get the time   noon

[Out]call the followings  lunch

# Here only 'cleaning', and 'washing' duties are provided.

[Out]get the correct duty and time

[Out]get the time   noon

[Out]call the following wash

[Out]the duties are   wash

[Out]the duties are over

Example 2:

#per day duties.

set_time=["morning","noon","evening", "night"]

list_duty = []

duties=input("assign duties ")

list_duty = duties.split()

# split command helps to retrieve individual items in the list.

print(list_duty)

for w in list_duty:

    a=input("get the time   ")

    if a in set_time:

        a1=input("call the the followings  ")

        if a1 in list_duty:

           print("the duties are  ", a1)

           print("the duties are over")

        else:

            print("get the correct duty and time")

            continue

    else:

        print("get the correct time again")

[Out]assign duties _____________

[In] cook, wash, study, pray

['cook,', 'wash,', 'study,', 'pray']

[Out]get the time   ___________

[In]noon

[Out]call the following ______

[In] cook, study

[Out]the duties are   cook, study

[Out]the duties are over

[Out]get the time   ___________

[In]morn

[Out]call the following ______

[In] study

[Out]the duties are  study

[Out]the duties are over

 

CHAPTER 18 EXPLORING THERMODYNAMICS WITH PYTHON: UNDERSTANDING CARNOT'S THEOREM AND MORE

  Python is a versatile programming language that can be used to simulate and analyze various physical phenomena, including thermal physics ...