Operators: Boolean

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

GitHub Course Summary

Operators
Boolean
A Boolean data type represents one of two values: _True_ or _False_. The first letter (T) for True and (F) for False
should be capital.

Example: Boolean Values:

Operators
Python language supports several types of operators. In this section, we will focus on few of them.

Assignment Operators
Assignment operators are used to assign values to variables. Let us take = as an example. Equal sign in mathematics
shows that two values are equal, however in Python it means we are storing a value in a certain variable and we call
it assignment or a assigning value to a variable. The table below shows the different types of python assignment
operators, taken from [w3school](https://www.w3schools.com/python/python_operators.asp).

Arithmetic Operators:
- Addition(+): a + b
- Subtraction(-): a - b
- Multiplication(*): a * b
- Division(/): a / b
- Modulus(%): a % b
- Floor division(//): a // b
- Exponentiation(**): a ** b

Example: Integers
GitHub Course Summary

Example: Floats

Example: Complex numbers

Let's declare a variable and assign a number data type. I am going to use single character variable but remember do
not develop a habit of declaring such types of variables. Variable names should be all the time mnemonic.

Pandas
Pandas is an open-source, high-performance, easy-to-use data structure, and data analysis tool for the Python
programming language.

Pandas add data structures and tools designed to work with table-like data which is *Series* and *Data
Frames*.

Pandas provide tools for data manipulation:

- reshaping
- merging
- sorting
- slicing
- aggregation
- imputation.

If you are using anaconda, you do not have to install pandas.


GitHub Course Summary

Installing Pandas
For Mac: For Windows:
pip install conda pip install conda
conda install pandas pip install pandas

 Pandas data structure is based on *Series* and *Data Frames*.


 A *series* is a *column*
 A Data Frame is a *multidimensional table* made up of a collection of *series*.
 In order to create a Pandas series we should use NumPy to create a one-dimensional array or a python list.
 Pandas series is just one column of data. If we want to have multiple columns, we use data frames. The
example below shows pandas Data Frames.
 Data frame is a collection of rows and columns.

Importing Pandas
import pandas as pd

Creating Pandas Series with Default Index

p = [1, 2, 3, 4, 5]
s = pd.Series(p)
print(s)

Creating Pandas Series with custom index


p = [1, 2, 3, 4, 5]
s = pd.Series(p, index = [1,2,3,4,5])
print(s)

Numbering
GitHub Course Summary

Creating Pandas Series from a Dictionary

Creating a Constant Pandas Series

Creating a Pandas Series Using Linspace

s = pd.Series(np.linspace(5, 20, 10)) # linspace(starting, end, items)


print(s)

Data Frames(df)
Pandas data frames can be created in different ways.

Classes and Objects


Python is an object-oriented programming language. Everything in Python is an object, with its properties and
methods. A number, string, list, dictionary, tuple, set etc. used in a program is an object of a corresponding built-in
class. We create class to create an object. A class is like an object constructor, or a "blueprint" for creating objects.
We instantiate a class to create an object. The class defines attributes and the behavior of the object, while the
object, on the other hand, represents the class.
GitHub Course Summary

Creating a Class
To create a class, we need the key word **class** followed by the name and colon. Class name should be
**CamelCase**.

Syntax

Example

Creating an Object
We can create an object by calling the class.

Class Constructor
In the examples above, we have created an object from the Person class. However, a class without a constructor is
not useful in real applications. Let us use constructor function to make our class more useful. Like the constructor
function in Java or JavaScript, Python has also a built-in init() constructor function. The init constructor
function has self parameter which is a reference to the current instance of the class.

Example

Let us add more parameters to the constructor function.


GitHub Course Summary

Object Methods
Objects can have methods. The methods are functions which belong to the object.

Object Default Methods


Sometimes, you may want to have a default value for your object methods. If we give default values for the
parameters in the constructor, we can avoid errors when we call or instantiate our class without parameters. Let's
see how it looks:

Method to Modify Class Default Values


In the example below, the person class, all the constructor parameters have default values. In addition to that, we
have skills parameter, which we can access using a method. Let us create add_skill method to add skills to the
skills list.
GitHub Course Summary

Inheritance
Using inheritance we can reuse parent class code. Inheritance allows us to define a class that inherits all the methods
and properties from parent class. The parent class or super or base class is the class which gives all the methods and
properties. Child class is the class that inherits from another or parent class.

Create a student class by inheriting from person class.

You might also like