Python Introductory Course

Welcome to day 3:

  • Git Intro (commit, push, pull)
  • Complex data types II (Dictionaries, Sets)
  • Functions II (*args, **kwargs)
  • Testing (briefly)
  • File-IO

Presenter Notes

Why you should use Version Control (VCS)

Landscape

Remember: *There are always at least two programmers working on a given project ... *

  • You
  • You in three monthes from now

Presenter Notes

Why you should use VCS - Seriously

  • make sure the code is not broken
  • find out, when (and by whom) it was broken
  • easily test something new and without fear to break the code
  • separate working (production) code from dev code
  • the above for your team of 500 Devs

Presenter Notes

And why Git ?

Landscape

Source: Wikipedia (from Stack Overflow Surveys)

Presenter Notes

Git Starter

  • Create a Git repository from your directory: git init
  • Git has created a subfolder .git where it keeps the records
  • Set your user name and email appropriately:
  • git config [--global] user.name "Your Name Here"
  • git config [--global] user.email your@email.example
  • Add files (to staging area): git add <FILE-1> [<FILE-2>, ..., <FILE-N>]
  • Check with: git status
  • Commit your changes with: git commit -m "Useful commit message"

Presenter Notes

Some Words about branches

  • When you start, there is the master branch
  • GitHub and GitLab have renamed it to main recently
  • master / main reflect the latest software state
  • Feature branches for developing new features, fixing bugs etc.
  • Released versions: usually a specially "tagged" commits

Presenter Notes

Git Workflow

Portrait

Presenter Notes

Git - Graphical Helpers

  • most modern IDEs have git integration
  • Same concepts (staging area, commits, branches)
  • Usually lower support for advanced features (like rebase)
  • gitk
  • gitextensions

Presenter Notes

Git - Recommendations

  • use it
  • small commits
  • with descriptive commit message (avoid "", "just commiting", "some changes" etc.)
  • separate into branches according to topic
  • don't mix topics in commit, e.g. fix a bug in passing and commit to feature topic

Presenter Notes

Git - Thus ...

Landscape

Presenter Notes

Exercises : Day 3 - 1

  • use git-bash to initialize your project directory as a git repository
  • add your files to the staging area
  • commit your changes to git
  • check each step with git status
  • change your file(s) again and try to commit these changes using PyCharm's git interface

Presenter Notes

Complex Data Types - Dictionaries

  • unordered map key → value (python>=3.7: ordered)
  • keys are unique
  • no restriction on values
  • the type is <class 'dict'>
  • construction via dict() constructor

Presenter Notes

Dictionaries - First examples

 1 fruits = {
 2     "apples": "usually appreciated round fruit",
 3     "banana": "yellow is quite a nice color",
 4     "grapes": "the basis for a good wine"
 5 }
 6 
 7 print(fruits["banana"])
 8 
 9 fruits["banana"] = "actually, I don't like them"
10 print(fruits["banana"])

Presenter Notes

Dictionaries - When to use?

  • need to find elements based on a key
  • complexity for get() is between O(1) and O(N)
  • Construction of a "value object" (no functionality, just properties)
  • Useful to construct parameter sets also for possibly missing input: e.g. length = parameters.get("length", length_default)
  • dict comprehension

Presenter Notes

Dictionaries - Methods

Landscape

Presenter Notes

Sets

  • unordered
  • unchangeable elements (but you may add and remove)
  • unindexed
  • unique elements
  • useful for mathematical set operations like intersection, complement etc...

Presenter Notes

Sets - Example

 1 a = set([1, 2, 5, 7, 9])
 2 b = set([12, 2, 10, 9])
 3 c = {4, 5, 6, 7}
 4 
 5 a_int_b = a.intersection(b)
 6 print(a_int_b)
 7 >>> {9, 2}
 8 c_int_a = c.intersection(a)
 9 print(c_int_a)
10 >>> {5, 7}
11 
12 a_union_b = a.union(b)
13 print(a_union_b)
14 >>> {1, 2, 5, 7, 9, 10, 12}

Presenter Notes

Exercises : Day 3 - 2

  • Consider the two dictionaries: {"a": 1, "b": 2, "d": 4, "f": 6} and {"b": 5, "c": 7, "f": 8, "g": 9}. Write an algorithm that combines the two dictionaries keeping only key that are present in both dicts. Add the values of these keys.

  • Reform the following dictionary input_dict = {'A' : {'x' : 5, 'y' : 6}, 'B' : {'x' : 1, 'y' : 4}, 'C' : {'x' : 8, 'y' : 3}} to look as follows: {'x': (5, 1, 8), 'y': (6, 4, 3)}.

Presenter Notes

Functions - Some Python Concepts

"functions are first class objects in Python"

A function...

  • is an instance of the type Object.
  • can be stored in a variable (simple var, list, dictionary, etc...)
  • can be passes as parameter to another function
  • can be returned from a function

Presenter Notes

Functions II - More on arguments

  • recap: positional and named arguments from caller
  • we can allow for further arguments in the function's header def f(a, b, *args, c=1, **kwargs):
  • useful e.g. if you want to call other functions from your function and you want to pass over arguments there
  • don't over-do!

Presenter Notes

Functions II - More on arguments

from within your function:

 1 def f2(*args, **kwargs):
 2     pos_args = args
 3     print(type(pos_args))
 4 
 5     keyword_args = kwargs
 6     print(type(keyword_args))
 7 
 8 
 9 f2(3, 4, a=1, b=7)
10 >>> <class 'tuple'>
11 >>> <class 'dict'>

Presenter Notes

Presenter Notes