52 Questions
What is the purpose of a class in Python?
A class is a collection of objects, representing a logical entity that contains attributes and methods.
How are attributes accessed in a Python class?
Attributes are accessed using the dot (.) operator.
What is the purpose of the init function in Python classes?
The init function is used as a constructor method which is automatically called when a new object is created. It is responsible for initializing the attributes of the class.
What is the significance of the self parameter in Python classes?
The self parameter refers to the instance of the class itself and is a reference to the current object. It is used to access variables and methods within the class.
Describe the purpose of the str function in Python classes.
The str function is used to define a printable string representation of an object. It provides a way to control how an object is represented as a string.
To create an object in Python, the syntax involves using ______.
class_name()
Which keyword is used to create a new class in Python?
class
The init function in Python classes is optional.
False
What parameter should be added in the init() function of the Student class according to Experiment #5?
year
What does the + operator do on string data types?
Concatenation
What method is added to the Student class in Experiment #5?
welcome
If a method in the child class has the same name as a function in the parent class, will the inheritance of the parent method be overridden?
True
What is the result of the multiplication operation: $10 * 5$?
50
What is the result of the multiplication operation on a number and a string: $3 * 'Python'$?
PythonPythonPython
In Experiment #1 of Python Inheritance, what type of inheritance is demonstrated when a derived class inherits from only one base class? [Single or ______]
Single inheritance
What type of inheritance is depicted when a child class inherits from multiple parent classes?
Multiple inheritance
Which method in Python involves redefining certain methods and attributes to fit a child class?
Method Overriding
To create a new file in Python, the 'open()' method is used with the parameter '____'.
x
Match the Python inheritance types with their descriptions:
Single inheritance = Derived class inherits from only one base class Multiple inheritance = Derived class inherits from multiple parent classes Multilevel inheritance = Derived class inherits another derived class Hierarchical inheritance = More than one derived class created from a single base Hybrid inheritance = Combines more than one inheritance type
What is the key function for working with files in Python?
open
What is another term for operator overloading in Python?
Polymorphism
Match the file opening modes in Python with their descriptions:
r = Opens a file for reading a = Opens a file for appending w = Opens a file for writing x = Creates the specified file, returns an error if the file exists
Polymorphism in Python allows you to execute different operations with the same operator depending on the operands. (True/False)
True
What function in Python can be used with different data types and estimates an object's length?
len
In Python, what is the purpose of a getter method?
To retrieve the value of a private attribute
Abstraction in object-oriented programming helps reduce complexity and increase efficiency.
True
What is the abbreviation for 'Numerical Python'?
NumPy
What is a 0-D array also known as?
Scalar
What is the key attribute in NumPy Arrays to determine the number of dimensions?
ndim
How do you access the first element of an array in Python using NumPy?
arr[0]
What does negative indexing refer to in NumPy Arrays?
Accessing an array from the end using negative numbers
How do you specify a step when slicing elements in a NumPy Array?
arr[start:end:step]
What is the purpose of the 'random' module in NumPy?
To work with random numbers
In Pandas, what does a Series represent?
A column in a table
What method is used in Pandas to return specified row(s) from a DataFrame?
loc
What is the primarily used library for plotting in Python mentioned in the content?
Matplotlib
Which submodule of Matplotlib is usually imported under the alias plt?
pyplot
What is the function used in Matplotlib to draw points in a diagram?
plot()
Matplotlib's plot() function by default draws individual points without connecting them with lines.
False
Matplotlib's plot() function takes two arrays as parameters specifying the points on the ___ and the ___.
x-axis, y-axis
How can you pull the 'Apples' wedge from the center of the pie using Matplotlib?
By setting the explode parameter to 0.2
True or False: You can add a shadow to a pie chart in Matplotlib by setting the shadows parameter to True.
True
Which parameter can be used to set the colors of each wedge in a pie chart?
colors
You can use __ values, color names, or shortcuts like 'r', 'g', 'b' to specify colors in Matplotlib.
Hexadecimal color
Match the following color shortcuts with their corresponding colors:
'r' = Red 'g' = Green 'b' = Blue 'c' = Cyan 'm' = Magenta 'y' = Yellow 'k' = Black 'w' = White
How can you add a legend to a pie chart in Matplotlib?
By using the legend() function
True or False: A title parameter can be added to the legend function in Matplotlib to include a header.
True
What function is used in Matplotlib to set a label for the x-axis?
xlabel()
What function is used in Matplotlib to set a label for the y-axis?
ylabel()
What can be specified using the fontdict parameter in the xlabel() and ylabel() functions?
All of the above
The loc parameter is used with the xlabel() function in Matplotlib.
False
The bar() function takes the keyword argument ______ to set the color of the bars.
color
Study Notes
Al-Zaytoonah Private University of Jordan
Lab Manual Overview
- The lab manual is designed to teach students about Object-Oriented Paradigm (OOP) using Python.
- Students will learn about encapsulation, inheritance, and polymorphism.
Required Tools and Resources
- Pycharm is the required tool.
Rules and Policies
- Rules for lab sessions:
- Attend lab sessions on time.
- Bring your notebook and laptop.
- No food or beverages allowed in the lab.
- Interact with lab supervisors and ask questions.
- Maintain silence and discipline.
- Log in to your computer using your student account.
- Complete lab exercises.
- Clean up after lab sessions.
- Attendance policy:
- Attendance is required.
- Absence from lab sessions is recorded.
- Absence warning system is in place.
- Grading policy:
- Lab supervisor records progress during lab sessions.
- Assignments are submitted.
- Final lab grade is based on overall performance.
Learning Outcomes
- ILO1-k: Understand advanced topics in object-oriented programming in Python.
- ILO2-k: Knowledge of Python programming language structure and model.
- ILO3-s: Ability to use Python libraries for artificial intelligence and machine learning.
- ILO4-s: Write Python programs using NumPy, matplotlib, and pandas.
- ILO5-c: Implement programs using OOP concepts.
- ILO6-c: Write programs using common libraries for AI.
Exercise 1: Python Classes and Objects
- Class definition syntax:
class class_name: ...
- Attributes are variables that belong to a class.
- Attributes are public and can be accessed using the dot (.) operator.
- ClassDocstring: a brief description of the class.
- Creating an object:
my_object = MyClass()
- State, behavior, and identity of an object: example of a class Dog.
Exercise 2: The __init__()
Function, The self
Parameter, and The __str__()
Function
- Method: a function associated with an object.
-
self
is a pointer to the current object. -
__init__()
function: initializes the object's attributes. -
__str__()
function: returns a string representation of the object. - Experiment 1: creating a Person class with
__init__()
andmyfunc()
methods. - Experiment 2: using the
__str__()
function. - Experiment 3: using
mysillyobject
andabc
instead ofself
.
Exercise 3: Accessing Attributes and Methods
- Attributes and methods are accessed using the dot (.) operator.
- Modifying properties on objects:
my_object.age = 40
- Deleting object properties:
del my_object.age
- Deleting objects:
del my_object
- The
pass
statement: when a class definition has no content.
Exercise 4: Python Inheritance
- Inheritance: a class that inherits all the methods and properties from another class.
- Parent class: the class being inherited from.
- Child class: the class that inherits from another class.
- Syntax:
class ChildClass(ParentClass): ...
- Experiment 1: creating a
Person
class and aStudent
class that inherits fromPerson
. - Experiment 2: using the
super()
function. - Experiment 3: adding properties and methods to the
Student
class.
Exercise 5: Different types of Python Inheritance
- Single inheritance: a child class inherits from only one parent class.
- Multiple inheritances: a child class inherits from multiple parent classes.
- Multilevel inheritance: a child class inherits from a parent class that inherits from another class.
- Hierarchical inheritance: more than one derived class is created from a single base class.
- Hybrid inheritance: a combination of multiple types of inheritance.
Exercise 6: Python Polymorphism:1
- Polymorphism: methods/functions/operators with the same name that can be executed on many objects or classes.
- Class polymorphism: methods with the same name in different classes.
- Experiment 1: different classes with the same method:
Car
,Boat
, andPlane
classes withmove()
methods. - Experiment 2: inheritance class polymorphism.### Classes and Objects
- A class called
Vehicle
can be created with child classesCar
,Boat
, andPlane
that inherit its properties and methods. - The
Car
class can be empty, but it still inheritsbrand
,model
, andmove()
fromVehicle
. - The
Boat
andPlane
classes also inheritbrand
,model
, andmove()
fromVehicle
, but they override themove()
method. - Polymorphism allows for executing the same method for all classes, even if they have different implementations.
Polymorphism
- Polymorphism is the ability of an object to take on multiple forms.
- It allows for different classes to respond to the same method call.
- Polymorphism can be achieved through method overriding, where a child class provides a different implementation of a method inherited from its parent class.
Operator Overloading
- Operator overloading is a type of polymorphism where the same operator performs different operations depending on the operands.
- Examples of operator overloading include:
- The
+
operator, which can be used for arithmetic addition, string concatenation, or list extension. - The
*
operator, which can be used for multiplication or string repetition.
- The
File Handling
- The
open()
function is used to create a new file or open an existing file. - The
open()
function takes two parameters: the filename and the mode (e.g.,r
for read,w
for write,a
for append,x
for create). - The
open()
function can be used with various modes, such asr
for reading,w
for writing, anda
for appending. - The
close()
function is used to close a file.
Encapsulation
- Encapsulation is the concept of hiding the implementation details of an object from the outside world.
- Encapsulation is achieved through the use of private and public members.
- Private members are denoted by a single underscore (
_
) and can only be accessed within the class. - Public members are denoted by no underscore and can be accessed from outside the class.
- Getter and setter methods are used to access and modify private members.
- The
@property
decorator is used to define a getter method. - The
@property_name.setter
decorator is used to define a setter method.
Abstraction
- Abstraction is the concept of showing only the necessary information to the outside world while hiding the implementation details.
- Abstraction is achieved through the use of abstract classes and interfaces.
- An abstract class is a class that cannot be instantiated and is used as a base class for other classes.
- An abstract class must have at least one abstract method.
- An abstract method is a method that is declared but not implemented.
- A concrete class is a class that provides an implementation for all the abstract methods of its parent class.
NumPy Modules
- NumPy is a Python library used for working with arrays.
- NumPy is used to provide an array object that is up to 50x faster than traditional Python lists.
- The array object in NumPy is called
ndarray
. - NumPy can be installed using pip:
pip install numpy
. - NumPy can be imported using
import numpy as np
. - NumPy arrays can be created using the
array()
function. - NumPy arrays can be 0-D, 1-D, 2-D, or 3-D.
- The
ndim
attribute returns the number of dimensions of a NumPy array. - Array indexing is used to access elements of a NumPy array.
- Accessing elements of a 2-D or 3-D array requires using comma-separated integers representing the dimension and the index of the element.Here are the study notes in detailed bullet points:
- Experiment #14: Accessing the third element of the second array of the first array*
- Use
arr[0, 1, 2]
to access the third element of the second array of the first array - The first number represents the first dimension, which contains two arrays
- The second number represents the second dimension, which also contains two arrays
- The third number represents the third dimension, which contains three values
- Negative Indexing*
- Use negative indexing to access an array from the end
- Example:
arr[1, -1]
returns the last element of the 2nd dimension - NumPy Array Slicing*
- Slicing in Python means taking elements from one given index to another given index
- Use
[*start*:*end*]
to specify the range - Use
[*start*:*end*:*step*]
to specify the step - If you don't pass
start
, it's considered 0 - If you don't pass
end
, it's considered the length of the array in that dimension - If you don't pass
step
, it's considered 1 - Experiment #1-#8: Array Slicing*
- Examples of slicing:
-
arr[1:5]
returns elements from index 1 to 5 (excluding 5) -
arr[4:]
returns elements from index 4 to the end -
arr[:4]
returns elements from the beginning to index 4 (excluding 4) -
arr[1:5:2]
returns every other element from index 1 to 5 (excluding 5)
-
- Generate Random Number*
- Use
from numpy import random
to work with random numbers - Example:
x = random.randint(100)
generates a random integer from 0 to 100 - Pandas Tutorial*
- Pandas is a Python library for data analysis
- Pandas can:
- Analyze data
- Delete rows that are not relevant or contain wrong values
- Clean data
- The source code for Pandas is located on GitHub
- Experiment #1-#10: Pandas*
- Examples of using Pandas:
- Creating a DataFrame from a dictionary
- Creating a Series from a list
- Adding labels to a DataFrame
- Locating rows and columns in a DataFrame
- Loading files into a DataFrame
- Plotting data using Matplotlib
- Matplotlib*
- Matplotlib is a low-level graph plotting library in Python
- Matplotlib is used for visualization
- Examples of using Matplotlib:
- Plotting x and y points
- Plotting without lines
- Using markers (e.g., 'o' for circle, '*' for star)
- Using formats (e.g., 'o:r' for circle marker with red color)
- Setting marker size and color
- Setting line style and color
- Using hexadecimal color values and color names
Quiz about the basics of Python classes, objects, and their attributes. Understand the purpose of the __init__ and __str__ functions, and how to access attributes in Python classes.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free