Object – Oriented Programming in Python-1-1
Object – Oriented Programming in Python-1-1
• What is API ?
• What is Web API?
• Advantages of using a Web API.
• Requests Library(requests library).
• Git and GitHub Repositories.
What is API?
• API – (Application Programming interface)
• API is a set of protocols , routines, and tools for building software and applications.
• It specifies how software components should interact and API’s allow
communication b/w different systems or platforms.
• API acts as an intermediary between two systems.
API Key:
• API Key acts as a secret code that identifies the calling program and grants access
to the API’s data and functionality.
• The use of API key helps to ensure that the API is used only by authorized clients
and helps to maintain the security and integrity of the data and services provided
by the API.
Requests Library(requests):
• It is a python library used to send http requests.
• It is designed to provide a simple and intuitive interface for making http requests.
• The basic use of requests involves sending an http requests to specified URL , and
processing the response that is received from the server.
• This process can be done by using requests. get method for making GET requests,
or the requests.post method for making POST requests.
• commands to install requests library pip install requests.
• Ex: https://api.genderize.io offers an API that determines the gender, to utilize the
API, the name is passed as a parameter in the url.
URL : https://api.genderize.io/?name=Chandhana
URL : https://api.apilayer.com/fixer/convert?
to={to_currency}&from={from_currency}&amount={amount}&apikey={api_ke
y}
• The API then returns the results in JSON format, including information such as the
name, gender and a probability score.
• An example of the API’s response, When the name “Chandhana ” is passed.
• Response : {"count":26,"name":"chandhana","gender":"female","probability":0.54}
This program uses web API to find the gender based on name.
import requests
Access Modifiers: It is used to limit the access to the variable and methods of a class.
python provides three types of access modifiers such as public, private and protected.
Public : By default, all attributes and methods are public and can be accessed from
anywhere in the code.
Private : To make an attribute or method private in python, prefix its name with two
underscore (__). Private properties can only be accessed within the same class and not
accessible from outside the class.
Protected : we use single underscore (_). Protected properties can’t be accessed from
outside the class, but can still be accessed within subclasses.
class Person:
def _int_(self, name, age, sal):
self.name = name #public member, can be accessed from outside the class.
self._age = age #protected member, used within the class and its subclasses.
self.__salary = sal # private member, accessed within the same class.
def display_details(self):
print(“Name:”, self.name)
print(“Age:”, self._age)
print(“Salary:”, self.__salary)