Programming Principles in Python (CSCI 503)
Programming Principles in Python (CSCI 503)
Strings
Dr. David Koop
(some slides adapted from Dr. Reva Freedman)
D. Koop, CSCI 503/490, Fall 2021
Sets & Operations
? s = {'DeKalb', 'Kane', 'Cook', 'Will'} t = {'DeKalb', 'Winnebago', 'Will'}
? Union: s | t # {'DeKalb', 'Kane', 'Cook', 'Will', 'Winnebago'} ? Intersection: s & t # {'DeKalb', 'Will'} ? Difference: s - t # {'Kane', 'Cook'} ? Symmetric Difference: s ^ t # {'Kane', 'Cook', 'Winnebago'} ? Object method variants: s.union(t), s.intersection(t),
s.difference(t), s.symmetric_difference(t)
? *_update and augmented operator variants
D. Koop, CSCI 503/490, Fall 2021
2
Comprehension
? Shortcut for loops that transform or lter collections ? Functional programming features this way of thinking:
Pass functions to functions! ? Imperative: a loop with the actual functionality buried inside ? Functional: specify both functionality and data as inputs
D. Koop, CSCI 503/490, Fall 2021
3
if
List Comprehension
? output = [] for d in range(5): output.append(d ** 2 - 1)
? Rewrite as a map:
- output = [d ** 2 - 1 for d in range(5)]
? Can also lter:
- output = [d for d in range(5) if d % 2 == 1]
? Combine map & lter:
- output = [d ** 2 - 1 for d in range(5) if d % 2 == 1]
D. Koop, CSCI 503/490, Fall 2021
4
if
if
Comprehensions for other collections
? Dictionaries
- {k: v for (k, v) in other_dict.items() if k.startswith('a')}
- Example: one-to-one map inverses
? {v: k for (k, v) in other_dict.items()}
? Be careful that the dictionary is actually one-to-one! ? Sets:
- {s[0] for s in names}
? Tuples? Not exactly
- (s[0] for s in names)
- Not a tuple, a generator expression
D. Koop, CSCI 503/490, Fall 2021
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- an introduction to python
- programming principles in python csci 503
- package fuzzyjoin
- learn python in 1 day guru99
- s python cheat sheet data science free
- pandas cheat sheet python data analysis library
- python programming 1 variables loops and input output
- python mock test iii rxjs ggplot2 python data
- python cheat sheet functions and tricks
Related searches
- financial management principles in healthcare
- financial management principles in health
- financial principles in healthcare
- finance principles in health care
- adult learning principles in nursing
- ethical principles in the workplace
- 7 ethical principles in nursing
- personal principles in life
- ethical principles in nursing definitions
- 4 ethical principles in medicine
- basic ethical principles in research
- basic ethical principles in business