0% found this document useful (0 votes)
50 views5 pages

Assignment 1

The document contains instructions for students to write Python programs that demonstrate concepts like classes, objects, methods, dictionaries, and more. It includes 11 programming tasks/questions for students to complete, covering topics such as creating classes with constructors and methods, taking user input, formatting output, using dictionaries and recursion.

Uploaded by

sourabh k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
50 views5 pages

Assignment 1

The document contains instructions for students to write Python programs that demonstrate concepts like classes, objects, methods, dictionaries, and more. It includes 11 programming tasks/questions for students to complete, covering topics such as creating classes with constructors and methods, taking user input, formatting output, using dictionaries and recursion.

Uploaded by

sourabh k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

FYBCA

SEMESTER – II
BCA1211C08 : Object Oriented Programming
Date : 10/01/2023

1. Create a dictionary of Cars and print the car company of a specific model.
cars={
‘hyundai’ : “i10”,
‘maruti’ : “swift”,
‘honda’ : “city”
}
Print(‘Cars information:’, cars)
Cars.update({‘honda’ : “jazz”}) or cars[‘honda’]=’jazz’
Print(‘Cars information:’, cars) #after appending

2. Write a program to evaluate an expression entered by the user. Also solve that manually and
verify your result with the evaluated one.

3. Write a program to input student’s name, age and percentage in single input() and print the
result.
name, age, marks = input("Enter your Name, Age, Percentage separated by space
").split()
print("\n")
print("User Details: ", name, age, percentage)

4. Write a program to input name, class and marks in five subjects of a student in single input() and
print the percentage of the student.

5. Write a program to take multi-line input from the user.


# list to store multi line input
# press enter two times to exit
data = []
print("Tell me about yourself")
while True: #infinite loop will run unless break is written
line = input()
if line:
data.append(line)
else:
break
finalText = '\n'.join(data)
print("\n")
print("Final text input")
print(finalText)

Most of the time, we need to format output instead of merely printing space-
separated values. For example, we want to display the string left-justified or in the
center. We want to show the number in various formats.

For that,use str.format() function

str.format(*args, **kwargs)

 The str is the string on which the format method is called. It can contain text or
replacement fields delimited by braces {}.
 Each replacement field contains either the numeric index of a positional argument
present in the format method or the name of a keyword argument.
 The format method returns a formatted string as an output. Each replacement field
gets replaced with the actual string value of the corresponding argument present
in the format method. i.e., args.

print('FirstName - {0}, LastName - {1}'.format('Ault', 'Kelly'))

Here {0} and {1} is the numeric index of a positional argument present in the format
method. i.e., {0} = Ault and {1} = Kelly. Anything that not enclosed in braces {} is
considered a plain literal text.

6.  Write a program to print names of 5 Planets using {} and format function by using index.
7. Write a Python program to create a Student class
a. Add documentation comments within class as and when required.
b. Add one no arguments constructor which prints some message.
c. Add method studinfo() inside class.Include parameters like rollno,name,contactnum in
method definition.
d. Create three object reference variables of it.
e. Call method and constructor.
8. Write a Python program to create an Employee class
a. Add one constructor which has name, empno, empdept as parameters.
b. Create object reference variable of Employee class
9. Write a Python program to create a Test class.
a. Add factorial() method to print factorials of all the numbers between 1 to n using
recursion. n is taken as the input from the user
b. Call factorial() method.

Every object in Python has a property that is indicated by the symbol __dict__.
Furthermore, this object has every property that has been specified for it. Another
name for __dict__ is mappingproxy object. We can use the dictionary via applying the
__dict__ property to a class object.

10. Write a Python


# class Flowers is defined  
class Flowers:        
   # constructor  
    def __init__(self):            
       self.Rose = 'red'  
        self.Lily = 'white'  
        self.Lotus = 'pink'  
  
    def displayit(self):  
        print("The Dictionary from object fields belongs to the class Flowers :")  
    
flower = Flowers()  

# calling displayit function  
flower.displayit()  
# calling the attribute __dict__ on flower  
# object and making it print it  

print(flower.__dict__)  

Output:

The Dictionary from object fields belongs to the class Flowers :


{'Rose': 'red', 'Lily': 'white', 'Lotus': 'pink'}

11. See the output of the following Code snippets:

i) Class Test:

a=10 # a is static variable

def __init__(self):

self.b=20 # b is instance variable

t1=Test()

t2=Test()
Test.a=888

Test.b=999

Print(‘t1:’ , t1.a,t1.b)

Print(‘t2:’ , t2.a,t2.b)

ii) Class Test:

a=10 # a is static variable

def __init__(self):

self.b=20 # b is instance variable

def m1(self):

self.a=888

self.b=999

t1=Test()

t2=Test()

t1.m1()

Print(‘t1:’ , t1.a,t1.b)

Print(‘t2:’ , t2.a,t2.b)

iii) Class Test:

a=10 # a is static variable

def __init__(self):

self.b=20 # b is instance variable

@classmethod

def m1(cls):

cls.a=888

cls.b=999
t1=Test()

t2=Test()

t1.m1()

Print(‘t1:’ , t1.a,t1.b)

Print(‘t2:’ , t2.a,t2.b)

Print(‘Test class variables:’, Test.a,Test.b)

You might also like