CBSE CS and IP

CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Showing posts with label Class 12 IP. Show all posts
Showing posts with label Class 12 IP. Show all posts

Python Pandas - Series Attribute

Python Pandas - Series Attribute


Attributes are the properties of any object. Here we will discuss all the Series attributes with programming examples. All the important Series attributes according to the CBSE Class 12 Informatics practices syllabus are given below in the table:-


Attributes Description
Series.index Range of the index (axis labels) of the Series.
Series.values Return Series as ndarray or ndarray like depending upon dtype
Series.dtype Return the dtype object of the underlying data.
Series.shape Return a tuple of the shape of the underlying data.
Series.nbytes Return the number of bytes in the underlying data.
Series.ndim The number of dimensions of the underlying data, by definition 1.
Series.size Return the number of elements in the underlying data.
Series.hasnans Return if I have any nans; enables various perf speedups.
Series.empty Return true if Series is empty
at, iat To access a single value from Series
loc, iloc To access slices from Series


Let us now check all the attribute with programming example. We will consider the following Series Student and check all the attributes on this Series Student.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import pandas as pd
student = pd.Series(["Sonal", "Rahul", "Mohan", "Siya",])
print(student)

'''
Output:
0    Sonal
1    Rahul
2    Mohan
3     Siya
dtype: object
'''

1. Series.index
This attribute is used to get the range of the index (axis labels) of the Series. Let us try this function on the student Series.
1
2
>>> student.index
RangeIndex(start=0, stop=4, step=1)

2. Series.values
values attribute returns Series as ndarray or ndarray like depending upon dtype.
1
2
>>> student.values
array(['Sonal', 'Rahul', 'Mohan', 'Siya'], dtype=object)

3. Series.dtype
dtype attribute is used to check the data type of the Series Object. Since the student series is of object type, below output is showing 'o'.
1
2
>>> student.dtype
dtype('O')

4. Series.shape
shape attribute gives the shape of the underlying data structure in the form of a tuple. Since the student Series is having 4 elements the output is (4,).
1
2
>>> student.shape
(4,)
5. Series.nbytes
nbyte attribute gives the total number of bytes taken by the Series object to store the data. The below-given output tells that the student object takes 32 bytes of memory.
1
2
>>> student.nbytes
32

6. Series.ndim
ndim gives the dimension of the underlying data structure. Since series is a 1-D data structure, for all series object it gives 1.
1
2
>>> student.ndim
1

7. Series.size
size gives the total number of elements in the series. Since the student series has 4 elements size will give 4.
1
2
>>> student.size
4

8. Series.hasnans
hasnans returns Boolean value. If any of the series elements is NaN it will return True. Otherwise false.
1
2
>>> student.hasnans
False

9. Series.empty
empty attribute returns Boolean True if Series is empty, otherwise the output will be False.
1
2
>>> student.empty
False

10. at, iat
We will discuss at and iat in detail in our upcoming Post. You can click here to go to the post.

11. loc, iloc
We will discuss loc and iloc in detail in our upcoming Post. You can click here to go to the post.




head() and tail() functions of Series Object

head() and tail() functions of Series Object

Pandas Series provides two very useful methods for extracting the data from the top and bottom of the Series Object. These methods are head() and tail().

head() Method

head() method is used to get the elements from the top of the series. By default, it gives 5 elements.

Syntax:
<Series Object> . head(n = 5)

Example:
Consider the following Series, we will perform the operations on the below given Series S.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import pandas as pd
s = pd.Series({'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8})
print(s)

Output:
A    1
B    2
C    3
D    4
E    5
F    6
G    7
H    8
dtype: int64


head() Function without argument

If we do not give any argument inside head() function, it will give by default 5 values from the top.
1
2
3
4
5
6
7
8
9
s.head()
Output:
A    1
B    2
C    3
D    4
E    5
dtype: int64


head() Function with Positive Argument

When a positive number is provided, the head() function will extract the top n rows from Series Object. In the below given example, I have given 7, so 7 rows from the top has been extracted.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
s.head(7)
Output:
A    1
B    2
C    3
D    4
E    5
F    6
G    7
dtype: int64


head() Function with Negative Argument

We can also provide a negative value inside the head() function. For a negative value, it will check the index from the bottom and provide the data from the top.
1
2
3
4
5
s.head(-7)

Output:
A    1
dtype: int64

tail() Method

tail() method gives the elements of series from the bottom.

Syntax:
<Series Object> . tail(n = 5)

Example:
Consider the following Series, we will perform the operations on the below given Series S.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import pandas as pd
s = pd.Series({'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8})
print(s)

Output:
A    1
B    2
C    3
D    4
E    5
F    6
G    7
H    8
dtype: int64


tail() function without argument

If we do not provide any argument tail() function gives be default 5 values from the bottom of the Series Object.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
s.tail()
'''
Output:
D    4
E    5
F    6
G    7
H    8
dtype: int64
'''

tail() function Positive argument

When a positive number is provided tail() function given bottom n elements of the Series Object.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
s.tail(7)
'''
Output:
B    2
C    3
D    4
E    5
F    6
G    7
H    8
dtype: int64
'''

tail() function Negative argument

When a negative number is provided it will give the data as follows:

1
2
3
4
5
6
s.tail(-7)
'''
Output:
H    8
dtype: int64
'''



Mathematical operations on Pandas Series

Mathematical operations on Pandas Series



  1. You can perform arithmetic operations like addition, subtraction, division, multiplication on two Series objects.
  2. The operations are performed only on the matching indexes.
  3. For all non-matching indexes, NaN (Not a Number) will be returned

Note:-  Like NumPy array, series support vector operations. Batch operations on data without writing any for loops. This is usually called vectorization.

Let us consider the following two Series S1 and S2. We will perform mathematical operations on these Series.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import pandas as pd
s1 = pd.Series(data = [ 1 , 2 , 3 , 4 , 5 ], index = [ 'A' , 'B' , 'C' , 'D' , 'E' ])
s2 = pd.Series(data = [ 1 , 2 , 3 , 4 , 5 ], index = [ 'B' , 'C' , 'D' , 'E' , 'F' ])
print(s1)

A 1
B 2
C 3
D 4
E 5
dtype: int64

print(s2)

B 1
C 2
D 3
E 4
F 5
dtype: int64


Now let us perform mathematical operations on these two Series.
  1. Addition:  We can use the '+' Operator or add() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    >>> s1 + s2                                                 
    A    NaN
    B    3.0
    C    5.0
    D    7.0
    E    9.0
    F    NaN
    dtype: float64
    
    >>> s1.add(s2)
    A    NaN
    B    3.0
    C    5.0
    D    7.0
    E    9.0
    F    NaN
    dtype: float64
     
    

  2. Subtraction: We can use the '-' Operator or sub() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    >>> s1 - s2
    A    NaN
    B    1.0
    C    1.0
    D    1.0
    E    1.0
    F    NaN
    dtype: float64
    
    >>> s1.sub(s2)                                      
    A    NaN
    B    1.0
    C    1.0
    D    1.0
    E    1.0
    F    NaN
    dtype: float64
    

  3. Division: We can use the '/' Operator or div() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    >>> s1 / s2
    A         NaN
    B    2.000000
    C    1.500000
    D    1.333333
    E    1.250000
    F         NaN
    dtype: float64                                         
    
    >>> s1.div(s2)
    A         NaN
    B    2.000000
    C    1.500000
    D    1.333333
    E    1.250000
    F         NaN
    dtype: float64
    

  4. Multiplication: We can use the '*' Operator or mul() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    >>> s1*s2
    A     NaN
    B     2.0
    C     6.0
    D    12.0
    E    20.0
    F     NaN
    dtype: float64                                      
     
    >>> s1.mul(s2)
    A     NaN
    B     2.0
    C     6.0
    D    12.0
    E    20.0
    F     NaN
    dtype: float64
    



Network Topologies in Computer Network

 

network topology
The term Network Topology defines the Geographic, Physical, or logical arrangement of computer and networking devices.
The pattern of the interconnection of nodes on a network is called the topology.

FACTORS for choosing a Topology of a network are:-
  • Cost: which offers minimum installation cost based on the network under consideration.
  • Flexibility: Can offer easy move of existing nodes and adding new ones.
  • Reliability: Offers least failure.
Types of topology

BUS TOPOLOGY-:

  • It consists of one continuous length of cable (trunk) that is shared by all the nodes in the network.
  • It has a terminating resistor (terminator) at each end that absorbs the signal when it reaches the end of the line.
bus topology
Advantage -:
  1. Easy to connect a computer.
  2. Requires less cable length.
  3. Failure of one node does not affect the network functioning. 
Disadvantage -:
  1. The entire network shuts down if there is a break in the main cable.
  2. Terminators are required at both ends.
  3. Difficult to identify the problem.
  4. Nodes must be intelligent.

RING TOPOLOGY -:

  • Each node has two neighboring nodes.
  • Data packet is received from one neighboring and is transmitted to the next.
  • Data travels in one direction in the ring-like structure.
Ring topology

Advantage -:
  1. Short cable length
  2. The possibility of collision is minimum

Disadvantages -:
  1. One node causes network failure
  2. It is very difficult to diagnose faults
  3. Network reconfiguration is difficult.

STAR TOPOLOGY -:

  • Each node connected directly to a central network hub or concentrator.
  • Data passes through the hub or concentrator before continuing.
  • The hub or concentrator manages and controls all functions of the network.
Star topology

Advantage -:
  1. Easy to install.
  2. No disruptions to the network when connecting or removing devices.
  3. Easy to detect faults and to remove parts.
Disadvantage -:
  1. Requires more cable length than a BUS/RING topology.
  2. If the hub or concentrator fails, the nodes attached are disabled.
  3. More expensive than linear bus topologies because of the cost of the concentrators.

MESH TOPOLOGY -:

  • Each node is connected to another Node.
mesh topology

Advantage -:
  1. It is robust.
  2. Provides security and privacy.
  3. The fault is diagnosed easily.
Disadvantage -:
  1. The cost to implement is higher than other network topologies.
  2. Installation and configuration are difficult.

TREE TOPOLOGY -:

  • A tree topology combines characteristics of linear BUS and STAR topologies.
  • Inverted tree-like structure.
  • It consists of groups of star-configured workstations connected to a linear bus backbone cable.
tree topology

Advantage -:
  1. Point-to-point wiring for individual segments
  2. It is highly flexible
  3. Centralized monitoring 
Disadvantage -:
  1. If the backbone line breaks, the entire segment goes down.
  2. It is difficult to configure the network if there is a single point of failure.
  3. More wire is required than other topologies.

HYBRID TECHNOLOGY -:

  • A hybrid topology is an integration of two or more different topologies to form a resultant topology.
Hybrid topology














Computer Network Devices

 

Computer Network Devices

Introduction -

Specialized hardware is required to carry out various roles in a network e.g. Establishing connections, controlling network traffic, and many more.
Network Devices


NIC (Network Interface card) -

It is a device attached to each workstation and server. Helps to make connections within the network.
Each NIC has a unique number identifying it called node address/MAC address (Media Access Control) /Physical Address.
Types:
  1. Ethernet Card
  2. WiFi Card

RJ - 45 -:

  • RJ-45 stands for Registered Jack-45.
  • RJ-45 is an eight-wire connector, which is commonly used to connect computers on LAN especially Ethernet.

MODEM (Modulator - Demodulator) -:

  • The modem is an abbreviation for Modulator – Demodulator. 
  • The modulator converts information from digital mode to analog mode at the transmitting end and the demodulator converts the same from analog to digital at receiving end.
MODEM


REPEATER - 

  • It is a device that accepts the weak signals and amplifies the signals for further transmission.
  • Different types of wires have different transmission distances, hence repeater should be used according to the types of wire.
  • Generally, we consider that most wires require a repeater after a 100m distance.
REPEATER


HUB - :

  • Hub is an electronic device that connects several nodes to form a network and redirect the received information to all the nodes in a broadcast mode. It is called a non-intelligent device. 
  • It is mostly used in Star or Ring topology and works on MAC addresses. 

Types:

Active hub:- electrically amplify the signal.
Passive hub:- do not amplify the data.
HUB

BRIDGE - :

  • In telecommunication networks, a bridge is a product that connects a local area network (LAN) to another local area network that uses the same protocol.
  • The bridge acts on MAC/Physical Address.
  • A bridge examines each message on a LAN, "passing" those known to be within the same LAN, and forwarding those known to be on the other interconnected LAN (or LANs).
BRIDGE


SWITCH - :

  • It is an intelligent device that connects several nodes to form a network and redirect the received information only to the intended node(s).
  • A switch is also called Intelligent HUB.
  • Switch stores the MAC address table of all the connected nodes, which helps it to send the data to the desired node.
SWITCH


ROUTER - :

The router works on IP address.
Routers normally connect LANs and WANs together.
It has a dynamically updating routing table (stores IP Addressed) based on which they make decisions on routing the data packets.
ROUTER


GATEWAY - :

  • Gateway is a device that connects dissimilar networks.
  • Establishes an intelligent connection between a local network and external networks with completely different structures.
  • Gateway is the ISP(Internet Service Provider) that connects the user to the internet. The gateway can also act as Firewall, API Gateways, etc.
GATEWAY