catherine and annie python: part 4. strings strings are interesting creatures. although words are...
TRANSCRIPT
CATHERINE AND ANNIE
Python: Part 4
Strings
Strings are interesting creatures. Although words are strings, anything contained within a set of quotes is treated as a string in Python Examples: ‘38949’ is a string, as is ‘…\w{][{}’, as is ‘hello
world!’ Spaces count as characters! So do punctuation marks
Computers treat strings like ‘lists’ of characters, all strung together So you can access each character of a string using exactly
the same syntax that you would to access an element in a list
In your folder, open the file that says “stringExample.py” Right-click > Edit with IDLE
How do you think the for loop in this program works? What would happen if we didn’t include the comma after the print statement in the loop?
ASCII
ASCII stands for American Standard Code for Information Interchange (meaning it’s a way to communicate information) In ASCII, each character, including numbers, punctuation,
and white space characters are given a number, which is its ASCII value
You can see a full ASCII table at http://en.wikipedia.org/wiki/Ascii
There are two methods that you can use with ASCII: ord(x) – gives you the ASCII value of x, a character chr(x) – gives you the character that corresponds to x, an
ASCII value You can use ord and chr to do some very simple
cryptography (ask Annie or me about it later if you want to know more)
Some useful string operators
+ joins two strings together Example: ‘soccer’ + ‘ball’ = ‘soccerball’
* repeats a string Example: ‘cool’ * 3 = ‘coolcoolcool’
Indexing, works the same way indexing with lists does word = ‘hello’, word[1] = ‘e’
Slicing Used if you want to access a smaller section of a
string; looks kind of like indexing word[1:4] = ‘ell’ **NOTE: this did not return
word[4]; slicing is exclusive: it goes up to, but does not include the second index
The string library
Just as there is a math library that you can use in your programs, there is also a string library that you can use
At the top of the program, you must type import string
This tells the computer that you want to see the functions contained in the string library file
More of the string library
Here’s a partial list of the functions contained in the string library:
string.capitalize(s) – capitalizes s (a string you would give the computer)
string.upper(s) – makes all the characters of s capital letters
string.strip(s) – removes all the white space (blank spaces, tabs, etc.) from the beginning and end of s
string.find(s, sub) – sub is a smaller string that you want to find within the larger string, s
string.count(s, sub) – counts the number of times sub occurs in s
Conditionals
Do you remember the work we did with logic gates yesterday?
Both of these are also used in Python When might you want to use conditionals?
Boolean logic: a quick review
There are a few basic logic gates that we’re working with: NOT: Requires one input, and makes the input opposite; turns true
into false and false into true OR: Requires two inputs, and as long as one input is true, then the OR
gate returns true (or 1) AND: Requires two inputs, and both must be true in order for the
AND gate to be true (or 1) Then there are the exclusive gates (which we don’t really have to worry
about) XOR: Works the same as OR, except that if both inputs are true, XOR
is false XAND: If one input is true, XAND is true. If both inputs are true,
XAND is false And last are the gates that combine NOT and either AND or OR
NAND: AND + NOT NOR: OR + NOT
For both of these, complete the OR or AND gate first, and then negate itusing NOT
Some quick practice using the Python shell
Let’s open the Python shell and see this logic at work
Type in this: x = 5 then press Enter Now type in x == 4 or x > 2 then press
Enter. Did you get the result you were expecting?
Type in x == 5 and x > 8 then press Enter. Did you get the result you were expecting?
Conditional statements
Conditional statements are statements that depend on one or more conditions.
Conditional statements are used in programming when you want to complete a task, but only when certain conditions are met. Example: You want to find the sum of two
numbers, but only if they are both less than 5. How could you test that?
You (the programmer) set the condition, but the computer decides whether or not the condition is met!
There are a few different structures you can use with conditional statements in Python
If-statements
The simplest kind of conditional is a simple if-statement. Python evaluates the condition of the if-statement is true, the statement is completed. Otherwise, Python ignores it and moves on.
Syntax: if(<condition>): The condition can be anything you like, and there can
be one or more than one. You can also use the logic gates OR and AND to test multiple conditions
Example:
If-else statements
If-else statements are used when you want the computer to perform an action even if the condition of your if-statement isn’t true. For example: as long as two number are each less
than five, find their sum. Otherwise, find their difference.
Coded: Though an if-statement on its own will be ignored if its
condition is false, an if-else statement will go straight to the else if the if statement’s condition is false
Notice that an else statement has no condition
If-elif-else statements
If-elif-else statements are used when there are three or more options For example: A traffic light. IF the light is red, stop.
ELSE IF the light is yellow, go. ELSE: go (because the only possibility left is that the light is green.)
The “elif” stands for else if There is only ever one if statement and one else
statement but there can be as many elif statements as you want.
Each condition is evaluated, beginning with the if and ending with the else. Whenever the computer reaches a statement for which the condition is true, it performs that code within that statements and then skips the rest of the statements
What structure would you use in each of these situations?
Determining where a point is on the Cartesian plane If-elif-else
Determining the winner of a basketball game If-else
Determining the number of days in a month If-elif-else
Determining whether you passed a class or not If-else
Your turn!
Open the file called “Python – Lesson 4 Exercises” and complete the exercises