Last active
December 8, 2022 12:16
-
-
Save abayomi185/d7412b484f57aa0b757bda9088590ae2 to your computer and use it in GitHub Desktop.
Getting Started with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Python 101\n", | |
"In line with tradition, the first thing to code when starting with a new programming language is to print \"Hello, World!\". With this, you can confidently say, I know how to code in Python." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"print(\"Hello, World!\")" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Variables\n", | |
"Storage locations or containers used to store known or unknown data of different types<br>Think of them as buckets." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"name = \"Yomi\"\n", | |
"university = \"Loughborough University\"\n", | |
"\n", | |
"current_student = False\n", | |
"former_student = True\n", | |
"\n", | |
"integer = 20\n", | |
"decimal = 1.01\n", | |
"\n", | |
"nothing = None" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Comments" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# This is a comment and it will not be executed.\n", | |
"# It is also used to convey information about the code.\n", | |
"\n", | |
"# This is a string data type, identified by double quotes or single quotes.\n", | |
"name = \"Yomi\"\n", | |
"university = 'Loughborough University'\n", | |
"\n", | |
"# Boolean data type\n", | |
"current_student = False\n", | |
"former_student = True\n", | |
"\n", | |
"# Number data types\n", | |
"integer = 20\n", | |
"decimal = 1.01 # Float\n", | |
"\n", | |
"# None data type\n", | |
"nothing = None" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Conditionals\n", | |
"To control the flow of a program, there needs to be a way to check the state of the program.<br><br>\n", | |
"An example is deciding whether to take the bus, cycle or walk depending on the weather." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# A variable to store a string, indicating the weather\n", | |
"weather = \"sunny\"\n", | |
"\n", | |
"# An if statement to check the weather is sunny\n", | |
"if weather == \"sunny\":\n", | |
" # This line is indented, so it is part of the if statement\n", | |
" # It will only be executed if the weather is sunny\n", | |
" print(\"It's a nice day, let's go for a walk!\")\n", | |
"\n", | |
"# An elif statement to check the weather is rainy. Must be after the if statement.\n", | |
"elif weather == \"rainy\":\n", | |
" # This line is also indented, so it is part of the elif statement\n", | |
" print(\"It's raining, let's stay inside or take the bus.\")\n", | |
"\n", | |
"# An else statement to check the weather is anything else. Must be after the if and elif statements.\n", | |
"else:\n", | |
" print(\"I don't know what to do.\")\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"```python\n", | |
"# This would be invalid python code due to incorrect indentation\n", | |
"else:\n", | |
"print(\"I don't know what to do.\")\n", | |
"\n", | |
"# Indentation may be set with multiples of 4 spaces or tabs.\n", | |
"# These are all valid indentations\n", | |
"else:\n", | |
" print(\"I don't know what to do.\") # 2 space indentation\n", | |
"else:\n", | |
" print(\"I don't know what to do.\") # 4 space or 1 tab indentation\n", | |
"else:\n", | |
" print(\"I don't know what to do.\") # 8 space or 2 tab indentation" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The same code above without comments because they sometimes make things harder to read" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"weather = \"sunny\"\n", | |
"\n", | |
"if weather == \"sunny\":\n", | |
" print(\"It's a nice day, let's go for a walk!\")\n", | |
"elif weather == \"rainy\":\n", | |
" print(\"It's raining, let's stay inside or take the bus.\")\n", | |
"else:\n", | |
" print(\"I don't know what to do.\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Conditional Operators" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"variable_x = 10\n", | |
"variable_y = 20\n", | |
"\n", | |
"if variable_x == variable_y:\n", | |
" print(\"The variable values are equal.\")\n", | |
"elif variable_x > variable_y:\n", | |
" print(\"The first variable value is greater than the second.\")\n", | |
"elif variable_x < variable_y:\n", | |
" print(\"The first variable value is less than the second.\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Other Operators;\n", | |
"- `>=` Greater than or equals to\n", | |
"- `<=` Less than or equals to\n", | |
"- `and` Logical AND operator\n", | |
"- `or` Logical OR operator\n", | |
"- `in` Value in another value" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Collections\n", | |
"Sometimes, information is better represented as a collection of items as opposed to single item variables. Think of a music playlist or a list of books.<br><br>\n", | |
"Computers are typically also able process collections faster." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# A list, the simplest collection data type in Python\n", | |
"list_of_numbers = [1, 2, 3, 4, 5]\n", | |
"list_of_books = [\"Atomic Habits\", \"Life After Google\", \"The Minimalist Entrepreneur\"]\n", | |
"list_of_songs = [\"All I Want For Christmas is You\", \"Ye\", \"All I Want For Christmas is You\"]\n", | |
"\n", | |
"# A tuple, a collection data type that is immutable. It cannot be changed.\n", | |
"tuple_of_numbers = (1, 2, 3, 4, 5)\n", | |
"tuple_of_artists = (\"Mariah Carey\", \"Michael Jackson\", \"Kokoroko\")\n", | |
"\n", | |
"# A dictionary, a collection data type that stores key-value pairs\n", | |
"dictionary_of_numbers = {\n", | |
" \"one\": 1,\n", | |
" \"two\": 2,\n", | |
" \"three\": 3\n", | |
"}\n", | |
"dictionary_of_books = {\n", | |
" \"Atomic Habits\": \"James Clear\",\n", | |
" \"Life After Google\": \"George Gilder\",\n", | |
" \"The Minimalist Entrepreneur\": \"Sahil Lavingia\"\n", | |
"}" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Loops\n", | |
"In the case of a List or a tuple collection, it is not possible to access any element other than the first or last without iterating through the collection." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Using the list of numbers variable in the previous code cell above.\n", | |
"print(\"List\")\n", | |
"for number in list_of_numbers:\n", | |
" print(number)\n", | |
"\n", | |
"# Using the tuple of numbers variable in the previous code cell above.\n", | |
"print(\"Tuple\")\n", | |
"for number in tuple_of_numbers:\n", | |
" print(number)\n", | |
"\n", | |
"# Every element in the List and Tuple data type has an index. The index can be used to access the element directly and starts at 0.\n", | |
"print(\"Collection Indexing\")\n", | |
"print(list_of_numbers[0]) # Prints the first element in the list\n", | |
"print(list_of_numbers[1]) # Prints the second element in the list\n", | |
"# ...\n", | |
"print(list_of_numbers[-1]) # Prints the last element in the list\n", | |
"\n", | |
"# Indexes may also be used in loops instead of using `in` to iterate over the collection.\n", | |
"print(\"Collection Indexing in Loops\")\n", | |
"for index in range(len(tuple_of_numbers)):\n", | |
" print(tuple_of_numbers[index])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# A dictionary is a bit different because it has a key that is used to access the value. This makes it more efficient for certain tasks provided the key is known and is unique.\n", | |
"value_from_dict_with_key = dictionary_of_numbers[\"one\"]\n", | |
"print(value_from_dict_with_key)" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Functions\n", | |
"A function is a sectioned block of code that only runs when it is called. It is able to receive inputs and return outputs." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Defining a function to add two numbers.\n", | |
"# The function takes two inputs, number_one and number_two and returns the sum of the two numbers.\n", | |
"def function_to_add_two_numbers(number_one, number_two):\n", | |
" sum = number_one + number_two\n", | |
" return sum\n", | |
"\n", | |
"# Calling the function to add two numbers.\n", | |
"# The function takes two inputs, 10 and 20 and returns the sum of the two numbers.\n", | |
"sum_of_numbers = function_to_add_two_numbers(10, 20)\n", | |
"print(sum_of_numbers)\n", | |
"\n", | |
"# Functions may not always recieve inputs or return a value. In this case, the function returns None.\n", | |
"def function_to_print_hello_world():\n", | |
" print(\"Hello, World!\")" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Modules\n", | |
"Modules, called libraries in other programming languages are packaged programs that can be imported into your python program to add functionality or make certain things easier to implement." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Importing the built-in Python datetime module\n", | |
"import datetime as dt\n", | |
"\n", | |
"# Getting the current date and time using the datetime module\n", | |
"current_datetime = dt.datetime.now()\n", | |
"print(current_datetime)" | |
] | |
}, | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Beyond 101\n", | |
"- Object-oriented Programming\n", | |
" - Classes\n", | |
" - Methods\n", | |
" - Inheritance\n", | |
"- File Handling\n", | |
" - Reading\n", | |
" - Writing\n", | |
"- Web requests\n", | |
" - JSON\n", | |
" - APIs" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.9.15 ('python-testbench-hwqv')", | |
"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.15" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "1864174c7f5bc3ade8955e727c97de677c338e72a0c85b5ad61865ac83228667" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple Python script |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Variables | |
# Comments | |
# Conditionals | |
# Conditional Operators | |
# Collections | |
# Loops | |
# Functions | |
# Modules | |
# Simple example using inputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment