Docsity

Python course: strings, Lecture notes of Programming Languages

Partial preview of the text

Download Python course: strings and more Lecture notes Programming Languages in PDF only on Docsity!

FUNCTIONS

OPTIONAL PARAMETERS

def func_name(par1, par2 = 5): return par1 + par

func_name(4,9) func_name(3)

REVIEW: BOOLEAN

FUNCTIONS

Functions that return Boolean values def is_divisible_by_2(num): return (num % 2 == 0)

REVIEW: DEBUGGING IN

FUNCTIONS

 Preconditions  Postconditions

RECURSION

A technique that solves problem by solving smaller versions of the same problem!

RECURSION

When you turn this into a program, you end up with functions that call themselves (i.e., recursive functions )

FACTORIAL

n! = n*(n-1)! f(n) = n * f(n-1)

RECURSION PARTS

  1. Base case
  2. Recursive Call

FIBONACCI SEQUENCE

0, 1, 1, 2, 3, 5, 8, 13…

FIBONACCI SEQUENCE

f(n) = f(n-1) + f(n-2) where f(0) = 1 f(1) = 1

INFINITE RECURSION

STRING DATA-TYPE

 A string is a sequence of characters  A string literal uses quotes 'Hello' or “Hello”  For strings, + means “concatenate”  When a string contains numbers, it is still a string

TRAVERSING

Computations on string start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal.