+ - 0:00:00
Notes for current slide
Notes for next slide

Lecture 7


DANL 100: Programming for Data Analytics

Byeong-Hak Choe

September 20, 2022

1 / 40

Announcement

Tutoring and TA-ing Schedules

  • Marcie Hogan (Tutor):
    1. Sunday, 2:00 PM--5:00 PM
    2. Wednesday, 12:30 PM--1:30 PM
  • Andrew Mosbo (Tutor):
    1. Mondays, 4:00 PM--5:00 PM
    2. Wednesdays, 11:00 A.M.--noon
    3. Thursdays, 5:00 PM--6:00 PM
  • Emine Morris (TA):
    1. Mondays and Wednesdays, 5:00 PM--6:30 PM
    2. Tuesdays and Thursdays, 3:00 PM--4:45 PM
2 / 40

Workflow

Shortcuts

  • F9 runs a current line (where the blinking cursor bar is) or selected lines.

  • Home/End moves the blinking cursor bar to the beginning/end of the line.

    • Fn + / works too.
  • PgUp/PgDn moves the blinking cursor bar to the top/bottom line of the script on the screen.

    • Fn + / works too.
3 / 40

Workflow

Shortcuts

Mac

  • command + N opens a new script.
  • command + 1 is the shortcut for #.
  • command + 4 is the shortcut for block comment.

Windows

  • Ctrl + N opens a new script.
  • Ctrl + 1 is the shortcut for #.
  • Ctrl + 4 is the shortcut for block comment.
  • # %% defines a coding block in Spyder IDE.
4 / 40

Workflow

More Shortcuts

  • Ctrl (command for Mac Users) + Z undoes the previous action.
  • Ctrl (command for Mac Users) + Shift + Z redoes when undo is executed.
  • Ctrl (command for Mac Users) + F is useful when finding a phrase in the script.
  • Ctrl (command for Mac Users) + R is useful when replacing a specific phrase with something in the script.
  • Ctrl (command for Mac Users) + D deletes a current line.
5 / 40

Choose with if


6 / 40

Choose with if

if-else statement

  • The if and else lines are Python statements that check whether a condition is a boolean True value, or can be evaluated as True.
disaster = True
if disaster:
print("Woe!")
else:
print("Whee!")
  • The recommended style is to use four spaces before the statement below if/else.
7 / 40

Choose with if

Compare with if, elif, and else

If there are more than two possibilities to test, use if for the first, elif (meaning else if) for the middle ones, and else for the last:

color = "mauve"
if color == "red":
print("It's a rasberry")
elif color == "green":
print("It's a green chili")
elif color == "bee purple":
print("I don't know what it is, but only bees can see it")
else:
print("I've never heard of the color", color)
8 / 40

Choose with if

Compare with if, elif, and else

  • Indentation determines how the if and else sections are paired.
furry = True
large = True
if furry:
if large:
print("It's a yeti.")
else:
print("It's a cat!")
else:
if large:
print("It's a whale!")
else:
print("It's a human. Or a hairless cat.")
9 / 40

Choose with if

Comparison Operators

  • Here are Python's comparison operators:

10 / 40

Choose with if

Comparison Operators

  • Here are Python's comparison operators:
# Assign x to 7
x = 7
x == 5 # Test equality
x == 7
5 < x
x < 10
11 / 40

Choose with if

Boolean Operator

  • The shaded regions show which parts each Boolean operator (and, or, not) selects.

  • A and B is True if both A and B statements are True.

  • A and B is False otherwise.

  • A or B is True if either A or B statement is True.

  • A or B is False otherwise.

  • A and not B is True if A statement is True and B statement is False.

  • A and not B is False otherwise.

12 / 40

Choose with if

Comparison Operators

  • Logical operators have lower precedence than the chunks of code that they’re comparing.

    • This means that the chunks are calculated first, and then compared.
5 < x and x < 10
  • The easiest way to avoid confusion about precedence is to add parentheses:
(5 < x) and (x < 10)
13 / 40

Choose with if

Comparison Operators

  • Here are some other tests:
5 < x or x < 10
5 < x and x > 10
5 < x and not x > 10
  • If you’re and-ing multiple comparisons with one variable, Python lets you do this:
5 < x < 10 # It’s the same as 5 < x and x < 10
14 / 40

Choose with if

What Is True?

  • What does Python consider True and False?
  • A False value doesn’t necessarily need to explicitly be a boolean False.
    • The followings are all considered False:

  • Anything else is considered True.
15 / 40

Choose with if

What Is True?

  • Python programs use these definitions of "truthiness" and "falsiness" to check for empty data structures as well as False conditions:
some_list = []
if some_list:
print("There's something in here")
else:
print("Hey, it's empty!")
16 / 40

Choose with if

Do Multiple Comparisons with in

  • Suppose that you have a letter and want to know whether it’s a vowel.
    • One way would be to write a long if statement:
letter = 'o'
if letter == 'a' or letter == 'e' or letter == 'i' \
or letter == 'o' or letter == 'u':
print(letter, 'is a vowel')
else:
print(letter, 'is not a vowel')
17 / 40

Choose with if

Do Multiple Comparisons with in

  • Whenever you need to make a lot of comparisons like that, separated by or, use Python’s membership operator in, instead.
vowels = 'aeiou'
letter = 'o'
letter in vowels
if letter in vowels:
print(letter, 'is a vowel')
18 / 40

Choose with if

Do Multiple Comparisons with in

  • Here are some examples of how to use in with some data types:
letter = 'o'
vowel_set = {'a', 'e', 'i', 'o', 'u'}
letter in vowel_set
vowel_list = ['a', 'e', 'i', 'o', 'u']
letter in vowel_list
vowel_tuple = ('a', 'e', 'i', 'o', 'u')
letter in vowel_tuple
vowel_dict = {'a': 'apple', 'e': 'elephant',
'i': 'impala', 'o': 'ocelot',
'u': 'unicorn'}
letter in vowel_dict
  • For the dictionary, in looks at the keys (the left-hand side of the :) instead of their values.
vowel_string = "aeiou"
letter in vowel_string
19 / 40

Choose with if

Walrus Operator

  • The walrus operator looks like this:

tweet_limit = 280
tweet_string = "Blah" * 50
diff = tweet_limit - len(tweet_string)
if diff >= 0:
print("A fitting tweet")
else:
print("Went over by", abs(diff))
tweet_limit = 280
tweet_string = "Blah" * 50
if ( diff := tweet_limit - len(tweet_string) ) >= 0:
print("A fitting tweet")
else:
print("Went over by", abs(diff))
20 / 40

Choose with if

Class Exercises

  1. Choose a number between 1 and 10 and assign it to the variable secret. Then, select another number between 1 and 10 and assign it to the variable guess. Next, write the conditional tests (if, else, and elif) to print the string 'too low' if guess is less than secret, 'too high' if greater than secret, and 'just right' if equal to secret.

  2. Assign True or False to the variables small and green, respectively. Write some if/else statements to print which of these matches those choices: cherry, pea, watermelon, pumpkin.

21 / 40

Text Strings


22 / 40

Text Strings

  • Data scientists often work with strings of text.

  • Strings in Python are immutable.

    • We can’t change a string in place, but we can copy parts of strings to another string to get the same effect.
23 / 40

Text Strings

Create with Quotes

  • We make a Python string by enclosing characters in matching single or double quotes:
'Business'
"Data"
  • The Console (interactive interpreter) echoes strings with a single quote, but all are treated exactly the same by Python.
24 / 40

Text Strings

Create with Quotes

  • Why have two kinds of quote characters?
    • The main purpose is to create strings containing quote characters.
    • We can have single quotes inside double-quoted strings, or double quotes inside single-quoted strings.
"'Nay!' said the naysayer. 'Neigh?' said the horse."
'The rare double quote in captivity: ".'
'A "two by four" is actually 1 1⁄2" × 3 1⁄2".'
"'There's the man that shot my paw!' cried the limping hound."
25 / 40

Text Strings

Create with Quotes

  • We can also use three single quotes (''') or three double quotes ("""):
'''Hi!'''
"""Hello!"""
  • The most common use of triple quotes is to create multiline strings:
poem_bluebird = '''there’s a bluebird in my heart that
wants to get out
but I’m too tough for him,
I say, stay in there, I’m not going
to let anybody see
you.'''
26 / 40

Text Strings

Create with Quotes

  • There’s a difference between the output of print() and the automatic echoing done by the interactive interpreter:
poem_bluebird
print(poem_bluebird)
print('Give', "us", '''some''', """space""")
  • print() strips quotes from strings and prints their contents. - It’s meant for human output.
27 / 40

Text Strings

Create with str()

  • We can make a string from another data type by using the str() function:
str(3.141592)
str(1.0e4)
str(True)
28 / 40

Text Strings

Escape with \

  • Python lets us escape the meaning of some characters within strings to achieve effects that would otherwise be difficult to express.

    • By preceding a character with a backslash (\), we give it a special meaning.
  • The most common escape sequence is \n, which means to begin a new line.

    • With this we can create multiline strings from a one-line string:
palindrome = 'A man,\nA plan,\nA canal:\nPanama.'
print(palindrome)
29 / 40

Text Strings

Escape with \

  • The escape sequence \t (tab) is used to align text:
print('\tabc')
print('a\tbc')
print('ab\tc')
print('abc\t')
  • We might also need \' or \" to specify a literal single or double quote inside a string that’s quoted by the same character:
testimony = "\"I did nothing!\" he said. \"Or that other thing.\""
testimony
print(testimony)
  • If we need a literal backslash (\), type two of them (the first escapes the second):
speech = 'The backslash (\\) bends over backwards to please you.'
print(speech)
  • A raw string (r) negates these escapes:
info = r'Type a \n to get a new line in a normal string'
info
  • A raw string does not undo any real (not \n) newlines:
poem = r'''Boys and girls, come out to play.
The moon doth shine as bright as day.'''
poem
print(poem)
30 / 40

Text Strings

Combine by Using +

  • We can combine literal strings or string variables in Python by using the + operator:
'DANL 100: Programming for ' + 'Data Analytics'
  • We can also combine literal strings (not string variables) just by having one after the other:
'DANL 100: Programming for ' 'Data Analytics'
31 / 40

Text Strings

Duplicate with *

  • We use the * operator to duplicate a string.
start = 'Na ' * 4 + '\n'
middle = 'Hey ' * 3 + '\n'
end = 'Goodbye.'
print(start + start + middle + end)
32 / 40

Text Strings

Get a Character with []

  • To get a single character from a string, specify its offset inside square brackets after the string’s name.

    • The first (leftmost) offset is 0, the next is 1, and so on.
    • The last (rightmost) offset can be specified with –1, so you don’t have to count; going to the left are –2, –3, and so on:
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[0]
letters[1]
letters[-1]
letters[5]
letters[100]
33 / 40

Text Strings

Get a Character with []

  • Because strings are immutable, we can’t insert a character directly into one or change the character at a specific index.
name = 'Macintosh'
name[0] = 'P'
  • Instead, we need to use some combination of string functions such as replace() or a slice (which we look at in a moment):
name = 'Macintosh'
name.replace('M', 'P')
'P' + name[1:]
34 / 40

Text Strings

Get a Substring with a Slice

  • We can extract a substring (a part of a string) from a string by using a slice.

  • We define a slice by using square brackets ([]), a start offset, an end offset, and an optional step count between them.

    • We can omit some of these.
  • The slice will include characters from offset start to one before end:

35 / 40

Text Strings

Get a Substring with a Slice

  • [:] extracts the entire sequence from start to end.
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[:]
  • [ start :] specifies from the start offset to the end.
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[20:]
letters[10:]
letters[-3:]
letters[-50:]
  • [: end ] specifies from the beginning to the end offset minus 1.
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[:3]
letters[:-3]
letters[:70]
  • [ start : end ] indicates from the start offset to the end offset minus 1.
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[12:15]
letters[-51:-50]
letters[70:71]
  • [ start : end : step ] extracts from the start offset to the end offset minus 1, skipping characters by step.
letters = 'abcdefghijklmnopqrstuvwxyz'
letters[4:20:3] # From offset 4 to 20, by steps of 3 characters
letters[::7] # From the start to the end, in steps of 7 characters
letters[19::4] # From offset 19 to the end, by 4
letters[:21:5] # From the start to offset 20 by 5:
letters[-1::-1] # Starts at the end and ends at the start
letters[::-1]
  • Offsets go 0, 1, and so on from the start to the right.
  • Offsets go –1,–2, and so forth from the end to the left.
36 / 40

Text Strings

Get Length with len()

  • The len() function counts characters in a string:
len(letters)
empty = ""
len(empty)

Split with split()

  • We can use the built-in string split() function to break a string into a list of smaller strings based on some separator.
    • If we don’t specify a separator, split() uses any sequence of white space characters---newlines, spaces, and tabs:
tasks = 'get gloves,get mask,give cat vitamins,call ambulance'
tasks.split(',')
tasks.split()

Combine by Using join()

  • join() collapses a list of strings into a single string.
crypto_list = ['Yeti', 'Bigfoot', 'Loch Ness Monster']
crypto_string = ', '.join(crypto_list)
print('Found and signing book deals:', crypto_string)

Substitute by Using replace()

  • We use replace() for simple substring substitution.
setup = "a duck goes into a bar..."
setup.replace('duck', 'marmoset')
setup
setup.replace('a ', 'a famous ', 100) # Change up to 100 of them
setup.replace('a', 'a famous', 100) # If we're unsure the exact substring

Strip with strip()

  • The strip() functions assume that we want to get rid of whitespace characters (' ', '\t', '\n') if we don’t give them an argument.
  • strip() strips both ends, lstrip() only from the left, and rstrip() only from the right.
world = " earth "
world.strip()
world.lstrip()
world.rstrip()

Strip with strip()

  • We can also tell strip() to remove any character in a multicharacter string:
world = " earth "
world.strip(' ')
world.strip('!')
blurt = "What the...!!?"
blurt.strip('.?!')

Count with count()

  • How many times does the three-letter sequence the occur?
poem = '''All that doth flow we cannot liquid name
Or else would fire and water be the same;
But that is liquid which is moist and wet
Fire that property can never get.
Then 'tis not cold that doth the fire put out
But 'tis the wet that makes it die, no doubt.'''
word = 'the'
poem.count(word)
37 / 40

Text Strings

Case and Allignment

  • We have more built-in functions for case and allignment:
setup = 'a duck goes into a bar...'
setup.capitalize() # Capitalize the first word
setup.title() # Capitalize all the words
setup.upper() # Convert all characters to uppercase
setup.lower() # Convert all characters to lowercase
setup.swapcase() # Swap uppercase and lowercase
setup.center(30) # Center the string within 30 spaces
setup.ljust(30) # Left justify
setup.rjust(30) # Right justify
38 / 40

Text Strings

Formatting

  • We’ve seen that we can concatenate strings by using +.

  • Let’s look at how to interpolate data values into strings using f-strings formats.

    • f-strings appeared in Python 3.6, and are now the recommended way of formatting strings.

    • There exists an old style of string formatting with % and a new style of it with {}, which we may not discuss here.

39 / 40

Text Strings

Formatting

  • To make an f-string:

    • Type the letter f or F directly before the initial quote.
    • Include variable names or expressions within curly brackets ({}) to get their values into the string.
thing = 'wereduck'
place = 'werepond'
f'The {thing} is in the {place}'
f'The {thing.capitalize()} is in the {place.rjust(20)}'
40 / 40

Announcement

Tutoring and TA-ing Schedules

  • Marcie Hogan (Tutor):
    1. Sunday, 2:00 PM--5:00 PM
    2. Wednesday, 12:30 PM--1:30 PM
  • Andrew Mosbo (Tutor):
    1. Mondays, 4:00 PM--5:00 PM
    2. Wednesdays, 11:00 A.M.--noon
    3. Thursdays, 5:00 PM--6:00 PM
  • Emine Morris (TA):
    1. Mondays and Wednesdays, 5:00 PM--6:30 PM
    2. Tuesdays and Thursdays, 3:00 PM--4:45 PM
2 / 40
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
oTile View: Overview of Slides
Esc Back to slideshow