A Gentle Introduction To Python
A Gentle Introduction To Python
Python
Iraklis Akritas Pipeline TD/Plugin-Tools Programmer Candidate
Presenter Information
Please feel free to email me with any questions as well as connect with me:
Google+
Website
We wish
An important goal of the Python developers is making Python fun to use. This
is reflected in the origin of the name which is derived from the television
seriesMonty Python's Flying Circus.
Python Creator
Python Lingo
HINT: When googling how to get something done in python preppend pythonic
way of
Python has a lot of free material online, do not rush to buy expensive books.
One of the best books available is Dive into Python.
Code Academy and Mechanical MOOC have great series about learning Python.
When programming or shall I say scripting in Python, ensure that you are
following a valid code formatting standard to the letter. When posting your
code for help in various forums, unformatted Python code normally leads to
complaints about unreadability, you will be instructed to re-format your code
before getting an answer. I highly recommend Googles style guide or for the
hard-core amongst us you can head to the official PEP(Python enhancement
proposals). As we will see soon, formatting is a big part of Python scripting.
Python FAQ
Everything can not only be done, but it can be done fast. For example a program that takes you weeks in C++ might take you a day in
Python.
Because it comes with "batteries included" i.e. libraries for whatever you want.
Research
Game Development
Windows Applications
What do we notice?
Avoid mixing tabs and spaces in the indentation of a given single block, unless you know
what every system that touches your code may do with tabs. Otherwise, what you see in
your editor may not be what Python sees when it counts tabs as a number of spaces. It's
safer to use all tabs or all spaces for each block; how many is up to you.
First of all, you can write the inner block all on one line if you like, therefore not having to
care about indentation at all. The following three versions of an "if" statement are all valid
and do exactly the same thing (output omitted for brevity):
The "else" always applies to the nearest "if", unless you use braces. This is an
essential problem in C and C++. Of course, you could resort to always use
braces, no matter what, but that's tiresome and bloats the source code, and it
doesn't prevent you from accidentally obfuscating the code by still having the
wrong indentation. (And that's just a very simple example. In practice, C code
can be much more complex.)
In Python, the above problems can never occur, because indentation levels and
logical block structure are always consistent. The program always does what you
expect when you look at the indentation.
What are those weird variables that have two leading and ending underscores
such as __name__ and what is the use of __main__?
rather than devising a new syntax for special kinds of class methods (such as
initializers and destructors), I decided that these features could be handled by
simply requiring the user to implement methods with special names such as
__init__, __del__, and so forth. This naming convention was taken from C where
identifiers starting with underscores are reserved by the compiler and often have
special meaning (e.g., macros such as __FILE__ in the C preprocessor).
The if __name__ == "__main__": ... trick exists in Python so that our Python files
can act as either reusable modules, or as standalone programs. -- [Demo 5]
Python Versions
Newer versions add new functionality but many times also modify existing
functionality, beware of the changes! You may be unpleasantly surprised that a
function you used to use in the past takes additional or fewer arguments or does
not require brackets etc
HINT: The virtualenv kit provides the ability to create virtual Python environments
that do not interfere with either each other, or the main Python installation.
Python Variables
Dynamic typing
In Java, C++, and other statically typed languages, you must specify the data type of the function return value and each
function argument. On the other hand, Python is a dynamically typed language. In Python you never have to explicitly
specify the data type of anything. Based on what value you assign, Python will keep track of the data type internally.
Int
Float
Strings
Tuples
Mutable vs Unmutable
The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is
unchangeable once they are created are called immutable.
An objects mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and
lists are mutable.
Remember when I was talking about Python different versions? Well when I was
trying to divide two integers together I was always getting a float result! Why?
You need to know at least that the math functionalityischanging, even if you
don't care as much about the actual implementation. Many programmers recognize
that this is one of the most controversial updates to Python so far.
What is "true division" you ask? It means that even if with a pair of integer
operands, the result is a floating-point value instead of a truncated integer result.
Python - Strings
str class
String functions
String concatenation
Python - Lists
List functionality
Mutable - that is, you can delete or add elements, or change the value of any of the elements inside
the list.
Lists cannot be used in certain contexts. For example, you can't use a list as the key in a dictionary.
List slicing/stepping
How do we go about retrieving list members in popular languages such as C++ and Java? Last, first, first five,
last six, reverse etc?
To
Python - Tuples
Immutable - Once you have assembled a tuple, you cannot add or delete
elements or change the value of an element inside the tuple.
You can't add elements to a tuple. Tuples have no append or extend method.
You can't remove elements from a tuple. Tuples have no remove or pop method.
Tuples are faster than lists. If you're defining a constant set of values and all you're
ever going to do with it is iterate through it, use a tuple instead of a list.
Python - Dictionaries
Capabilities:
Look-up by key.
Python uses True and False for bool operations. Please note the capitalized
first letter.
None. Basically, the data type means non existent, not known or empty. Use
for error checking etc
If/else
Parentheses arenotneeded around the condition. Use parentheses to group subexpressions and control order of evaluation when the natural operator precedence
is not what you want.
For loop
While loop
Python Functions
Remember the indentation rules and the colon after the parenthesis.
Python functions do not need to specify return types, if you need to return
one or more values (data structure) then just use the return keyword.
Depending on who will use the program you may have to validate input
parameters.
Python - Classes
Python encapsulation
Many Python users don't feel the need for private variable. Some
considerinformation hidingto beunpythonic, in that it suggests that the class in
question contains ill-planned internals
Comparison
Ternary operator
Power of operator
List comprehensions
Lambdas
Text Editors
Sublime Text
Notepad++
IDEs
Eclipse
PyCharm
Wing
Start Coding!
Summary
Python has a lot of hidden gems, I am amazed that every time I sit down to code in Python I encounter
something new. Angry about the lack of braces? How about from __future__ import braces? [discovered
last night]
How many people think I had to install Python on the machine today to show you the demos?
Python usage and community is rapidly growing. Community is also as fun and friendly as Python itself ->
comments about the ability to import braces
As we are moving towards the cloud era, web languages are useful to know
Python has countless sources online to help you learn, making it tantalizingly hard to resist!
Q&A