Introduction

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

It is used for:
web development (server-side),
software development,
mathematics,
system scripting.

Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open.

Prerequisites

Not any as such, but if you know basics of programming languages like C, C++ or Java that might come in handy.

Syntax

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code.

Correct syntax:

if 5 > 2:
    print("Five is greater than two!")

Wrong syntax:

if 5 > 2:
print("Five is greater than two!")

Hello World

print("Hello World")

Data types

In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:

  • Text Type: str
    a = "Hello"


    Numeric Types: int, float, complex
    x = 1 # int
    y = 2.8 # float
    z = 1j # complex


  • Sequence Types: list, tuple
    thislist = ["apple", "banana", "cherry"] # list
    thistuple = ("apple", "banana", "cherry") # tuple


  • Mapping Type: dict
    thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }

  • Set Types: set
    thisset = {"apple", "banana", "cherry"}

  • Boolean Type: bool
  • Binary Types: bytes, bytearray, memoryview

To know datatype of variable x:
print(type(x))

And More

Loops

The while Loop :

With the while loop we can execute a set of statements as long as a condition is true.

For example: Print i as long as i is less than 6-
i = 1
while i < 6:
    print(i)
    i += 1

The for Loop :

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

For example: Print each fruit in a fruit list-
fruits = ["apple", "banana", "cherry"]
for x in fruits: < 6:
    print(x)

And More Loops

Functions

A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.

For example:
def my_function():
    print("Hello from a function")

my_function()

Classes and Objects

Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.

For example: Create a class named MyClass, with a property named x:
class MyClass:
    x = 5

Now we can use the class named MyClass to create objects:
p1 = MyClass()
print(p1.x)

Fetch Internet Resources Using The urllib Package

urllib.request is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function.

The simplest way to use urllib.request is as follows:

import urllib.request with urllib.request.urlopen('http://python.org/') as response: html = response.read()

References