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)
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
Example 2: Secret Message
Task: Rename file names to remove numbers from the name. (example pictures)Program: Click here to download
Example 3: Drawing Turtles
https://docs.python.org/2/library/turtle.htmlTask: Use classes to draw shapes and structures
Program: Click here to download
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)Example 5: Profanity Editor
Task: Detect certain words in a text documentProgram: Click here to download
Example 6: Movie Website
Task: Create a movie website that shows storyline, poster and trailerProgram: Download class, Download instance, Download fresh_tomatoes
No comments:
Post a Comment