Skip to content

Instantly share code, notes, and snippets.

@HariSan1
Created February 20, 2017 20:12
Show Gist options
  • Save HariSan1/ebb2726d866fd2f2dd9a4ba78b63549e to your computer and use it in GitHub Desktop.
Save HariSan1/ebb2726d866fd2f2dd9a4ba78b63549e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***File Input and Output***\n",
"===========================\n",
"Use the format below to:\n",
"1. Open a file \"file\", for \"r+\" (reading and writing)\n",
"+ Read the file (lines)\n",
"+ Close the file\n",
"\n",
"This is a simple example to illustrate file input and output."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Thank You OSSSB\\n', 'Thank You\\n', 'God Bless and Thank you Lord']\n"
]
}
],
"source": [
"import fileinput\n",
"with open(\"tsb.txt\", \"r+\") as f:\n",
" lines = f.readlines() # read entire file into memory\n",
" print (lines)\n",
" #f.seek(0) # go back to the beginning of the file\n",
" #f.writelines(filter(good, lines)) # dump the filtered lines back\n",
" #f.truncate() # wipe the remains of the old file\n",
" f.close()\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Creating and Calling a Function***\n",
"=====================================\n",
"The box below illustrates creating a function in Python, then using that function to do something, and finally calling the function with an argument (parameter), and printing it to see the result.\n",
"\n",
"1. Define a function - name, and parameter/argument\n",
"+ Create details of whatever the function will do - in this case, count and print items in a list\n",
"+ The function will count the number of items in the list and return that total\n",
"+ go through each member of the list (\"item\", \"list\")\n",
"+ Increase the count by 1, to keep track\n",
"+ When complete, it will return the total\n",
"+ Call the function with any valid list (\"my_list\") and see what the function returns (the total)\n",
"+ Note: The list items are printed IN THE FUNCTION\n",
"+ Note: The total count is a result of the function call and is printed OUTSIDE the function (last line)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def test_fun(test):\n",
" count = 0\n",
" for item in (test):\n",
" print (count, \": \", item)\n",
" count = count + 1\n",
" return count\n",
"\n",
"my_list = ['Hari', 'Rob', 'Dan', 'Audrey', 'Laurie', 'Thea', 'Taz', 'Staci', 'Leticia']\n",
"print (\"The total number of names in the list is: \", test_fun(my_list))\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"raw_mimetype": "text/html"
},
"source": [
"http://nbviewer.ipython.org/gist/manujeevanprakash/7e47301f0b50a98232ca\n",
"\n",
"Item / Process | Name\n",
" ------------- | -------------\n",
" Function | test_fun\n",
" arguments | (test)\n",
" variable | count\n",
" returns | countItem \n",
"\n",
" \n",
" ***Test a List***\n",
"========================\n",
"In the simple function definition and detail - *test_fun()* above, we pass a list, called test, into the function, so that it can process it.\n",
"When you actually call the function, it can be called by any other allowed name. \n",
"Inside the function:\n",
"1. We define a variable, count, and set it to 0 initially.\n",
"+ Then, for every item in the list, we list the sequence #, and format and print it\n",
"* We increase the counter by 1\n",
"* Then we return the total # of items in the list (this is the function's purpose)\n",
"\n",
"Then, in the main area, we define a list of our own (\"my_list\").\n",
"**Then we call and print the results of calling the function in one line.**\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 89. 56.34 76. 89. 98. ]\n",
"float64\n"
]
}
],
"source": [
"# easiest way to create an array is by using an array function\n",
"import numpy as np # I am importing numpy as np\n",
"\n",
"scores = [89,56.34, 76,89, 98]\n",
"first_arr =np.array(scores)\n",
"print (first_arr)\n",
"print (first_arr.dtype) # .dtype return the data type of the array object"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"***Writing and Displaying Equations***\n",
"======================================\n",
"\n",
"The example below illustrates how to write an equation that will be displayed mathematically, with symbols"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"# \\begin{equation*}\n",
"\\left( \\sum_{k=1}^n a_k b_k \\right)^2 \\leq \\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)\n",
"\\end{equation*}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\begin{equation*}\n",
"\\left( \\sum_{k=1}^n a_k b_k \\right)^2 \\leq \\left( \\sum_{k=1}^n a_k^2 \\right) \\left( \\sum_{k=1}^n b_k^2 \\right)\n",
"\\end{equation*}"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment