Skip to content

Instantly share code, notes, and snippets.

@ecotg
Created February 24, 2018 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecotg/08322fa69d661de5db554531f106c2e3 to your computer and use it in GitHub Desktop.
Save ecotg/08322fa69d661de5db554531f106c2e3 to your computer and use it in GitHub Desktop.
# Additional: Exploratory data analysis using python
# >> csv data: http://bit.ly/2oodrwJ
# >> This is a data set that records various attributes of passengers on the Titanic, including who survived and who didn’t.
# >> Dimensions of the data:
# >> # passengers, % per gender, % per class
# >> Average age of survivors?
# >> What class had the most survivors?
# >> What were the average fares per class
# >>
# >>
# ex6. FINAL EXERCISE!
# 29. GOING BIGGER
# 28. FINDING A WORD IN A STRING
# >> 'abc'.find('a') -> 0
# >> 'abcdabc'.find('a') -> 0
# >> returns -1 if not found
"Ladies Learning Code".find("Code") # returns 16, because Code starts with C at index 16
# 27. COUNTING LOOPS
# ex5 FIND YOUR CHAPTER LEAD (15 MINS)
# 26. EXAMPLE: WORKING IN A for LOOP
# >> DictReader returns a reader object that we can iterate over
# >>
import csv
with open('llc-chapters.csv') as chapters_file:
chapters = csv.DictReader(chapters_file) # a reader object
for chapter in chapters: # Each chapter is a dictionary datatype
print(chapter['City'] + ', ' + chapter['Province'])
print(chapter['Chapter Lead(s)'])
# 25. DICTIONARIES -
# >> a datatype,
# >> create: key values, {1: 'one'}
# >> unordered
# >> Rather than being indexed numerically (i.e. 0, 1, 2, 3, …),
# >> The keys behave in a way similar to indices in an array,
# except that array indices are numeric and keys are arbitrary strings.
# >> Each key in a single Dictionary object must be unique.
# >> When to use: Dictionaries are frequently used when some items need to be stored and recovered by name.
# >> use for efficency
# 24. READING DATA FROM A CSV FILE
# 23. Using Libraries
# >> why: efficent, well-tested, easy to start projects
# >> what does import do:
# >> module: any *.py file. Its name is the file name.
# >> When a module named spam is imported, the interpreter first searches for a built-in module with
# that name. If not found, it then searches for a file named spam.py in a list of directories
# given by the variable sys.path.
# >> Also, Python imports are case-sensitive. import Spam is not the same as import spam.
# 22. Libraries
# >> examples:
# >> furl (parse urls), django/flask/pyrmid - web frameworks, numpy (sci computing),
# tkinter (create guis),
# [LUNCH]
# 21. LOOPS: READING ALL THE LINES IN A FILE -
# >> Note that the last character of each line is newline character.
# returns a list
# >> It is not memory efficient to read all file in one gulp if your files are really big.
# 20. One Line at a time
# >> more memory efficent
# 19. Comma Separated Values (CSV) Files
# 18. Opening a file -
# a) f = open('my_text_file.txt', "r")
# lines = f.readlines()
# f.close()
# b) lines = list(f)
# c)
# Ex. 4 - Smart Loops
# 17. Counting
# 16. Loops: for loops are traditionally used when you have a block of code which you want to repeat a fixed number of times.
# >> for in (strings, lists, tuples, sets) - anything that can be turned into a sequence/list
# >> for k,v in dict.items()
# >> xrange: memory efficiency/generator , range
# >> other loops: while
# >> can define you own iter/next function
# 15. Lists - items can be of different data-types, index start at 0
# >> A list in Python is just an ordered collection of items which can be of any type.
# By comparison an array is an ordered collection of items of a single type -
# Ex. 3 - Smarter Weather
# >> index starts at 0
# >> slicing: myList[2:5] or mylist[2] or myList[5:] or myList[:5 or myList[:]
# 14. AND/OR
# 13. TESTING WITH MATH COMPARISON OPERATORS
# 12. Identation - code blocks, function, readability
# Ex. 2 - Ask for weather
# 11. Booleans
# value can only be true or false
# 10. Conditionals - add logic to your code. tackle complex routing/valiations
# EX.1 - ask for HELLO WORLD
# 9. Comments - leave notes
# 8. Reassigning variables
# 7. Variables - where to store data, . A variable is a name that refers to a value
# >> http://www.diveintopython3.net/native-datatypes.html
# what: A variable is a name that refers to a value
#
# 6. Typecasting - change the datatype
# what: convert a variable value from one type to another.
# This is, in Python, done with functions such as int() or float() or str() .
# A very common pattern is that you convert a number, currently as a string into a proper number.
# >> float(), int(), str(), bool()
# 5b. Errors: Python has defined errors
# >> TypeError: data is of the wrong type
# >> SyntaxError: you spelt something incorrectly
# >> ValueError: correct data type but the value of the data is invalid
# len(42) # this causes a TypeError, because a number is the wrong type for the function
# int("dog") # this causes a ValueError, because "dog" cannot be converted to an int value
# 5. Concatenation: join two variables
# NOTE: can only work when data is all of the same type
# ex: 4 + 4
# ex: [1,2,3] + [4,5,6]
# ex: 'hello' + 'world'
# 4. Printing -
# why: built in function, view/debug your code
# 3. Functions: set of instructions on what to do with the data
# >> Why use functions:
# bundle a set of instructions that you want to use repeatedly
# Complex & are better self-contained in a sub-program and called when needed.
# Function might or might not need multiple inputs.
# Function can or can not return one or more values.
# >> Types:
# built-in: print(), type() etc
# user defined (includes modules)
# anonymous/lambdas
# >> How to define a function
# NOTE: A method refers to a function which is part of a class.
# This means that all methods are functions but not all functions are methods.
# Link: https://www.datacamp.com/community/tutorials/functions-python-tutorial
# - Use the keyword def to declare the function and follow this up with the function name.
# - Add parameters to the function: they should be within the parentheses of the function.
# - End your line with a colon.
# - Add statements that the functions should execute.
# - End your function with a return statement if the function should output something.
# Without the return statement, your function will return an object None.
# - if you want to continue to work with the result, add a return statement,
# - functions immediately exit when they come across a return statement,
#
# 2.. Data-types: TYPES Of data we can work with
# What:
# >>
# >> way of telling the compiler how and what the programmer intends to do with the data
# >> data type determines what can be done with the data
# Types:
# >> str, int, float, sets, tuples, bool, list,
# >> tuple: (1,2,3,4)
# >> list [1,2,3]
# >> set(1,2,3)
# >> {"class": 1, "type": 2}
# >> which are immutable
# >> which allow for index
# Links: http://interactivepython.org/courselib/static/thinkcspy/SimplePythonData/Variables.html
# IDE vs code editor: productivity
# You do more than write code, IDE helps you take care of the rest: debug, test, version control, etc
# Code completion, debugging, refactoring, version control integration, smart typing
# Installation
# Check if python is installed:
# windows 7
# >> open the Windows menu and type “command” in the search bar. Select Command Prompt from the search results.
# >> In the Command Prompt window, type 'python -V'
# mac: In Terminal, type 'python -V'
#
# Why Python
# Created by Guido van Rossum
# + Open source: free to use, free to distribute, developed by a community. So if you
# So as a developer, you're free to see what the language does, how it does it, and suggest changes
# anyone can contribute to its development
# - Write once, run on any OS
# - Great community
# >> style guide: https://www.python.org/dev/peps/pep-0008/
# >> multiple resources to learn
# >> design philosophy that emphasizes code readability
# >> zen of python
# >> Python community was one of the first communities to adopt conference and community codes
# of conducts as well as incident guidelines that set the tone for safe and inclusive environments
# - Fast growing:
# >> stackoverflow: https://zgab33vy595fw5zq-zippykid.netdna-ssl.com/wp-content/uploads/2017/09/growth_major_languages-1-1400x1200.png
# >> github: https://i.imgur.com/KpbiDkV.png
# - Multi-use
#
# What is programming
# A set of precise instructions given to a computer - like a recipe, but for a little kid who needs
# explicit instructions
# Scenario:
# National Learn to Code day - annual event where over 1,500 Canadians
# will gather together in over 25 communities to challenge themselves
# to learn a new skill:
# Python is dynamically typed - Dynamic type checking is the process of verifying the type safety of a program at runtime
# Python 2 vs 3:
# >> py3 from 2008, py2 till 2020
# >> not entirely backward compatability
# >> changes: xrange, Unicode support, print is now a built-in function, Division with Integers
# >> Python 3.x introduced some Python 2-incompatible keywords and features that can be imported via the in-built __future__ module in Python 2.
3 / 2 = 1 # Python 2.7.6
3 / 2 = 1.5 # Python 3
# try/except
# in Python, almost everything is an object - functions, classes, variables, etc.
#
#
#
# modules: https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html
#https://data36.com/python-for-data-science-python-basics-1/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment