#!/usr/bin/env python # coding: utf-8 # Introduction to Python (1) # === # # About Python # === # # History # --- # # * Created early 90's by Guido van Rossem at CWI. # - Name: Monty Python. # * General purpose, high-level programming language. # * Design is driven by code readability. # About Python # === # # Features # --- # # * Interpreted, no separate compilation step needed. # * Imperative and object-oriented programming. # - And some functional programming. # * Dynamic type system. # * Automatic memory management. # # We'll come back to most of this. # About Python # === # # Why Python? # --- # # * Readable and low barrier to entry. # * Rich scientific libraries. # * Many other libraries available. # * Widely used with a large community. # About Python # === # # Python 2 versus Python 3 # --- # # * Python 3 is backwards incompatible. # * Some libraries don't support it yet. # * Python 2.7 is the last Python 2. # * Some Python 3 features are backported in Python 2.7. # * Default Python on most distributions is Python 2.7. # # We use Python 2.7 for the time being. # Running Python code # === # # Two main ways of writing and executing Python code. # # Interactively # --- # # * Statement by statement directly in the interpreter. # # Non-interactively # --- # # * By editing in a file and running the code afterwards. # We'll start with the first option. # Running Python code # === # # The standard Python interpreter # --- # # Start it by typing `python` on the command line: # # $ python # Python 2.7.3 (default, Jan 2 2013, 13:56:14) # [GCC 4.7.2] on linux2 # Type "help", "copyright", "credits" or "license" for more information. # >>> # # * It shows an interpreter prompt. # * You can give it Python code to interpret. # Running Python code # === # # The IPython interpreter # --- # # * Similar to the standard Python interpreter, but with # * syntax highlighting, # * tab completion, # * cross-session history, etcetera... # # Start it by typing `ipython` on the command line: # # $ ipython # Python 2.7.3 (default, Jan 2 2013, 13:56:14) # Type "copyright", "credits" or "license" for more information. # # IPython 0.13.1 -- An enhanced Interactive Python. # ? -> Introduction and overview of IPython's features. # %quickref -> Quick reference. # help -> Python's own help system. # object? -> Details about 'object', use 'object??' for extra details. # # In [1]: # From now on, play along in your own IPython interpreter. # Python as a calculator # === # # Integers # --- # In[1]: 17 # In[2]: (17 + 4) * 2 # Python as a calculator # === # # Floating point numbers # --- # In[3]: 3.2 * 18 - 2.1 # In[4]: 36. / 5 # Scientific notation: # In[5]: 1.3e20 + 2 # In[6]: 1.3 * 10**20 # Python as a calculator # === # # The division operator # --- # In[7]: 35 / 5 # Division is a bit weird: if you give it integer arguments, the result will also be an integer. # In[8]: 36 / 5 # Python as a calculator # === # # The division operator # --- # In[9]: 35 / 5 # In[10]: 36 / 5 # Solution 1 # --- # # Give floating point arguments instead of integer arguments. # In[11]: 36. / 5. # Python as a calculator # === # # The division operator # --- # In[12]: 35 / 5 # In[13]: 36 / 5 # Solution 2 # --- # # From Python 3 onwards, division behaves differently. You can actually get that behaviour in Python 2.7: # In[14]: from __future__ import division 36 / 5 # Variables # === # # * We can use names to reference values (variables). # * No need to declare them first or define the type. # In[20]: a = 1.3e20 b = 2 # In[21]: a # In[28]: c = a + 1.5e19 * b c * 2 # Python's type system (1/4) # === # # Every value has a type, view it using `type`: # In[18]: type(27) # In[19]: type(3.0 * 2.7) # In[20]: type(a) # Python's type system (2/4) # === # # Another example of a builtin datatype is `str`, we'll see more later: # In[21]: type('I am a homo sapiens') # Some operations are defined on more than one type, possibly with different meanings. # In[22]: 'beer' * 5 + 'whiskey' # Python's type system (3/4) # === # # Dynamic typing means that variables can be assigned values of different types during runtime. # In[9]: a # In[10]: type(a) # In[5]: a = 'spezi' type(a) # Python's type system (4/4) # === # # Python is strongly typed, meaning that operations on values with incompatible types are forbidden. # In[24]: 'beer' + 34 #
#

Hands on!

# #
    #
  1. We’ve seen that b = 2 is legal. #
      #
    • What about 2 = b?
    • #
    • How about a = b = 1?
    • #
    #
  2. #
  3. In math notation you can multiply x and y like this: xy. # What happens if you try that in Python?
  4. #
  5. How many seconds are there in 42 minutes and 42 seconds?
  6. #
  7. How many miles are there in 16 kilometers? Hint: there are 1.61 kilometers in a mile.
  8. #
  9. Let's assume that you run a 42 kilometer race in 4 hours 42 minutes and 42 seconds. #
      #
    • What is your average pace (time per mile in minutes and seconds)?
    • #
    • What is your average speed in miles per hour?
    • #
    #
  10. #
  11. Use string operations to reference string 'tra la la la' in a variable named song.
  12. #
  13. If an article costs 249 Euros including the 19% Value Added Tax (VAT), what is the actual VAT amount in Euros for the corresponding article?
  14. #
# #
# In[1]: from IPython.display import HTML def css_styling(): styles = open('../styles/custom.css', 'r').read() return HTML('') css_styling() # Acknowledgements # ======== # # Martijn Vermaat # # [Jeroen Laros](mailto:j.f.j.laros@lumc.nl) # # Based on # --------- # [Python Scientific Lecture Notes](http://scipy-lectures.github.io/) # # License # -------- # [Creative Commons Attribution 3.0 License (CC-by)](http://creativecommons.org/licenses/by/3.0)