Skip to content

Instantly share code, notes, and snippets.

@vaibhavb
Created January 9, 2023 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavb/cc3f917b5012905c0f540ffce8fcd9b6 to your computer and use it in GitHub Desktop.
Save vaibhavb/cc3f917b5012905c0f540ffce8fcd9b6 to your computer and use it in GitHub Desktop.
2022-01-09-intro2python.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true,
"authorship_tag": "ABX9TyN+aBcSN4UYnAWKSPZPDp1s",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/vaibhavb/cc3f917b5012905c0f540ffce8fcd9b6/2022-01-09-intro2python.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"#Data types"
],
"metadata": {
"id": "n_vb1Gn8GBRj"
}
},
{
"cell_type": "markdown",
"source": [
"Program has data and control"
],
"metadata": {
"id": "MxgRMzfizxDb"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FL5vz7a_sOwE"
},
"outputs": [],
"source": [
"# variables\n",
"temp_int = 10\n",
"temp_float= 10.2\n",
"temp_string = \"ten\"\n",
"temp_list = [10.333, 10, 10.5]\n",
"temp_dict = {\"today\": 10, \"yesterday\": 16}\n",
"temp_set = {10, 11}"
]
},
{
"cell_type": "markdown",
"source": [
"### what is type of temp2?"
],
"metadata": {
"id": "FmF3iDXHx2GO"
}
},
{
"cell_type": "code",
"source": [
"type(temp_int)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1Z6QJIgsyAOu",
"outputId": "2472dd1e-1ecf-4ac2-d29c-1099b4f8e2d5"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"int"
]
},
"metadata": {},
"execution_count": 18
}
]
},
{
"cell_type": "markdown",
"source": [
"#Getting help?\n",
"* dir()\n",
"* help()\n",
"* python.org"
],
"metadata": {
"id": "Q-c34oAwzQzA"
}
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "AO-RUhN2yVwx"
}
},
{
"cell_type": "markdown",
"source": [
"#Conditionals"
],
"metadata": {
"id": "z-TfnoGhF305"
}
},
{
"cell_type": "markdown",
"source": [
"* if-elif-else statement\n",
"* while statement\n",
"* for statement"
],
"metadata": {
"id": "Qn1QWy3pJvVx"
}
},
{
"cell_type": "code",
"source": [
"# Write a program which tell you if a number is odd or even?\n",
"\n",
"num = 4\n",
"# if the number when divided by 2 leaves zero remainder\n",
"if(num % 2 == 0):\n",
" print(\"even\")\n",
"else:\n",
" print(\"odd\")\n",
"# then its even\n",
"# otherwise its odd\n",
"# if statement is called conditional"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "RzYHrI11z9_4",
"outputId": "d443f6ec-248c-42a4-e0c9-471e5a3d8d82"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"even\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# if the day is working day print \"workday\" otherwise print \"holiday\"\n",
"day = \"monday\"\n",
"work_day = [\"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\"]\n",
"holiday = [\"saturday\", \"sunday\"]\n",
"# in operator is described in https://docs.python.org/3/reference/expressions.html#in\n",
"if day in work_day:\n",
" print(\"workday\")\n",
"else:\n",
" print(\"holiday\")"
],
"metadata": {
"id": "iPmaK3s13EaC",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ec3cf909-ab50-4412-af81-4173102ae12e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"workday\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# if the day is working day prin \"workday\" otherwise print \"holiday\"\n",
"\n",
"#while day is not Quit continue\n",
"day = 0\n",
"while (day != \"Quit\"):\n",
" day = input(prompt = \"What day is today? Enter Quit to exit\")\n",
" #change the input case to all lowercase\n",
" day_lowercase = str.lower(day)\n",
" work_day = {\"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\"}\n",
" holiday = {\"saturday\", \"sunday\"}\n",
"\n",
" if (day != \"Quit\"):\n",
" if day_lowercase in work_day:\n",
" print(\"It's a workday?\")\n",
" elif day_lowercase in holiday: \n",
" print(\"It's a holiday\")\n",
" else:\n",
" print(\"Its an unkown day\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "u0APBRml7qUm",
"outputId": "63dfb965-9a03-4923-d2e6-0ac45fdcb216"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What day is today? Enter Quit to exitquit\n",
"Its an unkown day\n",
"What day is today? Enter Quit to exitquit\n",
"Its an unkown day\n",
"What day is today? Enter Quit to exitQuit\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Functions"
],
"metadata": {
"id": "0n5xMn7jFu3c"
}
},
{
"cell_type": "markdown",
"source": [
"Building blocks of your program"
],
"metadata": {
"id": "N3tCOwrHJ_la"
}
},
{
"cell_type": "code",
"source": [
"#check_workday(day) -> print workday or not\n",
"def check_workday(day):\n",
" \"\"\"takes a day and prints whether its a workday or holiday\"\"\"\n",
" workday = {\"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\"}\n",
" holiday = {\"saturday\", \"sunday\"}\n",
" if day in workday:\n",
" print(\"workday\")\n",
" elif day in holiday:\n",
" print(\"holiday\")\n",
" else:\n",
" print(\"unknown\")\n",
"\n"
],
"metadata": {
"id": "mHiJ6SJWGfGR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"day = input(prompt = \"What day of week you want to check?\")\n",
"check_workday(day)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "F2nxi1BGICTi",
"outputId": "1628097f-4e65-4495-df1b-90ed0fc65182"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"What day of week you want to check?tuesday\n",
"workday\n"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment