james jesus bermas on crash course on python
TRANSCRIPT
Software Freedom Day presentation slide by Manik
Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline LevelEighth Outline LevelNinth Outline Level
Introducing
Agenda
What is Python?
Features
Characteristics
Programming in Python
Modules and Tools
Q&A
Let's agree of what it means, or at least convey a general consensus
What is Python?
Created by Guido Van Rossum as a successor to the ABC programming language
Conceived in 1980, started development in 1989, v1.0 out in 1994
Name is derived from Monty Pythons Flying Circus
Python in the real world
Uses millions of lines of Python code for ad management to build automation
Employs the creator of Python himself
Python in the real world
Industrial Light & Magic Uses Python to automate visual effects
Python in the real world
Uses Python for their desktop application
Python automates the synchronization and file sending over the internet to Dropboxs cloud storage servers
Python in the real world
Django web framework is built using Python
Notable users of Django are some of the biggest websites today
Python in the real world
Blender, a 3D Imaging program uses Python as its main scripting language
Python in the real world
Minecraft uses Python as its scripting language to automate building
Python in the real world
Industrial Light and Magic (Star Wars)
Django web framework (used in Pinterest, Instagram, Mozilla)
Dropbox
LibreOffice
Games (Battlefield, EVE Online, Civilization IV, Minecraft, etc.)
...and many more!
Why Python?
Simple, like speaking English
Its FOSS! Lots of resources available
High level programming language
Object-oriented, but not enforced
Portable, works on a lot of different systems
Can be embedded in programs for scripting capabilities
Extensive libraries available
Extensible
Getting Started
Go to https://www.python.org and download the latest version of Python for your OS
Installing on Windows
Add Python 3.5 to PATH
Running Python
Run the Python interpreter by typing python in the command line
Basic Characteristics
Organized syntax Indentation is strictly enforced
Encourages proper programming practices
Dynamic variable typing Variables are simply names that refer to objects
No defined data type during compile time
Simple and Readable
Python programs look like pseudo-code compared to other prominent languages
Sample code helloworld.py
Indentation
Proper Indentation is enforced to identify sections within your program (functions, procedures, classes)
Sample code guessgame.py
Indentation
Built-in Data Types
Boolean
Numbers (integers, real, complex)
Strings
Sequence Types Tuples
Lists (resize-able arrays)
Range
Set Types Set, FrozenSet
Dictionary
Data Types Tuples
class tuple([iterable]) Immutable sequence of data once assigned, elements within cannot be changed Using a pair of parentheses to denote the empty tuple: ()
Using a trailing comma for a singleton tuple: a, or (a,)
Separating items with commas: a, b, c or (a, b, c)
Using the tuple() built-in: tuple() or tuple(iterable)
Data Types Tuples
Sample code:
Output:
Data Types Tuples
Python ExpressionResultDescription
Len((1,2,3))3Length
(1,2,3) + (4,5,6)(1,2,3,4,5,6)Concatenation
('Hi!',) * 4('Hi!', 'Hi!', 'Hi!', 'Hi!')Repetition
3 in (1, 2, 3)TrueMembership
for x in (1, 2, 3): print(x)1 2 3Iteration
Basic tuple operations
Data Types List
class list([iterable]) Mutable sequence of data once assigned, elements within cannot be changed Using a pair of square brackets to denote the empty list: []
Using square brackets, separating items with commas: [a], [a, b, c]
Using a list comprehension: [x for x in iterable]
Using the type constructor: list() or list(iterable)
Data Types List
Sample code:
Output:
Data Types List
Basic list operations
Python ExpressionResultDescription
Len([1,2,3])3Length
[1,2,3] + [4,5,6][1,2,3,4,5,6]Concatenation
['Hi!',] * 4['Hi!', 'Hi!', 'Hi!', 'Hi!']Repetition
3 in [1, 2, 3]TrueMembership
for x in [1, 2, 3]: print(x)1 2 3Iteration
Built-in Functions
Some built-in functions that work with tuples and lists min() - Return the smallest item
max() - Return the largest item
len() - Return the length of the object
sorted() - Sorts an iterable object
When to use Tuple and List
A tuples contents cannot be changed individually once assigned
Each element of a list can be dynamically assigned
Data Types Range
class range(stop)class range(start, stop[, step]) Represents an immutable sequence of numbers and is commonly used for looping a specific number of times start - The value of the start parameter (or 0 if the parameter was not supplied)
stop - The value of the stop parameter
step - The value of the step parameter (or 1 if the parameter was not supplied)
It only stores the start, stop and step values, calculating individual items and subranges as needed
Data Types Range
Sample code and output:
Data Types Set
class set([iterable])class frozenset([iterable]) An unordered collection of distinct hashable (non-dynamic) objects
Common use includes membership testing, removing duplicates from a sequence, mathematical operations (union, intersection, difference, symmetric difference)
Data Types Set
Sample code and output:
Data Types Dictionary
class dict(**kwarg)class dict(mapping, **kwarg)class dict(iterable, **kwarg) Immutable sequence of data once assigned, elements within cannot be changed
Can be created by placing a comma-separated list of key: value pairs within braces Example: {'apple': 10, 'orange': 12} or {10: 'apple', 12: 'orange'}, or by the dict() constructor
Data Types Dictionary
Sample code:
Output:
Defining Functions
You can define functions to provide the required functionality. Here are simple rules to define a function in Python. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation string of the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function. A return statement with no arguments is the same as return None.
Defining Functions
Sample Code and Output:
Object-oriented Programming
Object: A unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.
Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
Class variable: A variable that is shared by all instances of a class. Class variables are defined within a class but outside any of the class's methods. Class variables are not used as frequently as instance variables are.
Data member: A class variable or instance variable that holds data associated with a class and its objects.
Object-oriented Programming
Instance variable: A variable that is defined inside a method and belongs only to the current instance of a class.
Inheritance: The transfer of the characteristics of a class to other classes that are derived from it.
Instance: An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
Instantiation: The creation of an instance of a class.
Method : A special kind of function that is defined in a class definition.
Object-oriented Programming
In this example, we can see a basic demonstration of using Classes and Object-Oriented programmingClass Employee
Class variable empCount
Data member name, salary
Instance emp1 and emp2
Inhertiance emp1 and emp2 inherits the properties of the class Employee
Method displayCount(), displayEmployee()
Commonly Used Standard Modules
time provides objects and functions for working with Time
calendar - provides objects and functions for working with Dates
os, shutil contains functions that allow Python to interact with the operating system and the shell
sys common utility scripts needed to process command line arguments
re regular expression tools for advanced string processing
math gives access to floating point Mathematical functions and operations
urllib access internet and processing internet protocols
Smtplib sending email
Graphical Interfaces
Notable Third-party Modules
Tools