Python is a powerful, high-level and general-purpose programming language. This is used in creating software, web designing, data analytics, creating software prototype. Python is dynamically typed and garbage-collected. It supports integrated programming paradigms, including procedural, object-oriented, and functional programming. Python is often described as a “rich library” language due to its comprehensive standard library which makes developer’s life easy in code. Python language simple syntax and object-oriented approach is focused to help coder write intelligible, insightful code for small and complex projects. Python is simple easy-to-use syntax makes python an excellent language to start for beginners.
Python was fabricated by Guido van Rossum within the late 1980 at Centrum Wiskunde & Informatica (CWI) within the Netherlands as a successor to fundamentals artificial language, that was intended by SETL, capable of exception handling and interfacing with the Amoeba OS. Its implementation began in Dec 1989.
Python interpreters are supported for mainstream operating systems. A International group of programmer develops and maintains CPython, a free and open-source. A not-for-profit organization, the Python Software Foundation, manages and directs resources for Python and CPython development.
Advantage of Python Programming:
- Free to use and open-source programming language.
- Easy to understand because of simple syntax and coding style as compared to other programming language like C++, JAVA and C#.
- Platform independent in nature.
- This programming language can be used as a server side scripting.
- Due to rich library Python is simple to code.
- Python is very much supportable to almost all the database.
- Python is used to handle data analytics and perform complex mathematical calculation.
- Python is a Platform independent means it works for all operating System(Mac, Unix/Linux, Windows, Raspberry Pi, etc).
- Python language syntax is similar to the English language.
- Python has standard library that allows developers to write the complex logic programs with fewer lines of code as compared to other programming language.
- Python debugging and Exception handling is very easy.
Python is entirely object oriented programming language, and not “statically typed”. In python declaration is not needed before using the variable, or type. Every variable in Python is an object. Variables means to assign memory locations for storing values. This means that when we create a variable it assign some space in memory.
Data available in memory is possible to have many type. For example Employee id will be possible to stored in either numeric or alphanumeric but Employee mobile no is always in numeric case. Managing these category python has standard data types for simplicity of programming and the storage method for each of them.
Python is having 5 standard data type:
- Numbers
- String
- List
- Tuples
- Dictionary
Let’s understand each variable in detail:
Numbers:
a = 10
b = -10
Value present in a, b is a form of signed and and non signed “Int” which is also called as Integer.
Python supports four different numerical types −
- int (signed integers) Ex: 10, -10
- long (long integers, they can also be represented in octal and hexadecimal) Ex: 0xDAEBACFE, 76226726L
- float (floating point real values) Ex: a=1.4, 3.3 …
- complex (complex numbers) Ex: a= 10j which is a complex variable
str = 'Hi Rai Gurukul!!'
print str (Prints full string print)
str[1] (Prints 2nd character of the string)
print str[3:9] (Prints characters starting from 4th to 9th print)
str[4:] (Prints string starting from 5th character)
print str * 3 (Prints string 3 times)
print str + “RAI” (Prints concatenated string)
Result:
Hi Rai Gurukul!!
i
ai Guru
ai Gurukul!!
Hi Rai Gurukul!!Hi Rai Gurukul!!Hi Rai Gurukul!!
Hi Rai Gurukul!!RAI
Veg = ( 'Flower', 7 , 2.5, 'Cabbage', 6 , 70.2 )
Veg = [ 'Flower', 7 , 2.5, 'Cabbage', 6 , 70.2 ]
Employee = {'Fname': 'drew','ID':73, 'Team': 'IT'}
Tuple
Dictionary
List
Arrays
Working with HTTP requests is not a simple task in any computer programming language. If you know about Python, it comes with two in-built modules urllib and urllib2 to handle HTTP-related operations. Both the modules come with a different set of functionalities and many times they need to be used together. The main drawback of using urllib is bit confusing because the documentation is not clear and we need to write a lot of code to make even a simple HTTP request. To make these things handy to use a third-party library known as requests. It’s not an in-build module; we install it using the pip command. The requests module is available and most developers prefer to use it instead of urllib and urllib2. It is an Apache2 licensed HTTP library powered by urllib3 and httplib.
The requests module allows us to send HTTP requests using python. And the HTTP request returns a Response Object with all the response data like content, encoding, status, etc. When one makes a request to a URI and it returns the response. Python requests module provides inbuilt functionalities for managing both the request and response. To play with the web the python-requests are a must whether it is hitting APIs, downloading entire Facebook pages, and much more cool stuff one will have to make a request to the URL. The requests module plays a major role in dealing with REST APIs and Web Scraping.
How do we install the requests module in python?
The first step is to install the package using the pip command and module name.
Command:
pip install requests
After the installation, we will use this module’s functionality. There are many functions and
class which are explained below.
To Make Request:
Python requests module has several built-in methods to make HTTP requests to specified URIs using the GET, POST, PUT, DELETE, PATCH, OR HEAD requests.
How we make GET request:
The requests module provides the in-built method called get() for making a GET request to a specified URI. It takes 3 arguments url, parameters, and args. The GET method is used to retrieve information from the given server using a given URI.
Syntax:
requests.get(url, params={key: value}, args)
Example:
import requests r = requests.get("https://api.github.com/") print(r) print(r.content)
Output:
<Response[200]>
b'{\n "current_user_url": "https://api.github.com/user",\n "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",\n "authorizations_url": "https://api.github.com/authorizations",\n "code_search_url": "https://api.github.com/search/code?q= {query}{&page,per_page,sort,order}",\n "commit_search_url": "https://api.github.com/search/commits?q= {query}{&page,per_page,sort,order}",\n "emails_url": "https://api.github.com/user/emails",\n "emojis_url": "https://api.github.com/emojis",\n "events_url": "https://api.github.com/events",\n "feeds_url": "https://api.github.com/feeds",\n "followers_url": "https://api.github.com/user/followers",\n "following_url": "https://api.github.com/user/following{/target}",\n "gists_url": "https://api.github.com/gists{/gist_id}",\n "hub_url": "https://api.github.com/hub",\n "issue_search_url": "https://api.github.com/search/issues?q={query} {&page,per_page,sort,order}",\n "issues_url": "https://api.github.com/issues",\n "keys_url": "https://api.github.com/user/keys",\n "label_search_url": "https://api.github.com/search/labels?q={query}&repository_id= {repository_id}{&page,per_page}",\n "notifications_url": "https://api.github.com/notifications",\n "organization_url": "https://api.github.com/orgs/{org}",\n "organization_repositories_url": "https://api.github.com/orgs/{org}/repos {?type,page,per_page,sort}",\n "organization_teams_url": "https://api.github.com/orgs/{org}/teams",\n "public_gists_url": "https://api.github.com/gists/public",\n "rate_limit_url": "https://api.github.com/rate_limit",\n "repository_url": "https://api.github.com/repos/{owner}/{repo}",\n "repository_search_url": "https://api.github.com/search/repositories?q= {query}{&page,per_page,sort,order}",\n "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}",\n "starred_url": "https://api.github.com/user/starred{/owner}{/repo}",\n "starred_gists_url": "https://api.github.com/gists/starred", \n "user_url": "https://api.github.com/users/{user}",\n "user_organizations_url": "https://api.github.com/user/orgs",\n "user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}",\n "user_search_url": "https://api.github.com/search/users?q= {query}{&page,per_page,sort,order}"\n}\n'