Skip to main content
Engineering LibreTexts

3.4: The Three Kinds in Python

  • Page ID
    39351
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Now the three kinds of atomic data described above are language-general: this means that they’re conceptual, not tied to any specific programming language or analysis tool. Any technology used for Data Science will have the ability to deal with those three basic types. The specific ways they do so will differ somewhat from language to language. Let’s learn about how Python implements them.

    Whole numbers: int

    One of the most basic Python data types is the “int,” which stands for “integer.” It’s what we use to represent whole numbers.

    In Python, you create a variable by simply typing its name, an equals sign, and then its initial value, like so:

    Code \(\PageIndex{1}\) (Python):

    revolution = 1776

    This is our first line of code3 . As we’ll see, lines of code are executed one by one – there is a time before, and a time after, each line is actually carried out. This will turn out to be very important. (Oh, and a “line of code” is sometimes also called a statement.)

    Python variable names can be as long as you like, provided they consist only of upper and lower case letters, digits, and underscores. (You do have to be consistent with your capitalization and your spelling: you can’t call a variable Movie in one line of code and movie in another.) Underscores are often used as pseudo-spaces, but no other weird punctuation marks are allowed in a variable’s name.4

    And while we’re on the subject, let me encourage you to name your variables well. This means that each variable name should reflect exactly what the value that it stores represents. Example: if a variable is meant to store the rating (in “stars”) that an IMDB user gave to a movie, don’t name it movie. Name it rating. (Or even better, movie_rating.) Trust me: when you’re working on a complex program, there’s enough hard stuff to think about without confusing yourself (and your colleagues) by close-but-not-exact variable names.5

    Now remember that a variable has three things – a name, value, and type. The first two explicitly appear in the line of code itself. As for the type, how does Python know that revolution should be an “int?” Simple: it’s a number with no decimal point.

    As a sanity check, we can ask Python to tell us the variable’s type explicitly, by writing this code:

    Code \(\PageIndex{2}\) (Python):

    type(revolution)

    If this line of code is executed after the previous one is executed, Python responds with:

    |int

    So there you go. Here’s another “code snippet” (a term that just means “some lines of code I’m focusing on, which are generally only part of a larger program”):

    Code \(\PageIndex{3}\) (Python):

    revolution = 1776

    moon_landing = 1969

    revolution = 1917

    Now if this were a math class, that set of equations would be nonsensical. How could the same variable (revolution) have two contradictory values? But in a program, this is perfectly legit: it just means that immediately after the first line of code executes, revolution has the value 1776, and moments later, after the third line executes, its value has changed to 1917. Its value depends entirely on “where the program is” during its execution.

    Real (fractional) numbers: float

    The only odd thing about the second data type in Python is its name. In some other universe it might have been called a “real” or a “decimal” or a “fractional” variable, but for some bizarre historical reasons it is called a float.6

    All the same rules and regulations pertain to floats as they do to ints; the only difference is you type a decimal point. So:

    Code \(\PageIndex{4}\) (Python):

    GPA = 3.17

    price_of_Christian_Louboutin_shoes = 895.95

    interest_rate = 6.

    Note that the interest_rate variable is indeed a float type (even though it has no fractional part) because we typed a period:

    Code \(\PageIndex{5}\) (Python):

    type(interest_rate)

    | float

    Text: str

    Speaking of weird names, a Python text variable is of type str, which stands for “string.” You could think of it as a bunch of letters “strung” together like a beaded necklace.

    Important: when specifying a str value, you must use quotation marks (either single or double). For one thing, this is how Python know that you intend to create a str as opposed to some other type. Examples:

    Code \(\PageIndex{6}\) (Python):

    slang = 'lit'

    grade = "3rd"

    donut_store = "Paul's Bakery"

    url = 'http://umweagles.com'

    Notice, by the way, that a string of digits is not the same as an integer. To wit:

    Code \(\PageIndex{7}\) (Python):

    schwarzenegger_weight = 249

    action_movie = "300"

    type(schwarzenegger_weight)

    Code \(\PageIndex{8}\) (Python):

    type(action_movie)

    See? The quotes make all the difference.

    The Length of a String

    We’ll do many things with strings in this book. Probably the most basic is simply to inquire as to a string’s length, or the number of characters it contains. To do this, we enclose the variable’s name in parentheses after the word len:

    Code \(\PageIndex{9}\) (Python):

    len(slang)

    | 3

    Code \(\PageIndex{10}\) (Python):

    len(donut_store)

    As we’ll see, the len() operation (and many others like it) is an example of a function in Python. In proper lingo, when we write a line of code like len(donut_store) we say we are “calling the function,” which simply means to invoke or trigger it.

    More lingo: for obscure reasons, the value inside the bananas (here, donut_store) is called an argument to the function. And we say that we “pass” one or more arguments to a function when we call it.

    All these terms may seem pedantic, but they are precise and universally used, so be sure to learn them. The preceding line of code can be completely summed up by saying:

    “We are calling the len() function, and passing it the donut_store variable as an argument.”

    I recommend you say that sentence out loud at least four times in a row to get used to its rhythm.

    Note, by the way, that the len() function expects a str argument. You can’t call len() with an int or a float variable as an argument:

    Code \(\PageIndex{11}\) (Python):

    schwarzenegger_weight = 249

    len(schwarzenegger_weight)

    | TypeError: object of type 'int' has no len()

    (You might think that the “length” of an int would be its number of digits, but nope.)

    One thing that students often get confused is the difference between a named string variable and that of an (unnamed) string value. Consider the difference in outputs of the following:

    Code \(\PageIndex{12}\) (Python):

    slang = 'lit'

    len(slang)

    | 3

    Code \(\PageIndex{13}\) (Python):

    len('slang')

    | 5

    In the first example, we asked “how long is the value being held in the slang variable?” The answer was 3, since “lit” is three characters long. In the second example, we asked “how long is the word 'slang'?” and the answer is 5. Remember: variable names never go in quotes. If something is in quotes, it’s being taken literally.

    Combining and Printing Variables

    There’s a whole lot of stuff you can do with variables other than just creating them. One thing you’ll want to do frequently is print a variable, which means to dump its value to the page so you can see it. This is easily done by calling the print() function:

    Code \(\PageIndex{14}\) (Python):

    print(donut_store)

    print(price_of_Christian_Louboutin_shoes)

    print("slang")

    print(slang)

    | Paul's Bakery

    | 895.95

    | slang

    | lit

    Again, don’t miss the crucial difference between printing "slang" and printing slang. The former is literal and the latter is not. In the first of these, we’re passing the word “slang” as the argument, not the variable slang.

    Often we’ll want to combine bits of information into a single print statement. Typically one of the variables is a string that contains the overall message. There are several ways to accomplish this, but the most flexible will turn out to be the .format() method.

    I hate to hit you with so much new lingo. A method is very similar to a function, but not exactly. The difference is in the syntax used to call it. When you call a function (like type() or len()) you simply type its name, followed by a pair of bananas inside of which you put the arguments (separated by commas, if there’s more than one). But when you “call a method,” you put a variable before a dot (“.”) and the method name, then the bananas. This is referred to as “calling the method on the variable.”

    It sounds more confusing than it is. Here’s an example of .format() in action:

    Code \(\PageIndex{15}\) (Python):

    price_of_Christian_Louboutin_shoes = 895.95

    message = "Honey, I spent ${} today!"

    print(message.format(price_of_Christian_Louboutin_shoes))

    Take note of how we write “message.format” instead of just “format”. This is because .format() is a method, not a function. We say that we are calling .format() “on” message, and passing price_of_ Christian_Louboutin_shoes as an argument.7 Also be sure to notice the double bananas “))” at the end of that last line. We need both of them because in programming, every left-banana must match a corresponding right-banana. Since we’re calling two functions/methods on one line (print() and .format()), we had two left-bananas on that line. Each one needs a partner.

    As for the specifics of how .format() works, you’ll see that the string variable you call it on may include pairs of curlies (squiggly braces). These are placeholders for where to stick the values of other variables in the output. Those variables are then included as arguments to the .format() method. The above code produces this output:

    | Honey, I spent $895.95 today!

    Often, instead of creating a new variable name to hold the preformatted string, we’ll just print() it literally, like this:

    Code \(\PageIndex{16}\) (Python):

    print("Honey, I spent ${} today!".format( price_of_Christian_Louboutin_shoes))

    We’re still actually calling .format() on a variable here, it’s just that we haven’t bothered to name the variable. Also, notice that our code was too long to fit on one line nicely, so we broke it in two, and indented the second line to make it clear that “price_of_...” wasn’t starting its own new line. Crucially, all the bananas are still paired up, two-by-two, even though the left bananas are on a different line than the corresponding right bananas.

    Finally, here’s a longer example with more variables:

    Code \(\PageIndex{17}\) (Python):

    name = "Pedro Pascal"

    num_items = 3

    cost = 91.73

    print("Customer {} bought {} items worth ${}.".format(name, num_items, cost))

    | Customer Pedro Pascal bought 3 items worth $91.73.

    You can see how we can pass more than one argument to a function/method simply by separating them with commas inside the bananas.

    3By the way, the word code is grammatically a mass noun, not a count noun. Hence it is proper to say “I wrote some code last night,” not “I wrote some codes last night.” If you misuse this, it will brand you as a newbie right away.

    4Oh, and another rule: a variable name can’t start with a digit. So r2d2 is a legal variable name, but not 007bond

    5And I fully own up to the fact that the revolution variable isn’t named very well. I chose it to make a different point shortly.

    6If you’re curious, this is because in computer programming parlance a “floating-point number” means a number where the decimal point might be anywhere. With an integer like -52, the decimal point is implicitly at the far right-hand side of the sequence of digits. But with numbers like -5.2 or -.52 or -.000052 or even 520000, the decimal point has “floated” away from this fixed position.

    7Btw, in this book, whenever I refer to a method, I’ll be sure to put a dot before its name. For example, it’s not the “format()” method, but the “.format()” method.


    This page titled 3.4: The Three Kinds in Python is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?