1 / 18

Python Basics

Python Basics. Tom LeFebvre. Python Language. Very High-Level Language Scripting Language (no compiling) Simple syntax Easy to Learn/Use Slow execution. Indentation. Python uses indentation to identify blocks of code Might be difficult to get used to Code is more readable.

ludwig
Download Presentation

Python Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Basics Tom LeFebvre

  2. Python Language • Very High-Level Language • Scripting Language (no compiling) • Simple syntax • Easy to Learn/Use • Slow execution Python Basics

  3. Indentation • Python uses indentation to identify blocks of code • Might be difficult to get used to • Code is more readable Python Basics

  4. Code Comments • Code comments are identified with the “#” symbol • Example # Print the value print “The value is:”, value Python Basics

  5. Statements • The “if” statement has the syntax: if condition1 : doSometing elif condition2: doSomethingDifferent elif condition3: doAnotherThing else: doSomethingElse Python Basics

  6. Statements (cont.) • The “for” loop has the syntax: • The range statement is very useful for iterator in list: doSomethingWithTheIterator range(0,10) is the same as [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for i in range(10): print i Python Basics

  7. The def Statement • The “def” statement starts a method. def myMethod: print “myMethod executing.” Python Basics

  8. Data Structures - Lists • List: a (mixed) collection of objects • Lists are mutable • Lists may be nested: [ [1, 2, 3] , [ 6, 7 ]] myList = [“hello”, 2, 10.24] myList[2] = 20.48 print myList [“hello”, 2, 20.48] Python Basics

  9. Data Structures - Tuples • Tuple: an immutable (mixed) collection of objects cityInfo = (‘Boulder’, (40.0, -105.0)) cityName, location = cityInfo #copy out components print cityName Boulder print location (40.0, -105.0) Python Basics

  10. Data Structures - Dictionaries • Dictionary: non-ordered collection referred to by a “key”. key : data myDict = { ‘joe’ : (‘Joseph Smith’, ‘Private”, 5226677), ‘sam’:(‘Samuel Jones’, ‘Sergeant”, 5124989), ‘bob’:(‘Robert Doe’, ‘General’, 7689098), } Python Basics

  11. Other Peoples’ Code • Much code has already been written that can greatly help you. • Two types of “outside” code: • Modules – loose collection of methods • Classes – tightly-coupled, coherent collection of data and methods Python Basics

  12. Modules • Modules are a collection of Python methods • You can access modules by using “import” import math print math.pi --- 3.14159265358 OR from math import * print pi --- 3.14159265358 Python Basics

  13. Classes • Python is an Object-Oriented language • Class: a collection of data and methods • Data - things • Methods - actions class MyClass: def DoSomething(): def printSomething(): Python Basics

  14. Inheritance • Code can “inherit” from classes • myTool inherits from class SmartScript • myTool has access to the data and methods in SmartScript • To access data or methods in any class use: myTool(SmartScript.SmartScript): self. Python Basics

  15. Where to use self. • When calling method: • When accessing data: • When defining a new function: self.method() a = self.dataVariable def myNewMethod(self, arg1, arg2): Python Basics

  16. Good Programming Practices • Modularization • Break up code into smaller pieces each of which performs some small task • avoids long methods • improves readability • easier to understand/maintain • easier to find bugs Python Basics

  17. Good Programming Practices • Documentation • Should be short • Include one sentence for each method • In-line documentation where necessary • Choose names well • For methods – use verbs • For data variables – use nouns Python Basics

  18. Troubleshooting • Use print statements • Add code incrementally & test Python Basics

More Related