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

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

x=9 y="ram" z=14.5 #print(x,y,z) print(y,x,z, sep="\t")
ram 9 14.5

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 and Place Holders

x=9 y=10 z=12 #print(x,y,z) print(y,x,z, sep="\t")
10 9 12
## %s-strings,%d-int,%f-decimal print("%s is %d years old and his height = %f inches" %(y,x,z))
ram is 9 years old and his height = 14.500000 inches
## Place holders print("first num= %d second number = %d Third number = %d" %(y,x,z))
first num= 10 second number = 9 Third number = 12
#type function type(12),type(18.6),type("ashi"),type(-13),type(-14.88888888),type("b"),type("&"),type("67A")
(int, float, str, int, float, str, str, str)
##Type Casting float(18) int(13.7) str(8.9) int("13") int(13.5)
13

INDENTATION

##Variable Decalartion t=9 x,y,z=1,"a",14.5 a_1=9

Input

## name = str, age = int ,years of exper = float name=input(" Enter your Name") age=int(input("Age:")) exp=float(input("Job exp in years:")) print("your details are:") print("Name = %s" %(name))
Enter your Nameashi Age:12 Job exp in years:3.2 your details are: Name = ashi

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

x=9 x*x+2*x-6
93
print(round(23/8,3))
2.875
## Numbers 3+4 3-8 4*5 3**4 14/3 12//3 14%3
2

If Else

user=input("create user name") x=input("Enter username") t_pin=int(input("Create a four digit t-pin")) if x==user: pwd=input("Create your password of 6 letters") print("welcome to home page") t=int(input("Enter your 4 digits pin for further information:")) if t==t_pin: print("Credits in your account is 40000k") else: print("sorry invalid input") else: print("invalid user name")
create user nameabc Enter usernameabc Create a four digit t-pin1122 Create your password of 6 lettersashi123 welcome to home page Enter your 4 digits pin for further information:1122 Credits in your account is 40000k
## Create a welcome interface with user name and pwd

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