Python Notes
-
Return multiple values from a function in Python Example
def f(in_str): out_str = in_str.upper() return True, out_str # Creates tuple automatically succeeded, b = f("a") # Automatic tuple unpacking print ("var1" , succeeded , " var2" , str(b))
-
Loop Statement Example
print("-----") print("for loop example ") for letter in 'Python': # First Example print ('Current Letter :', letter) fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # Second Example print…
-
Multiline comment Example
''' This is a multi line comment. You can use triple-quoted strings. When they're not a docstring (first thing in a class/function/module), they are ignored. ''' print("program starts after comments")
-
How to swap variables in python ?
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.…
-
Python Utilities that can be used to make everyday life easy
print("Day_Over_Day_Rate_Adjustment".lower()) #---- print('Return multiple values from a function in Python Example'.title().replace(' ', ''))
-
is Python pass by value or pass by reference ?
You can not pass a simple primitive by reference in Python, but you can do things like: def foo(y): y[0] = y[0]**2 x = [5] foo(x) print x[0] That is a weird way to go about it, however. Note that in…
-
Start simple http server with python
If you need a quick web server running and you don't want to mess with setting up apache or something similar, then Python can help. Python comes with a simple builtin HTTP server. With the help of…
-
Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i].
print ('-- product array puzzle --') arr = [10, 3, 5, 6, 2] prod = [] product = 1 for data in arr: product = product * data for data in arr: prod.append((product / data)) print (prod)
-
Reverse the array
Sample Input 4 1 4 3 2 Sample Output 2 3 4 1 #!/bin/python3 import sys n = int(input().strip()) arr = [int(arr_temp) for arr_temp in input().strip().split(' ')] import re arr.reverse() print…
-
Get Node data of the Nth Node from the end
""" Get Node data of the Nth Node from the end. head could be None as well for empty list Node is defined as """ class Node(object): def __init__(self, data=None, next_node=None): self.data = data…
-
Write data to file on disk in json format
import json a = {'hello': 'world'} b = 'mohit' with open('C:\\Users\\mohit\\temp\\filename.json', 'wt') as handle: json.dump(a, handle)
-
Map Reduce And Lambda example
# example 1 def varToSqFeet(var): return var*9 x = list(map(varToSqFeet, [100])) print(x) # output [900] # example 2 varToSqFeetLamda = lambda var : 9*var print (varToSqFeetLamda(100)) # output 900 #…
-
Beginner’s guide to Web Scraping in Python (using BeautifulSoup)
What is Web Scraping? Web scraping is a computer software technique of extracting information from websites. This technique mostly focuses on the transformation of unstructured data (HTML format) on…
-
MAC OS Popup
import os # The notifier function def notify(title, subtitle, message): t = '-title {!r}'.format(title) s = '-subtitle {!r}'.format(subtitle) m = '-message {!r}'.format(message)…
-
splinter
from splinter import Browser with Browser('chrome') as browser: # Visit URL url = "http://www.google.com" browser.visit(url) browser.fill('q', 'splinter - python acceptance testing for web…
-
Print Colored output to console
from termcolor import colored print (colored('hello', 'red'), colored('world', 'green')) added on 10-Feb-2018
-
Functional Programming Python
Functional Programming Python
-
Zip In Python
Zip In Python
-
Concatenating two lists in Python
added on 06 July,2018 Lists in Python can be concatenated in two ways Using + operator Using extend Let list_a and list_b be defined as below list_a = [1, 2, 3, 4] list_b = [5, 6, 7, 8] Using +…