Download presentation
Presentation is loading. Please wait.
Published byLorraine Robbins Modified over 6 years ago
1
String operations; More on function definitions; Conditional execution
Intro2CS – week 2
2
Essence of Programming
Take basic operations Combine them to do something more complex print(“some string”) print(“*******”) print(“* HI! *”)
3
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()
4
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)
5
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)
6
This Lecture More basic operations on Strings
More flexibility in defining new “operations” (functions) Conditional execution Later in the week: loops
7
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”
8
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] )
9
The Authoritative Reference
10
The Python Standard Library
11
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. …
12
Google it
13
Stack Overflow
14
Strings are sequences of characters
15
Negative indices; Slices
16
Operations
17
Unicode
18
Unicode
19
Special character encoding
20
Input
21
More operations
22
Defining functions Parameters Return Values
Abstraction and Encapsulation
23
Defining and Using
24
Comments
25
Parameters def print_twice(message): “““print the given message twice“““ print(message) s = “Hello” print_twice(s)
26
Parameters def print_twice(message): “““print the given message twice“““ print(message) Hello s = “Hello” print_twice(s) s
27
Parameters def print_twice(message): “““print the given message twice“““ print(message) Hello s = “Hello” print_twice(s) s message
28
Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) s = “Hello” print_twice(s)
29
Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) Hello s = “Hello” print_twice(s) s message
30
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
31
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
32
Scope
33
Name conflict
34
Name conflict
35
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)
36
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)
37
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)
38
Default values
39
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)
40
Returning a value import math def fourth_root(x): “”” returns the fourth root of x “”” y = math.sqrt(x) return math.sqrt(y)
41
Assigning multiple values
42
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)
43
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
44
Conditional Execution
If statement More on Boolean values
45
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()
46
If - else
47
Multiple statements in If-else
48
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”
49
Boolealn Expressions
50
Boolean Functions
51
Another Implementation
52
If as an expression
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.