Skip to content

Instantly share code, notes, and snippets.

@thomasjk10
Last active July 2, 2018 14:21
Show Gist options
  • Save thomasjk10/579a7fae01655d730793bbe1adf53537 to your computer and use it in GitHub Desktop.
Save thomasjk10/579a7fae01655d730793bbe1adf53537 to your computer and use it in GitHub Desktop.
Notes for Python learned on Coursera
Python Introduction:
***********************
• Variables, constants
variable can start with letter or _ not number or any special character can be included
• Operators - add, subtract, multiply, divide, %(to get remainder), **(for power)
Comparison operators - ==, <=, >= !=
• indentation matters
• Numeric expressions and operators for solving numeric calculations
• Integer, Float, String are types of expressions/variables
• Conversion of a numeric value in strings can be converted to int or float
a = '1234'
b = int(a)
c = float(a)
• Traceback error- if any error in type conversion, invalid values
• Conditional operators (if, for)
if --else(one way if and two way if)
if --elif(for muliple if conditions)
• try and except - to handle errors
• Functions - def x(): -Built in (float(), int(), max(), min(), type()) and user defined
call function by x() without the def part
• Arguments - values passed into functions
• Return values (return is a keyword)
• Loops and iteration
while xx:
control flow of loop using break
control using continue - breaks out of current loop and goes to next check
• for x in [x,y,z]:
xxxxx
• Use None keyword to contain no value(in use for smallest/largest number comparison)
• is/is not -operators (use when comparing with None,True/False)
if x is None
if x is True
Python Data Structures:
***********************
Python Strings:
---------------
• Can concatenate strings with '+'
• index of a string --> fruit = 'banana' ---> fruit[1]
• len() function --> find lenght of string
• string splicing ---> a = 'Python' ---> y = a[0:2] ---> Py ( 1st number is start of index, 2nd number indicates upto that index but not including it)
• using 'in' operator to check a character in a string
• Stirng library:
.lower()
.upper()
.split()
.find()
• dir() ---> has list of all inbuilt string functions
Files:
------
• open() --> to open a file
open('input.txt','r')
• \n --> newling non-printing character(is just one character)
• read() --> to read a file into a string
Lists:
------
• collection of data in a variable
x = ['apple','mango','orange']
y = ['apple',10,50.36]
• string cannot be changed once assigned, but list values can be changed by using index values
• range function - gives a list of number of elements or to generate lists
ex: range(4) --> [0,1,2,3]
• min,max,len,sum functions can be directly used on lists ---> x =[1,2,3,4,5] ----> sum(x) --->15
• lists can be concatenated with "+"
• lists can be sliced --> x = [0,1,2,3] , y =x[1:2] ----> 1,2
• data can be appended to a list ---> .append(), data can be sorted ---> .sort()
• split() can be used to split strings into lists
x = 'This is a word'
y = x.split()
print y ----> This, is, a, word
y = x.split(';') ---> can be split on spaces or any other character
Dictionaries:
-------------
• organization of data
• contains in format --> key: value
ex: abc = {'jan':1,'feb':2}
• abc = dict() --> order is not preserved in dict but lists maintain order
• use .get() to get a default value if keyvalue not found for a lookup key
ex: counts.get(name,0) +1
• dictionary can be converted to a list
• counts.items() ---> puts values into pairs ---> [('a',1),('b',2),('c',3)]
Tuples:
-------
• It's a non-changegle list ---> () , lists are in square brackets []
• sort, append --> not available in tuples
• are used when temporary values are required
Python to Access Web Data :
--------------------------
Regular expressions - almost like search
first import regular expressions :
import re
if re.search('From', line)
if re.search('^From', line) ---tells to start searching from beginning of line
.* - any number of times
\S+ - any non blank character followed by atleast one non-blank char
re.findall() ----
[0-9] - numbers 0 to 9
\S+@\S+ ----find characters before and after a search of @
() --- parenthesis mentions start and stop of extraction
[^ ] ---single non blank char
Sockets:
--------
import socket --- gets all details of the the http
import urllib --- gets only contents of the url
• parsing HTML (web scrapping/web crawling)
• Beautifulsoup.py ---> used to retrieve tags from the html page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment