String operations; More on function definitions; Conditional execution Intro2CS – week 2
Essence of Programming Take basic operations Combine them to do something more complex print(“some string”) print(“*******”) print(“* HI! *”)
Abstraction It is often useful to take something complex and encapsulate it. Then we can use it as a new basic operation def print_boxed_hi(): print(“*******”) print(“* HI! *”) print(“And now, a greeting:”) print_boxed_hi()
Basic operations seen so far Output Assignment, Arithmetic operations Turtle stuff print(“Hello World”) r1 = (-b + math.sqrt(b*b – 4*a*c)) / (2*a) turtle.forward(150)
Combination methods seen so far Sequential execution Conditional Execution Repeated Execution (loops) do_something1() do_something2() Combination methods to come if condition: do_something() for x in some_range_of_values: do_something(x)
This Lecture More basic operations on Strings More flexibility in defining new “operations” (functions) Conditional execution Later in the week: loops
What are all the basic operations? “Old school” programming: minimal set of basic operations; come with the language; need to know all of them “New school” programming: many basic operations; can get more libraries; need to know how to find what you need Python philosophy: “batteries included”
Why spend time on String Operations? Example of quickly learning new basic operations No need to remember everything Useful Maybe you should remember… Example of a complex data type More to come later (e.g. list=[3, 5, 8] )
The Authoritative Reference
The Python Standard Library
Strings in The Python Standard Library 1. Introduction 2. Built-in Functions 3. Built-in Constants 3.1. Constants added by the site module 4. Built-in Types 4.1. Truth Value Testing 4.2. Boolean Operations — and, or, not ... 4.7. Text Sequence Type – str … 4.13. … 37. …
Google it
Stack Overflow
Strings are sequences of characters
Negative indices; Slices
Operations
Unicode
Unicode
Special character encoding
Input
More operations
Defining functions Parameters Return Values Abstraction and Encapsulation
Defining and Using
Comments
Parameters def print_twice(message): “““print the given message twice“““ print(message) s = “Hello” print_twice(s)
Parameters def print_twice(message): “““print the given message twice“““ print(message) Hello s = “Hello” print_twice(s) s
Parameters def print_twice(message): “““print the given message twice“““ print(message) Hello s = “Hello” print_twice(s) s message
Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) s = “Hello” print_twice(s)
Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) Hello s = “Hello” print_twice(s) s message
Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) Hello Hello\nHello s = “Hello” print_twice(s) s message
Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) Hello Hello\nHello s = “Hello” print_twice(s) s message
Scope
Name conflict
Name conflict
Multiple parameters def print_k_times(message, k): “““print k times the given message“““ message = k *(message + “\n”) print(message) s = “Hello” print_k_times(s, 3)
Multiple parameters def print_k_times(message, k): “““print k times the given message“““ message = k *(message + “\n”) print(message) s = “Hello” print_k_times(message=s, k=3)
Multiple parameters def print_k_times(message, k): “““print k times the given message“““ message = k *(message + “\n”) print(message) s = “Hello” print_k_times(k=3, message=s)
Default values
Returning a value def cubic_root(x): “”” returns the cubic root of x “”” return x**(1/3) y = cubic_root(8) # 2.0 print(cubic_root(y / 2) + 1)
Returning a value import math def fourth_root(x): “”” returns the fourth root of x “”” y = math.sqrt(x) return math.sqrt(y)
Assigning multiple values
Returning multiple values def my_divmod(x, y): “”” returns the quotient and remainder of the integer division x/y“”” return x//y, x%y q, r = my_divmod(23, 4)
Returning multiple values def split_date(s): “”” split a date give in the format dd/mm/yyyy into its components. returns year, date, month (as integers) “”” y = int(s[-4:]) m = int(s[3:5]) d = int(s[:2]) return y, m, d
Conditional Execution If statement More on Boolean values
Combining Operations Do this and then do that do_this() do_that() In case 1 do this; otherwise do that Case 1? yes no do_this() do_this() do_that() do_that() do_this() do_that() if case1(): do_this() else do_that()
If - else
Multiple statements in If-else
Elif and Nested Ifs if age > 18: if country == “Germany”: drink = “beer” elif country == “France”: drink = “wine” elif country == “Russia”: drink = “vodka” else drink = “water” elif age > 5: drink = “soda” else: drink = “milk”
Boolealn Expressions
Boolean Functions
Another Implementation
If as an expression