Introduction to Python
What is Python (1)? It’s one of the most popular dynamic programming languages It’s a general purpose programming language It’s used for just about everything It’s syntax looks similar to C or Pascal with a few exceptions It’s dynamically typed It’s interpreted
What is Python (2)? It is object-oriented There is an extensive standard library with over 100 modules and growing Regular expressions, mathematical functions, operating system calls, network programming and just about everything else There are many 3rd party modules too supplying GUI toolkits and interfaces to relational databases
Who uses Python? Google has millions of lines of Python code It’s probably more popular than ASP.NET or PHP. Yahoo maps and groups Industrial Light & Magic And the list goes on
Python Variables In this regard, Python is truly object-oriented Every variable is an object Variables can be used before they are declared Numbers are of two types (integers and floating point numbers) myInt = 7 myFloat = 7.0 String are defined with either single or double quotes myString = “hello”
Notes about the Examples Note that many of the examples in these slides were used from www.learnpyton.org
Python Expressions They work the same as expressions in most other languages although you cannot mix operators between numbers and strings
Python Lists Python implements arrays as lists They can contain any type of variables They are easy to iterate Elements can be accessed via an index
Python Strings There are a number of functions that operate on strings Strings also have methods like index, count, upper, lower and so on As in JavaScript + is the strong concatenation operator
Python Blocks Here, Python differs from other languages Indentation is used to define code blocks instead of braces
Python Loops Like most languages, there are for and while loops
Python Functions Python functions are defined using the def keyword Functions can have zero, one, or many arguments Functions can return a value
Python Functions (Calling) Call a function by specifying the function name, followed by () Arguments appear between the () Example to call the function declared in the previous slide Python also works with a variable number of arguments
Python Modules Modules are Python files that implement one or more functions One modules imports another module via the import keyword You can create your own modules by creating a new .py file with the module name.
Python Exception Handling Exceptions happen you a statement tries to do the impossible Python uses the try syntax to handle exceptions
They Python Standard Library The following link documents the Python standard library https://docs.python.org/2/library/
Some Interesting Python Constructs (1) Sets are lists with no duplicate entries They operate on the notion of set theory There are many set methods like intersection, symmetric_difference and difference