Safi ML Lab3
Safi ML Lab3
Abstract
In this lab we will learned to implement the function in python and understand the concept of
OOP in python and clear our concept about classes and objects. In this lab we will also learned
to implement the concept of constructor and its uses in python.
Objectives
The objectives of this lab are as follows:
1. To understand basics of Python Functions.
2. Implementing Python Functions and OPP concepts.
3. To understand basics of Object and Classes.
4. Implementing Constructors and its types.
5. Implementing Inheritance and its types.
Software
Spyder
Introduction
Functions
The most crucial part of an application is its functions. A function is an organised unit of
reusable code that may be invoked whenever it is needed.
Python allows us to break down a big programme into its component parts, known as functions.
The set of programming statements enclosed by encloses the function. A function can be called
numerous times in a Python application to provide reusability and modularity.
To put it another way, a programme is made up of a set of functions. In other programming
languages, the function is referred to as a process or a subroutine.
Tasks
Task #01:
Write a Python script to generate and print a dictionary that contains a number (between 1
and n) in the form (‘x’, x*x).
Sample: Input a number 4
Output: {'1': 1, '2': 4, '3': 9, '4': 16}
Code:
Task #02:
1. Create a dictionary of phone numbers. Create the function Find for the name ‘usman’
and get his phone
number and country code.
Sample:
Enter Name: usman
---------Information------------
Name: usman
Phone No: 3012435522
Code:
Task #03:
Write a Python function that accepts a string and calculate the number of
uppercase letters and lowercase letters. Hint: char.isupper() and char.islower()
Code:
Task #04:
Find the unique values in a list through a function and print Unique list.
Sample List : [1,2,3,3,4,4,5,3,7,5]
Unique List : [1, 2, 3, 4, 5,7]
Code:
Task #05:
Write a Python function to check whether a number is perfect or not.
Example: The first perfect number is 6, because 1, 2, and 3 are its proper positive
divisors, and 1 + 2 + 3 = 6.
Code:
Task #06:
Write a Python function that checks whether a passed string is palindrome or not.
Code:
Task #07:
Write a Python class named Circle constructed by a radius and two methods which will
compute the area and the perimeter of a circle.
Code:
Task #08:
Write a Python class to implement pow(x, n). Do not use built-in math.pow() function for
this code. Also handle this code for all +ve as well as -ve numbers.
Code:
Task #09:
Code:
Conclusion
This lab provided a solid foundation in the basics of Python programming, including functions,
object-oriented programming (OOP), classes, and constructors. We learned how to implement
functions to encapsulate reusable code and how to use OOP to model real-world problems. We
also learned how to use constructors to initialize the state of new objects.
Functions are a powerful tool for organizing code and making it more reusable. We learned how
to define functions with parameters and return values, and how to use functions to pass data
around our programs. We also learned how to write functions that can be nested within other
functions.
OOP is a programming paradigm that allows us to model real-world problems in a more natural
way. We learned how to define classes to represent real-world entities, and how to use objects to
instantiate instances of those classes. We also learned how to use methods to encapsulate the
behavior of our objects.
Constructors are a special type of method that is called when a new object is created. We learned
how to use constructors to initialize the state of new objects and to perform any necessary setup.
The skills that we learned in this lab will be essential for building more complex and
sophisticated Python programs in the future. We are now well-equipped to start applying OOP to
solve real-world problems.