Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JenniferNorthrup/1757a6622f028328f3fc7eff405018a7 to your computer and use it in GitHub Desktop.
Save JenniferNorthrup/1757a6622f028328f3fc7eff405018a7 to your computer and use it in GitHub Desktop.
Practice+Exercise+-+Data-Types.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/JenniferNorthrup/1757a6622f028328f3fc7eff405018a7/practice-exercise-data-types.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NRpNbwRgXyC4"
},
"source": [
"# **Data Types in Python**\n",
"\n",
"This notebook is aimed at shedding some light on the different data types in Python. This shall form a strong foundation for your programming and Data Science journey."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MT1FiyGBXyC5"
},
"source": [
"Data belongs to different categories. It can be numeric, text, true/false, complex numbers, date, time etc. Data should be stored correctly for us to be able to do the desired operations on the data. If the data is stored incorrectly, for example numerical data is stored as text data, we can not do numerical or arithmetic operations on the data because the methods and the operations for a data depend largely on the type of data (object) being referred to."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z17GkJzLXyC6"
},
"source": [
"There are various data types in Python. Listed below are the most prominently used ones:\n",
"1. Numeric - This is further divided into\n",
" (a.) Integer\n",
" (b.) Float\n",
" (c.) Complex\n",
" \n",
"2. String\n",
"\n",
"3. Boolean"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bdemUEcoXyC6"
},
"source": [
"## **Numeric Data Type**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NH_6dQERXyC6"
},
"source": [
"As the name suggests, Numeric data type consists of numbers. These numbers can be whole numbers, decimal numbers or complex numbers.\n",
"\n",
"Whole numbers are stored in Integer (Int) type.\n",
"\n",
"Decimal numbers are stored in Float (Float64) type.\n",
"\n",
"Complex numbers are stored in Complex type.\n",
"\n",
"You will find Integer and Float type being used most of the times."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8Rw9rT48XyC6"
},
"source": [
"You can check the data type of an object using the type() command."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Eb3pDYnNXyC7",
"outputId": "6c0dbc86-7ed6-4302-9c96-480985cabaac"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"int"
]
},
"metadata": {},
"execution_count": 1
}
],
"source": [
"# Let's look at some examples.\n",
"\n",
"Integer = 7\n",
"type(Integer)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fpKzIXleXyC8",
"outputId": "852a073b-0625-407f-a010-1a79033bdabe"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"float"
]
},
"metadata": {},
"execution_count": 2
}
],
"source": [
"Float = 2.356\n",
"type(Float)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hwzg9XD3XyC8",
"outputId": "e4815c0a-ab96-48d3-e86e-76ea72919437"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"complex"
]
},
"metadata": {},
"execution_count": 6
}
],
"source": [
"Complex = 3.908J\n",
"type(Complex)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cwbyCjgYXyC9",
"outputId": "bb9e9c56-e87b-4b38-a941-187feaad652d"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<class 'int'>\n",
"<class 'complex'>\n",
"<class 'float'>\n"
]
}
],
"source": [
"# Your turn now.\n",
"# Store 2, 1086M, 3.14567 into three different objects and check their data types.\n",
"obj_1 = 2\n",
"print(type(obj_1))\n",
"\n",
"obj_2 = 1086J\n",
"print(type(obj_2))\n",
"\n",
"obj_3 = 3.14567\n",
"print(type(obj_3))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MTe9k69lXyC9"
},
"source": [
"## **String Data Type**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mJKj2Jz5XyC9"
},
"source": [
"String Data type usually is used to store text. The data to be stored in this data type is enclosed between single ('') or double (\"\") quotes.\n",
"Recall that you printed your name in the previous Notebook. That was string data type.\n",
"Let's look at an example."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "xFYfWqIpXyC9",
"outputId": "5e54b42e-5904-4ecf-94b7-14213c9c8c1f"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'My name is Jupyter!'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"# Printing your name\n",
"My_Name = \"My name is Jupyter!\"\n",
"My_Name"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "D0b2-cIuXyC-",
"outputId": "0e31971f-ecb8-42d5-ad34-ad817d168e21"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"type(My_Name)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5aZZorpDXyC-",
"outputId": "b6ac2ad5-ece4-4e71-c81a-bf1989528799"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {},
"execution_count": 12
}
],
"source": [
"# Your turn to play around now.\n",
"# Save your name to an object and check its data type.\n",
"name = \"Jenny\"\n",
"type(name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "47jmd853XyC-"
},
"source": [
"Various functions can be performed using strings like searching within a string, conversion to lowercase/uppercase, count, length, splitting, replacing, trimming, partitioning etc."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cXlDpwdZXyC-",
"outputId": "a25c7a35-c8e5-46d0-aae0-50b6dcfbe171"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"19\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"execution_count": 15
}
],
"source": [
"# Let's check how many characters does your name contain.\n",
"print(len(My_Name))\n",
"len(name)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YRuhYhURXyC-",
"outputId": "a3947ddf-6812-499d-9c01-ccccfccf0837"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 16
}
],
"source": [
"# Let's see if My_Name is all caps or not.\n",
"# isupper() returns True if all the letters are capitals, False if atleast one letter in in lower case.\n",
"\n",
"My_Name.isupper()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "Hb0m2NM6XyC_",
"outputId": "1e4161c7-d1bf-4312-fd9e-6b6eda075d91"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'MY NAME IS JUPYTER!'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 17
}
],
"source": [
"# Let's convert My_Name to all caps.\n",
"\n",
"My_Name = My_Name.upper()\n",
"My_Name"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "X9qabI4LXyC_",
"outputId": "3fd7f5cb-497e-40fa-cd47-2b3502fcfbf9"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {},
"execution_count": 18
}
],
"source": [
"# Now the output of isupper() changes to True.\n",
"My_Name.isupper()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"id": "MBfYxM7yXyC_"
},
"outputs": [],
"source": [
"# Your turn to try it now.\n",
"# Write a sentence and store it into an object.\n",
"my_sentence = \"Wut wut is this?\"\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Beuit_vlXyC_",
"outputId": "95028528-12be-4998-b2b0-2931c9839a14"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<class 'str'>\n"
]
}
],
"source": [
"# Check the data type of the object you just created.\n",
"print(type(my_sentence))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sZNTk4QgXyC_",
"outputId": "06433d2c-1718-4398-c6c9-19337033fd58"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 21
}
],
"source": [
"# Check if it is all caps or not.\n",
"my_sentence.isupper()\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_MrPUcGvXyDA",
"outputId": "a0699ace-4016-4265-b224-0f1405272308"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {},
"execution_count": 22
}
],
"source": [
"# Challenge - Check if it is all lower case or not.\n",
"\n",
"my_sentence.islower()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "2Utj8PRaXyDA",
"outputId": "ab02a8ed-2ed3-4a65-ccec-91172f5a99c5"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'WUT WUT IS THIS?'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 23
}
],
"source": [
"# Convert your object into all caps.\n",
"\n",
"my_sentence.upper()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "sB0uD3NgXyDA",
"outputId": "f35e868b-b1d3-4561-c129-750d57c4c760"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'wut wut is this?'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 24
}
],
"source": [
"# Challenge - Convert your object into all lower case.\n",
"my_sentence.lower()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eSREcB3nXyDA"
},
"source": [
"## **Boolean Data Type**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nBbMWYbHXyDA"
},
"source": [
"The boolean data type has just two values, i.e., True or False."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"id": "MVkP2-eIXyDA"
},
"outputs": [],
"source": [
"# Let's look at examples.\n",
"\n",
"x = True\n",
"y = False"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4QtPBMqIXyDA",
"outputId": "30464879-a525-487f-e14f-f6662021c093"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"bool"
]
},
"metadata": {},
"execution_count": 26
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GL2fMLjOXyDB",
"outputId": "a66a4ed3-29d7-4599-e7d3-c780f4b35e67"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"bool"
]
},
"metadata": {},
"execution_count": 27
}
],
"source": [
"type(y)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TFg15B-UXyDB",
"outputId": "641b0014-98fe-49a5-ef7e-c138de631527"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"bool"
]
},
"metadata": {},
"execution_count": 28
}
],
"source": [
"# Your turn!\n",
"unknown = True\n",
"type(unknown)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JqhVIlzkXyDB"
},
"source": [
"## **Data Type Conversions**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "q0djJ7E-XyDB"
},
"source": [
"As explained earlier, data should be stored in correct form so that it can be manipulated efficiently later.\n",
"Quite a lot of times, data is not stored correctly or it gets imported incorrectly.\n",
"In such cases, data needs to be converted to its correct type so that it can be optimally used in our analysis.\n",
"Thus, let's look at data type conversion exercises to get you comfortable with the process."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"id": "gBCbdl3CXyDB"
},
"outputs": [],
"source": [
"# Consider an object containing an integer.\n",
"string1 = 1"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xEDoCjfkXyDB",
"outputId": "7412d94b-2dae-46aa-f240-23fff90107c1"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"int"
]
},
"metadata": {},
"execution_count": 30
}
],
"source": [
"type(string1)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"id": "ZS_W1KGKXyDC"
},
"outputs": [],
"source": [
"# I want to use the value 1 as a text value. So let's convert it.\n",
"string1 = str(string1)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Hhve_bXVXyDC",
"outputId": "71772da0-0019-4895-d452-eb2b62876dea"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {},
"execution_count": 32
}
],
"source": [
"type(string1)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XRHQZIjDXyDC",
"outputId": "7f6bcb07-571e-4d4f-ebd0-907914b6cf6e"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {},
"execution_count": 34
}
],
"source": [
"# Exercise for you. Store the number 123456 as string.\n",
"stringaling = str(123456)\n",
"type(stringaling)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8_xJYEOpXyDL",
"outputId": "58f338ff-1f24-4ec9-d114-a20e8c7aaa57"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"float"
]
},
"metadata": {},
"execution_count": 38
}
],
"source": [
"# Let's do the opposite now. Let's learn to convert string to float now.\n",
"floataling = float(stringaling)\n",
"type(floataling)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WoFkB7F0XyDL",
"outputId": "3ddb05f4-3d33-4102-da84-afa62167191d"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {},
"execution_count": 36
}
],
"source": [
"int1 = \"500\"\n",
"type(int1)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TDRHiYRWXyDL",
"outputId": "f99edc61-cb76-4bb9-dc5e-7e5b98895e01"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"int"
]
},
"metadata": {},
"execution_count": 37
}
],
"source": [
"int1 = int(int1)\n",
"type(int1)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "i6J8PewXXyDM",
"outputId": "4e894586-6ebc-4236-d213-ccd53a4a4fe2"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Instructions unclear, so I'll just do some casting... Is it called casting in Python?\n",
"<class 'int'>\n",
"<class 'str'>\n",
"<class 'float'>\n",
"<class 'int'>\n"
]
}
],
"source": [
"# Your turn now.\n",
"print(\"Instructions unclear, so I'll just do some casting... Is it called casting in Python?\")\n",
"my_int = 1234\n",
"print(type(my_int))\n",
"my_string = str(my_int)\n",
"print(type(my_string))\n",
"my_float = float(my_string)\n",
"print(type(my_float))\n",
"my_ending_int = int(my_float)\n",
"print(type(my_ending_int))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vx3m20CLXyDM"
},
"source": [
"Good job so far!\n",
"You can also try converting strings to boolean and vice versa.\n",
"You could also try conversions from integers to float and float to integer.\n",
"\n",
"Python is an intuitive language, so by default it stores data into correct formats.\n",
"But very often we come across data being stored incorrectly especially while importing."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment