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

Lecture 8


DANL 100: Programming for Data Analytics

Byeong-Hak Choe

September 22, 2022

1 / 36

Announcement

Tutoring/TA-ing at Data Analytics Lab (South 321)

  • 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 / 36

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 / 36

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 / 36

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 / 36

Text Strings


6 / 36

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.
7 / 36

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.
8 / 36

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."
9 / 36

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.'''
10 / 36

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)
11 / 36

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)
12 / 36

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)
13 / 36

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'
14 / 36

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)
15 / 36

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:

16 / 36

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]
17 / 36

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:]
18 / 36

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:

19 / 36

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:10:3] # From offset 4 to 9, 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]
20 / 36

Text Strings

Get Length with len()

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

Dot operation

  • In Python, we can access attributes by using a dot notation (.).

  • Unlike len(), some functions use a dot to access to strings.

  • To use those string functions, type (1) the name of the string, (2) a dot, (3) the name of the function, and (4) any arguments that the function needs:

    • string_name.some_function(arguments).

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.'''
word = 'the'
poem.count(word)
21 / 36

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
22 / 36

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 are (1) an old style of string formatting with % and (2) a new style of string formatting with {}, for which we may not discuss here.

23 / 36

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)}'
24 / 36

Loop with while and for


25 / 36

Loop with while and for

  • Sometimes, we need to do something more than once.
    • We need a loop, and Python gives us two choices: while and for.

Repeat with while

count = 1
while count <= 5:
print(count)
count += 1
  • We first assigned the value 1 to count.
  • The while loop compared the value of count to 5 and continued if count was less than or equal to 5.
  • Inside the loop, we printed the value of count and then incremented its value by one with the statement count += 1.
  • Python goes back to the top of the loop, and again compares count with 5.
  • The value of count is now 2, so the contents of the while loop are again executed, and count is incremented to 3.
  • This continues until count is incremented from 5 to 6 at the bottom of the loop.
  • On the next trip to the top, count <= 5 is now False, and the while loop ends.
26 / 36

Repeat with while

Asking the user for input

  • Sometimes we would like to take the value for a variable from the user via their keyboard.
    • The input() function gets input from the keyboard.
    • When the input() is called, the program stops and waits for the user to type something on Console (interactive Python interpreter).
    • When the user presses Return or Enter on Console, the program resumes and input returns what the user typed as a string.
stuff = input()
# Type something and press Return/Enter on Console
# before running print(stuff)
print(stuff)
27 / 36

Repeat with while

Cancel with break

  • While loop is used to execute a block of code repeatedly until given boolean condition evaluated to False.
    • while True loop will run forever unless we write it with a break statement.
  • If we want to loop until something occurs, but we’re not sure when that might happen, we can use an infinite loop with a break statement.
while True:
stuff = input("String to capitalize [type q to quit]: ")
if stuff == "q":
break
print(stuff.capitalize())
28 / 36

Repeat with while

Skip Ahead with continue

  • Sometimes, we don’t want to break out of a loop but just want to skip ahead to the next iteration for some reason.

  • The continue statement is used to skip the rest of the code inside a loop for the current iteration only.

while True:
value = input("Integer, please [q to quit]: ")
if value == 'q': # quit
break
number = int(value)
if number % 2 == 0: # an even number
continue
print(number, "squared is", number*number)
29 / 36

Repeat with while

Check break Use with else

  • We can consider using while with else when we’ve coded a while loop to check for something, and breaking as soon as it’s found.
numbers = [1, 3, 5]
position = 0
while position < len(numbers):
number = numbers[position]
if number % 2 == 0:
print('Found even number', number)
break
position += 1
else: # break not called
print('No even number found')
  • Consider it a break checker.
30 / 36

Loop with while and for

Iterate with for and in

  • Sometimes we want to loop through a set of things such as a string of text, a list of words or a list of numbers.

    • When we have a list of things to loop through, we can construct a for loop.

    • A for loop makes it possible for you to traverse data structures without knowing how large they are or how they are implemented.

31 / 36

Loop with while and for

Iterate with for and in

  • Let's see two ways to walk through a string here:
word = 'thud'
offset = 0
while offset < len(word):
print(word[offset])
offset += 1
word = 'thud'
for letter in word:
print(letter)
  • Which one do you prefer?
32 / 36

Iterate with for and in

Cancel with break

  • A break in a for loop breaks out of the loop, as it does for a while loop:
word = 'thud'
for letter in word:
if letter == 'u':
break
print(letter)

Skip with continue

  • Inserting a continue in a for loop jumps to the next iteration of the loop, as it does for a while loop.
word = 'thud'
for letter in word:
if letter == 'u':
continue
print(letter)
33 / 36

Iterate with for and in

Check break Use with else

  • Similar to while, for has an optional else that checks whether the for completed normally. If break was not called, the else statement is run.

    • This is useful when we want to verify that the previous for loop ran to completion instead of being stopped early with a break:
word = 'thud'
for letter in word:
if letter == 'x':
print("Eek! An 'x'!")
break
print(letter)
else:
print("No 'x' in there.")
34 / 36

Iterate with for and in

Generate Number Sequences with range()

  • The range() function returns a stream of numbers within a specified range, without first having to create and store a large data structure such as a list or tuple.

    • This lets us create huge ranges without using all the memory in our computers and crashing our program.

    • range() returns an iterable object, so we need to step through the values with for ... in, or convert the object to a sequence like a list.

  • We use range() similar to how we use slices: range( start, stop, step ).
    • If we omit start, the range begins at 0.
    • The only required value is stop; as with slices, the last value created will be just before stop.
    • The default value of step is 1, but we can change it.
for x in range(0, 3):
print(x)
list( range(0, 3) )
  • How can we make a range from 2 down to 0?
for x in range(2, -1, -1):
print(x)
list( range(2, -1, -1) )
  • How can we get only even numbers?
for x in range(0, 11, 2):
print(x)
list( range(0, 11, 2) )
35 / 36

Loop with while and for

Class Exercises

  1. Use a while loop to print the values of the list [3, 2, 1, 0].

  2. Assign the value 7 to the variable guess_me, and the value 1 to the variable number. Write a while loop that compares number with guess_me. Print 'too low' if number is less than guess me. If number equals guess_me, print 'found it!' and then exit the loop. If number is greater than guess_me, print 'oops' and then exit the loop. Increment number at the end of the loop.

  1. Use a for loop to print the values of the list [3, 2, 1, 0].

  2. Assign the value 5 to the variable guess_me. Use a for loop to iterate a variable called number over range(10). If number is less than guess_me, print 'too low'. If number equals guess_me, print 'found it!' and then break out of the for loop. If number is greater than guess_me, print 'oops' and then exit the loop.

36 / 36

Announcement

Tutoring/TA-ing at Data Analytics Lab (South 321)

  • 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 / 36
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