{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Primitive Data Types\n", "--------------------\n", "\n", "These are the basic data types that constitute all of the more complex data structures in python. The basic data types are the following:\n", "\n", "* Strings (for text)\n", "* Numeric types (integers and decimals)\n", "* Booleans\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String variables\n", "\n", "String variables are used to store textual data, characters and sequences of characters. Can be specified by surrounding some text with single `'` or double `\"` quotes. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_1 = \"Hello World!\"\n", "print(str_1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_1 = \"Hello World!\"\n", "print(str_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiline strings\n", "\n", "If we have a piece of text with multiple lines, then triple quotes (either `'''` or `\"\"\"`) can be used to surround the text." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_2 = \"\"\"\n", "(CNN)AirAsia Flight QZ8501 climbed rapidly before it crashed,\n", "a top Indonesian official said Tuesday, according to The Jakarta Post.\n", "\n", "Then the plane stalled, Transportation Minister Ignasius Jonan said\n", "at a parliamentary hearing, according to the AFP and Reuters news agencies.\n", "\n", "\"The plane, during the last minutes, went up faster than normal\n", "speed ... after then, it stalled. That is according to the data\n", "from the radar,\" Jonan said, according to the news agencies.\n", "\"\"\"\n", "print(str_2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str_3 = \"\"\"\n", "If we want to have multiple lines in the string\n", "then we can use triple quotes: This is a multiline\n", "string!\n", "\"\"\"\n", "print(str_3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Operations on Strings \n", "\n", "We have seen some common operations on numeric variables. Now, let’s discuss a few **common** operations on string variables. The complete list of string operations is [available here](http://docs.python.org/3/library/string.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Concatenation\n", "\n", "The `+` operator concatenates two strings:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# note that + concatenates strings\n", "str1 = \"hello\"\n", "str2 = \"world\"\n", "message = str1 + \" \" + str2 + \"!\"\n", "print(message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice the difference of the `+` operation when operating on text vs. numeric variables:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 41\n", "x = x + 1\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = \"41\"\n", "x = x + \"1\"\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Exercise\n", "\n", "You are given a variable `name = 'Panos'` and variable `phone = '212-998-0803'`. Use the values of these variables to create and print the message `Call Panos at 212-998-0803`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = \"Panos\"\n", "phone = \"212-998-0803\"\n", "# your code here (2 lines)\n", "message = \"Call \" + name + \" at \" + phone\n", "print(message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### String length\n", "\n", "`len(str)`: length of a string, number of characters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "word = \"Python is the word. And on and on and on....\"\n", "print(len(word))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(word)\n", "print(\"The length of the text above is \", len(word), \"characters\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Exercise\n", "\n", "You are given a brief news article, stored in the variable `news`. Compute the length of the article, in characters." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "news = \"\"\"\n", "Technology stocks are suffering one of their worst beatings in years,\n", "as investors reassess a sector that has been considered the growth\n", "engine of the global economy but now faces the prospect of greater\n", "regulatory scrutiny. The tech-heavy Nasdaq Composite Index\n", "fell 2.9% Tuesday. That selloff carried over to the broader market,\n", "where the S&P 500 index slumped 1.7%. The Dow Jones Industrial\n", "Average fell 1.4%, giving back some of Monday’s 2.8% rebound.\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(len(news))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(news)\n", "print(\"The length of the text above is\", len(news), \"characters\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Capitalization\n", "\n", "Now let's see how we can change the capitalization of the text. This will also be our first, informal, introduction to a new type of function. \n", "\n", "* `str.upper()`: returns an uppercase version of a string\n", "* `str.lower()`: returns a lowercase version of a string" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(news.lower())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(news.upper())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Notice that the original 'news' variable did not change\n", "print(news)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If we want to keep the changed-case version,\n", "# we need to store it in a variable\n", "l_news = news.lower()\n", "print(l_news)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Small deviation\n", "\n", "The `.lower()` is a function that applies only to string variables. Notice the different way that we apply the function, compared to, say, the `len(news)` above. In this case, we have a string variable (`news`), then we have a dot (`.`), and then we put the name of the function: (`lower()`). Notice that we have the two parentheses at the end, which indicate that the function does not have any arguments, except for the `news` variable. Notice that if we omit the parentheses, we will get a strange result:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "news.lower" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }