Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python core/DAY 1Basic Implementation in Python.ipynb
3074 views
Kernel: Python 3

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.

image.png

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

image.png

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.

image.png

  • 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

print("Hi")

Installation

The following link provides the complete installation guide for python basic IDLE

https://www.python.org/downloads/

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.

image.png

The following link will guide you to anaconda interface and download details: https://www.anaconda.com/

2.Python Scripting

class P1: x="HI" print(P1.x)#Tell about status of class. class P1: x="HI" print(P1.x)#Tell about status of class. #Class variables class P2: name="RAM" age = 29 print("NAME is %s and age is %d"%(P2.name,P2.age)) #Class &Objects class Phone(): Color=" " Price =00 Type=" " ob1=Phone()#Creation of objects and assinging it to class phone. ob2=Phone() ob1.Color="RED" ob1.Price=10000 ob1.Type="Nokia" ob2.Color="Black" ob2.Price=12000 ob2.Type="Asus" print("This is a %s phone of %s color and its price is %drs"%(ob1.Type,ob1.Color,ob1.Price)) print("This is a %s phone of %s color and its price is %drs"%(ob2.Type,ob2.Color,ob2.Price))
class X: def __init__(self,name,age): self.name=name self.age=age ob=X("ran",18) print(ob.name , ob.age)
# dictionary from an class's fields. class dObj(): def __init__(self): self.x = 'age' self.y = 'name' self.z = 'Gender' def do(self): pass d = dObj() print(d.__dict__)

Object Types

Built-in Types

Approximately two dozen types are built into the Python interpreter and grouped into a few major categories, as shown : image.png

image.png

image.png

Modules

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable name.

# Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print() def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while a < n: result.append(a) a, b = b, a+b return result
## Now enter the Python interpreter and import this module with the following command: import fibo fibo.fib(1000)
fibo.__name__ ##To get name of module
## use a function often to assign it to a local name: fib = fibo.fib fib(500)
## There is even a variant to import all names that a module defines: from fibo import * fib(500)

Note :

  • This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

  • Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code.

  • For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use importlib.reload(), e.g. import importlib; importlib.reload(modulename).

## if the module name is followed by as, then the name following as is bound directly to the imported module. import fibo as fib fib.fib(500)
## It can also be used when utilising from with similar effects: from fibo import fib as fibonacci fibonacci(500)

The Module Search Path

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).

  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  • The installation-dependent default.

Note : On file systems which support symlinks, the directory containing the input script is calculated after the symlink is followed. In other words the directory containing the symlink is not added to the module search path.

  • After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory

The dir() Function

  • The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings

  • Without arguments, dir() lists the names you have defined currently

import fibo, sys dir(fibo)
a = [1, 2, 3, 4, 5] import fibo fib = fibo.fib dir() #lists all types of names: variables, modules, functions, etc.
#names of built-in functions and variables import builtins dir(builtins)

Variable reference & Garbage Collection

Memory Management

Python does not necessarily release the memory back to the Operating System. Instead, it has a dedicated object allocator for small objects (smaller or equal to 512 bytes), which keeps some chunks of already allocated memory for further use in the future. The amount of memory that Python holds depends on the usage patterns. In some cases, all allocated memory is never released.

Garbage collection algorithms

Standard CPython's garbage collector has two components :

  • the reference counting collector

  • the generational garbage collector, known as gc module.

Reference counting

Reference counting is a simple technique in which objects are deallocated when there is no reference to them in a program. Every variable in Python is a reference (a pointer) to an object and not the actual value itself. Reference counting works by counting the number of times an object is referenced by other objects in the system. When references to an object are removed, the reference count for an object is decremented. When the reference count becomes zero, the object is deallocated. To keep track of references, every object (even integer) has an extra field called reference count that is increased or decreased when a pointer to the object is copied or deleted.

Examples, where the reference count increases:

  • assignment operator

  • argument passing

  • appending an object to a list (object's reference count will be increased).

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.

S = "Python" #string assignment print(" String =",S) print("Type=",type(S))
String = Python Type= <class 'str'>
L = [1,2,"a"] # List Assignment print(" List =",L) print("Type=",type(L))
List = [1, 2, 'a'] Type= <class 'list'>
T = (1,2,"a",6) # List Assignment print(" Tuple =",T) print("Type=",type(T))
Tuple = (1, 2, 'a', 6) Type= <class 'tuple'>

Mutable vs Immutable Objects

Operation & Methods applicable to all sequences

image.png

Operations & Methods applicable to Mutable Sequence

  • List

image.png

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).

d={} # dictionary assignment d={1:"one",2:"Two",3:"Three",4:"four"} print("Mapping=",d) print("Type= " ,type(d))
Mapping= {1: 'one', 2: 'Two', 3: 'Three', 4: 'four'} Type= <class 'dict'>

Dictionary Methods & Operations

image.png

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.

s = set({1,2,3,4}) # set assignment print("set=",s) print("Type=",type(s)) s.add(9)# mutuable opeartion s
set= {1, 2, 3, 4} Type= <class 'set'>
{1, 2, 3, 4, 9}

Methods & Operations for all set types

image.png

Methods & Operations for mutuable set types¶

image.png

fs = frozenset({1,2,3,4}) # set assignment print("set=",fs) print("Type=",type(fs))
set= frozenset({1, 2, 3, 4}) Type= <class 'frozenset'>

List Methods

image.png

List iteration

l={1,2,3,4} for i in l: print(l)
{1, 2, 3, 4} {1, 2, 3, 4} {1, 2, 3, 4} {1, 2, 3, 4}
for i in l: print(i)
1 2 3 4

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.

a="aapple is good for health" b="apple" #a in b #b in a # compares menbers #b==a #b is a # exact equal #b is not a #"apple" is b
True

Multi Target Assignment

a , b = 1,2 print("a=",a ) print("b=",b)
a= 1 b= 2
c , s = 26.2 ,"Ashi" print("%s age = %f"%(s,c))
Ashi age = 26.200000
a,b = b,a # swapping a
2
b
1

Day 1

-4. Python String Types(2hrs)

Day 1

  1. Python String Types(2hrs)

  • 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 Manipulation a="Python is a data science tool" b="1234" print("This is concading=",a+b) print("Length of string a =",len(a)) #len() for finding length print(a.isupper())# checks upper case print(b.islower())# checks lower case c=a.upper() print("Converting a into uppercase=",c) d=b.capitalize() print(d)
This is concading= Python is a data science tool1234 Length of string a = 29 False False Converting a into uppercase= PYTHON IS A DATA SCIENCE TOOL 1234

String Methods & Functions

image.png

Type Conversion

Converting one sequence type to other.

x = 124 y=float(x)# int to float y
124.0
y ="12345" z=int(y) # string to int z
12345

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.

import xml.etree.ElementTree as ET tree = ET.parse('sam.txt')
##Once you have the ElementTree object, you can get the root Element object using the tree.getroot() method root = tree.getroot()

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.

print (ET.fromstring('<a><b>1</b>2</a>')) # prints "<Element 'a' at ...>"
<Element 'a' at 0x00000264C2AD4408>
##From the Element object, you can get the XML tag name using the tag property. print (root.tag ) # outputs "sam"
note

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’).

## recursively processes all the elements in the XML and prints the name of the tag. def show(elem): print (elem.tag) for child in elem.findall('*'): show(child) show(root)
note to from heading body
## retrieve an element’s text content using the elem.text property as follows: print (ET.fromstring('<a>Hello<b>1</b>2</a>').text) print (ET.fromstring('<a>Hello<b>1</b>2</a>').find('b').text)
Hello 1

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.

import json person = '{"name": "Bob", "languages": ["English", "Fench"]}' person_dict = json.loads(person) # Output: {'name': 'Bob', 'languages': ['English', 'Fench']} print( person_dict) # Output: ['English', 'French'] print(person_dict['languages'])
{'name': 'Bob', 'languages': ['English', 'Fench']} ['English', 'Fench']

read JSON file

You can use json.load() method to read a file containing JSON object.

import json with open("F:\d.json") as f: data = json.load(f) print(data)
{'name': 'Ram', 'exp': 10, 'role': 'CEO'}

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.

import json person_string = '{"name": "Ram", "languages": "English", "numbers": [2, 1.6, null]}' # Getting dictionary person_dict = json.loads(person_string) # Pretty Printing JSON string back print(json.dumps(person_dict, indent = 4, sort_keys=True))
{ "languages": "English", "name": "Ram", "numbers": [ 2, 1.6, null ] }

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.

import requests req = requests.get('https://github.com/timeline.json') req
<Response [410]>
## 1. Script to check validity a password. pwd=input('enter passowrd') d=a=u=s=l=sp=0 w="@#$%^&*_><?/" for c in pwd: if c.isdigit(): d=d+1 elif c.isalpha(): a=a+1 if c.isupper(): u=u+1 elif c.islower(): l=l+1 elif c in w: s=s+1 elif c.isspace(): sp=sp+1 else: pass if d==0: print("invalid password no digits") if sp>0: print("Invalid space not allowed") elif a==0: print("INVALID ENTER SOME LETTERS") elif u==0: print("INVALID ENTER one in uppercase") elif l==0: print("INVALID ENTER one in lowercase") elif s==0: print("INVALID enter special character") elif len(pwd)<10: print("INVALID MINIMUM LENGTH SHOULD BE 9") else: print("Valid pasword")
enter passowrd123abc@ INVALID ENTER one in uppercase
## 2. Script that accepts a sentence and calculate the number of letters and digits. H=input("Enter your sentence ") d=l=0 for i in H: if i.isdigit(): d=d+1 elif i.isalpha(): l=l+1 else: pass print("Letters", l) print("Digits", d) if d==0: print("PURE STRING") elif l==0: print("ONLY DIGITS") else: print("Merged string")
Enter your sentence 12ab 12ab Letters 4 Digits 4 Merged string
## Send mail alerts with using python from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart receiver = '[email protected],[email protected] ' me ="[email protected]" msg = MIMEMultipart() msg['Subject'] = 'python class ' # me == the sender's email address # family = the list of all recipients' email addresses msg["from"] = me msg['To'] = receiver msg.preamble = 'suyashi trainer' # Send the email via our own SMTP server. server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(me, "hemlata52") text = msg.as_string() server.sendmail(me, receiver, text) server.quit()
(221, b'2.0.0 closing connection p6sm1936564pfp.88 - gsmtp')

API acess : CLOUD Platform

import requests#internet acess import os #file & data creation #import time import urllib #url post x = int(input("Enter Number")) if x>0: print("Status is ON") url= "https://api.thingspeak.com/update?api_key=8QCQUB1WHQPEDAUJ&field1=1" #python2channel response = requests.post(url) elif x==0: print("Staus is OFF") url= "https://api.thingspeak.com/update?api_key=8QCQUB1WHQPEDAUJ&field1=0" response = requests.post(url) else: print("BYE")
Enter Number2 Status is ON