Strings and Exceptions

If you’re looking for a crash course in Python. You’re in the right place.

Strings

This is a continuation of my coverage of Data Science from Scratch, by Joel Grus (ch2) where the Python crash course brings us to strings and exceptions.

These topics are more nice-to-know rather than central for data science, so we’ll cover them briefly (of course I’ll revisit this section if I find that they are more important than I had thought).

For strings, the main concept highlighted is string interpolation where a variable is inserted in a string in some fashion. There are several ways to do this, but the f-string approach is most up-to-date and recommended.

Here are some examples:

# first we'll create variables that are pointed at strings (my first and last names)first_name = "Paul"
last_name = "Apivat"
# f-string (recommended)
f_string = f"{first_name} {last_name}"
# string addition, 'Paul Apivat'
string_addition = first_name + " " + last_name
# string format, 'Paul Apivat'
string_format = "{0} {1}".format(first_name, last_name)
# percent format (NOT recommended), 'Paul Apivat'
pct_format = "%s %s" %('Paul','Apivat')

Exceptions

The author covers exceptions to make the point that they’re not all that bad in Python and it’s worth handling exceptions yourself to make code more readable. Here’s my own example that’s slightly different from the book:

integer_list = [1,2,3]heterogeneous_list = ["string", 0.1, True]# you can sum a list of integers, here 1 + 2 + 3 = 6
sum(integer_list)
# but you cannot sum a list of heterogeneous data types
# doing so raises a TypeError
sum(heterogeneous_list)
##### Start :error crashes ######
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-3287dd0c6c22> in <module>
----> 1 sum(heterogeneous_list)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
--------------------------------------------------------------------##### End :error crashes ######
# so the idea is to handle the exceptions with your own messagestry:
sum(heterogeneous_list)
except TypeError:
print("cannot add objects of different data types")

At this point, the primary benefit to handling exceptions may be for code readability, so we’ll come back to this section if we see more useful examples.

***

For more content on data science, machine learning, R, Python, SQL and more, find me on Twitter.

--

--

--

Data-Informed People Decisions

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

3 Ways To Measure And Increase The Value Of Your Analytics Team

Data Analysis and Predication using Logistic Regression

How to get your first job in Data Science?

【Data Analysis(2)】「Garbage in, garbage out.」

First Steps

Grand Theft Data: how to legally steal $1 trillion and get away with it

What’s Wrong with LIME

Welcome to the 4th Dimension of Data

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Paul Apivat

Paul Apivat

Data-Informed People Decisions

More from Medium

Leetcode 1886. Determine Whether Matrix Can Be Obtained By Rotation

image.png

Two types of abstractions in Python: Instance of ; Inherited from

Python, the developer-oriented programming language

Regular Expression in ython