Python for Developers

Python Classes in RU

  • Objective:
    • 20% lecture
    • 80% labs and interaction

You Are Here

Introductions

  • Length of time at Rackspace
  • Job/Role
  • What is your programming experience?
  • How long have you been programming?
  • Why are you learning Python?
  • Favorite programming language?
  • Most hated programming language?

Resources

  • https://github.com/jeremyprice/PythonForDevs
  • Two books in PDF format:
    • Think Python
    • Python for Informatics
  • DemoProgs
    • a collection of Python programs that concentrate on specific aspects of the language
  • Samples
    • a set of screen shots designed to augment classroom discussion.
  • LabsData
    • data and code to be used in lab exercises.

Resources

  • https://github.com/jeremyprice/PythonForDevs
  • Python Notes
    • a set of notes with explanations of various topics and pointers to articles containing more explanations.
    • It is also a collection of methods used by various Python data structures.
  • Python Notes Addendum
    • a short document containing pointers to intermediate-level Python topics.
  • LabsDone
    • a collection of completed labs to be sent at the conclusion of the class.

Logistics

The Zen of Python

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Our Lab Environment

  • Command line / Vim / Any other editor
  • I suggest Atom unless you have a favorite already  https://atom.io
  • IDLE
    • Very modest IDE
    • Really primarily a calculator/editor
    • Interactive vs Editing windows
  • Demo

Python Help

  • Google!
  • Interactive shell - help()
  • At bash prompt: pydoc
    • pydoc -k string
      pydoc module
      pydoc -p 9999
  • Windows Powershell 
    • python –m pydoc module
  • Pydoc documentation
    • pydoc pydoc

Python Basics

Print Statement

  • The print statement is used to output one or more expressions separated by commas.
  • General form:
  •  
    • where expression can be a variable, literal or any valid expression
  • By default, each item is separated by a space.
  • Automatically skips to next line when done, unless ended with a comma (,)
print expression1[, expression2,…,expressionN][,]

Print Statement Examples

# Examples:
a = 12.3
print 'The value of', a, 'is in variable a.'
print 'Is that enough?'
print 'The value of', a, 'is in variable a.',
print 'Is that enough?'
# Output:
The value of 12.3 is in variable a.
Is that enough?
The value of 12.3 is in variable a. Is that enough?

Basic Data Types

integer

float

string

boolean

None

Identifiers and Types

  • Python uses weak data typing
    • Duck Typing FTW!
  • Data type is established with an assignment statement.
    • x = 10         (integer)
    • y = 12.34         (float)
    • z = ‘text data’      (string)
  • You do not specify a size or precision for numbers.
  • Integers are essentially unlimited in size
  • Floats are all double precision
  • Python is very generous in allocating memory to variables.
  • Demo program – sizecalc.py

Math Operators

  • Compound operators (e.g., +=, -=, etc) work the same in Python as other languages.  

    • No ++ postfix/prefix!

** Exponentiation
/ Division
* Multiplication
+ Addition
- Subtraction
% Modulus (Remainder)
// Floor Division

Operator Precedence

  • Precedence is the same as all other languages
  1. Exponentiation
  2. Multiplication/Division (left to right)
  3. Addition/Subtraction (left to right)
  4. Only parentheses change the order of execution.

Integer Math

  • Math with integers produces an integer result in Python 2.

    • 7/3 = 2

    • 7//3 = 2

  • In Python 3, this changes.  

    • Any division in Python 3 yields a float.  

    • 7/3 = 2.3333

    • Only floor division gives  7//3 = 2

Variable Assignment

  • General assignments:
x = 12.3
x = x + 1
y = x + 117
z = 12 * x / y
a = “This is a line of text”
b = “ and so is this”
c = a + b    # (string concatenation – do not mix strings with numbers)
d = 20 * ‘-’ # (string replication)

Variable Assignment

  • In Python, there can be more than one variable on the left side.

 

 

  • This swaps the values of the two variables
  • Later, this technique will be used to unpack multi-valued variables into individual variables.
  • See a0Assignments.jpg in Samples
x, y = y, x  

Comments and String Literals

  • Comments work the same as most languages. A # is used as the delimiter.
  • String literals can be enclosed in single quotes (') or double quotes (") if they are contained on one line. 
  • triple single-quotes (''') or triple double-quotes (""") are usually employed with literals or documentation that spans multiple lines.

Lab 01 - Formulas

  • Create a program to solve these problems and print the results.  Place each value specified by the problem into a variable before doing any calculations.  Do not worry about the formatting of the answers.  Your program should be such that you can change any of the numbers below and get a new answer.
    • You bought 125 shares of a stock at $25.32 and you sold it at $48.97.  What is the profit?
    • A product with a price of $127.99 is going on sale.  If the price is reduced by 16%, what will the new price be?

Formatting Data

Formatting Data into Strings

  • General examples:
x = 'Some text {} more text {}'.format(12, 17.426)
# Result: 'Some text 12 more text 17.426'

x = 'Some text {1} more text {0}'.format(12, 17.426)
# Result: 'Some text 17.426 more text 12 '

x = 'Some text {0:5d} more text {1:7.2f}'.format(12, 17.426)
# Result: 'Some text    12 more text   17.43'
# (Note rounding)

See examples in Sample folder - a2Formats.jpg and a3Formats.jpg

a2Formats

a3Formats

Lab 02

  • Go back and format the results from the last lab.

 

  • For more examples, see Python Notes and a2Formats.jpg and a3Formats.jpg in the Samples folder.

 

  • This is the newer method.  The older formatting style in Python is a bit different, but quite similar. 

 

  • Example: “There are %d %s in the box” % (5, “cookies”)

 

  • Further examples are in a1Formats.jpg in the Samples folder.

Thanks!

Don't forget to submit your feedback in mylearn

Python for Developers

By Rackspace University

Python for Developers

Introduction to Python for Developers already using other languages

  • 900