Skip to content

Instantly share code, notes, and snippets.

@Vineet-Mehta
Last active September 10, 2019 07:46
Show Gist options
  • Save Vineet-Mehta/cce371133cf84e4d76f4ebc7d0d4e541 to your computer and use it in GitHub Desktop.
Save Vineet-Mehta/cce371133cf84e4d76f4ebc7d0d4e541 to your computer and use it in GitHub Desktop.

Introduction to Python

When I need to build a web app, I reach for Python. When I need to automate some small task on my system, I reach for Python. When I want to find the most common colors in an image, I reach for Python. When I…OK, I think you get the picture. Basically, when I need to code something and the language doesn’t matter, I use Python. So what is Python?

Python is a general purpose programming language created in the late 1980s, that’s used by thousands of people to do things from testing microchips at Intel, to powering Instagram, to building video games with the PyGame library. It’s small, very closely resembles the English language, and has hundreds of existing third-party libraries.

So what are the major reasons why I, personally, choose Python and recommend it to as many people as possible? It comes down to three reasons.

  1. Readability
  2. Libraries
  3. Community

So lets get our hands dirty and write some Code!

print "Not Hello world this time!"
# comment

The arithmetic operators +, -, *, /, %, **, //

** Exponential calculation

// Floor Division

print"5 + 2 =", 5+2
print"5 - 2 =", 5-2
print"5 * 2 =", 5*2
print"5 / 2 =", 5/2
print"5 % 2 =", 5%2
print"5 ** 2 =", 5**2
print"5 // 2 =", 5//2

# Order of Operation states * and / is performed before + and -

print"1 + 2 - 3 * 2 =", 1 + 2 - 3 * 2
print"(1 + 2 - 3) * 2 =", (1 + 2 - 3) * 2
a = 5
print a
a = "string"
print a
# A string is a string of characters surrounded by " or '
# If you must use a " or ' between the same quote escape it with \
quote = "\"Always remember your unique,"

# A multi-line quote
multi_line_quote = ''' just
like everyone
else '''
print quote + multi_line_quote
# To embed a string in output use %s
print"%s %s %s" % ('I like the quote', quote, multi_line_quote)

# To keep from printing newlines use end=""
print"I don't like ",
print"newlines"

# You can print a string multiple times with *
print'hello\n' * 5
# LISTS -------------
# A list allows you to create a list of values and manipulate them
# Each value has an index with the first one starting at 0

my_list = [1,'string',3, 4.05]
print my_list
print my_list[0]

Since Pythons philosophy is to keep it simple, you can have any type of variable in list

m_list = [my_list, 1]
print m_list

Slicing

You can get a subset of the list with [min:up to but not including max]

print my_list[1:3]
print my_list
my_list = [1,2,3,4,5,6,7]
a_list = my_list[0:6:2]
print a_list
a_list = my_list[0:6:2]
print a_list
a_list = my_list[-1::-2]
print a_list
a_list = my_list[::-1]
print a_list

Other List functions implemented below

grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas']

# You can change the value stored in a list box
grocery_list[0] = "Green Juice"
print grocery_list

# You can get a subset of the list with [min:up to but not including max]

print grocery_list[1:3]

# You can put any data type in a a list including a list
other_events = ['Wash Car', 'Pick up Kids', 'Cash Check']
to_do_list = [other_events, grocery_list]

print to_do_list

# Get the second item in the second list (Boxes inside of boxes)
print to_do_list[1][1]

# You add values using append
grocery_list.append('onions')
print to_do_list

# Insert item at given index
grocery_list.insert(1, "Pickle")

# Remove item from list
grocery_list.remove("Pickle")

# Sorts items in list
grocery_list.sort()

# Reverse sort items in list
grocery_list.reverse()

# del deletes an item at specified index
del grocery_list[4]
print to_do_list

# We can combine lists with a +
to_do_list = other_events + grocery_list
print(to_do_list)

# Get length of list
print(len(to_do_list))

# Get the max item in list
print(max(to_do_list))

# Get the minimum item in list
print(min(to_do_list))
dir(my_list)
help(my_list.append)
# TUPLES -------------
# Values in a tuple can't change like lists

pi_tuple = (3, 1, 4, 1, 5, 9)
# Convert tuple into a list

new_tuple = list(pi_tuple)
print new_tuple
# Convert a list into a tuple
new_list = tuple(grocery_list)
print new_list
# tuples also have len(tuple), min(tuple) and max(tuple)

Dictionary

# DICTIONARY or MAP -------------
# Made up of values with a unique key for each value
# Similar to lists, but you can't join dicts with a +

super_villains = {'Fiddler' : 'Isaac Bowin',
                  'Captain Cold' : 'Leonard Snart',
                  'Weather Wizard' : 'Mark Mardon',
                  'Mirror Master' : 'Sam Scudder',
                  'Pied Piper' : 'Thomas Peterson'}

print(super_villains['Captain Cold'])

# Delete an entry
del super_villains['Fiddler']
print(super_villains)

# Replace a value
super_villains['Pied Piper'] = 'Hartley Rathaway'

# Print the number of items in the dictionary
print(len(super_villains))

# Get the value for the passed key
print(super_villains.get("Pied Piper"))

# Get a list of dictionary keys
print(super_villains.keys())

# Get a list of dictionary values
print(super_villains.values())
# STRINGS -------------
# A string is a series of characters surrounded by ' or "
long_string = "I'll catch you if you fall - The Floor"

# Retrieve the first 4 characters
print(long_string[0:4])

# Get the last 5 characters
print(long_string[-5:])

# Everything up to the last 5 characters
print(long_string[:-5])

# Concatenate part of a string to another
print(long_string[:4] + " be there")

# String formatting
print("%c is my %s letter and my number %d number is %.5f" % ('X', 'favorite', 1, .14))

# Capitalizes the first letter
print(long_string.capitalize())

# Returns the index of the start of the string
# case sensitive
print(long_string.find("Floor"))

# Returns true if all characters are letters ' isn't a letter
print(long_string.isalpha())

# Returns true if all characters are numbers
print(long_string.isalnum())

# Returns the string length
print(len(long_string))

# Replace the first word with the second (Add a number to replace more)
print(long_string.replace("Floor", "Ground"))

# Remove white space from front and end
print(long_string.strip())

# Split a string into a list based on the delimiter you provide
quote_list = long_string.split(" ")
print(quote_list)

CONDITIONALS

The if, else and elif statements are used to perform different actions based off of conditions

Comparison Operators : ** ==, !=, >, <, >=, <= **

The if statement will execute code if a condition is met

White space is used to group blocks of code in Python

Use the same number of proceeding spaces for blocks of code

# You can combine conditions with logical operators
# Logical Operators : and, or, not
age = 30
if age >= 1 and age <= 18:
    print("You get a birthday party")
elif age == 21 or age >= 65:
    print("You get a special birthday party")
elif not age == 30:
    print("You don't get a birthday party")
else:
    print("You get a birthday party yeah")

Loop Contructs

for i in range(10):
    print i
print a_list
for x in a_list:
    print x,
# You can double up for loops to cycle through lists
num_list =[[1,2,3],[10,20,30],[100,200,300]];

for x in range(0,3):
    for y in range(0,3):
        print num_list[x][y],
v_str = 'python loves you'

# Take steps of 2 in a string sequence
for x in v_str[0::2]:
    print x,

print "---"

# Now reverse the order
for x in v_str[-1::-1]:
    print x,
# WHILE LOOPS -------------
# While loops are used when you don't know ahead of time how many
# times you'll have to loop
# An iterator for a while loop is defined before the loop
i = 0
while (i <= 20):
    if(i%2 == 0):
        print(i)
    elif(i == 9):
        # Forces the loop to end all together
        break
    else:
        # Shorthand for i = i + 1
        i += 1
        # Skips to the next iteration of the loop
        continue
    i += 1
print "outside while loop", i

Functions

# FUNCTIONS -------------
# Functions allow you to reuse and write readable code
# Type def (define), function name and parameters it receives
# return is used to return something to the caller of the function
def addNumbers(fNum, sNum):
    sumNum = fNum + sNum
    return sumNum
addNumbers(56,3)
addNumbers("number 1"," number 2")
# NOTE ON ALIASING
def do_something(arg):
    arg.append(1)
x = [5]
do_something(x)
print x
def do_something(arg):
    arg = arg * 2 # BREAKS alisasing
    arg.append(1)
x = [5]
do_something(x)
print x

PIP

Python Package index

#LINUX OR MACOS USERS
#sudo easy_install pip
#sudo pip <command> <args>
# Windows
#python -m pip install -U pip setuptools
#python
#import pip
#python -m pip <command> <args>
#python -m pip install <package-name>
# for those on Windows having problem visit
#http://stackoverflow.com/questions/4750806/how-do-i-install-pip-on-windows?rq=1
#pip
#pip install
#pip download
#pip uninstall
#pip freeze
#pip list
#pip show
#pip search
#pip wheel
#pip hash
#pip install requests            # latest version
#pip install SomePackage==1.0.4     # specific version
#pip install 'SomePackage>=1.0.4'     # minimum version
#sudo pip install requests>=2.12.5
#sudo pip install BeautifulSoup
#-I, ignore installed
#--upgrade
#pip uninstall [options] <package>
#pip seach modulename
#pip list
#pip show <modulename>

Virtual Environment

How does virtualenv help?

virtualenv solves this problem by creating a completely isolated virtual environment for each of your programs. An environment is simply a directory that contains a complete copy of everything needed to run a Python program, including a copy of the python binary itself, a copy of the entire Python standard library, a copy of the pip installer, and (crucially) a copy of the site-packages directory mentioned above. When you install a package from PyPI using the copy of pip that's created by the virtualenv tool, it will install the package into the site-packages directory inside the virtualenv directory. You can then use it in your program just as before.

$sudo pip install virtualenv
$cd ~/code/myproject/
$virtualenv env
$ls env
$which python
/usr/bin/python
$ source env/bin/activate
$ deactivate

pip is a tool for installing packages from the Python Package Index.

virtualenv is a tool for creating isolated Python environments containing their own copy of python, pip, and their own place to keep libraries installed from PyPI.

It's designed to allow you to work on multiple projects with different dependencies at the same time on the same machine. After installing it, run virtualenv env to create a new environment inside a directory called env.

You'll need one of these environments for each of your projects. Make sure you exclude these directories from your version control system.

To use the versions of python and pip inside the environment, type env/bin/python and env/bin/pip respectively.

You can "activate" an environment with source env/bin/activate and deactivate one with deactivate. This is entirely optional but might make life a little easier.

Running virtualenv with the option --no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default behavior for virtualenv 1.7 and later.]

Requests HTTP Library

1 GET The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

2 HEAD Same as GET, but transfers the status line and header section only.

3 POST A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

4 PUT Replaces all current representations of the target resource with the uploaded content.

5 DELETE Removes all current representations of the target resource given by a URI.

6 CONNECT Establishes a tunnel to the server identified by a given URI.

7 OPTIONS Describes the communication options for the target resource.

8 TRACE Performs a message loop-back test along the path to the target resource.

import requests
r = requests.get('https://google.com')
#c = r.content
print r.status_code
#print c
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
r.headers
r = requests.put('http://httpbin.org/put', data = {'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')

Beautiful soup library

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
#sudo pip install beautifulsoup4
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

print(soup.prettify())
soup.title
# <title>The Dormouse's story</title>

soup.title.name
# u'title'

soup.title.string
# u'The Dormouse's story'

soup.title.parent.name
# u'head'

soup.p
# <p class="title"><b>The Dormouse's story</b></p>

soup.p['class']
# u'title'

soup.a
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
for link in soup.find_all('a'):
    print(link.get('href'))

Beautiful Soup transforms a complex HTML document into a complex tree of Python objects. But you’ll only ever have to deal with about four kinds of objects: Tag, NavigableString, BeautifulSoup, and Comment.

Youtube downloader

import requests
import os,sys
from bs4 import BeautifulSoup
search = raw_input('Enter the name of the song: ')
url = 'https://www.youtube.com/results?search_query='+search
sc = requests.get(url)
soup = BeautifulSoup(sc.text,'html.parser')
title = soup.findAll('h3',{'class':'yt-lockup-title '})
link = []
for i in range(min(10,len(title))):
    link.append(title[i].find('a')['href'])
for i in range(min(10,len(title))):
    print (str(i+1)+'. '+title[i].find('a').text)
os.system("youtube-dl --extract-audio --audio-format mp3 " + 'https://www.youtube.com'+link[0])

Selenium Automation

#DA_AUTO LOGIN SCRIPT
#Vineet Mehta
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
import time
def page_is_loaded(driver):
    return driver.find_element_by_tag_name("body") != None
options = webdriver.ChromeOptions()
#options.add_argument('--ignore-certificate-errors')

driver = webdriver.Chrome('/home/vineet/Downloads/chromedriver',chrome_options=options)
driver.get("https://10.100.56.55:8090/httpclient.html")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)
email_field = driver.find_element_by_name("username")
email_field.send_keys("ID")
password_field = driver.find_element_by_name("password")
password_field.send_keys("PASSWORD")
password_field.send_keys(Keys.RETURN)
time.sleep(5)
driver.quit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment