0% found this document useful (0 votes)
2 views

Week 2

The document covers basic concepts of strings in Python, including string literals, escape sequences, and string operators. It also introduces a function design recipe with six steps for creating functions, exemplified by a temperature conversion function from Fahrenheit to Celsius. Additionally, it discusses function reuse by demonstrating how to call one function within another, using examples related to triangle perimeter and area calculations.

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 2

The document covers basic concepts of strings in Python, including string literals, escape sequences, and string operators. It also introduces a function design recipe with six steps for creating functions, exemplified by a temperature conversion function from Fahrenheit to Celsius. Additionally, it discusses function reuse by demonstrating how to call one function within another, using examples related to triangle perimeter and area calculations.

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture 1

Type str: Strings in Python


String Literal

A string literal is a sequence of characters. In Python, this type is called str.


Strings in Python start and end with a single quotes (') or double quotes ("). A
string can be made up of letters, numbers, and special characters. For example:
>>> 'hello'
'hello'
>>> 'how are you?'
'how are you?'
>>> 'short- and long-term'
short- and long-term

If a string begins with a single quote, it must end with a single quote. The same
applies to double-quoted strings. You can not mix the type of quotes.

Escape Sequences

To include a quote within a string, use an escape character (\) before it. Otherwise
Python interprets that quote as the end of a string and an error occurs. For example,
the following code results in an error because Python does not expect anything to
come after the second quote:
>>> storm_greeting = 'wow, you're dripping wet.'
SyntaxError: invalid syntax
The escape sequence \' indicates that the second quote is simply a quote, not the
end of the string:
>>> storm_greeting = 'Wow, you\'re dripping wet.'
"Wow, you're dripping wet."

An alternative approach is to use a double-quoted string when including a a single-


quote within it, or vice-versa. Single- and double-quoted strings are equivalent. For
example, when we used double-quotes to indicate the beginning and end of the
string, the single-quote in you're no longer causes an error:
>>> storm_greeting = "Wow, you're dripping wet."
"Wow, you're dripping wet."

String Operators

Expression Description Example Output


str1 + str2 concatenate str1 and str1 print('ab' + 'c') abc
str1 * int1 concatenate int1 copies of str1 print('a' * 5) aaaaa
int1 * str1 concatenate int1 copies of str1 print(4 * 'bc') bcbcbcbc
Note: concatenate means to join together

The * and + operands obey by the standard precedence rules when used with
strings.

All other mathematical operators and operands result in a TypeError.

Lecture 2(Optional)

Function Design Recipe


The Six Steps

1. Examples
o What should your function do?
o Type a couple of example calls.
o Pick a name (often a verb or verb phrase): What is a short answer to
"What does your function do"?
2. Type Contract
o What are the parameter types?
o What type of value is returned?
3. Header
o Pick meaningful parameter names.
4. Description
o Mention every parameter in your description.
o Describe the return value.
5. Body
o Write the body of your function.
6. Test
o Run the examples.

Applying the Design Recipe

The problem:

The United States measures temperature in Fahrenheit and Canada measures it in


Celsius. When travelling between the two countries it helps to have a conversion
function. Write a function that converts from Fahrenheit to Celsius.

1. Examples
2. >>> convert_to_ccelsius(32)
3. 0
4. >>> convert_to_celsius(212)
5. 100
6.
7. Type Contract
8. (number) -> number
9.
10.Header
11. def convert_to_celsius(fahrenheit):
12.
13.Description
14. Return the number of Celsius degrees equivalent to fahrenheit
degrees.
15.
16.Body
17. return (fahrenheit - 32) * 5 / 9
18.
19.Test
20. Run the examples.
21.

Putting it all together:


def convert_to_celsius(fahrenheit):
''' (number) -> number

Return the number of Celsius degrees equivalent to fahrenheit degrees.

>>> convert_to_ccelsius(32)
0
>>> convert_to_celsius(212)
100
'''

return (fahrenheit - 32) * 5 / 9

LECTURE 3

Function Reuse
Calling functions within other function definitions

The problem: Calculate the semi-perimeter of a triangle.

The approach: Function semiperimeter calls function perimeter.


def perimeter(side1, side2, side3):
'''(number, number, number) -> number

Return the perimeter of a triangle with sides of length


side1, side2 and side3.

>>> perimeter(3, 4, 5)
12
>>> perimeter(10.5, 6, 9.3)
25.8
'''
return side1 + side2 + side3

def semiperimeter(side1, side2, side3):


'''(number, number, number) -> float

Return the perimeter of a triangle with sides of


length side1, side2 and side3.

>>> semiperimeter(3, 4, 5)
6.0
>>> semiperimeter(10.5, 6, 9.3)
12.9
'''
return perimeter(side1, side2, side3) / 2

Calling functions within other function calls

The problem: One triangle has a base of length 3.8 and a height of length 7.0. A
second triangle has a base of length 3.5 and a height of length 6.8. Calculate which
of two triangles' areas is biggest.

The approach: Pass calls to function area as arguments to built-in function max.
max(area(3.8, 7.0), area(3.5, 6.8))

You might also like