Sunday 16 April 2017

Notes: Python Fundamentals

Below are my notes taken while following the Udacity course “Foundations with Python
Please follow this course on Udacity to fully understand these notes.

Installing Python

  • Download Python 2 (Download)
  • Ensure “Add python.exe to Path” is selected (Windows only).
  • Run IDLE (Python GUI)
https://google.github.io/styleguide/pyguide.html

Python standard library

  • https://docs.python.org/2.7/library/index.html
  • The python standard library consists of many Modules such as ‘webbrowser’, ‘os’ and ‘time’. Inside ‘time’ there are scripts such as ‘def ctime’ and ‘def sleep’. These hide the complex code of doing a task and allows to only call upon the function (a.k.a abstraction – hiding of detail behind documentation).
  • New libraries used in the program must be imported using “import” in the first lines
  • Functions inside library modules can be accessed by calling them as follows.
    • eg. webbrowser.open()
  • There are many external libraries that we can download for python. (http://pypi-ranking.info/alltime)

Simple Guidelines

  • Python file extension – .py
  • Semicolon ‘;’ is not needed at end of lines
  • ctrl + c – Stop execution of python program
  • Variables don’t need to be defined as ‘int’ or ‘char’

Classes

  • Classes are blueprints – Gives critical information (Data and Methods)
  • Can use classes to create other objects/Instances
  • Creating an instance is done similar to calling functions
    • eg. turtle.Turtle()
    • This runs the def.init() [Constructor] function inside class ‘Turtle’ inside module ‘turtle’
    • This creates space in memory for an instance
  • Constructor (__init__) uses a keyword “self” to define itself and create instance variables rather than local variables.
    • ‘self’ must be added to the arguments when creating a function inside a class
    • ‘self’ usually takes the name of the instance
  • functions inside the class that makes use of ‘self’ are called instance methods.


Example 1: Take a break

Task: A program that schedules breaks throughout the day.
Program functions:
  • Wait 2 hours
  • Open Browser
  • Repeat 3 times
Program: Click here to download
image_thumb[27]

Example 2: Secret Message

Task: Rename file names to remove numbers from the name. (example pictures)
Program: Click here to download
image_thumb[28]

Example 3: Drawing Turtles

https://docs.python.org/2/library/turtle.html
Task: Use classes to draw shapes and structures
Program: Click here to download
image_thumb[29]
image_thumb[26]

Example 4: Send text Message

Install Twilio: Type ‘easy_install twilio’ or ‘pip install twilio’ in the command prompt (or Terminal for Mac) - https://www.twilio.com/docs/libraries/python (https://www.twilio.com/try-twilio)
image_thumb[33]

Example 5: Profanity Editor

Task: Detect certain words in a text document
Program: Click here to download
image_thumb[36]

Example 6: Movie Website

Task: Create a movie website that shows storyline, poster and trailer
Program: Download class, Download instance, Download fresh_tomatoes
image_thumb[45]
image_thumb[42]

Example 6.1: Class Variables

image_thumb[50]
image_thumb[51]

Example 7: Understanding Inheritance

image_thumb[57]
image_thumb[54]

No comments:

Post a Comment