Introduction to Python

Maria Dost, Jorin Diemer, Jens Hahn - 27/10/2017

Content

  1. Introduction

    • Python version
    • Ways to write Python
  2. Everything is an object

    • Objects, instances, methods, attributes
    • References, Mutable and Immutable objects
  3. Standard data types

    • Text: String
    • Numeric: int, float, complex
    • Sequence: list, tuple
    • Mapping: dictionary
    • Boolean: True/False
  1. Operators and Flow Control

    • Mathematical and Logical Operators
    • for
    • if/else/elif
    • while
  2. Functions and Methods

    • Definition
    • Return
  3. Classes, Objects, and Instances

    • Definition
    • Instances
  4. Literature & Links


Introduction

Python 2 or Python 3?

back

Short version: Python 2.x is legacy, Python 3.x is the present and future of the language final

  • 2.x version 2.7 was released 2010
  • 3.0 was released in 2008
  • 3.4 was released 2014
  • recent standard library improvements are only available in 3.x

Software

A Python interactive mode makes it easy to test short snippets of code, but there are also a bunch of editors like Sublime Text available as well as bundled development environments IDEs like PyCharm.

We will use:

You could use:


Everything is an object

back

  • Python is an object oriented programming language
  • Objects are instances of classes
  • Class = Blueprint of an object
  • Many objects can be made of the same class

Analogy: Blueprint of a house

  • All classes have attributes
  • Attributes can be either data or methods
  • A method is a function which belongs to an object

Example: the data type integer is a class

  • Defining a variable (a = 5) makes an instance (object) of class int
  • It holds the number 5
  • The variable a is a reference to this object
  • int has methods defined, for example bit_length()

Standard Data Types

back

  • Datatypes are classes $\rightarrow$ Variables are references to instances (objects) of these classes
  • What do you think are standard data types?
  • In general we will distinguish between mutable and immutable data types
  • a mutable object can be changed after it is created, an immutable object can’t

String

  • String is a sequence of characters
  • Strings can be single line ( 'This is a String' ) or multiline (''' This is a String...''' )
  • Indexing can be used to extract elements from a String, e.g. my_string[2] == 'i'
  • Strings are immutable
#create string
    typo_str = 'Peeface'

    #Oh no... a typo, not a good way to start a book, so change it
    typo_str[1] = 'r'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-a40cf4edfc3f> in <module>()
----> 1 typo_str[1] = 'r'

TypeError: 'str' object does not support item assignment

In [ ]:
# print something

Numerical Data Types

  • Python has integers, floating point numbers and complex numbers
  • Python assigns a data type to each variable $\rightarrow$ 1 is an int, 1.0 is a float (duck typing)
  • Mathematical and Logical Operators can be used, e.g.
    a = 5
    b = 6
    c = a + b

In [ ]:
print(c)

Sequence Data Types

List

  • List is an ordered sequence of items
  • Items don't have to be of the same type, because actually it's a list of references
  • Definition of a list: list_a = [5, 3.0, 'Hello']
  • Indexing starts from 0
  • Indexing can be used to extract (and set) elements from the list, e.g. list_a[2] would give 'Hello'
#creating the list
    list_a = [5, 3.0, 'Hello']

    #extract third element/object and create reference to it
    hello_str = list_a[2]

    #mutate list
    list_a[2] = 'Good bye'

    #print new list
    print(list_a)

[5, 3.0, 'Good bye']


In [ ]:
# what is hello_str

In [ ]:
# reverse indexing

In [ ]:
# stepwise indexing (start:stop:step)

Tuple

  • Tuples are immutable lists $\rightarrow$ can't be changed once created
  • Definition of a tuple: tuple_a = (5 , 3.0, 'Hello')
#create tuple
    tuple_a = (5, 3.0, 'Hello')

    #extract third element/object and create reference to it
    hallo_str = tuple_a[2]

    #mutate it???
    tuple_a[2] = 'Good bye'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-2ae0e9084ab6> in <module>()
----> 1 tuple_a[2] = 'Good bye'

TypeError: 'tuple' object does not support item assignment

In [ ]:

Dictionaries

  • a dictionary is an unordered collection of key:value pairs
  • it enables you to map keys and objects
  • a dictionary is mutable
my_dict = {'one':1, 'two':2, 'three':3}

In [ ]:
my_dict = {'one':1, 'two':2, 'three':3}
print(my_dict['one'])

In [ ]:
# how to test if certain key is in dict
print('one' in my_dict)
print('four' in my_dict)

Boolean

  • A Boolean can only hold the values True or False
  • Can be compared by logical operators

Mutable vs. Immutable


In [ ]:
### example immutable
x = 10
y = x
x = x + 1
print('y: ',y)

### example mutable
x_l = ['10']
y_l = x_l
x_l.append('11')
print('y_l: ', y_l)

Operators and Flow control

back

Operators

Arithmetic Operators

  • Used for mathematical operations
  • plus (+), minus (-), multiply (*), divide (/), modulus (%), raise (**)

Comparison Operators

  • Used to compare two values
  • Returns True or False as result
  • Greater than (>), lesser than (<), equal (==), not equal (!=)
  • Attention: = is an assignment, == is the comparison operator

Logical Operators

  • Used to connect logical statements
  • and, or and not

Flow Control

for Loop

  • the for loop is used to iterate over a sequence

    for x in y:    
      counter = counter +1
    
  • Loop continues until it reaches last item in y

  • Example: Sum of numbers

    # List of numbers
          numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
    
          # variable to store the sum
          sum = 0
    
          # iterate over the list
          for val in numbers:
              sum = sum + val
    

if/elif/else

  • These are used to make decisions in a program
    if x == 2:  # expression
              print(my_string)  # statement
    
  • expression is evaluated, if True, statements will be executed
    if x == 2:  # expression
              print(my_string)  # body of if
          else:
              print(your_string)  # body of else
    
  • expression is evaluated, if True, statement in body of if will be executed, otherwise statement in body of else will be executed
    if x == 2:  # 1st expression
              print(my_string)  # body of if
          elif x == 3:  # 2nd expression 
              print(our_string)  # body of elif
          else: 
              print(your_string)  # body of else
    
  • elif is short for else if
  • 1st expression is evaluated, if True, statement in body of if will be executed, otherwise 2nd expression will be checked and so on...

while Loop

  • while iterates over a block of code, as long as a statement is True
  • Is used when the number of times the loop has to be executed is not known beforehand (compare: for loop)

    while x < 2:  # expression
              print(our_string)
    
  • expression is checked before entering the loop for the first time


Functions and Methods

back

Functions

  • A function is a group of statements, which perform a specific task
  • Functions break a program into smaller pieces, making it easier to organize large programs
  • Make code reusable
    def my_statement_printer(statements):
              """This is my docstring:
                 Function to print my statements"""
              for statement in statements:
                  print('I think ' + statement)
    
  • def is the keyword for the function definition
  • Parameters or arguments are objects which are passed to the function when calling it
  • A docstring contains a description of what the function does
  • In the function body are the tasks performed by the function
  • Function can have a return statement, if no return value is given the function returns None

Example of a Function

def add(num1,num2):
            """This function adds two numbers"""
            return num1 + num2
  • To call this function, we need to type the function name and the arguments
    print(add(2,3))
    
  • If you don't now what a function does, you can call the docstring
    print(add.__doc__)
    
  • The return statement exits a function

Parameters inside the Function (Scope)

  • A variable defined inside a function is not visible outside the function

    def func():
      x = 10
    print(x)
    
      ---------------------------------------------------------------------------
      NameError                                 Traceback (most recent call last)
      <ipython-input-3-fc17d851ef81> in <module>()
      ----> 1 print(x)
    
      NameError: name 'x' is not defined
  • The x inside func is not visible outside, so they can have the same name
  • x inside func is a local variable inside the function

Default values

def add(num1,num2=1):
            """This function adds two numbers, if no second number is defined it adds one to num1."""
            return num1 + num2

Methods

  • A method is a function inside a class

Classes, Objects, Instances

back

As already mentioned everything, that means every thing in python is an object. An object is an instance of a class, which defines which attributes and methods an object holds.

  • class is the keyword for class definition
  • a class holds attributes (data and methods)
  • using python's dir argument one can list all methods of an object

In [ ]:
y = 5
dir(y)
  • you see: y is not only a number, it has a lot of methods which we can use

In [ ]:
print(y.bit_length())

Creating your own class

which ingredients have cakes in general? $\rightarrow$ data attributes of a class

what can happen with cakes? $\rightarrow$ methods of a class


In [ ]:
import time

class MinimalCake:
    
    def __init__(self, eier=2, mehl=500, milch=250):
        self.eggs = eier
        self.flour = mehl
        self.milk = milch
        self.stirred = False
    
    def stir(self):
        print('stirring...')
        time.sleep(2)
        print('...dough')
        self.stirred = True

In [ ]:
one_minimal_cake = MinimalCake()
print(one_minimal_cake.eggs, one_minimal_cake.stirred)

In [ ]:
one_minimal_cake.stir()
print(one_minimal_cake.stirred)

Ok, we have dough. But it really doesn't taste well and it can't be baked... That's a disaster, we want choclate and sugar!!

Question: Is a MinimalCake object mutable or immutable?

Please write another class which holds attributes for choclate, sugar and whatever you want. Further, we should be able to bake it. But attention!!! If it is not stirred, baking it would be a waste.


In [ ]:


In [ ]:


In [ ]:


In [ ]:


back