Path: blob/master/Python Basics/DAY 1 Basic Python.ipynb
3074 views
What is python and why it is so popular ?
Python is one among the most popular dynamic programming languages that is being used today. Python is an open-source and object-oriented programming language developed by Dutchman Guido van Possum in 1980s. This language can be utilized for a wide range of applications like scripting, developing and testing. Due to its elegance and simplicity, top technology organizations like Dropbox, Google, Quora, Mozilla, Hewlett-Packard, Qualcomm, IBM, and Cisco have implemented Python.
Several websites state that Python is one among the most famous programming language of 2016. Because of its implementation and syntax, it pressures more on code readability. When compared to other programming languages like C++ and Java, it requires the programmer to develop lesser codes. It offers automatic memory management and several standard libraries for the programmer. Once a programmer completes Python certification training, he can gain knowledge and experience in a wide range of top IT organizations. It is a general-purpose and high-level coding language.
Because of its features, a large number of programmers across the world, showing interest in making use of this language to develop websites, GUI applications, and mobile applications. The main reason that brings Python one among the top coding languages is that it allows the developers to figure out the concepts by developing readable and less code. Several advantages of Python supports the programmers to alleviate the effort as well as the time required for developing complex and large applications.
1. Using the Interpreter
Interpreter : An interpreter is a program that reads and executes code. This includes source code, pre-compiled code, and scripts. So if we talk about python interpreter it will execute the code in pytyhon by taking single code line at a time.
Scripting : Any program written in python is called a script.The interpreter reads and executes each line of code one at a time, just like a SCRIPT for a play or an audition, hence the the term "scripting language". Python uses an interpreter to translate and run its code and that's why it's called a scripting language
Python Program Executing Model & Architecture
A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a command line argument to the interpreter) is a code block. A script command (a command specified on the interpreter command line with the -c option) is a code block. The string argument passed to the built-in functions eval() and exec() is a code block.
A code block is executed in an execution frame. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed.
A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name.
Run a Python Program
Installation
The following link provides the complete installation guide for python basic IDLE
Anaconda & Jupyter notebook
Anaconda is a free and open-source distribution of the Python and R programming languages for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), that aims to simplify package management and deployment. Package versions are managed by the package management system conda.The Anaconda distribution is used by over 13 million users and includes more than 1400 popular data-science packages suitable for Windows, Linux, and MacOS.
The following link will guide you to anaconda interface and download details: https://www.anaconda.com/
2.Python Scripting and Place Holders
INDENTATION
Input
Object Types
Built-in Types
Approximately two dozen types are built into the Python interpreter and grouped into a few major categories, as shown :
If Else
Sequence type & Assignment
Sequences represent ordered sets of objects indexed by nonnegative integers and include strings, Unicode strings, lists, and tuples. -Strings are sequences of characters -
lists and tuples are sequences of arbitrary Python objects.
Strings and tuples are immutable; lists allow insertion, deletion, and substitution of elements.
All sequences support iteration.
Mutable vs Immutable Objects
Operation & Methods applicable to all sequences
Operations & Methods applicable to Mutable Sequence
List
Mapping Types objects : Dictionary
A mapping object represents an arbitrary collection of objects that are indexed by another collection of nearly arbitrary key values. Unlike a sequence, a mapping object is unordered and can be indexed by numbers, strings, and other objects. Mappings are mutable.
You can use any immutable object as a dictionary key value (strings, numbers, tuples, and so on). Lists, dictionaries, and tuples containing mutable objects cannot be used as keys (the dictionary type requires key values to remain constant).
Dictionary Methods & Operations
Set Types Objects
A set is an unordered collection of unique items. Unlike sequences, sets provide no indexing or slicing operations. They are also unlike dictionaries in that there are no key values associated with the objects. In addition, the items placed into a set must be immutable.
Two different set types are available: set is a mutable set, and frozenset is an immutable set.
Methods & Operations for all set types
Methods & Operations for mutuable set types¶
List Methods
List iteration
Membership Statements
Membership operators are operators used to validate the membership of a value. It test for membership in a sequence, such as strings, lists, or tuples.
Multi Target Assignment
Day 1
-4. Python String Types(2hrs)
Day 1
String Manipulation
Type Conversion in Python
XML Parsing
Json Parsing
API Invocation (REST, SOAP)
HTTP request – Request – Response – Parse Output
SSH, Paramiko, FTP, Mail sending (SMTP)
String Methods & Functions
Type Conversion
Converting one sequence type to other.
XML Parsing
XML can be parsed in python using the xml.etree.ElementTree library.
While this library is easy to use, it loads the whole XML document into memory and hence may not be suitable for processing large XML files or where run-time memory is an issue.
Example Parsing an XML File
It is very simple to parse an XML using ElementTree. The following returns an ElementTree object which contains all the XML artifacts.
Parsing String
We use the ElementTree.fromstring() method to parse an XML string. The method returns root Element directly: a subtle difference compared with the ElementTree.parse() method which returns an ElementTree object.
Get a list of child Elements from any Element object using element.findall(‘*’). You can find specific children by name by passing the name of the tag e.g. element.findall(‘book’).
Parse JSON in Python
The json module makes it easy to parse JSON strings and files containing JSON object.
Python JSON to dict
You can parse a JSON string using json.loads() method. The method returns a dictionary.
read JSON file
You can use json.load() method to read a file containing JSON object.
Python pretty print JSON
To analyze and debug JSON data, we may need to print it in a more readable format. This can be done by passing additional parameters indent and sort_keys to json.dumps() and json.dump() method.
API Invocation (REST, SOAP)
An API (Application Programming Interface) is a framework for building HTTP services that can be consumed by a wide variety of clients. Web APIs use HTTP protocol to handle requests between the client and the web server.
One of the most important reasons to use an API as opposed to other static data sources is because it's real time.