Coder Social home page Coder Social logo

python-p3-oxford-comma-lab's Introduction

Oxford Comma Lab

Learning Goals

  • Convert data types
  • Practice iterating through lists
  • Use various methods to manipulate lists and strings

Key Vocab

  • Interpreter: a program that executes other programs. Python programs require the Python interpreter to be installed on your computer so that they can be run.
  • Python Shell: an interactive interpreter that can be accessed from the command line.
  • Data Type: a specific kind of data. The Python interpreter uses these types to determine which actions can be performed on different data items.
  • Exception: a type of error that can be predicted and handled without causing a program to crash.
  • Code Block: a collection of code that is interpreted together. Python groups code blocks by indentation level.
  • Function: a named code block that performs a sequence of actions when it is called.
  • Scope: the area in your program where a specific variable can be called.

Converting Types

In Python, there are a few methods available to us for converting data types. For example, it is possible to convert an integer into a float, a string that represents a number into a number type, and a string into a list or a list into a string, among other conversions. In this lesson we'll learn about converting between lists and strings, but you can learn about more of them in this tutorial on converting data types in Python.

String to List

To convert a string into a list, we can use the .split method:

"Hello World!".split()
# => ["Hello", "World!"]

The .split method uses whitespace as the separator by default. If we want to use a different separator, we can pass it as an argument:

"hippo,giraffe,monkey,horse".split(",")
# => ["hippo", "giraffe", "monkey", "horse"]

List to String

We can use Python's .join method to convert a list to a string. The syntax for the command is:

separator.join(items-to-join)

The method is called on a string which represents the separator you want to be inserted between each of the items in the list. If you want to join the items into a single string without any separators, you would call the method on an empty string:

''.join(["a", "b", "c"])
# => "abc"

On the other hand, if we want to separate each item in our list with a " :-) " ("smiley face"), we would do this:

" :-) ".join(["a", "b", "c"])
# => "a :-) b :-) c"

Instructions

To get started, run pipenv install to create your virtual environment and pipenv shell to enter the virtual environment. Then run pytest -x to run your tests. Use these instructions and pytest's error messages to complete your work in the lib/ folder.

Write a function oxford_comma() in the lib/oxford_comma.py file that takes an array of string elements as an argument and converts it into a string using the Oxford comma.

oxford_comma(["fiddleheads", "okra", "kohlrabi"])
# => "fiddleheads, okra, and kohlrabi"

Hint: You will need to refer to the section above about converting lists into strings, but note that coding this function will involve a couple of extra challenges.

This might be a challenging lab, so take your time using Google and playing around with your code. Good luck and have fun!


Resources

python-p3-oxford-comma-lab's People

Contributors

lizbur10 avatar professor-ben avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.