Giter Site home page Giter Site logo

ds-skills2-numeric-types's Introduction

Introduction to Variables: Numeric Types and Booleans

Introduction

So, we know that we have a type for representing text, the String. But what if we want to represent other types of data, like Numbers? We are data scientists after all, and we'll be working with numbers a lot. In this lesson, we'll introduce both Numbers and Booleans, which are other data types we will frequently use in Python.

Objectives

You will be able to:

  • Understand, explain and use the correct data types (numeric types) for various types of information

What Are Numeric Data Types?

All of us are familiar with numbers. 1492 is a number. So is 34.75. If we think about what the common operations are with numbers, we get a pretty good idea for what Python allows us to do with numbers.

Note: to see the output of the following operations, press the shift + enter keys and run each cell

1 + 1
2 * 5

If we look at a number for its type, we find something slightly different.

type(10)
type(10.2)

Python is simply indicating that a number without a decimal is called an int for integer, and a number with a decimal is called a float. Both floats and integers are numeric data types, but for now, we can think of both of these simply as numbers.

10 + 0.75

As we can see in the above example, the float, 0.75, plus the int, 10, resolves to the float, 10.75. So, a float combined with an int returns a number that is a float.

What is a boolean?

A Boolean (bool) has two possible values: True or False.

type(True)

It's fairly rare for programmers to explicitly write the word True or False. Instead our programs can respond to questions for us in this form. We have already seen one such example:

"Homer Simpson".endswith("Simpson")
"Homer Simpson".endswith("Homer")

And as you might imagine, a boolean can be returned from a math operation as well.

3 * 5 < 10

You will see later on that by utilizing these returned booleans, we can make decisions with our code. For example: send this email if a user's last name is Simpson, or send an invite if the user is in a target age group. We aren't there yet, but we'll get there!

Data Types As A Choice

"Bad programmers worry about the code. Good programmers worry about data structures and their relationships." - Torvald

For now, it's interesting to think of how methods allow us to change between datatypes and to think of when we may want our data to be in one datatype versus another. We started this lesson by saying that 34 is a number. But what if it's not?

"west 34th street"

We could make the argument that in certain contexts, like an address, 34 is text. In others, when we are judging distance in blocks it feels like a number. So how do we decide?

The answer is by really thinking about what we want to do with the data. If we want to capitalize all of the words in the string, to mail a letter (which I admit, sounds awful), we should keep the data in the format of a string.

"34th street".title()

What if we are trying to ask if a number in that string is larger than another number? For example, a restaurant that only delivers food below 22nd street might use a program to write something like:

34 < 22

But would a method like less than ( < ) work with a string? Does it make sense for a string or text to answer whether it is less than or greater than a number? Trying things is free, so let's give it a shot.

"34th street" < 22

Well, now we know for sure. So, if we want to help our restaurant with deliveries, we should convert our number from a string to a number and then make the comparison.

int('34') < 22
# False
False

And if we want to go from a number to a string, for example to produce an address, we again need to pay attention to the type.

str(34) + 'th Street'

Here we saw our first method for switching between types: simply write the name of the type followed by parentheses and the data on which we want to operate. After introducing this pattern, we can start to explore with others types such as the Boolean.

bool(100)
bool(0)

Great, so we can coerce a number to a boolean as well. And we are beginning to think about keeping our data in one form or another based on what we want to do with that data.

Summary

In this section, we introduced two new types of data: numbers and booleans. We saw that numbers allow us to perform standard math operations, and we saw that booleans answer whether something is True or False, and serve as a way our program or different methods can respond to questions.

We have seen almost all of our Python datatypes, we talked about how to choose a datatype, and we talked about how to switch between datatypes. We said that we choose a datatype based on the capabilities that we want to give to that data: should it answer whether it is larger or smaller, or does it make sense to capitalize? The goal of this discussion is to begin thinking about why we decide to put data in specific types (i.e. string, number, boolean). We also introduced coercion methods like bool and str that switch between datatypes.

ds-skills2-numeric-types's People

Contributors

mathymitchell avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

doyarzunv

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.