Skip to content

brettkromkamp/uit-python-course

Repository files navigation

Introductory Python Course - UiT

Welcome to this introductory programming course based on the Python programming language.

Recommended Python Learning Path

Basic Topics

  • Variables
  • Conditions
  • Chained Conditionals
  • Operators
  • Control Flow
  • Loops and Iterables
  • Basic Data Structures
  • Functions
  • Mutable vs. Immutable
  • Common Methods
  • File IO

Intermediate Topics

  • Object Oriented Programming
  • Data Structures
  • Comprehensions
  • Lambda Functions
  • Map, Filter
  • Collections
  • *args & **kwargs
  • Inheritance
  • Dunder Methods
  • PIP
  • Environments
  • Modules
  • Async IO

Advanced Topics

  • Decorators
  • Generators
  • Context Managers
  • Metaclasses
  • Concurrency
  • Parallelism
  • Testing
  • Packages

Iterators and Generators

The Iterator Protocol

x = iter([1, 2, 3])
>>> x
<listiterator object at 0x1004ca850>
>>> next(x)
1
>>> next(x)
2
>>> next(x)
3
>>> next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Example Implementations

class Range:
    def __init__(self, n):
        self.i = 0
        self.n = n

    def __iter__(self):
        return self

    def __next__(self):
        if self.i < self.n:
            i = self.i
            self.i += 1
            return i
        else:
            raise StopIteration()
class ReverseRange:
	def __init__(self, count):
		self.i = count
		self.count = count
	def __iter__(self):
		return self
	def __next__(self):
		if self.i > 0:
			i = self.i
			self.i -= 1
			return i
		else:
			raise StopIteration()

Generators

def yrange(n):
    i = 0
    while i < n:
        yield i
        i += 1
def even_generator(numbers):
    for number in numbers:
        if number % 2 == 0:
            yield int(number)

Generator Comprehension

Template

my_gen = (<expression> for <item> in <iterable> if <condition>)

Example

eg = (int(number) for number in my_list if number % 2 == 0)

Comprehensions

List Comprehension

Template

my_list = [<expression> for <item> in <iterable> if <condition>]

Example

squares = [i * i for i in range(10)]

Dictionary Comprehension

Template

my_dict = {<key>:<value> for <item> in <iterable> if <condition>}

Example

dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
# Double each value in the dictionary
double_dict1 = {k:v*2 for (k,v) in dict1.items()}  

Set Comprehension

Template

my_set = {<expression> for <item> in <iterable> if <condition>}

Example

>>> {s for s in [1, 2, 1, 0]}
set([0, 1, 2])
>>> {s**2 for s in [1, 2, 1, 0]}
set([0, 1, 4])
>>> {s**2 for s in range(10)}
set([0, 1, 4, 9, 16, 25, 36, 49, 64, 81])

Truthy and Falsy in Python

Falsy Values

Sequences and Collections

  • Empty lists []
  • Empty tuples ()
  • Empty dictionaries {}
  • Empty sets set()
  • Empty strings ""
  • Empty ranges range(0)

Numbers

  • Zero of any numeric type
  • Integer: 0
  • Float: 0.0
  • Complex: 0j

Constants

  • None
  • False

Truthy Values

By default, an object is considered true.

Truthy Values

  • Non-empty sequences or collections (lists, tuples, strings, dictionaries, sets).
  • Numeric values that are not zero.
  • True

Resources

About

Repository for the Python Introductory course and the AI and Machine Learning course at the UiT, August-December 2021.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages