Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JenniferNorthrup/febd23d755a54d54f63524f287c13160 to your computer and use it in GitHub Desktop.

Select an option

Save JenniferNorthrup/febd23d755a54d54f63524f287c13160 to your computer and use it in GitHub Desktop.
Practice+Exercise+-+Collection_of_variables.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/febd23d755a54d54f63524f287c13160/practice-exercise-collection_of_variables.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9bQ6lt0yXybc"
},
"source": [
"# **COLLECTION OF VARIABLES**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZADBm2UqXybf"
},
"source": [
"### 1. Create a list of repeated element (element 2, 48 times)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5us02XSGXybg",
"outputId": "47a2b9c3-e43e-482a-ba02-3ec2cbbceb2d"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"48\n"
]
}
],
"source": [
"this_list = [2] * 48\n",
"print(len(this_list))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c4mgN72QXybg"
},
"source": [
"### 2. Print the second element of the list\n",
"\n",
"list = [\"apple\",\"orange\",\"grapes\",\"watermelon\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "awws8IUgXybh",
"outputId": "aed2fea5-ef8d-48a9-e18c-2439f631f3cd"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"orange\n"
]
}
],
"source": [
"list = [\"apple\",\"orange\",\"grapes\",\"watermelon\"]\n",
"print(list[1])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vu4fsP3ZXybh"
},
"source": [
"### 3. Replace the second element of the list with \"strawberries\"\n",
"list = [\"apple\",\"orange\",\"grapes\",\"watermelon\"]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "m5mGYNovXybi",
"outputId": "fedc0ba3-fa1f-46c6-bf36-866b6779da40"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['apple', 'strawberries', 'grapes', 'watermelon']\n"
]
}
],
"source": [
"list[1] = \"strawberries\"\n",
"print(list)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "66LWC3r2Xybi"
},
"source": [
"### 4. Iterate through the list and print the values"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MgaBAftjXybj",
"outputId": "3003b9d8-3eb8-4d55-d2c0-dc095b03927e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"apple\n",
"strawberries\n",
"grapes\n",
"watermelon\n"
]
}
],
"source": [
"for fruit in list:\n",
" print(fruit)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "v6DM9__KXybj"
},
"source": [
"### 5. Write a Python code to print squares of all numbers present in a list = [1, 12, 24, 36, 11, 20,50]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ejygxnS5Xybj",
"outputId": "1b21f728-5c09-4d35-ff1a-15d9900562d3"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n",
"144\n",
"576\n",
"1296\n",
"121\n",
"400\n",
"2500\n"
]
}
],
"source": [
"numbers = [1, 12, 24, 36, 11, 20, 50]\n",
"for number in numbers:\n",
" print(number ** 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Z7S_vrl9Xybj"
},
"source": [
"### 6. Pop yellow color from the list below:\n",
"List=['white','red', 'blue', 'green','blue','yellow', 'black']"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M_SL-9qXXybk",
"outputId": "71fe28b7-8a2c-46c7-969e-44a3a9d22fd7"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['white', 'red', 'blue', 'green', 'blue', 'black']\n"
]
}
],
"source": [
"colors = ['white','red', 'blue', 'green','blue','yellow', 'black']\n",
"colors.pop(-2)\n",
"print(colors)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zP7CJMbkXybk"
},
"source": [
"### 7. Check length of list\n",
"\n",
"list=[\"a\",\"b\",\"c\",\"d\",\"e\"]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BhU-XoAPXybk",
"outputId": "b8ef4e36-ab73-4676-9ba1-24ececf034ef"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"5\n"
]
}
],
"source": [
"list=[\"a\",\"b\",\"c\",\"d\",\"e\"]\n",
"print(len(list))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tLSaElmUXybk"
},
"source": [
"### 8. Create a dictionary with the following information:\n",
"\n",
"brand = Audi\n",
"\n",
"Model = Q2\n",
"\n",
"Year = 1980"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OcPi5k4DXybl",
"outputId": "1f1135ba-ac37-434a-8b26-5ca5cf1f850b"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'brand': 'Audi', 'Model': 'Q2', 'Year': 1980}\n"
]
}
],
"source": [
"car = {\n",
" \"brand\": \"Audi\",\n",
" \"Model\": \"Q2\",\n",
" \"Year\": 1980\n",
"}\n",
"print(car)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WcErU8C7Xybl"
},
"source": [
"### 9. In the above dictionary change the year to 2019"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FHE4p6O_Xybl",
"outputId": "89b44474-187a-474c-93e4-6bfe7c273010"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'brand': 'Audi', 'Model': 'Q2', 'Year': 2019}\n"
]
}
],
"source": [
"car[\"Year\"] = 2019\n",
"print(car)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pqc2rAREXybm"
},
"source": [
"### 10. Create a set of following elements\n",
"\n",
"1.0, \"Hello\", 55 , (6,7,8)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OZ_nB01gXybm",
"outputId": "a337dc05-6563-434f-bd89-f9c759422d6b"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<class 'tuple'>\n",
"(1.0, 'Hello', 55, (6, 7, 8))\n"
]
}
],
"source": [
"this_set = (1.0, \"Hello\", 55, (6,7,8))\n",
"print(type(this_set))\n",
"print(this_set)"
]
}
],
"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