├── ml_lecture_01.py ├── ml_lab_02.py ├── (fa21_bscs_0030)lab_01_graded.py ├── syed_mustafa_hassan_sp24_intro_to_ml_assignment_01_.py ├── (FA21_BSCS_0030)Lab_01_Graded.ipynb ├── ML_Lecture_01.ipynb └── Lab_01_Basic_Python.ipynb /ml_lecture_01.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ML-Lecture-01.ipynb 3 | 4 | Automatically generated by Colaboratory. 5 | 6 | Original file is located at 7 | https://colab.research.google.com/drive/1MGjYwZbJFJsbKNIfHkSIYh-5dghtqms8 8 | 9 | # ***Numpy*** 10 | The Basics 11 | """ 12 | 13 | import numpy as np 14 | 15 | a = np.array([1,2,3]) 16 | print(a) 17 | 18 | b = np.array([[4, 5, 6], [7, 8, 9]]) 19 | print(b) 20 | 21 | a.ndim #number dimensional array 22 | b.ndim #number dimensional array 23 | 24 | a.shape 25 | 26 | a.dtype 27 | 28 | a.itemsize 29 | 30 | a.size 31 | 32 | """**Accessing/Channing sep. ele, rows clo.**""" 33 | 34 | a = np.array([[1,2,3,4,5,6,7],[8, 9, 10, 11, 12, 13, 14]]) 35 | print(a) 36 | 37 | a.shape 38 | 39 | print(a[1,-2]) 40 | 41 | print(a[0, :]) 42 | 43 | a[:, 2] 44 | 45 | a[0, 1:-1:2] 46 | 47 | a[1, 5] = 20 48 | print(a) 49 | a[:,2] = [1,2] 50 | print(a) 51 | 52 | np.zeros((2, 3)) 53 | 54 | np.ones((4,3), dtype = 'int32') 55 | 56 | np.full((2, 2), 4, dtype='float32') 57 | 58 | np.full_like(a.shape, 4) 59 | 60 | np.random.rand(4,2) 61 | 62 | np.random.randint(4, size=(4,2)) 63 | 64 | np.identity(5) 65 | 66 | arr = np.array([[1,2,3]]) 67 | r1 = np.repeat(arr, 3, axis=0) 68 | print(r1) 69 | 70 | out = np.ones((5, 5)) 71 | print(out) 72 | z = np.zeros((3, 3)) 73 | z[1,1] = 9 74 | print(z) 75 | out[1:-1, 1:-1] = z 76 | print(out) 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /ml_lab_02.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ML_LAB-02.ipynb 3 | 4 | Automatically generated by Colaboratory. 5 | 6 | Original file is located at 7 | https://colab.research.google.com/drive/1v4tv-rEnZ9e0YywHSd-lkOZg3WwqlpES 8 | 9 | https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/datasets/data/boston_house_prices.csv 10 | 11 | **Download** boston_house_price.csv 12 | """ 13 | 14 | # Commented out IPython magic to ensure Python compatibility. 15 | import numpy as np 16 | import matplotlib.pyplot as plt 17 | import pandas as pd 18 | import seaborn as sns 19 | 20 | # %matplotlib inline 21 | 22 | boston = pd.read_csv("boston_house_prices.csv") 23 | boston.keys() 24 | 25 | boston.head() 26 | 27 | boston.isnull().sum() 28 | 29 | sns.set(rc={'figure.figsize':(11.7,8.27)}) 30 | 31 | #bins take min and make and break it in to bins value 1-200 tak 5 5 ki bin bana ligi 32 | #displot ma line nhi hoti yah histrogram show kar nay kliya hai 33 | 34 | sns.displot(boston['MEDV'], bins=20) 35 | plt.show() 36 | 37 | sns.set(rc={'figure.figsize':(11.7,8.27)}) 38 | 39 | #bins take min and make and break it in to bins value 1-200 tak 5 5 ki bin bana ligi 40 | sns.distplot(boston['MEDV'], bins=30) 41 | plt.show() 42 | 43 | sns.set(rc={'figure.figsize':(11.7,8.27)}) 44 | 45 | #bins take min and make and break it in to bins value 1-200 tak 5 5 ki bin bana ligi 46 | sns.histplot(boston['MEDV'], bins=20) 47 | plt.show() 48 | 49 | correlation_matrix = boston.corr().round(2) 50 | #heatmap is use to identy correlation 51 | sns.heatmap(data=correlation_matrix, annot=True) 52 | 53 | plt.figure(figsize=(20,5)) 54 | 55 | features = ['LSTAT', 'RM'] 56 | target = boston['MEDV'] 57 | 58 | for i,col in enumerate(features): 59 | plt.subplot(1, len(features), i+1) 60 | x = boston[col] 61 | y = target 62 | plt.scatter(x,y, marker='o') 63 | plt.title(col) 64 | plt.xlabel(col) 65 | plt.ylabel('MEDV') 66 | 67 | X = pd.DataFrame(np.c_[boston['LSTAT'], boston['RM']], columns=['LSTAT','RM']) 68 | Y = boston['MEDV'] 69 | 70 | -------------------------------------------------------------------------------- /(fa21_bscs_0030)lab_01_graded.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """(FA21-BSCS-0030)Lab_01_Graded.ipynb 3 | 4 | Automatically generated by Colaboratory. 5 | 6 | Original file is located at 7 | https://colab.research.google.com/drive/1Z7qdadnp0nPUUc0C-tUGob2iBzRhhl6j 8 | 9 | # **Task 01** 10 | 11 | Create a nested dictionary representing a student database with information such as name, age, and grades for multiple students. Then, calculate the average grade for each student. 12 | 13 | Input: 14 | ``` 15 | students = { 16 | "student 1": {"age": 15, "grades": [86, 55, 38]}, 17 | "student 2": {"age": 17, "grades": [85, 92, 69]}, 18 | # Add more students... 19 | } 20 | ``` 21 | 22 | 23 | Expected Output: 24 | 25 | {'student 1': 59.666666666666664, 'student 2': 82.0} 26 | """ 27 | 28 | students = { 29 | "student 1": {"age": 15, "grades": [86, 55, 38]}, 30 | "student 2": {"age": 17, "grades": [85, 92, 98,99]}, 31 | "student 3": {"age": 19, "grades": [69, 85, 98]}, 32 | "student 4": {"age": 12, "grades": [59, 92, 99]}, 33 | "student 5": {"age": 14, "grades": [88, 55, 93]}, 34 | "student 6": {"age": 16, "grades": [88, 90, 90]}, 35 | } 36 | 37 | students.items() 38 | 39 | avg_grade ={} 40 | 41 | for student, info in students.items(): 42 | grades = info["grades"] 43 | avg_grades = sum(grades) / len(grades) 44 | avg_grade[student] = avg_grades 45 | 46 | print(avg_grade) 47 | 48 | """# **Task 02** 49 | 50 | Given a list of numbers, use a lambda function to filter out even numbers, square the remaining ones, and then find their sum. 51 | 52 | Input: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] 53 | 54 | Expected Output: Result will be 165 (1^2 + 3^2 + 5^2 + 7^2 + 9^2) 55 | """ 56 | 57 | numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] 58 | numbers2 = list(filter((lambda x: x%2 == 0), numbers)) 59 | for i in numbers: 60 | for j in numbers2: 61 | if i==j: 62 | numbers.remove(i) 63 | res = 0 64 | for i in numbers: 65 | res = i*i + res 66 | 67 | res 68 | 69 | """# **Task 03** 70 | 71 | Given a text string, create a list of unique words (case-insensitive) using list comprehension. 72 | 73 | Input: text = "This is a sample text with some repeated words. This is a sample." 74 | 75 | Expected Output: ['sample', 'text', 'words.', 'sample.', 'repeated', 'is' 'some', 'a', 'this', 'with'] 76 | """ 77 | 78 | text = input("Enter your text here: ") 79 | unique_words = list(set(word.lower() for word in text.split())) 80 | print(unique_words) 81 | 82 | """# **Task no 04** 83 | 84 | Write a function in which you ask for the following details from the user. 85 | 86 | Name 87 | username 88 | Degree Program 89 | Age 90 | Contact 91 | Percentage 92 | Gender 93 | isPassed (bool) 94 | After receiving the values of the attributes you have to save the data in a dictionary name as UserData at index 0. repeat the process five times with an increment of index = index +1. 95 | 96 | in the end, create a file named your id, save all the data in the file, and close it. 97 | 98 | 99 | 100 | ``` 101 | # Writing to a file 102 | contents = {"aa": 12, "bb": 21} # 103 | with open("myfile1.txt", "w+") as file: 104 | file.write(str(contents)) # writes a string to a file 105 | 106 | 107 | OR 108 | 109 | import json 110 | with open("myfile2.txt", "w+") as file: 111 | file.write(json.dumps(contents)) # writes an object to a file 112 | ``` 113 | 114 | 115 | """ 116 | 117 | import json 118 | 119 | def save_user_data(): 120 | UserData = {} 121 | for i in range(5): 122 | name = input("Enter name: ") 123 | username = input("Enter username: ") 124 | degree = input("Enter degree: ") 125 | program = input("Enter program: ") 126 | age = int(input("Enter age: ")) 127 | contact = input("Enter contact: ") 128 | percentage = float(input("Enter percentage: ")) 129 | gender = input("Enter gender: ") 130 | is_passed = input("Has the user passed? (True/False): ").lower() == 'true' 131 | 132 | UserData[i] = { 133 | "Name": name, 134 | "Username": username, 135 | "Degree": degree, 136 | "Program": program, 137 | "Age": age, 138 | "Contact": contact, 139 | "Percentage": percentage, 140 | "Gender": gender, 141 | "IsPassed": is_passed 142 | } 143 | 144 | # Save data to a file 145 | file_name = input("Enter your ID: ") + ".txt" 146 | with open(file_name, "w+") as file: 147 | file.write(json.dumps(UserData)) 148 | 149 | save_user_data() 150 | 151 | -------------------------------------------------------------------------------- /syed_mustafa_hassan_sp24_intro_to_ml_assignment_01_.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """SYED MUSTAFA HASSAN - SP24 - Intro to ML - Assignment 01 .ipynb 3 | 4 | Automatically generated by Colaboratory. 5 | 6 | Original file is located at 7 | https://colab.research.google.com/drive/15gsUQpiyq1jreVcQqD-HLdumltL-3OYi 8 | 9 | #Problem Introduction 10 | This exercise relates to the College data set, which can be found in 11 | the file College.csv on the book website. It contains a number of 12 | variables for 777 different universities and colleges in the US. The 13 | variables are 14 | * Private : Public/private indicator 15 | * Apps : Number of applications received 16 | * Accept : Number of applicants accepted 17 | * Enroll : Number of new students enrolled 18 | * Top10perc : New students from top 10 % of high school class 19 | * Top25perc : New students from top 25 % of high school class 20 | * F.Undergrad : Number of full-time undergraduates 21 | * P.Undergrad : Number of part-time undergraduates 22 | * Outstate : Out-of-state tuition 23 | * Room.Board : Room and board costs 24 | * Books : Estimated book costs 25 | * Personal : Estimated personal spending 26 | * PhD : Percent of faculty with Ph.D.s 27 | * Terminal : Percent of faculty with terminal degree 28 | * S.F.Ratio : Student/faculty ratio 29 | * perc.alumni : Percent of alumni who donate 30 | * Expend : Instructional expenditure per student 31 | * Grad.Rate : Graduation rate 32 | 33 | Before reading the data into Python, it can be viewed in Excel or a 34 | text editor. 35 | 36 | 37 | --- 38 | 39 | **Q.1** 40 | Use the pd.read_csv() function to read the data into Python. Call 41 | the loaded data college. Make sure that you have the directory 42 | set to the correct location for the data 43 | """ 44 | 45 | import numpy as np 46 | import pandas as pd 47 | 48 | Auto = pd.read_csv('College.csv') 49 | Auto 50 | 51 | """**Q.2** 52 | Look at the data used in the notebook by creating and running 53 | a new cell with just the code college in it. You should notice 54 | that the first column is just the name of each university in a 55 | column named something like Unnamed: 0. We don’t really want 56 | pandas to treat this as data. However, it may be handy to have 57 | these names for later. Try the following commands and similarly 58 | look at the resulting data frames: 59 | 60 | 61 | 62 | ``` 63 | college2 = pd.read_csv('College.csv', index_col=0) 64 | college3 = college.rename({'Unnamed: 0': 'College'}, 65 | axis=1) 66 | college3 = college3.set_index('College') 67 | ``` 68 | 69 | 70 | 71 | This has used the first column in the file as an index for the 72 | data frame. This means that pandas has given each row a name 73 | corresponding to the appropriate university. Now you should see 74 | that the first data column is Private. Note that the names of 75 | the colleges appear on the left of the table. We also introduced 76 | a new python object above: a dictionary, which is specified by 77 | (key, value) pairs. Keep your modified version of the data with 78 | the following: 79 | 80 | 81 | ``` 82 | college = college3 83 | ``` 84 | 85 | 86 | 87 | """ 88 | 89 | # college2 = pd.read_csv('College.csv', index_col=0) 90 | # college3 = college.rename({'Unnamed: 0': 'College'}, 91 | # axis=1) 92 | # college3 = college3.set_index('College') 93 | 94 | college2 = pd.read_csv('College.csv', index_col=0) 95 | 96 | college2.index.name = 'College' 97 | 98 | college = college2 99 | 100 | college 101 | 102 | """**Q.3** 103 | Use the describe() method of to produce a numerical summary 104 | of the variables in the data set. 105 | """ 106 | 107 | datasummary = college.describe() 108 | 109 | datasummary 110 | 111 | """**Q.4** 112 | Use the pd.plotting.scatter_matrix() function to produce a 113 | scatterplot matrix of the first columns [Top10perc, Apps, Enroll]. 114 | Recall that you can reference a list C of columns of a data frame 115 | A using A[C]. 116 | """ 117 | 118 | import matplotlib.pyplot as plt 119 | 120 | pd.plotting.scatter_matrix(college[['Top10perc', 'Apps', 'Enroll']]) 121 | plt.show() 122 | 123 | """**Q.5** 124 | Use the boxplot() method of college to produce side-by-side 125 | boxplots of Outstate versus Private. 126 | """ 127 | 128 | college.boxplot(column='Outstate', by='Private') 129 | plt.show() 130 | 131 | """**Q.6** 132 | Create a new qualitative variable, called Elite, by binning the 133 | Top10perc variable into two groups based on whether or not the 134 | proportion of students coming from the top 10% of their high 135 | school classes exceeds 50%. 136 | 137 | 138 | ``` 139 | college['Elite'] = pd.cut(college['Top10perc'], 140 | [0,0.5,1], 141 | labels=['No', 'Yes']) 142 | ``` 143 | Use the value_counts() method of college['Elite'] to see how 144 | many elite universities there are. Finally, use the boxplot() method 145 | again to produce side-by-side boxplots of Outstate versus Elite. 146 | 147 | """ 148 | 149 | college['Elite'] = pd.cut(college['Top10perc'], [0, 50, 100], labels=['No', 'Yes']) 150 | elite_counts = college['Elite'].value_counts() 151 | elite_boxplot = college.boxplot(column='Outstate', by='Elite') 152 | 153 | plt.show() 154 | 155 | """**Q.7** 156 | Use the plot.hist() method of college to produce some histograms with differing numbers of bins for a few of the quantitative variables. The command plt.subplots(2, 2) may be useful: it will divide the plot window into four regions so that four 157 | plots can be made simultaneously. By changing the arguments 158 | you can divide the screen up in other combinations. 159 | """ 160 | 161 | fig, axes = plt.subplots(2, 2) 162 | college['Apps'].plot.hist(ax=axes[0,0], bins=10) 163 | college['Accept'].plot.hist(ax=axes[0,1], bins=20) 164 | college['Enroll'].plot.hist(ax=axes[1,0], bins=15) 165 | college['Top10perc'].plot.hist(ax=axes[1,1], bins=5) 166 | plt.show() -------------------------------------------------------------------------------- /(FA21_BSCS_0030)Lab_01_Graded.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "# **Task 01**\n", 21 | "\n", 22 | "Create a nested dictionary representing a student database with information such as name, age, and grades for multiple students. Then, calculate the average grade for each student.\n", 23 | "\n", 24 | "Input:\n", 25 | "```\n", 26 | "students = {\n", 27 | " \"student 1\": {\"age\": 15, \"grades\": [86, 55, 38]},\n", 28 | " \"student 2\": {\"age\": 17, \"grades\": [85, 92, 69]},\n", 29 | " # Add more students...\n", 30 | "}\n", 31 | "```\n", 32 | "\n", 33 | "\n", 34 | "Expected Output:\n", 35 | "\n", 36 | "{'student 1': 59.666666666666664, 'student 2': 82.0}" 37 | ], 38 | "metadata": { 39 | "id": "VpYconGQ0xoJ" 40 | } 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 32, 45 | "metadata": { 46 | "id": "Z8LJOv0Kfb3H", 47 | "colab": { 48 | "base_uri": "https://localhost:8080/" 49 | }, 50 | "outputId": "bd19f3c0-0d3c-4bcb-b640-a5def5e4c689" 51 | }, 52 | "outputs": [ 53 | { 54 | "output_type": "execute_result", 55 | "data": { 56 | "text/plain": [ 57 | "dict_items([('student 1', {'age': 15, 'grades': [86, 55, 38]}), ('student 2', {'age': 17, 'grades': [85, 92, 98, 99]}), ('student 3', {'age': 19, 'grades': [69, 85, 98]}), ('student 4', {'age': 12, 'grades': [59, 92, 99]}), ('student 5', {'age': 14, 'grades': [88, 55, 93]}), ('student 6', {'age': 16, 'grades': [88, 90, 90]})])" 58 | ] 59 | }, 60 | "metadata": {}, 61 | "execution_count": 32 62 | } 63 | ], 64 | "source": [ 65 | "students = {\n", 66 | " \"student 1\": {\"age\": 15, \"grades\": [86, 55, 38]},\n", 67 | " \"student 2\": {\"age\": 17, \"grades\": [85, 92, 98,99]},\n", 68 | " \"student 3\": {\"age\": 19, \"grades\": [69, 85, 98]},\n", 69 | " \"student 4\": {\"age\": 12, \"grades\": [59, 92, 99]},\n", 70 | " \"student 5\": {\"age\": 14, \"grades\": [88, 55, 93]},\n", 71 | " \"student 6\": {\"age\": 16, \"grades\": [88, 90, 90]},\n", 72 | "}\n", 73 | "\n", 74 | "students.items()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "source": [ 80 | "avg_grade ={}\n", 81 | "\n", 82 | "for student, info in students.items():\n", 83 | " grades = info[\"grades\"]\n", 84 | " avg_grades = sum(grades) / len(grades)\n", 85 | " avg_grade[student] = avg_grades" 86 | ], 87 | "metadata": { 88 | "id": "SIo2VAndx35a" 89 | }, 90 | "execution_count": 33, 91 | "outputs": [] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "source": [ 96 | "print(avg_grade)" 97 | ], 98 | "metadata": { 99 | "colab": { 100 | "base_uri": "https://localhost:8080/" 101 | }, 102 | "id": "HapIC94qsIPe", 103 | "outputId": "1f693bf0-2aea-432e-b4e0-7cafd2f5e970" 104 | }, 105 | "execution_count": 35, 106 | "outputs": [ 107 | { 108 | "output_type": "stream", 109 | "name": "stdout", 110 | "text": [ 111 | "{'student 1': 59.666666666666664, 'student 2': 93.5, 'student 3': 84.0, 'student 4': 83.33333333333333, 'student 5': 78.66666666666667, 'student 6': 89.33333333333333}\n" 112 | ] 113 | } 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "source": [ 119 | "# **Task 02**\n", 120 | "\n", 121 | "Given a list of numbers, use a lambda function to filter out even numbers, square the remaining ones, and then find their sum.\n", 122 | "\n", 123 | "Input: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 124 | "\n", 125 | "Expected Output: Result will be 165 (1^2 + 3^2 + 5^2 + 7^2 + 9^2)" 126 | ], 127 | "metadata": { 128 | "id": "77_JtPdy1Gpt" 129 | } 130 | }, 131 | { 132 | "cell_type": "code", 133 | "source": [ 134 | "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 135 | "numbers2 = list(filter((lambda x: x%2 == 0), numbers))\n", 136 | "for i in numbers:\n", 137 | " for j in numbers2:\n", 138 | " if i==j:\n", 139 | " numbers.remove(i)\n", 140 | "res = 0\n", 141 | "for i in numbers:\n", 142 | " res = i*i + res\n", 143 | "\n", 144 | "res" 145 | ], 146 | "metadata": { 147 | "colab": { 148 | "base_uri": "https://localhost:8080/" 149 | }, 150 | "id": "nh7H7tvCzThg", 151 | "outputId": "ef9ea8c0-1dcd-47bd-a3cf-c28039b77e0c" 152 | }, 153 | "execution_count": null, 154 | "outputs": [ 155 | { 156 | "output_type": "execute_result", 157 | "data": { 158 | "text/plain": [ 159 | "165" 160 | ] 161 | }, 162 | "metadata": {}, 163 | "execution_count": 25 164 | } 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "source": [ 170 | "# **Task 03**\n", 171 | "\n", 172 | "Given a text string, create a list of unique words (case-insensitive) using list comprehension.\n", 173 | "\n", 174 | "Input: text = \"This is a sample text with some repeated words. This is a sample.\"\n", 175 | "\n", 176 | "Expected Output: ['sample', 'text', 'words.', 'sample.', 'repeated', 'is' 'some', 'a', 'this', 'with']" 177 | ], 178 | "metadata": { 179 | "id": "JmD6R_rC1LsS" 180 | } 181 | }, 182 | { 183 | "cell_type": "code", 184 | "source": [ 185 | "text = input(\"Enter your text here: \")\n", 186 | "unique_words = list(set(word.lower() for word in text.split()))\n", 187 | "print(unique_words)" 188 | ], 189 | "metadata": { 190 | "id": "DVBqZL0w1Mbr", 191 | "colab": { 192 | "base_uri": "https://localhost:8080/" 193 | }, 194 | "outputId": "f4896dd7-a88f-430f-f35c-c1ebae04e049" 195 | }, 196 | "execution_count": 36, 197 | "outputs": [ 198 | { 199 | "output_type": "stream", 200 | "name": "stdout", 201 | "text": [ 202 | "Enter your text here: This is a sample text with some repeated words. This is a sample.\n", 203 | "['this', 'sample.', 'sample', 'is', 'text', 'with', 'some', 'repeated', 'a', 'words.']\n" 204 | ] 205 | } 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "source": [ 211 | "# **Task no 04**\n", 212 | "\n", 213 | "Write a function in which you ask for the following details from the user.\n", 214 | "\n", 215 | "Name\n", 216 | "username\n", 217 | "Degree Program\n", 218 | "Age\n", 219 | "Contact\n", 220 | "Percentage\n", 221 | "Gender\n", 222 | "isPassed (bool)\n", 223 | "After receiving the values of the attributes you have to save the data in a dictionary name as UserData at index 0. repeat the process five times with an increment of index = index +1.\n", 224 | "\n", 225 | "in the end, create a file named your id, save all the data in the file, and close it.\n", 226 | "\n", 227 | "\n", 228 | "\n", 229 | "```\n", 230 | "# Writing to a file\n", 231 | "contents = {\"aa\": 12, \"bb\": 21} #\n", 232 | "with open(\"myfile1.txt\", \"w+\") as file:\n", 233 | " file.write(str(contents)) # writes a string to a file\n", 234 | "\n", 235 | "\n", 236 | "OR\n", 237 | "\n", 238 | "import json\n", 239 | "with open(\"myfile2.txt\", \"w+\") as file:\n", 240 | " file.write(json.dumps(contents)) # writes an object to a file\n", 241 | "```\n", 242 | "\n" 243 | ], 244 | "metadata": { 245 | "id": "9PZFuaxV1SX8" 246 | } 247 | }, 248 | { 249 | "cell_type": "code", 250 | "source": [ 251 | "import json\n", 252 | "\n", 253 | "def save_user_data():\n", 254 | " UserData = {}\n", 255 | " for i in range(5):\n", 256 | " name = input(\"Enter name: \")\n", 257 | " username = input(\"Enter username: \")\n", 258 | " degree = input(\"Enter degree: \")\n", 259 | " program = input(\"Enter program: \")\n", 260 | " age = int(input(\"Enter age: \"))\n", 261 | " contact = input(\"Enter contact: \")\n", 262 | " percentage = float(input(\"Enter percentage: \"))\n", 263 | " gender = input(\"Enter gender: \")\n", 264 | " is_passed = input(\"Has the user passed? (True/False): \").lower() == 'true'\n", 265 | "\n", 266 | " UserData[i] = {\n", 267 | " \"Name\": name,\n", 268 | " \"Username\": username,\n", 269 | " \"Degree\": degree,\n", 270 | " \"Program\": program,\n", 271 | " \"Age\": age,\n", 272 | " \"Contact\": contact,\n", 273 | " \"Percentage\": percentage,\n", 274 | " \"Gender\": gender,\n", 275 | " \"IsPassed\": is_passed\n", 276 | " }\n", 277 | "\n", 278 | " # Save data to a file\n", 279 | " file_name = input(\"Enter your ID: \") + \".txt\"\n", 280 | " with open(file_name, \"w+\") as file:\n", 281 | " file.write(json.dumps(UserData))\n", 282 | "\n", 283 | "save_user_data()" 284 | ], 285 | "metadata": { 286 | "id": "6QpeYTf51UQ9", 287 | "colab": { 288 | "base_uri": "https://localhost:8080/" 289 | }, 290 | "outputId": "52c89c79-1bfc-49c8-8374-5e6f6df152eb" 291 | }, 292 | "execution_count": 37, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "Enter name: Mustafa\n", 299 | "Enter username: Mus\n", 300 | "Enter degree: CS\n", 301 | "Enter program: CS\n", 302 | "Enter age: 20\n", 303 | "Enter contact: 0335\n", 304 | "Enter percentage: 99.99\n", 305 | "Enter gender: m\n", 306 | "Has the user passed? (True/False): T\n", 307 | "Enter name: Raza\n", 308 | "Enter username: riz\n", 309 | "Enter degree: BS\n", 310 | "Enter program: CS\n", 311 | "Enter age: 24\n", 312 | "Enter contact: 0347\n", 313 | "Enter percentage: 100\n", 314 | "Enter gender: M\n", 315 | "Has the user passed? (True/False): n\n", 316 | "Enter name: Moiz\n", 317 | "Enter username: mix\n", 318 | "Enter degree: bs\n", 319 | "Enter program: cs\n", 320 | "Enter age: 23\n", 321 | "Enter contact: 022\n", 322 | "Enter percentage: 93\n", 323 | "Enter gender: m\n", 324 | "Has the user passed? (True/False): F\n", 325 | "Enter name: sara\n", 326 | "Enter username: sa\n", 327 | "Enter degree: bs\n", 328 | "Enter program: cs\n", 329 | "Enter age: 22\n", 330 | "Enter contact: 8336\n", 331 | "Enter percentage: 99\n", 332 | "Enter gender: f\n", 333 | "Has the user passed? (True/False): False\n", 334 | "Enter name: jd\n", 335 | "Enter username: nd\n", 336 | "Enter degree: bd\n", 337 | "Enter program: bd\n", 338 | "Enter age: 2\n", 339 | "Enter contact: 2\n", 340 | "Enter percentage: 2\n", 341 | "Enter gender: m\n", 342 | "Has the user passed? (True/False): false\n", 343 | "Enter your ID: 1\n" 344 | ] 345 | } 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "source": [], 351 | "metadata": { 352 | "id": "2X_0-hKUtQIN" 353 | }, 354 | "execution_count": null, 355 | "outputs": [] 356 | } 357 | ] 358 | } -------------------------------------------------------------------------------- /ML_Lecture_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "# ***Numpy***\n", 21 | "The Basics" 22 | ], 23 | "metadata": { 24 | "id": "qHUlDRgCtz8m" 25 | } 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": { 31 | "id": "ZEToD6NKtv12" 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "import numpy as np" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "source": [ 41 | "a = np.array([1,2,3])\n", 42 | "print(a)" 43 | ], 44 | "metadata": { 45 | "colab": { 46 | "base_uri": "https://localhost:8080/" 47 | }, 48 | "id": "ijfSlVcit8eG", 49 | "outputId": "7a80d708-8dbc-4bdb-f184-d64b4351902d" 50 | }, 51 | "execution_count": null, 52 | "outputs": [ 53 | { 54 | "output_type": "stream", 55 | "name": "stdout", 56 | "text": [ 57 | "[1 2 3]\n" 58 | ] 59 | } 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "source": [ 65 | "b = np.array([[4, 5, 6], [7, 8, 9]])\n", 66 | "print(b)" 67 | ], 68 | "metadata": { 69 | "colab": { 70 | "base_uri": "https://localhost:8080/" 71 | }, 72 | "id": "JW5z6a97uHh_", 73 | "outputId": "6b5edb7e-1597-47fd-b4ca-bc833c6f2a00" 74 | }, 75 | "execution_count": null, 76 | "outputs": [ 77 | { 78 | "output_type": "stream", 79 | "name": "stdout", 80 | "text": [ 81 | "[[4 5 6]\n", 82 | " [7 8 9]]\n" 83 | ] 84 | } 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "source": [ 90 | "a.ndim #number dimensional array\n", 91 | "b.ndim #number dimensional array" 92 | ], 93 | "metadata": { 94 | "colab": { 95 | "base_uri": "https://localhost:8080/" 96 | }, 97 | "id": "TZ1MzEAEuMxX", 98 | "outputId": "22cf9472-27cb-4a7e-b4e2-3c2ed0786764" 99 | }, 100 | "execution_count": null, 101 | "outputs": [ 102 | { 103 | "output_type": "execute_result", 104 | "data": { 105 | "text/plain": [ 106 | "2" 107 | ] 108 | }, 109 | "metadata": {}, 110 | "execution_count": 5 111 | } 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "source": [ 117 | "a.shape" 118 | ], 119 | "metadata": { 120 | "colab": { 121 | "base_uri": "https://localhost:8080/" 122 | }, 123 | "id": "ssGW7E13uiLE", 124 | "outputId": "fa8f36aa-7f52-4ba8-8ea0-47f4cbe8ba1a" 125 | }, 126 | "execution_count": null, 127 | "outputs": [ 128 | { 129 | "output_type": "execute_result", 130 | "data": { 131 | "text/plain": [ 132 | "(3,)" 133 | ] 134 | }, 135 | "metadata": {}, 136 | "execution_count": 6 137 | } 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "source": [ 143 | "a.dtype" 144 | ], 145 | "metadata": { 146 | "colab": { 147 | "base_uri": "https://localhost:8080/" 148 | }, 149 | "id": "Ixjqg2SqunUi", 150 | "outputId": "0b80dd36-a157-49d8-8d27-be1779839db3" 151 | }, 152 | "execution_count": null, 153 | "outputs": [ 154 | { 155 | "output_type": "execute_result", 156 | "data": { 157 | "text/plain": [ 158 | "dtype('int64')" 159 | ] 160 | }, 161 | "metadata": {}, 162 | "execution_count": 7 163 | } 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "source": [ 169 | "a.itemsize" 170 | ], 171 | "metadata": { 172 | "colab": { 173 | "base_uri": "https://localhost:8080/" 174 | }, 175 | "id": "UGvam9Jxu5Eh", 176 | "outputId": "2168801e-aa0c-4ab3-f66a-9d8b01b01914" 177 | }, 178 | "execution_count": null, 179 | "outputs": [ 180 | { 181 | "output_type": "execute_result", 182 | "data": { 183 | "text/plain": [ 184 | "8" 185 | ] 186 | }, 187 | "metadata": {}, 188 | "execution_count": 8 189 | } 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "source": [ 195 | "a.size" 196 | ], 197 | "metadata": { 198 | "colab": { 199 | "base_uri": "https://localhost:8080/" 200 | }, 201 | "id": "dVdYnpEsu7_Q", 202 | "outputId": "f82ddaf5-95f5-4a1e-eb0b-3fe5d8ee5e06" 203 | }, 204 | "execution_count": null, 205 | "outputs": [ 206 | { 207 | "output_type": "execute_result", 208 | "data": { 209 | "text/plain": [ 210 | "3" 211 | ] 212 | }, 213 | "metadata": {}, 214 | "execution_count": 9 215 | } 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "source": [ 221 | "**Accessing/Channing sep. ele, rows clo.**" 222 | ], 223 | "metadata": { 224 | "id": "G5nZfrjSvf30" 225 | } 226 | }, 227 | { 228 | "cell_type": "code", 229 | "source": [ 230 | "a = np.array([[1,2,3,4,5,6,7],[8, 9, 10, 11, 12, 13, 14]])\n", 231 | "print(a)" 232 | ], 233 | "metadata": { 234 | "colab": { 235 | "base_uri": "https://localhost:8080/" 236 | }, 237 | "id": "WgLwWHnlu9Xl", 238 | "outputId": "448f4202-7d32-484d-ffc7-14a5d17a7f1a" 239 | }, 240 | "execution_count": null, 241 | "outputs": [ 242 | { 243 | "output_type": "stream", 244 | "name": "stdout", 245 | "text": [ 246 | "[[ 1 2 3 4 5 6 7]\n", 247 | " [ 8 9 10 11 12 13 14]]\n" 248 | ] 249 | } 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "source": [ 255 | "a.shape" 256 | ], 257 | "metadata": { 258 | "colab": { 259 | "base_uri": "https://localhost:8080/" 260 | }, 261 | "id": "4evuooITw4O0", 262 | "outputId": "795e925a-0237-4f01-bfed-6c468bfc4eb7" 263 | }, 264 | "execution_count": null, 265 | "outputs": [ 266 | { 267 | "output_type": "execute_result", 268 | "data": { 269 | "text/plain": [ 270 | "(2, 7)" 271 | ] 272 | }, 273 | "metadata": {}, 274 | "execution_count": 16 275 | } 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "source": [ 281 | "print(a[1,-2])" 282 | ], 283 | "metadata": { 284 | "colab": { 285 | "base_uri": "https://localhost:8080/" 286 | }, 287 | "id": "kOBvlUdLxYf-", 288 | "outputId": "e1171ce8-e261-47b5-df88-688fdd2bbc5d" 289 | }, 290 | "execution_count": null, 291 | "outputs": [ 292 | { 293 | "output_type": "stream", 294 | "name": "stdout", 295 | "text": [ 296 | "13\n" 297 | ] 298 | } 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "source": [ 304 | "print(a[0, :])" 305 | ], 306 | "metadata": { 307 | "colab": { 308 | "base_uri": "https://localhost:8080/" 309 | }, 310 | "id": "x65RqC5nxfCL", 311 | "outputId": "74af6669-7a09-4fa3-c610-483c48dc8181" 312 | }, 313 | "execution_count": null, 314 | "outputs": [ 315 | { 316 | "output_type": "stream", 317 | "name": "stdout", 318 | "text": [ 319 | "[1 2 3 4 5 6 7]\n" 320 | ] 321 | } 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "source": [ 327 | "a[:, 2]" 328 | ], 329 | "metadata": { 330 | "colab": { 331 | "base_uri": "https://localhost:8080/" 332 | }, 333 | "id": "Hak_kpcCxn2T", 334 | "outputId": "f336dca0-0a96-4e24-98e1-8170d78e9ade" 335 | }, 336 | "execution_count": null, 337 | "outputs": [ 338 | { 339 | "output_type": "execute_result", 340 | "data": { 341 | "text/plain": [ 342 | "array([ 3, 10])" 343 | ] 344 | }, 345 | "metadata": {}, 346 | "execution_count": 21 347 | } 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "source": [ 353 | "a[0, 1:-1:2]" 354 | ], 355 | "metadata": { 356 | "colab": { 357 | "base_uri": "https://localhost:8080/" 358 | }, 359 | "id": "5Q_oJbXxxvXJ", 360 | "outputId": "dd2c1bd0-4a93-4c6f-849c-a6d07ed8ee56" 361 | }, 362 | "execution_count": null, 363 | "outputs": [ 364 | { 365 | "output_type": "execute_result", 366 | "data": { 367 | "text/plain": [ 368 | "array([2, 4, 6])" 369 | ] 370 | }, 371 | "metadata": {}, 372 | "execution_count": 24 373 | } 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "source": [ 379 | "a[1, 5] = 20\n", 380 | "print(a)\n", 381 | "a[:,2] = [1,2]\n", 382 | "print(a)" 383 | ], 384 | "metadata": { 385 | "colab": { 386 | "base_uri": "https://localhost:8080/" 387 | }, 388 | "id": "ZskZPgdCyYLP", 389 | "outputId": "014abc84-911d-4bc0-f2aa-c5d23fa5c238" 390 | }, 391 | "execution_count": null, 392 | "outputs": [ 393 | { 394 | "output_type": "stream", 395 | "name": "stdout", 396 | "text": [ 397 | "[[ 1 2 1 4 5 6 7]\n", 398 | " [ 8 9 2 11 12 20 14]]\n", 399 | "[[ 1 2 1 4 5 6 7]\n", 400 | " [ 8 9 2 11 12 20 14]]\n" 401 | ] 402 | } 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "source": [ 408 | "np.zeros((2, 3))" 409 | ], 410 | "metadata": { 411 | "colab": { 412 | "base_uri": "https://localhost:8080/" 413 | }, 414 | "id": "6uzwRRtKyusn", 415 | "outputId": "075a34b0-74a0-4468-d6b8-1f7eef9c62fc" 416 | }, 417 | "execution_count": null, 418 | "outputs": [ 419 | { 420 | "output_type": "execute_result", 421 | "data": { 422 | "text/plain": [ 423 | "array([[0., 0., 0.],\n", 424 | " [0., 0., 0.]])" 425 | ] 426 | }, 427 | "metadata": {}, 428 | "execution_count": 34 429 | } 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "source": [ 435 | "np.ones((4,3), dtype = 'int32')" 436 | ], 437 | "metadata": { 438 | "colab": { 439 | "base_uri": "https://localhost:8080/" 440 | }, 441 | "id": "5YSgao7dzR8n", 442 | "outputId": "0edb2c55-c6e3-4f4b-9e57-d42447156be7" 443 | }, 444 | "execution_count": null, 445 | "outputs": [ 446 | { 447 | "output_type": "execute_result", 448 | "data": { 449 | "text/plain": [ 450 | "array([[1, 1, 1],\n", 451 | " [1, 1, 1],\n", 452 | " [1, 1, 1],\n", 453 | " [1, 1, 1]], dtype=int32)" 454 | ] 455 | }, 456 | "metadata": {}, 457 | "execution_count": 35 458 | } 459 | ] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "source": [ 464 | "np.full((2, 2), 4, dtype='float32')" 465 | ], 466 | "metadata": { 467 | "colab": { 468 | "base_uri": "https://localhost:8080/" 469 | }, 470 | "id": "6BsE40drzosS", 471 | "outputId": "c453da6a-f0ae-4d1a-b532-cbfbc8de3e43" 472 | }, 473 | "execution_count": null, 474 | "outputs": [ 475 | { 476 | "output_type": "execute_result", 477 | "data": { 478 | "text/plain": [ 479 | "array([[4., 4.],\n", 480 | " [4., 4.]], dtype=float32)" 481 | ] 482 | }, 483 | "metadata": {}, 484 | "execution_count": 38 485 | } 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "source": [ 491 | "np.full_like(a.shape, 4)" 492 | ], 493 | "metadata": { 494 | "colab": { 495 | "base_uri": "https://localhost:8080/" 496 | }, 497 | "id": "oPuJHu7vzvZ1", 498 | "outputId": "0fd65a43-4c7e-40ed-c53e-bd95e71b21d1" 499 | }, 500 | "execution_count": null, 501 | "outputs": [ 502 | { 503 | "output_type": "execute_result", 504 | "data": { 505 | "text/plain": [ 506 | "array([4, 4])" 507 | ] 508 | }, 509 | "metadata": {}, 510 | "execution_count": 41 511 | } 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "source": [ 517 | "np.random.rand(4,2)" 518 | ], 519 | "metadata": { 520 | "colab": { 521 | "base_uri": "https://localhost:8080/" 522 | }, 523 | "id": "BceIO1Qm0Dbk", 524 | "outputId": "f9b0771c-db84-4ce7-e8ed-b29648758129" 525 | }, 526 | "execution_count": null, 527 | "outputs": [ 528 | { 529 | "output_type": "execute_result", 530 | "data": { 531 | "text/plain": [ 532 | "array([[0.65178753, 0.24740837],\n", 533 | " [0.62826234, 0.95127623],\n", 534 | " [0.1679481 , 0.41872858],\n", 535 | " [0.74924335, 0.5287255 ]])" 536 | ] 537 | }, 538 | "metadata": {}, 539 | "execution_count": 43 540 | } 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "source": [ 546 | "np.random.randint(4, size=(4,2))" 547 | ], 548 | "metadata": { 549 | "colab": { 550 | "base_uri": "https://localhost:8080/" 551 | }, 552 | "id": "Ro4POcCB0Plr", 553 | "outputId": "67b7808d-0ca6-465b-ba45-cb35546b64ef" 554 | }, 555 | "execution_count": null, 556 | "outputs": [ 557 | { 558 | "output_type": "execute_result", 559 | "data": { 560 | "text/plain": [ 561 | "array([[0, 0],\n", 562 | " [3, 1],\n", 563 | " [1, 0],\n", 564 | " [2, 0]])" 565 | ] 566 | }, 567 | "metadata": {}, 568 | "execution_count": 46 569 | } 570 | ] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "source": [ 575 | "np.identity(5)" 576 | ], 577 | "metadata": { 578 | "colab": { 579 | "base_uri": "https://localhost:8080/" 580 | }, 581 | "id": "BHkRaUF40pZF", 582 | "outputId": "85acd3a5-bdb7-4ef2-950c-e0aa4d9f8375" 583 | }, 584 | "execution_count": null, 585 | "outputs": [ 586 | { 587 | "output_type": "execute_result", 588 | "data": { 589 | "text/plain": [ 590 | "array([[1., 0., 0., 0., 0.],\n", 591 | " [0., 1., 0., 0., 0.],\n", 592 | " [0., 0., 1., 0., 0.],\n", 593 | " [0., 0., 0., 1., 0.],\n", 594 | " [0., 0., 0., 0., 1.]])" 595 | ] 596 | }, 597 | "metadata": {}, 598 | "execution_count": 47 599 | } 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "source": [ 605 | "arr = np.array([[1,2,3]])\n", 606 | "r1 = np.repeat(arr, 3, axis=0)\n", 607 | "print(r1)" 608 | ], 609 | "metadata": { 610 | "colab": { 611 | "base_uri": "https://localhost:8080/" 612 | }, 613 | "id": "iCjiAxnD0xT-", 614 | "outputId": "219aa57c-0726-46f6-b2cf-00d6f3302318" 615 | }, 616 | "execution_count": null, 617 | "outputs": [ 618 | { 619 | "output_type": "stream", 620 | "name": "stdout", 621 | "text": [ 622 | "[[1 2 3]\n", 623 | " [1 2 3]\n", 624 | " [1 2 3]]\n" 625 | ] 626 | } 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "source": [ 632 | "out = np.ones((5, 5))\n", 633 | "print(out)\n", 634 | "z = np.zeros((3, 3))\n", 635 | "z[1,1] = 9\n", 636 | "print(z)\n", 637 | "out[1:-1, 1:-1] = z\n", 638 | "print(out)" 639 | ], 640 | "metadata": { 641 | "colab": { 642 | "base_uri": "https://localhost:8080/" 643 | }, 644 | "id": "Vvrs9imq1I1w", 645 | "outputId": "a7f86fee-93c8-44e0-beed-754423e23ee3" 646 | }, 647 | "execution_count": null, 648 | "outputs": [ 649 | { 650 | "output_type": "stream", 651 | "name": "stdout", 652 | "text": [ 653 | "[[1. 1. 1. 1. 1.]\n", 654 | " [1. 1. 1. 1. 1.]\n", 655 | " [1. 1. 1. 1. 1.]\n", 656 | " [1. 1. 1. 1. 1.]\n", 657 | " [1. 1. 1. 1. 1.]]\n", 658 | "[[0. 0. 0.]\n", 659 | " [0. 9. 0.]\n", 660 | " [0. 0. 0.]]\n", 661 | "[[1. 1. 1. 1. 1.]\n", 662 | " [1. 0. 0. 0. 1.]\n", 663 | " [1. 0. 9. 0. 1.]\n", 664 | " [1. 0. 0. 0. 1.]\n", 665 | " [1. 1. 1. 1. 1.]]\n" 666 | ] 667 | } 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "source": [], 673 | "metadata": { 674 | "colab": { 675 | "base_uri": "https://localhost:8080/" 676 | }, 677 | "id": "h-Td1bCg3ze3", 678 | "outputId": "61c9b300-70f0-46de-dc66-011decfa4a5a" 679 | }, 680 | "execution_count": null, 681 | "outputs": [ 682 | { 683 | "output_type": "stream", 684 | "name": "stdout", 685 | "text": [ 686 | "[[0. 0. 0.]\n", 687 | " [0. 0. 0.]\n", 688 | " [0. 0. 0.]]\n" 689 | ] 690 | } 691 | ] 692 | }, 693 | { 694 | "cell_type": "code", 695 | "source": [], 696 | "metadata": { 697 | "id": "dBACfqeT32HW" 698 | }, 699 | "execution_count": null, 700 | "outputs": [] 701 | } 702 | ] 703 | } -------------------------------------------------------------------------------- /Lab_01_Basic_Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "source": [ 20 | "# Lecture no : 01" 21 | ], 22 | "metadata": { 23 | "id": "ELZmFMGm7Weo" 24 | } 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "source": [ 29 | "####################################################\n", 30 | "## 0. Comments\n", 31 | "####################################################" 32 | ], 33 | "metadata": { 34 | "id": "DHr3cHKNbNM3" 35 | } 36 | }, 37 | { 38 | "cell_type": "code", 39 | "source": [ 40 | "# Single line comments start with a number symbol.\n", 41 | "\n", 42 | "\"\"\" Multiline strings can be written\n", 43 | " using three \"s, and are often used\n", 44 | " as documentation.\n", 45 | "\"\"\"\n" 46 | ], 47 | "metadata": { 48 | "id": "Wzpf_eZpaSgF", 49 | "colab": { 50 | "base_uri": "https://localhost:8080/", 51 | "height": 36 52 | }, 53 | "outputId": "6ad54522-1f32-456d-8292-0679d720102c" 54 | }, 55 | "execution_count": null, 56 | "outputs": [ 57 | { 58 | "output_type": "execute_result", 59 | "data": { 60 | "text/plain": [ 61 | "' Multiline strings can be written\\n using three \"s, and are often used\\n as documentation.\\n'" 62 | ], 63 | "application/vnd.google.colaboratory.intrinsic+json": { 64 | "type": "string" 65 | } 66 | }, 67 | "metadata": {}, 68 | "execution_count": 1 69 | } 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "source": [], 75 | "metadata": { 76 | "id": "Z23hfB3GbhWV" 77 | }, 78 | "execution_count": null, 79 | "outputs": [] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "source": [ 84 | "####################################################\n", 85 | "## 1. Primitive Datatypes and Operators\n", 86 | "####################################################" 87 | ], 88 | "metadata": { 89 | "id": "djnQ5ivlaD1D" 90 | } 91 | }, 92 | { 93 | "cell_type": "code", 94 | "source": [ 95 | "\n", 96 | "# You have numbers\n", 97 | "3 # => 3" 98 | ], 99 | "metadata": { 100 | "colab": { 101 | "base_uri": "https://localhost:8080/" 102 | }, 103 | "id": "aLE-mqgybVBt", 104 | "outputId": "d5c337a6-7306-490a-da3b-4773349ab06f" 105 | }, 106 | "execution_count": null, 107 | "outputs": [ 108 | { 109 | "output_type": "execute_result", 110 | "data": { 111 | "text/plain": [ 112 | "3" 113 | ] 114 | }, 115 | "metadata": {}, 116 | "execution_count": 2 117 | } 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "source": [ 123 | "# Math is what you would expect\n", 124 | "1 + 1 # => 2" 125 | ], 126 | "metadata": { 127 | "colab": { 128 | "base_uri": "https://localhost:8080/" 129 | }, 130 | "id": "yo-MdrAxbkk9", 131 | "outputId": "9f46eb8b-a3c8-497e-adc6-0498e885bbf0" 132 | }, 133 | "execution_count": null, 134 | "outputs": [ 135 | { 136 | "output_type": "execute_result", 137 | "data": { 138 | "text/plain": [ 139 | "2" 140 | ] 141 | }, 142 | "metadata": {}, 143 | "execution_count": 3 144 | } 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "source": [ 150 | "8 - 1 # => 7" 151 | ], 152 | "metadata": { 153 | "colab": { 154 | "base_uri": "https://localhost:8080/" 155 | }, 156 | "id": "-HvMKlTsboOG", 157 | "outputId": "cd2c62b7-9505-4eee-9177-837c1e4c1953" 158 | }, 159 | "execution_count": null, 160 | "outputs": [ 161 | { 162 | "output_type": "execute_result", 163 | "data": { 164 | "text/plain": [ 165 | "7" 166 | ] 167 | }, 168 | "metadata": {}, 169 | "execution_count": 4 170 | } 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "source": [ 176 | "10 * 2 # => 20" 177 | ], 178 | "metadata": { 179 | "colab": { 180 | "base_uri": "https://localhost:8080/" 181 | }, 182 | "id": "A4kEd_jHbpWF", 183 | "outputId": "dbb901ee-db90-4191-8c6d-5c8cb51d4f51" 184 | }, 185 | "execution_count": null, 186 | "outputs": [ 187 | { 188 | "output_type": "execute_result", 189 | "data": { 190 | "text/plain": [ 191 | "20" 192 | ] 193 | }, 194 | "metadata": {}, 195 | "execution_count": 5 196 | } 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "source": [ 202 | "35 / 5 # => 7.0" 203 | ], 204 | "metadata": { 205 | "colab": { 206 | "base_uri": "https://localhost:8080/" 207 | }, 208 | "id": "8QuePVAobiEv", 209 | "outputId": "14b11b33-1871-4dd9-b6c2-93066549d1fc" 210 | }, 211 | "execution_count": null, 212 | "outputs": [ 213 | { 214 | "output_type": "execute_result", 215 | "data": { 216 | "text/plain": [ 217 | "7.0" 218 | ] 219 | }, 220 | "metadata": {}, 221 | "execution_count": 6 222 | } 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "source": [ 228 | "# Integer division rounds down for both positive and negative numbers.\n", 229 | "5 // 3 # => 1" 230 | ], 231 | "metadata": { 232 | "colab": { 233 | "base_uri": "https://localhost:8080/" 234 | }, 235 | "id": "8OChlZC2bmB4", 236 | "outputId": "03666180-5da2-4878-db91-0298f8879d27" 237 | }, 238 | "execution_count": null, 239 | "outputs": [ 240 | { 241 | "output_type": "execute_result", 242 | "data": { 243 | "text/plain": [ 244 | "1" 245 | ] 246 | }, 247 | "metadata": {}, 248 | "execution_count": 7 249 | } 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "source": [ 255 | "-5 // 3 # => -2" 256 | ], 257 | "metadata": { 258 | "colab": { 259 | "base_uri": "https://localhost:8080/" 260 | }, 261 | "id": "FAjrs7zEbzbU", 262 | "outputId": "51a4cbd7-e201-4420-d20b-478a54525c5a" 263 | }, 264 | "execution_count": null, 265 | "outputs": [ 266 | { 267 | "output_type": "execute_result", 268 | "data": { 269 | "text/plain": [ 270 | "-2" 271 | ] 272 | }, 273 | "metadata": {}, 274 | "execution_count": 8 275 | } 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "source": [ 281 | "5.0 // 3.0 # => 1.0 # works on floats too" 282 | ], 283 | "metadata": { 284 | "colab": { 285 | "base_uri": "https://localhost:8080/" 286 | }, 287 | "id": "LtvMu5mUb1Vf", 288 | "outputId": "e0465954-36e4-464f-cb7b-a81bae1cb43c" 289 | }, 290 | "execution_count": null, 291 | "outputs": [ 292 | { 293 | "output_type": "execute_result", 294 | "data": { 295 | "text/plain": [ 296 | "1.0" 297 | ] 298 | }, 299 | "metadata": {}, 300 | "execution_count": 9 301 | } 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "source": [ 307 | "-5.0 // 3.0 # => -2.0" 308 | ], 309 | "metadata": { 310 | "colab": { 311 | "base_uri": "https://localhost:8080/" 312 | }, 313 | "id": "8gduxC3sb3EJ", 314 | "outputId": "d3a2dd41-3c1a-4f7f-cf1d-775debd258a9" 315 | }, 316 | "execution_count": null, 317 | "outputs": [ 318 | { 319 | "output_type": "execute_result", 320 | "data": { 321 | "text/plain": [ 322 | "-2.0" 323 | ] 324 | }, 325 | "metadata": {}, 326 | "execution_count": 10 327 | } 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "source": [ 333 | "# The result of division is always a float\n", 334 | "10.0 / 3 # => 3.3333333333333335" 335 | ], 336 | "metadata": { 337 | "colab": { 338 | "base_uri": "https://localhost:8080/" 339 | }, 340 | "id": "NsvdXja5b3J1", 341 | "outputId": "16df0c94-b524-4345-f566-e356efb49d06" 342 | }, 343 | "execution_count": null, 344 | "outputs": [ 345 | { 346 | "output_type": "execute_result", 347 | "data": { 348 | "text/plain": [ 349 | "3.3333333333333335" 350 | ] 351 | }, 352 | "metadata": {}, 353 | "execution_count": 11 354 | } 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "source": [ 360 | "# Modulo operation\n", 361 | "7 % 3 # => 1" 362 | ], 363 | "metadata": { 364 | "colab": { 365 | "base_uri": "https://localhost:8080/" 366 | }, 367 | "id": "ZI-KoQXtb623", 368 | "outputId": "8f66a94e-3eae-4bb9-b637-f39511c709f3" 369 | }, 370 | "execution_count": null, 371 | "outputs": [ 372 | { 373 | "output_type": "execute_result", 374 | "data": { 375 | "text/plain": [ 376 | "1" 377 | ] 378 | }, 379 | "metadata": {}, 380 | "execution_count": 12 381 | } 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "source": [ 387 | "\n", 388 | "# i % j have the same sign as j, unlike C\n", 389 | "-7 % 3 # => 2" 390 | ], 391 | "metadata": { 392 | "colab": { 393 | "base_uri": "https://localhost:8080/" 394 | }, 395 | "id": "XlEisvbEb8ct", 396 | "outputId": "2a8152f2-fde1-49bf-b302-247e2a3b739b" 397 | }, 398 | "execution_count": null, 399 | "outputs": [ 400 | { 401 | "output_type": "execute_result", 402 | "data": { 403 | "text/plain": [ 404 | "2" 405 | ] 406 | }, 407 | "metadata": {}, 408 | "execution_count": 13 409 | } 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "source": [ 415 | "# Exponentiation (x**y, x to the yth power)\n", 416 | "2**3 # => 8" 417 | ], 418 | "metadata": { 419 | "colab": { 420 | "base_uri": "https://localhost:8080/" 421 | }, 422 | "id": "dsrNf9dLcAh0", 423 | "outputId": "cce6d178-18bd-49f0-85c1-947d552d76de" 424 | }, 425 | "execution_count": null, 426 | "outputs": [ 427 | { 428 | "output_type": "execute_result", 429 | "data": { 430 | "text/plain": [ 431 | "8" 432 | ] 433 | }, 434 | "metadata": {}, 435 | "execution_count": 14 436 | } 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "source": [ 442 | "# Enforce precedence with parentheses\n", 443 | "1 + 3 * 2 # => 7" 444 | ], 445 | "metadata": { 446 | "colab": { 447 | "base_uri": "https://localhost:8080/" 448 | }, 449 | "id": "I8y8S3YacD6Z", 450 | "outputId": "454a043b-91b8-4b89-f87b-dee4f5f3f745" 451 | }, 452 | "execution_count": null, 453 | "outputs": [ 454 | { 455 | "output_type": "execute_result", 456 | "data": { 457 | "text/plain": [ 458 | "7" 459 | ] 460 | }, 461 | "metadata": {}, 462 | "execution_count": 15 463 | } 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "source": [ 469 | "(1 + 3) * 2 # => 8" 470 | ], 471 | "metadata": { 472 | "colab": { 473 | "base_uri": "https://localhost:8080/" 474 | }, 475 | "id": "p0sUFpV7cFT1", 476 | "outputId": "1e87c52d-8d68-4b40-f268-e88870d39a8d" 477 | }, 478 | "execution_count": null, 479 | "outputs": [ 480 | { 481 | "output_type": "execute_result", 482 | "data": { 483 | "text/plain": [ 484 | "8" 485 | ] 486 | }, 487 | "metadata": {}, 488 | "execution_count": 16 489 | } 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "source": [ 495 | "# Boolean values are primitives (Note: the capitalization)\n", 496 | "True # => True" 497 | ], 498 | "metadata": { 499 | "colab": { 500 | "base_uri": "https://localhost:8080/" 501 | }, 502 | "id": "V4ZesXdzcIyc", 503 | "outputId": "752d3346-b9e0-41dd-a405-2728638d6229" 504 | }, 505 | "execution_count": null, 506 | "outputs": [ 507 | { 508 | "output_type": "execute_result", 509 | "data": { 510 | "text/plain": [ 511 | "True" 512 | ] 513 | }, 514 | "metadata": {}, 515 | "execution_count": 17 516 | } 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "source": [ 522 | "False # => False" 523 | ], 524 | "metadata": { 525 | "colab": { 526 | "base_uri": "https://localhost:8080/" 527 | }, 528 | "id": "-uAwv8ihcKOk", 529 | "outputId": "ff8930ae-3bfb-48a8-8923-dd820df3f1d3" 530 | }, 531 | "execution_count": null, 532 | "outputs": [ 533 | { 534 | "output_type": "execute_result", 535 | "data": { 536 | "text/plain": [ 537 | "False" 538 | ] 539 | }, 540 | "metadata": {}, 541 | "execution_count": 18 542 | } 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "source": [ 548 | "# negate with not\n", 549 | "not True # => False" 550 | ], 551 | "metadata": { 552 | "colab": { 553 | "base_uri": "https://localhost:8080/" 554 | }, 555 | "id": "3kzoOQDscLyR", 556 | "outputId": "059e9647-7248-4c1c-e8df-44725c62a3a0" 557 | }, 558 | "execution_count": null, 559 | "outputs": [ 560 | { 561 | "output_type": "execute_result", 562 | "data": { 563 | "text/plain": [ 564 | "False" 565 | ] 566 | }, 567 | "metadata": {}, 568 | "execution_count": 19 569 | } 570 | ] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "source": [ 575 | "not False # => True" 576 | ], 577 | "metadata": { 578 | "colab": { 579 | "base_uri": "https://localhost:8080/" 580 | }, 581 | "id": "_oc9MQxJcPdd", 582 | "outputId": "1f11ba74-1156-494b-edb1-a3404fbdf59e" 583 | }, 584 | "execution_count": null, 585 | "outputs": [ 586 | { 587 | "output_type": "execute_result", 588 | "data": { 589 | "text/plain": [ 590 | "True" 591 | ] 592 | }, 593 | "metadata": {}, 594 | "execution_count": 20 595 | } 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "source": [ 601 | "# Boolean Operators\n", 602 | "# Note \"and\" and \"or\" are case-sensitive\n", 603 | "True and False # => False" 604 | ], 605 | "metadata": { 606 | "colab": { 607 | "base_uri": "https://localhost:8080/" 608 | }, 609 | "id": "0T4eB8EYcRiv", 610 | "outputId": "e0a6793e-7d04-49ad-f958-30293324ae82" 611 | }, 612 | "execution_count": null, 613 | "outputs": [ 614 | { 615 | "output_type": "execute_result", 616 | "data": { 617 | "text/plain": [ 618 | "False" 619 | ] 620 | }, 621 | "metadata": {}, 622 | "execution_count": 21 623 | } 624 | ] 625 | }, 626 | { 627 | "cell_type": "code", 628 | "source": [ 629 | "False or True # => True" 630 | ], 631 | "metadata": { 632 | "colab": { 633 | "base_uri": "https://localhost:8080/" 634 | }, 635 | "id": "sfOMvbAEcWKA", 636 | "outputId": "906d767f-669d-4e68-89ed-967055868cf9" 637 | }, 638 | "execution_count": null, 639 | "outputs": [ 640 | { 641 | "output_type": "execute_result", 642 | "data": { 643 | "text/plain": [ 644 | "True" 645 | ] 646 | }, 647 | "metadata": {}, 648 | "execution_count": 22 649 | } 650 | ] 651 | }, 652 | { 653 | "cell_type": "code", 654 | "source": [ 655 | "# True and False are actually 1 and 0 but with different keywords\n", 656 | "True + True # => 2" 657 | ], 658 | "metadata": { 659 | "colab": { 660 | "base_uri": "https://localhost:8080/" 661 | }, 662 | "id": "1xOUA4MpcYBV", 663 | "outputId": "da99b7af-0481-4e6a-e5b3-b3e3096b37ef" 664 | }, 665 | "execution_count": null, 666 | "outputs": [ 667 | { 668 | "output_type": "execute_result", 669 | "data": { 670 | "text/plain": [ 671 | "2" 672 | ] 673 | }, 674 | "metadata": {}, 675 | "execution_count": 23 676 | } 677 | ] 678 | }, 679 | { 680 | "cell_type": "code", 681 | "source": [ 682 | "True * 8 # => 8" 683 | ], 684 | "metadata": { 685 | "colab": { 686 | "base_uri": "https://localhost:8080/" 687 | }, 688 | "id": "urebgI17cZ0d", 689 | "outputId": "d25b3972-659d-4d55-fab8-0fa74a282334" 690 | }, 691 | "execution_count": null, 692 | "outputs": [ 693 | { 694 | "output_type": "execute_result", 695 | "data": { 696 | "text/plain": [ 697 | "8" 698 | ] 699 | }, 700 | "metadata": {}, 701 | "execution_count": 24 702 | } 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "source": [ 708 | "False - 5 # => -5" 709 | ], 710 | "metadata": { 711 | "colab": { 712 | "base_uri": "https://localhost:8080/" 713 | }, 714 | "id": "WfnNPqI3cboF", 715 | "outputId": "807b8c7b-d782-4a98-d7fa-9f4dbd910b45" 716 | }, 717 | "execution_count": null, 718 | "outputs": [ 719 | { 720 | "output_type": "execute_result", 721 | "data": { 722 | "text/plain": [ 723 | "-5" 724 | ] 725 | }, 726 | "metadata": {}, 727 | "execution_count": 25 728 | } 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "source": [ 734 | "# Comparison operators look at the numerical value of True and False\n", 735 | "0 == False # => True" 736 | ], 737 | "metadata": { 738 | "colab": { 739 | "base_uri": "https://localhost:8080/" 740 | }, 741 | "id": "Tfc-PasGcguQ", 742 | "outputId": "398bec9a-c84e-4975-ca0a-ea5b40885858" 743 | }, 744 | "execution_count": null, 745 | "outputs": [ 746 | { 747 | "output_type": "execute_result", 748 | "data": { 749 | "text/plain": [ 750 | "True" 751 | ] 752 | }, 753 | "metadata": {}, 754 | "execution_count": 26 755 | } 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "source": [ 761 | "2 > True # => True" 762 | ], 763 | "metadata": { 764 | "colab": { 765 | "base_uri": "https://localhost:8080/" 766 | }, 767 | "id": "igISzW6ucil_", 768 | "outputId": "2c8d0443-b146-40c3-d0d0-5db8a8b7ce5b" 769 | }, 770 | "execution_count": null, 771 | "outputs": [ 772 | { 773 | "output_type": "execute_result", 774 | "data": { 775 | "text/plain": [ 776 | "True" 777 | ] 778 | }, 779 | "metadata": {}, 780 | "execution_count": 27 781 | } 782 | ] 783 | }, 784 | { 785 | "cell_type": "code", 786 | "source": [ 787 | "2 == True # => False" 788 | ], 789 | "metadata": { 790 | "colab": { 791 | "base_uri": "https://localhost:8080/" 792 | }, 793 | "id": "-OP01kvUclHi", 794 | "outputId": "f834d14c-6ae0-443b-ca5f-ba8468426dd6" 795 | }, 796 | "execution_count": null, 797 | "outputs": [ 798 | { 799 | "output_type": "execute_result", 800 | "data": { 801 | "text/plain": [ 802 | "False" 803 | ] 804 | }, 805 | "metadata": {}, 806 | "execution_count": 28 807 | } 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "source": [ 813 | "-5 != False # => True\n" 814 | ], 815 | "metadata": { 816 | "colab": { 817 | "base_uri": "https://localhost:8080/" 818 | }, 819 | "id": "tsJYSBY2cmJY", 820 | "outputId": "0dc6d862-a63b-4e6b-85d6-c25408e2393b" 821 | }, 822 | "execution_count": null, 823 | "outputs": [ 824 | { 825 | "output_type": "execute_result", 826 | "data": { 827 | "text/plain": [ 828 | "True" 829 | ] 830 | }, 831 | "metadata": {}, 832 | "execution_count": 29 833 | } 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "source": [ 839 | "# None, 0, and empty strings/lists/dicts/tuples/sets all evaluate to False.\n", 840 | "# All other values are True\n", 841 | "bool(0) # => False" 842 | ], 843 | "metadata": { 844 | "colab": { 845 | "base_uri": "https://localhost:8080/" 846 | }, 847 | "id": "Ofs9g3KPcnv4", 848 | "outputId": "9c9dd182-a095-4b00-f628-1c990ee65ee6" 849 | }, 850 | "execution_count": null, 851 | "outputs": [ 852 | { 853 | "output_type": "execute_result", 854 | "data": { 855 | "text/plain": [ 856 | "False" 857 | ] 858 | }, 859 | "metadata": {}, 860 | "execution_count": 30 861 | } 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "source": [ 867 | "bool(\"\") # => False" 868 | ], 869 | "metadata": { 870 | "colab": { 871 | "base_uri": "https://localhost:8080/" 872 | }, 873 | "id": "XqQzMYCecqlx", 874 | "outputId": "0f0f4bf2-876c-4350-c8e6-fd169b7d46bf" 875 | }, 876 | "execution_count": null, 877 | "outputs": [ 878 | { 879 | "output_type": "execute_result", 880 | "data": { 881 | "text/plain": [ 882 | "False" 883 | ] 884 | }, 885 | "metadata": {}, 886 | "execution_count": 31 887 | } 888 | ] 889 | }, 890 | { 891 | "cell_type": "code", 892 | "source": [ 893 | "bool([]) # => False" 894 | ], 895 | "metadata": { 896 | "colab": { 897 | "base_uri": "https://localhost:8080/" 898 | }, 899 | "id": "i53FiQUScsUx", 900 | "outputId": "be0018fb-3b88-4465-b05e-8b62fe9cc7c3" 901 | }, 902 | "execution_count": null, 903 | "outputs": [ 904 | { 905 | "output_type": "execute_result", 906 | "data": { 907 | "text/plain": [ 908 | "False" 909 | ] 910 | }, 911 | "metadata": {}, 912 | "execution_count": 32 913 | } 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "source": [ 919 | "bool({}) # => False" 920 | ], 921 | "metadata": { 922 | "colab": { 923 | "base_uri": "https://localhost:8080/" 924 | }, 925 | "id": "rHj-KTCWcvfr", 926 | "outputId": "56f0a774-edbd-4b73-80bf-b08a0868cd14" 927 | }, 928 | "execution_count": null, 929 | "outputs": [ 930 | { 931 | "output_type": "execute_result", 932 | "data": { 933 | "text/plain": [ 934 | "False" 935 | ] 936 | }, 937 | "metadata": {}, 938 | "execution_count": 33 939 | } 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "source": [ 945 | "bool(()) # => False" 946 | ], 947 | "metadata": { 948 | "colab": { 949 | "base_uri": "https://localhost:8080/" 950 | }, 951 | "id": "w6EBNNq4cyMI", 952 | "outputId": "9f525504-0821-4c08-8e1f-cca63ffbf07e" 953 | }, 954 | "execution_count": null, 955 | "outputs": [ 956 | { 957 | "output_type": "execute_result", 958 | "data": { 959 | "text/plain": [ 960 | "False" 961 | ] 962 | }, 963 | "metadata": {}, 964 | "execution_count": 34 965 | } 966 | ] 967 | }, 968 | { 969 | "cell_type": "code", 970 | "source": [ 971 | "bool(set()) # => False" 972 | ], 973 | "metadata": { 974 | "colab": { 975 | "base_uri": "https://localhost:8080/" 976 | }, 977 | "id": "X6M1DOrAczWS", 978 | "outputId": "3fe5a9de-1151-400e-abdc-c0d5edee1e21" 979 | }, 980 | "execution_count": null, 981 | "outputs": [ 982 | { 983 | "output_type": "execute_result", 984 | "data": { 985 | "text/plain": [ 986 | "False" 987 | ] 988 | }, 989 | "metadata": {}, 990 | "execution_count": 35 991 | } 992 | ] 993 | }, 994 | { 995 | "cell_type": "code", 996 | "source": [ 997 | "bool(4) # => True" 998 | ], 999 | "metadata": { 1000 | "colab": { 1001 | "base_uri": "https://localhost:8080/" 1002 | }, 1003 | "id": "PVIEBeL6c0_C", 1004 | "outputId": "16dfd6be-0e3f-47ba-f4ea-de885ead469a" 1005 | }, 1006 | "execution_count": null, 1007 | "outputs": [ 1008 | { 1009 | "output_type": "execute_result", 1010 | "data": { 1011 | "text/plain": [ 1012 | "True" 1013 | ] 1014 | }, 1015 | "metadata": {}, 1016 | "execution_count": 36 1017 | } 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "code", 1022 | "source": [ 1023 | "bool(-6) # => True" 1024 | ], 1025 | "metadata": { 1026 | "colab": { 1027 | "base_uri": "https://localhost:8080/" 1028 | }, 1029 | "id": "6UojttSFc2xJ", 1030 | "outputId": "3f5b0d87-8047-4558-957b-d5195f2907ab" 1031 | }, 1032 | "execution_count": null, 1033 | "outputs": [ 1034 | { 1035 | "output_type": "execute_result", 1036 | "data": { 1037 | "text/plain": [ 1038 | "True" 1039 | ] 1040 | }, 1041 | "metadata": {}, 1042 | "execution_count": 37 1043 | } 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "code", 1048 | "source": [ 1049 | "# Using boolean logical operators on ints casts them to booleans for evaluation,\n", 1050 | "# but their non-cast value is returned. Don't mix up with bool(ints) and bitwise\n", 1051 | "# and/or (&,|)\n", 1052 | "bool(0) # => False" 1053 | ], 1054 | "metadata": { 1055 | "colab": { 1056 | "base_uri": "https://localhost:8080/" 1057 | }, 1058 | "id": "Ol2GwMOnc943", 1059 | "outputId": "26a0b92e-ab9b-4d20-d4f0-6d0a783c4df6" 1060 | }, 1061 | "execution_count": null, 1062 | "outputs": [ 1063 | { 1064 | "output_type": "execute_result", 1065 | "data": { 1066 | "text/plain": [ 1067 | "False" 1068 | ] 1069 | }, 1070 | "metadata": {}, 1071 | "execution_count": 38 1072 | } 1073 | ] 1074 | }, 1075 | { 1076 | "cell_type": "code", 1077 | "source": [ 1078 | "bool(2) # => True" 1079 | ], 1080 | "metadata": { 1081 | "colab": { 1082 | "base_uri": "https://localhost:8080/" 1083 | }, 1084 | "id": "g6qPSriJdAvo", 1085 | "outputId": "0e66c808-db7c-4cdb-fcb1-6b3d915e189f" 1086 | }, 1087 | "execution_count": null, 1088 | "outputs": [ 1089 | { 1090 | "output_type": "execute_result", 1091 | "data": { 1092 | "text/plain": [ 1093 | "True" 1094 | ] 1095 | }, 1096 | "metadata": {}, 1097 | "execution_count": 39 1098 | } 1099 | ] 1100 | }, 1101 | { 1102 | "cell_type": "code", 1103 | "source": [ 1104 | "0 and 2 # => 0" 1105 | ], 1106 | "metadata": { 1107 | "colab": { 1108 | "base_uri": "https://localhost:8080/" 1109 | }, 1110 | "id": "1twtyZmndE-b", 1111 | "outputId": "ba67800e-6cc7-4193-befc-5e345e5dd289" 1112 | }, 1113 | "execution_count": null, 1114 | "outputs": [ 1115 | { 1116 | "output_type": "execute_result", 1117 | "data": { 1118 | "text/plain": [ 1119 | "0" 1120 | ] 1121 | }, 1122 | "metadata": {}, 1123 | "execution_count": 40 1124 | } 1125 | ] 1126 | }, 1127 | { 1128 | "cell_type": "code", 1129 | "source": [ 1130 | "bool(-5) # => True" 1131 | ], 1132 | "metadata": { 1133 | "colab": { 1134 | "base_uri": "https://localhost:8080/" 1135 | }, 1136 | "id": "42A_mZcvdGjA", 1137 | "outputId": "3ddcfcdd-8650-4fa0-9190-cadcd28f8d39" 1138 | }, 1139 | "execution_count": null, 1140 | "outputs": [ 1141 | { 1142 | "output_type": "execute_result", 1143 | "data": { 1144 | "text/plain": [ 1145 | "True" 1146 | ] 1147 | }, 1148 | "metadata": {}, 1149 | "execution_count": 41 1150 | } 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "source": [ 1156 | "bool(2) # => True" 1157 | ], 1158 | "metadata": { 1159 | "colab": { 1160 | "base_uri": "https://localhost:8080/" 1161 | }, 1162 | "id": "TupXUFB8dIER", 1163 | "outputId": "9c9c391f-5207-4b06-bedb-4d24b8be82b6" 1164 | }, 1165 | "execution_count": null, 1166 | "outputs": [ 1167 | { 1168 | "output_type": "execute_result", 1169 | "data": { 1170 | "text/plain": [ 1171 | "True" 1172 | ] 1173 | }, 1174 | "metadata": {}, 1175 | "execution_count": 42 1176 | } 1177 | ] 1178 | }, 1179 | { 1180 | "cell_type": "code", 1181 | "source": [ 1182 | "-5 or 0 # => -5" 1183 | ], 1184 | "metadata": { 1185 | "colab": { 1186 | "base_uri": "https://localhost:8080/" 1187 | }, 1188 | "id": "G6zP3G-kdKWw", 1189 | "outputId": "9c809855-31da-44f1-cc90-63a958dab172" 1190 | }, 1191 | "execution_count": null, 1192 | "outputs": [ 1193 | { 1194 | "output_type": "execute_result", 1195 | "data": { 1196 | "text/plain": [ 1197 | "-5" 1198 | ] 1199 | }, 1200 | "metadata": {}, 1201 | "execution_count": 43 1202 | } 1203 | ] 1204 | }, 1205 | { 1206 | "cell_type": "code", 1207 | "source": [ 1208 | "# Equality is ==\n", 1209 | "1 == 1 # => True" 1210 | ], 1211 | "metadata": { 1212 | "colab": { 1213 | "base_uri": "https://localhost:8080/" 1214 | }, 1215 | "id": "vguheGWHdL47", 1216 | "outputId": "e6dec561-fbdd-4de7-9a72-9c46b3e28d02" 1217 | }, 1218 | "execution_count": null, 1219 | "outputs": [ 1220 | { 1221 | "output_type": "execute_result", 1222 | "data": { 1223 | "text/plain": [ 1224 | "True" 1225 | ] 1226 | }, 1227 | "metadata": {}, 1228 | "execution_count": 44 1229 | } 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "code", 1234 | "source": [ 1235 | "2 == 1 # => False" 1236 | ], 1237 | "metadata": { 1238 | "colab": { 1239 | "base_uri": "https://localhost:8080/" 1240 | }, 1241 | "id": "PwpfKLAZdNYn", 1242 | "outputId": "a0aaed32-9b26-4361-8683-27e9ea00809a" 1243 | }, 1244 | "execution_count": null, 1245 | "outputs": [ 1246 | { 1247 | "output_type": "execute_result", 1248 | "data": { 1249 | "text/plain": [ 1250 | "False" 1251 | ] 1252 | }, 1253 | "metadata": {}, 1254 | "execution_count": 45 1255 | } 1256 | ] 1257 | }, 1258 | { 1259 | "cell_type": "code", 1260 | "source": [ 1261 | "# Inequality is !=\n", 1262 | "1 != 1 # => False" 1263 | ], 1264 | "metadata": { 1265 | "colab": { 1266 | "base_uri": "https://localhost:8080/" 1267 | }, 1268 | "id": "g9F2pbKYdPMD", 1269 | "outputId": "9e612421-cf49-4b6b-a85a-cbd7ef61cbf7" 1270 | }, 1271 | "execution_count": null, 1272 | "outputs": [ 1273 | { 1274 | "output_type": "execute_result", 1275 | "data": { 1276 | "text/plain": [ 1277 | "False" 1278 | ] 1279 | }, 1280 | "metadata": {}, 1281 | "execution_count": 46 1282 | } 1283 | ] 1284 | }, 1285 | { 1286 | "cell_type": "code", 1287 | "source": [ 1288 | "2 != 1 # => True" 1289 | ], 1290 | "metadata": { 1291 | "colab": { 1292 | "base_uri": "https://localhost:8080/" 1293 | }, 1294 | "id": "1wSjEu1MdSCb", 1295 | "outputId": "d7807493-68e1-46e9-91a6-6476cc560692" 1296 | }, 1297 | "execution_count": null, 1298 | "outputs": [ 1299 | { 1300 | "output_type": "execute_result", 1301 | "data": { 1302 | "text/plain": [ 1303 | "True" 1304 | ] 1305 | }, 1306 | "metadata": {}, 1307 | "execution_count": 47 1308 | } 1309 | ] 1310 | }, 1311 | { 1312 | "cell_type": "code", 1313 | "source": [ 1314 | "# More comparisons\n", 1315 | "1 < 10 # => True" 1316 | ], 1317 | "metadata": { 1318 | "colab": { 1319 | "base_uri": "https://localhost:8080/" 1320 | }, 1321 | "id": "RqUJaVsBdT17", 1322 | "outputId": "8adb4900-c10a-4454-e3b6-8f2b8e7e9081" 1323 | }, 1324 | "execution_count": null, 1325 | "outputs": [ 1326 | { 1327 | "output_type": "execute_result", 1328 | "data": { 1329 | "text/plain": [ 1330 | "True" 1331 | ] 1332 | }, 1333 | "metadata": {}, 1334 | "execution_count": 48 1335 | } 1336 | ] 1337 | }, 1338 | { 1339 | "cell_type": "code", 1340 | "source": [ 1341 | "2 <= 2 # => True" 1342 | ], 1343 | "metadata": { 1344 | "colab": { 1345 | "base_uri": "https://localhost:8080/" 1346 | }, 1347 | "id": "uZ12hFiIdVF8", 1348 | "outputId": "203b457a-ca00-46ad-9d86-ad8dc0996706" 1349 | }, 1350 | "execution_count": null, 1351 | "outputs": [ 1352 | { 1353 | "output_type": "execute_result", 1354 | "data": { 1355 | "text/plain": [ 1356 | "True" 1357 | ] 1358 | }, 1359 | "metadata": {}, 1360 | "execution_count": 49 1361 | } 1362 | ] 1363 | }, 1364 | { 1365 | "cell_type": "code", 1366 | "source": [ 1367 | "2 >= 2 # => True" 1368 | ], 1369 | "metadata": { 1370 | "colab": { 1371 | "base_uri": "https://localhost:8080/" 1372 | }, 1373 | "id": "ydwXQogJdbkr", 1374 | "outputId": "57f1d9af-9249-4bb9-cfaf-4289fa9dcc73" 1375 | }, 1376 | "execution_count": null, 1377 | "outputs": [ 1378 | { 1379 | "output_type": "execute_result", 1380 | "data": { 1381 | "text/plain": [ 1382 | "True" 1383 | ] 1384 | }, 1385 | "metadata": {}, 1386 | "execution_count": 50 1387 | } 1388 | ] 1389 | }, 1390 | { 1391 | "cell_type": "code", 1392 | "source": [ 1393 | "# Seeing whether a value is in a range\n", 1394 | "1 < 2 and 2 < 3 # => True" 1395 | ], 1396 | "metadata": { 1397 | "colab": { 1398 | "base_uri": "https://localhost:8080/" 1399 | }, 1400 | "id": "hGFNvcvGdlNo", 1401 | "outputId": "56bc8777-e81e-4483-f911-eac11b66dbc7" 1402 | }, 1403 | "execution_count": null, 1404 | "outputs": [ 1405 | { 1406 | "output_type": "execute_result", 1407 | "data": { 1408 | "text/plain": [ 1409 | "True" 1410 | ] 1411 | }, 1412 | "metadata": {}, 1413 | "execution_count": 51 1414 | } 1415 | ] 1416 | }, 1417 | { 1418 | "cell_type": "code", 1419 | "source": [ 1420 | "2 < 3 and 3 < 2 # => False" 1421 | ], 1422 | "metadata": { 1423 | "colab": { 1424 | "base_uri": "https://localhost:8080/" 1425 | }, 1426 | "id": "wOPh-xIWdnuL", 1427 | "outputId": "9099d589-6360-40a2-abaa-002bb9497d82" 1428 | }, 1429 | "execution_count": null, 1430 | "outputs": [ 1431 | { 1432 | "output_type": "execute_result", 1433 | "data": { 1434 | "text/plain": [ 1435 | "False" 1436 | ] 1437 | }, 1438 | "metadata": {}, 1439 | "execution_count": 52 1440 | } 1441 | ] 1442 | }, 1443 | { 1444 | "cell_type": "code", 1445 | "source": [ 1446 | "# Chaining makes this look nicer\n", 1447 | "1 < 2 < 3 # => True" 1448 | ], 1449 | "metadata": { 1450 | "colab": { 1451 | "base_uri": "https://localhost:8080/" 1452 | }, 1453 | "id": "uCxujcwFdqmq", 1454 | "outputId": "8598a408-c255-46fd-ee6b-d011e3b2845f" 1455 | }, 1456 | "execution_count": null, 1457 | "outputs": [ 1458 | { 1459 | "output_type": "execute_result", 1460 | "data": { 1461 | "text/plain": [ 1462 | "True" 1463 | ] 1464 | }, 1465 | "metadata": {}, 1466 | "execution_count": 53 1467 | } 1468 | ] 1469 | }, 1470 | { 1471 | "cell_type": "code", 1472 | "source": [ 1473 | "2 < 3 < 2 # => False" 1474 | ], 1475 | "metadata": { 1476 | "colab": { 1477 | "base_uri": "https://localhost:8080/" 1478 | }, 1479 | "id": "9ItGnXVNdrxy", 1480 | "outputId": "6a24f419-b864-48d5-e0b2-b401d57929b8" 1481 | }, 1482 | "execution_count": null, 1483 | "outputs": [ 1484 | { 1485 | "output_type": "execute_result", 1486 | "data": { 1487 | "text/plain": [ 1488 | "False" 1489 | ] 1490 | }, 1491 | "metadata": {}, 1492 | "execution_count": 54 1493 | } 1494 | ] 1495 | }, 1496 | { 1497 | "cell_type": "code", 1498 | "source": [ 1499 | "# (is vs. ==) is checks if two variables refer to the same object, but == checks\n", 1500 | "# if the objects pointed to have the same values.\n", 1501 | "a = [1, 2, 3, 4] # Point a at a new list, [1, 2, 3, 4]" 1502 | ], 1503 | "metadata": { 1504 | "id": "l4OXiY71dt2U" 1505 | }, 1506 | "execution_count": null, 1507 | "outputs": [] 1508 | }, 1509 | { 1510 | "cell_type": "code", 1511 | "source": [ 1512 | "b = a # Point b at what a is pointing to" 1513 | ], 1514 | "metadata": { 1515 | "id": "9cRqvUh7dwBz" 1516 | }, 1517 | "execution_count": null, 1518 | "outputs": [] 1519 | }, 1520 | { 1521 | "cell_type": "code", 1522 | "source": [ 1523 | "b is a # => True, a and b refer to the same object" 1524 | ], 1525 | "metadata": { 1526 | "colab": { 1527 | "base_uri": "https://localhost:8080/" 1528 | }, 1529 | "id": "bhentEZbd0mD", 1530 | "outputId": "f1577dd9-8881-4cb8-a223-106a45dc3861" 1531 | }, 1532 | "execution_count": null, 1533 | "outputs": [ 1534 | { 1535 | "output_type": "execute_result", 1536 | "data": { 1537 | "text/plain": [ 1538 | "True" 1539 | ] 1540 | }, 1541 | "metadata": {}, 1542 | "execution_count": 57 1543 | } 1544 | ] 1545 | }, 1546 | { 1547 | "cell_type": "code", 1548 | "source": [ 1549 | "b == a # => True, a's and b's objects are equal" 1550 | ], 1551 | "metadata": { 1552 | "colab": { 1553 | "base_uri": "https://localhost:8080/" 1554 | }, 1555 | "id": "Y_BBXthKd0sp", 1556 | "outputId": "fa3b639a-c357-414c-9285-095403e26f0d" 1557 | }, 1558 | "execution_count": null, 1559 | "outputs": [ 1560 | { 1561 | "output_type": "execute_result", 1562 | "data": { 1563 | "text/plain": [ 1564 | "True" 1565 | ] 1566 | }, 1567 | "metadata": {}, 1568 | "execution_count": 58 1569 | } 1570 | ] 1571 | }, 1572 | { 1573 | "cell_type": "code", 1574 | "source": [ 1575 | "b = [1, 2, 3, 4] # Point b at a new list, [1, 2, 3, 4]" 1576 | ], 1577 | "metadata": { 1578 | "id": "tC3bu1gGd0v9" 1579 | }, 1580 | "execution_count": null, 1581 | "outputs": [] 1582 | }, 1583 | { 1584 | "cell_type": "code", 1585 | "source": [ 1586 | "b is a # => False, a and b do not refer to the same object" 1587 | ], 1588 | "metadata": { 1589 | "colab": { 1590 | "base_uri": "https://localhost:8080/" 1591 | }, 1592 | "id": "3td8ymded0yt", 1593 | "outputId": "79f74929-9a93-4305-bc93-a5b0172b17e0" 1594 | }, 1595 | "execution_count": null, 1596 | "outputs": [ 1597 | { 1598 | "output_type": "execute_result", 1599 | "data": { 1600 | "text/plain": [ 1601 | "False" 1602 | ] 1603 | }, 1604 | "metadata": {}, 1605 | "execution_count": 60 1606 | } 1607 | ] 1608 | }, 1609 | { 1610 | "cell_type": "code", 1611 | "source": [ 1612 | "b == a # => True, a's and b's objects are equal" 1613 | ], 1614 | "metadata": { 1615 | "colab": { 1616 | "base_uri": "https://localhost:8080/" 1617 | }, 1618 | "id": "d2hEjUxHd01d", 1619 | "outputId": "dc893976-141f-49d9-9f8a-4db779aa6a1d" 1620 | }, 1621 | "execution_count": null, 1622 | "outputs": [ 1623 | { 1624 | "output_type": "execute_result", 1625 | "data": { 1626 | "text/plain": [ 1627 | "True" 1628 | ] 1629 | }, 1630 | "metadata": {}, 1631 | "execution_count": 61 1632 | } 1633 | ] 1634 | }, 1635 | { 1636 | "cell_type": "code", 1637 | "source": [ 1638 | "# Strings are created with \" or '\n", 1639 | "\"This is a string.\"" 1640 | ], 1641 | "metadata": { 1642 | "colab": { 1643 | "base_uri": "https://localhost:8080/", 1644 | "height": 36 1645 | }, 1646 | "id": "Xx6XL9EFd04E", 1647 | "outputId": "11315129-16dd-40c4-f8bc-1615c366e28c" 1648 | }, 1649 | "execution_count": null, 1650 | "outputs": [ 1651 | { 1652 | "output_type": "execute_result", 1653 | "data": { 1654 | "text/plain": [ 1655 | "'This is a string.'" 1656 | ], 1657 | "application/vnd.google.colaboratory.intrinsic+json": { 1658 | "type": "string" 1659 | } 1660 | }, 1661 | "metadata": {}, 1662 | "execution_count": 62 1663 | } 1664 | ] 1665 | }, 1666 | { 1667 | "cell_type": "code", 1668 | "source": [ 1669 | "'This is also a string.'" 1670 | ], 1671 | "metadata": { 1672 | "colab": { 1673 | "base_uri": "https://localhost:8080/", 1674 | "height": 36 1675 | }, 1676 | "id": "Hj6ZqVGqeD37", 1677 | "outputId": "1fa97e8e-e29c-482a-b087-dfca3718ba2a" 1678 | }, 1679 | "execution_count": null, 1680 | "outputs": [ 1681 | { 1682 | "output_type": "execute_result", 1683 | "data": { 1684 | "text/plain": [ 1685 | "'This is also a string.'" 1686 | ], 1687 | "application/vnd.google.colaboratory.intrinsic+json": { 1688 | "type": "string" 1689 | } 1690 | }, 1691 | "metadata": {}, 1692 | "execution_count": 63 1693 | } 1694 | ] 1695 | }, 1696 | { 1697 | "cell_type": "code", 1698 | "source": [ 1699 | "# Strings can be added too\n", 1700 | "\"Hello \" + \"world!\" # => \"Hello world!\"" 1701 | ], 1702 | "metadata": { 1703 | "colab": { 1704 | "base_uri": "https://localhost:8080/", 1705 | "height": 36 1706 | }, 1707 | "id": "YRPGJhgieD9v", 1708 | "outputId": "96df438d-9142-4349-f914-8a8537b7a081" 1709 | }, 1710 | "execution_count": null, 1711 | "outputs": [ 1712 | { 1713 | "output_type": "execute_result", 1714 | "data": { 1715 | "text/plain": [ 1716 | "'Hello world!'" 1717 | ], 1718 | "application/vnd.google.colaboratory.intrinsic+json": { 1719 | "type": "string" 1720 | } 1721 | }, 1722 | "metadata": {}, 1723 | "execution_count": 64 1724 | } 1725 | ] 1726 | }, 1727 | { 1728 | "cell_type": "code", 1729 | "source": [ 1730 | "# String literals (but not variables) can be concatenated without using '+'\n", 1731 | "\"Hello \" \"world!\" # => \"Hello world!\"" 1732 | ], 1733 | "metadata": { 1734 | "colab": { 1735 | "base_uri": "https://localhost:8080/", 1736 | "height": 36 1737 | }, 1738 | "id": "2UFP29wqeEEd", 1739 | "outputId": "6e0e5d01-9200-41c1-b17a-930b78d8f6e8" 1740 | }, 1741 | "execution_count": null, 1742 | "outputs": [ 1743 | { 1744 | "output_type": "execute_result", 1745 | "data": { 1746 | "text/plain": [ 1747 | "'Hello world!'" 1748 | ], 1749 | "application/vnd.google.colaboratory.intrinsic+json": { 1750 | "type": "string" 1751 | } 1752 | }, 1753 | "metadata": {}, 1754 | "execution_count": 65 1755 | } 1756 | ] 1757 | }, 1758 | { 1759 | "cell_type": "code", 1760 | "source": [ 1761 | "# A string can be treated like a list of characters\n", 1762 | "\"Hello world!\"[0] # => 'H'" 1763 | ], 1764 | "metadata": { 1765 | "colab": { 1766 | "base_uri": "https://localhost:8080/", 1767 | "height": 36 1768 | }, 1769 | "id": "3wuadepKeIyb", 1770 | "outputId": "2158f3cb-5b81-495e-c8af-ece80d71921f" 1771 | }, 1772 | "execution_count": null, 1773 | "outputs": [ 1774 | { 1775 | "output_type": "execute_result", 1776 | "data": { 1777 | "text/plain": [ 1778 | "'H'" 1779 | ], 1780 | "application/vnd.google.colaboratory.intrinsic+json": { 1781 | "type": "string" 1782 | } 1783 | }, 1784 | "metadata": {}, 1785 | "execution_count": 66 1786 | } 1787 | ] 1788 | }, 1789 | { 1790 | "cell_type": "code", 1791 | "source": [ 1792 | "# You can find the length of a string\n", 1793 | "len(\"This is a string\") # => 16" 1794 | ], 1795 | "metadata": { 1796 | "colab": { 1797 | "base_uri": "https://localhost:8080/" 1798 | }, 1799 | "id": "Isex6D-geOpC", 1800 | "outputId": "3aeefaf8-d803-49e9-ae78-5cef11520fdd" 1801 | }, 1802 | "execution_count": null, 1803 | "outputs": [ 1804 | { 1805 | "output_type": "execute_result", 1806 | "data": { 1807 | "text/plain": [ 1808 | "16" 1809 | ] 1810 | }, 1811 | "metadata": {}, 1812 | "execution_count": 67 1813 | } 1814 | ] 1815 | }, 1816 | { 1817 | "cell_type": "code", 1818 | "source": [ 1819 | "# Since Python 3.6, you can use f-strings or formatted string literals.\n", 1820 | "name = \"Reiko\"\n", 1821 | "f\"She said her name is {name}.\" # => \"She said her name is Reiko\"" 1822 | ], 1823 | "metadata": { 1824 | "colab": { 1825 | "base_uri": "https://localhost:8080/", 1826 | "height": 36 1827 | }, 1828 | "id": "obQMRzyPeOwU", 1829 | "outputId": "4d0cec6b-7647-4e1d-c224-fa76fc2baad6" 1830 | }, 1831 | "execution_count": null, 1832 | "outputs": [ 1833 | { 1834 | "output_type": "execute_result", 1835 | "data": { 1836 | "text/plain": [ 1837 | "'She said her name is Reiko.'" 1838 | ], 1839 | "application/vnd.google.colaboratory.intrinsic+json": { 1840 | "type": "string" 1841 | } 1842 | }, 1843 | "metadata": {}, 1844 | "execution_count": 68 1845 | } 1846 | ] 1847 | }, 1848 | { 1849 | "cell_type": "code", 1850 | "source": [ 1851 | "# Any valid Python expression inside these braces is returned to the string.\n", 1852 | "f\"{name} is {len(name)} characters long.\" # => \"Reiko is 5 characters long.\"" 1853 | ], 1854 | "metadata": { 1855 | "colab": { 1856 | "base_uri": "https://localhost:8080/", 1857 | "height": 36 1858 | }, 1859 | "id": "LTbFBcDUeSgZ", 1860 | "outputId": "17c9fcf6-3be6-4dbc-a55f-be3fde50e971" 1861 | }, 1862 | "execution_count": null, 1863 | "outputs": [ 1864 | { 1865 | "output_type": "execute_result", 1866 | "data": { 1867 | "text/plain": [ 1868 | "'Reiko is 5 characters long.'" 1869 | ], 1870 | "application/vnd.google.colaboratory.intrinsic+json": { 1871 | "type": "string" 1872 | } 1873 | }, 1874 | "metadata": {}, 1875 | "execution_count": 69 1876 | } 1877 | ] 1878 | }, 1879 | { 1880 | "cell_type": "code", 1881 | "source": [ 1882 | "# None is an object\n", 1883 | "None # => None" 1884 | ], 1885 | "metadata": { 1886 | "id": "C4xIW32ReWL3" 1887 | }, 1888 | "execution_count": null, 1889 | "outputs": [] 1890 | }, 1891 | { 1892 | "cell_type": "code", 1893 | "source": [ 1894 | "# Don't use the equality \"==\" symbol to compare objects to None\n", 1895 | "# Use \"is\" instead. This checks for equality of object identity.\n", 1896 | "\"etc\" is None # => False" 1897 | ], 1898 | "metadata": { 1899 | "colab": { 1900 | "base_uri": "https://localhost:8080/" 1901 | }, 1902 | "id": "aNM4euxBeWbV", 1903 | "outputId": "39837f52-9ebc-4016-b3d7-e1c157a2c550" 1904 | }, 1905 | "execution_count": null, 1906 | "outputs": [ 1907 | { 1908 | "output_type": "stream", 1909 | "name": "stderr", 1910 | "text": [ 1911 | "<>:3: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n", 1912 | "<>:3: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n", 1913 | ":3: SyntaxWarning: \"is\" with a literal. Did you mean \"==\"?\n", 1914 | " \"etc\" is None # => False\n" 1915 | ] 1916 | }, 1917 | { 1918 | "output_type": "execute_result", 1919 | "data": { 1920 | "text/plain": [ 1921 | "False" 1922 | ] 1923 | }, 1924 | "metadata": {}, 1925 | "execution_count": 71 1926 | } 1927 | ] 1928 | }, 1929 | { 1930 | "cell_type": "code", 1931 | "source": [ 1932 | "None is None # => True" 1933 | ], 1934 | "metadata": { 1935 | "colab": { 1936 | "base_uri": "https://localhost:8080/" 1937 | }, 1938 | "id": "PsVqD7cwef85", 1939 | "outputId": "15b0e0e3-9541-45cd-c49e-f4a760147839" 1940 | }, 1941 | "execution_count": null, 1942 | "outputs": [ 1943 | { 1944 | "output_type": "execute_result", 1945 | "data": { 1946 | "text/plain": [ 1947 | "True" 1948 | ] 1949 | }, 1950 | "metadata": {}, 1951 | "execution_count": 72 1952 | } 1953 | ] 1954 | }, 1955 | { 1956 | "cell_type": "markdown", 1957 | "source": [ 1958 | "####################################################\n", 1959 | "## 2. Variables and Collections\n", 1960 | "####################################################" 1961 | ], 1962 | "metadata": { 1963 | "id": "i_TR0eqIeml9" 1964 | } 1965 | }, 1966 | { 1967 | "cell_type": "code", 1968 | "source": [ 1969 | "# Python has a print function\n", 1970 | "print(\"I'm Python. Nice to meet you!\") # => I'm Python. Nice to meet you!" 1971 | ], 1972 | "metadata": { 1973 | "colab": { 1974 | "base_uri": "https://localhost:8080/" 1975 | }, 1976 | "id": "XRp4DJURepOE", 1977 | "outputId": "7acc0476-4df2-4db9-f3c8-409f043b64f8" 1978 | }, 1979 | "execution_count": null, 1980 | "outputs": [ 1981 | { 1982 | "output_type": "stream", 1983 | "name": "stdout", 1984 | "text": [ 1985 | "I'm Python. Nice to meet you!\n" 1986 | ] 1987 | } 1988 | ] 1989 | }, 1990 | { 1991 | "cell_type": "code", 1992 | "source": [ 1993 | "# By default the print function also prints out a newline at the end.\n", 1994 | "# Use the optional argument end to change the end string.\n", 1995 | "print(\"Hello, World\", end=\"!\") # => Hello, World!" 1996 | ], 1997 | "metadata": { 1998 | "colab": { 1999 | "base_uri": "https://localhost:8080/" 2000 | }, 2001 | "id": "-QKMVn8AesLI", 2002 | "outputId": "5fa1b46f-0a34-4dd9-eb5e-20db328c7cae" 2003 | }, 2004 | "execution_count": null, 2005 | "outputs": [ 2006 | { 2007 | "output_type": "stream", 2008 | "name": "stdout", 2009 | "text": [ 2010 | "Hello, World!" 2011 | ] 2012 | } 2013 | ] 2014 | }, 2015 | { 2016 | "cell_type": "code", 2017 | "source": [ 2018 | "# Simple way to get input data from console\n", 2019 | "input_string_var = input(\"Enter some data: \") # Returns the data as a string" 2020 | ], 2021 | "metadata": { 2022 | "colab": { 2023 | "base_uri": "https://localhost:8080/" 2024 | }, 2025 | "id": "O6iBeycPet9F", 2026 | "outputId": "05455299-1aa3-4b7a-e81d-f2497db286ae" 2027 | }, 2028 | "execution_count": null, 2029 | "outputs": [ 2030 | { 2031 | "name": "stdout", 2032 | "output_type": "stream", 2033 | "text": [ 2034 | "Enter some data: Fozia Khan\n" 2035 | ] 2036 | } 2037 | ] 2038 | }, 2039 | { 2040 | "cell_type": "code", 2041 | "source": [ 2042 | "# There are no declarations, only assignments.\n", 2043 | "# Convention is to use lower_case_with_underscores\n", 2044 | "some_var = 5" 2045 | ], 2046 | "metadata": { 2047 | "id": "tbfmnO-YezJ1" 2048 | }, 2049 | "execution_count": null, 2050 | "outputs": [] 2051 | }, 2052 | { 2053 | "cell_type": "code", 2054 | "source": [ 2055 | "some_var # => 5" 2056 | ], 2057 | "metadata": { 2058 | "colab": { 2059 | "base_uri": "https://localhost:8080/" 2060 | }, 2061 | "id": "Uy8g6n7ae1UC", 2062 | "outputId": "e6aa866a-3492-4f05-9f53-87ee7db65065" 2063 | }, 2064 | "execution_count": null, 2065 | "outputs": [ 2066 | { 2067 | "output_type": "execute_result", 2068 | "data": { 2069 | "text/plain": [ 2070 | "5" 2071 | ] 2072 | }, 2073 | "metadata": {}, 2074 | "execution_count": 77 2075 | } 2076 | ] 2077 | }, 2078 | { 2079 | "cell_type": "code", 2080 | "source": [ 2081 | "# Accessing a previously unassigned variable is an exception.\n", 2082 | "# See Control Flow to learn more about exception handling.\n", 2083 | "some_unknown_var # Raises a NameError" 2084 | ], 2085 | "metadata": { 2086 | "colab": { 2087 | "base_uri": "https://localhost:8080/", 2088 | "height": 219 2089 | }, 2090 | "id": "CX9xgk5he38b", 2091 | "outputId": "3a0f97ab-bd83-4477-aeb4-7d29c32defbf" 2092 | }, 2093 | "execution_count": null, 2094 | "outputs": [ 2095 | { 2096 | "output_type": "error", 2097 | "ename": "NameError", 2098 | "evalue": "ignored", 2099 | "traceback": [ 2100 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 2101 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 2102 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Accessing a previously unassigned variable is an exception.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# See Control Flow to learn more about exception handling.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0msome_unknown_var\u001b[0m \u001b[0;31m# Raises a NameError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 2103 | "\u001b[0;31mNameError\u001b[0m: name 'some_unknown_var' is not defined" 2104 | ] 2105 | } 2106 | ] 2107 | }, 2108 | { 2109 | "cell_type": "code", 2110 | "source": [ 2111 | "# if can be used as an expression\n", 2112 | "# Equivalent of C's '?:' ternary operator\n", 2113 | "\"yay!\" if 0 > 1 else \"nay!\" # => \"nay!\"" 2114 | ], 2115 | "metadata": { 2116 | "colab": { 2117 | "base_uri": "https://localhost:8080/", 2118 | "height": 36 2119 | }, 2120 | "id": "fPMWkNYZe9E6", 2121 | "outputId": "79ce3d56-894f-4a5e-c59e-0e40dec285c6" 2122 | }, 2123 | "execution_count": null, 2124 | "outputs": [ 2125 | { 2126 | "output_type": "execute_result", 2127 | "data": { 2128 | "text/plain": [ 2129 | "'nay!'" 2130 | ], 2131 | "application/vnd.google.colaboratory.intrinsic+json": { 2132 | "type": "string" 2133 | } 2134 | }, 2135 | "metadata": {}, 2136 | "execution_count": 79 2137 | } 2138 | ] 2139 | }, 2140 | { 2141 | "cell_type": "code", 2142 | "source": [ 2143 | "# Lists store sequences\n", 2144 | "li = [] #empty list" 2145 | ], 2146 | "metadata": { 2147 | "id": "5mtKG1kbfAYm" 2148 | }, 2149 | "execution_count": null, 2150 | "outputs": [] 2151 | }, 2152 | { 2153 | "cell_type": "code", 2154 | "source": [ 2155 | "# You can start with a prefilled list\n", 2156 | "other_li = [4, 5, 6]" 2157 | ], 2158 | "metadata": { 2159 | "id": "AZToxc9nfCYV" 2160 | }, 2161 | "execution_count": null, 2162 | "outputs": [] 2163 | }, 2164 | { 2165 | "cell_type": "code", 2166 | "source": [ 2167 | "# Add stuff to the end of a list with append\n", 2168 | "li.append(1) # li is now [1]" 2169 | ], 2170 | "metadata": { 2171 | "id": "XkOexKN0fD5p" 2172 | }, 2173 | "execution_count": null, 2174 | "outputs": [] 2175 | }, 2176 | { 2177 | "cell_type": "code", 2178 | "source": [ 2179 | "li.append(2) # li is now [1, 2]" 2180 | ], 2181 | "metadata": { 2182 | "id": "1FpBZRUjfF6S" 2183 | }, 2184 | "execution_count": null, 2185 | "outputs": [] 2186 | }, 2187 | { 2188 | "cell_type": "code", 2189 | "source": [ 2190 | "li.append(4) # li is now [1, 2, 4]" 2191 | ], 2192 | "metadata": { 2193 | "id": "9DHnzAjMfJY5" 2194 | }, 2195 | "execution_count": null, 2196 | "outputs": [] 2197 | }, 2198 | { 2199 | "cell_type": "code", 2200 | "source": [ 2201 | "li.append(3) # li is now [1, 2, 4, 3]" 2202 | ], 2203 | "metadata": { 2204 | "id": "dPbKy0c_fK21" 2205 | }, 2206 | "execution_count": null, 2207 | "outputs": [] 2208 | }, 2209 | { 2210 | "cell_type": "code", 2211 | "source": [ 2212 | "# Remove from the end with pop\n", 2213 | "li.pop() # => 3 and li is now [1, 2, 4]" 2214 | ], 2215 | "metadata": { 2216 | "colab": { 2217 | "base_uri": "https://localhost:8080/" 2218 | }, 2219 | "id": "NWhoAz9pfMZr", 2220 | "outputId": "e626877c-c5b2-436f-ee34-cfdbce31d91b" 2221 | }, 2222 | "execution_count": null, 2223 | "outputs": [ 2224 | { 2225 | "output_type": "execute_result", 2226 | "data": { 2227 | "text/plain": [ 2228 | "3" 2229 | ] 2230 | }, 2231 | "metadata": {}, 2232 | "execution_count": 86 2233 | } 2234 | ] 2235 | }, 2236 | { 2237 | "cell_type": "code", 2238 | "source": [ 2239 | "# Let's put it back\n", 2240 | "li.append(3) # li is now [1, 2, 4, 3] again." 2241 | ], 2242 | "metadata": { 2243 | "id": "Zpve0UQ1fONq" 2244 | }, 2245 | "execution_count": null, 2246 | "outputs": [] 2247 | }, 2248 | { 2249 | "cell_type": "code", 2250 | "source": [ 2251 | "# Access a list like you would any array\n", 2252 | "li[0] # => 1" 2253 | ], 2254 | "metadata": { 2255 | "colab": { 2256 | "base_uri": "https://localhost:8080/" 2257 | }, 2258 | "id": "XiIcVruHfSgy", 2259 | "outputId": "18e9c38b-3e10-476c-fe84-e0caf16ed055" 2260 | }, 2261 | "execution_count": null, 2262 | "outputs": [ 2263 | { 2264 | "output_type": "execute_result", 2265 | "data": { 2266 | "text/plain": [ 2267 | "1" 2268 | ] 2269 | }, 2270 | "metadata": {}, 2271 | "execution_count": 88 2272 | } 2273 | ] 2274 | }, 2275 | { 2276 | "cell_type": "code", 2277 | "source": [ 2278 | "# Look at the last element\n", 2279 | "li[-1] # => 3" 2280 | ], 2281 | "metadata": { 2282 | "colab": { 2283 | "base_uri": "https://localhost:8080/" 2284 | }, 2285 | "id": "KelLzGAtfUzR", 2286 | "outputId": "483de676-2a3a-45fe-f554-c0acc77cdd29" 2287 | }, 2288 | "execution_count": null, 2289 | "outputs": [ 2290 | { 2291 | "output_type": "execute_result", 2292 | "data": { 2293 | "text/plain": [ 2294 | "3" 2295 | ] 2296 | }, 2297 | "metadata": {}, 2298 | "execution_count": 89 2299 | } 2300 | ] 2301 | }, 2302 | { 2303 | "cell_type": "code", 2304 | "source": [ 2305 | "# Looking out of bounds is an IndexError\n", 2306 | "li[4] # Raises an IndexError" 2307 | ], 2308 | "metadata": { 2309 | "colab": { 2310 | "base_uri": "https://localhost:8080/", 2311 | "height": 200 2312 | }, 2313 | "id": "wo0kId-4fVsS", 2314 | "outputId": "e13d6ab8-cd41-4af5-b8b7-87097e55881f" 2315 | }, 2316 | "execution_count": null, 2317 | "outputs": [ 2318 | { 2319 | "output_type": "error", 2320 | "ename": "IndexError", 2321 | "evalue": "ignored", 2322 | "traceback": [ 2323 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 2324 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 2325 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Looking out of bounds is an IndexError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mli\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# Raises an IndexError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 2326 | "\u001b[0;31mIndexError\u001b[0m: list index out of range" 2327 | ] 2328 | } 2329 | ] 2330 | }, 2331 | { 2332 | "cell_type": "code", 2333 | "source": [ 2334 | "# You can look at ranges with slice syntax.\n", 2335 | "# The start index is included, the end index is not\n", 2336 | "# (It's a closed/open range for you mathy types.)\n", 2337 | "li[1:3] # Return list from index 1 to 3 => [2, 4]" 2338 | ], 2339 | "metadata": { 2340 | "colab": { 2341 | "base_uri": "https://localhost:8080/" 2342 | }, 2343 | "id": "rHc9ENlIfXqk", 2344 | "outputId": "b63320ad-763e-4a03-9438-e797c4498304" 2345 | }, 2346 | "execution_count": null, 2347 | "outputs": [ 2348 | { 2349 | "output_type": "execute_result", 2350 | "data": { 2351 | "text/plain": [ 2352 | "[2, 4]" 2353 | ] 2354 | }, 2355 | "metadata": {}, 2356 | "execution_count": 91 2357 | } 2358 | ] 2359 | }, 2360 | { 2361 | "cell_type": "code", 2362 | "source": [ 2363 | "li[2:] # Return list starting from index 2 => [4, 3]" 2364 | ], 2365 | "metadata": { 2366 | "colab": { 2367 | "base_uri": "https://localhost:8080/" 2368 | }, 2369 | "id": "a0phj1iFfZqD", 2370 | "outputId": "725c3190-0711-4f93-b20c-65ae9b7796c1" 2371 | }, 2372 | "execution_count": null, 2373 | "outputs": [ 2374 | { 2375 | "output_type": "execute_result", 2376 | "data": { 2377 | "text/plain": [ 2378 | "[4, 3]" 2379 | ] 2380 | }, 2381 | "metadata": {}, 2382 | "execution_count": 92 2383 | } 2384 | ] 2385 | }, 2386 | { 2387 | "cell_type": "code", 2388 | "source": [ 2389 | "li[:3] # Return list from beginning until index 3 => [1, 2, 4]" 2390 | ], 2391 | "metadata": { 2392 | "colab": { 2393 | "base_uri": "https://localhost:8080/" 2394 | }, 2395 | "id": "03WoAKh5fbM6", 2396 | "outputId": "ad352834-e504-4aa5-e350-e3429774e447" 2397 | }, 2398 | "execution_count": null, 2399 | "outputs": [ 2400 | { 2401 | "output_type": "execute_result", 2402 | "data": { 2403 | "text/plain": [ 2404 | "[1, 2, 4]" 2405 | ] 2406 | }, 2407 | "metadata": {}, 2408 | "execution_count": 93 2409 | } 2410 | ] 2411 | }, 2412 | { 2413 | "cell_type": "code", 2414 | "source": [ 2415 | "li[::2] # Return list selecting elements with a step size of 2 => [1, 4]" 2416 | ], 2417 | "metadata": { 2418 | "colab": { 2419 | "base_uri": "https://localhost:8080/" 2420 | }, 2421 | "id": "GBfwzLAPfdK0", 2422 | "outputId": "50760a5d-f657-462d-91d9-6e84a5895f1f" 2423 | }, 2424 | "execution_count": null, 2425 | "outputs": [ 2426 | { 2427 | "output_type": "execute_result", 2428 | "data": { 2429 | "text/plain": [ 2430 | "[1, 4]" 2431 | ] 2432 | }, 2433 | "metadata": {}, 2434 | "execution_count": 94 2435 | } 2436 | ] 2437 | }, 2438 | { 2439 | "cell_type": "code", 2440 | "source": [ 2441 | "li[::-1] # Return list in reverse order => [3, 4, 2, 1]" 2442 | ], 2443 | "metadata": { 2444 | "colab": { 2445 | "base_uri": "https://localhost:8080/" 2446 | }, 2447 | "id": "LZNfFGZCfe5C", 2448 | "outputId": "7ada2c6d-a6eb-4aa3-fc84-be64d4384d4d" 2449 | }, 2450 | "execution_count": null, 2451 | "outputs": [ 2452 | { 2453 | "output_type": "execute_result", 2454 | "data": { 2455 | "text/plain": [ 2456 | "[3, 4, 2, 1]" 2457 | ] 2458 | }, 2459 | "metadata": {}, 2460 | "execution_count": 95 2461 | } 2462 | ] 2463 | }, 2464 | { 2465 | "cell_type": "code", 2466 | "source": [ 2467 | "# Use any combination of these to make advanced slices\n", 2468 | "# li[start:end:step]" 2469 | ], 2470 | "metadata": { 2471 | "id": "I3GYr0X9fiNC" 2472 | }, 2473 | "execution_count": null, 2474 | "outputs": [] 2475 | }, 2476 | { 2477 | "cell_type": "code", 2478 | "source": [ 2479 | "# Make a one layer deep copy using slices\n", 2480 | "li2 = li[:] # => li2 = [1, 2, 4, 3] but (li2 is li) will result in false." 2481 | ], 2482 | "metadata": { 2483 | "id": "t3jACXM3flha" 2484 | }, 2485 | "execution_count": null, 2486 | "outputs": [] 2487 | }, 2488 | { 2489 | "cell_type": "code", 2490 | "source": [ 2491 | "# Remove arbitrary elements from a list with \"del\"\n", 2492 | "del li[2] # li is now [1, 2, 3]" 2493 | ], 2494 | "metadata": { 2495 | "id": "sufdmZnBfn-6" 2496 | }, 2497 | "execution_count": null, 2498 | "outputs": [] 2499 | }, 2500 | { 2501 | "cell_type": "code", 2502 | "source": [ 2503 | "# Remove first occurrence of a value\n", 2504 | "li.remove(2) # li is now [1, 3]" 2505 | ], 2506 | "metadata": { 2507 | "id": "JUHwrGAwfsLC" 2508 | }, 2509 | "execution_count": null, 2510 | "outputs": [] 2511 | }, 2512 | { 2513 | "cell_type": "code", 2514 | "source": [ 2515 | "li.remove(2) # Raises a ValueError as 2 is not in the list" 2516 | ], 2517 | "metadata": { 2518 | "colab": { 2519 | "base_uri": "https://localhost:8080/", 2520 | "height": 182 2521 | }, 2522 | "id": "K1-kgepGfvIG", 2523 | "outputId": "79bd3d8b-0cae-4387-e9fa-77b49db0f497" 2524 | }, 2525 | "execution_count": null, 2526 | "outputs": [ 2527 | { 2528 | "output_type": "error", 2529 | "ename": "ValueError", 2530 | "evalue": "ignored", 2531 | "traceback": [ 2532 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 2533 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 2534 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mli\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Raises a ValueError as 2 is not in the list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 2535 | "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" 2536 | ] 2537 | } 2538 | ] 2539 | }, 2540 | { 2541 | "cell_type": "code", 2542 | "source": [ 2543 | "# Insert an element at a specific index\n", 2544 | "li.insert(1, 2) # li is now [1, 2, 3] again" 2545 | ], 2546 | "metadata": { 2547 | "id": "jrvIgLOcfw49" 2548 | }, 2549 | "execution_count": null, 2550 | "outputs": [] 2551 | }, 2552 | { 2553 | "cell_type": "code", 2554 | "source": [ 2555 | "# Get the index of the first item found matching the argument\n", 2556 | "li.index(2) # => 1" 2557 | ], 2558 | "metadata": { 2559 | "colab": { 2560 | "base_uri": "https://localhost:8080/" 2561 | }, 2562 | "id": "ViueijB7fw_R", 2563 | "outputId": "b17cc3ae-a8b3-4634-929b-f83ef5fa8628" 2564 | }, 2565 | "execution_count": null, 2566 | "outputs": [ 2567 | { 2568 | "output_type": "execute_result", 2569 | "data": { 2570 | "text/plain": [ 2571 | "1" 2572 | ] 2573 | }, 2574 | "metadata": {}, 2575 | "execution_count": 102 2576 | } 2577 | ] 2578 | }, 2579 | { 2580 | "cell_type": "code", 2581 | "source": [ 2582 | "li.index(4) # Raises a ValueError as 4 is not in the list" 2583 | ], 2584 | "metadata": { 2585 | "colab": { 2586 | "base_uri": "https://localhost:8080/", 2587 | "height": 182 2588 | }, 2589 | "id": "nzOgpe1Ff2Mi", 2590 | "outputId": "65ced1f1-17c2-4fb4-ab9c-d63a7e492cce" 2591 | }, 2592 | "execution_count": null, 2593 | "outputs": [ 2594 | { 2595 | "output_type": "error", 2596 | "ename": "ValueError", 2597 | "evalue": "ignored", 2598 | "traceback": [ 2599 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 2600 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 2601 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mli\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Raises a ValueError as 4 is not in the list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 2602 | "\u001b[0;31mValueError\u001b[0m: 4 is not in list" 2603 | ] 2604 | } 2605 | ] 2606 | }, 2607 | { 2608 | "cell_type": "code", 2609 | "source": [ 2610 | "# You can add lists\n", 2611 | "# Note: values for li and for other_li are not modified.\n", 2612 | "li + other_li # => [1, 2, 3, 4, 5, 6]" 2613 | ], 2614 | "metadata": { 2615 | "colab": { 2616 | "base_uri": "https://localhost:8080/" 2617 | }, 2618 | "id": "JD6ctZt2f5AT", 2619 | "outputId": "644e336a-d3f6-4081-d30e-7cabd0a3d187" 2620 | }, 2621 | "execution_count": null, 2622 | "outputs": [ 2623 | { 2624 | "output_type": "execute_result", 2625 | "data": { 2626 | "text/plain": [ 2627 | "[1, 2, 3, 4, 5, 6]" 2628 | ] 2629 | }, 2630 | "metadata": {}, 2631 | "execution_count": 104 2632 | } 2633 | ] 2634 | }, 2635 | { 2636 | "cell_type": "code", 2637 | "source": [ 2638 | "# Concatenate lists with \"extend()\"\n", 2639 | "li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6]" 2640 | ], 2641 | "metadata": { 2642 | "id": "5yoNtM_zf8_i" 2643 | }, 2644 | "execution_count": null, 2645 | "outputs": [] 2646 | }, 2647 | { 2648 | "cell_type": "code", 2649 | "source": [ 2650 | "# Check for existence in a list with \"in\"\n", 2651 | "1 in li # => True" 2652 | ], 2653 | "metadata": { 2654 | "colab": { 2655 | "base_uri": "https://localhost:8080/" 2656 | }, 2657 | "id": "PtRSbMR3f-x1", 2658 | "outputId": "33b859b7-bac3-4b22-d213-cc3bb88fcf16" 2659 | }, 2660 | "execution_count": null, 2661 | "outputs": [ 2662 | { 2663 | "output_type": "execute_result", 2664 | "data": { 2665 | "text/plain": [ 2666 | "True" 2667 | ] 2668 | }, 2669 | "metadata": {}, 2670 | "execution_count": 106 2671 | } 2672 | ] 2673 | }, 2674 | { 2675 | "cell_type": "code", 2676 | "source": [ 2677 | "# Examine the length with \"len()\"\n", 2678 | "len(li) # => 6" 2679 | ], 2680 | "metadata": { 2681 | "colab": { 2682 | "base_uri": "https://localhost:8080/" 2683 | }, 2684 | "id": "5HihOrywgDsi", 2685 | "outputId": "13b57df6-5f55-4b25-879c-8b04c6b53a26" 2686 | }, 2687 | "execution_count": null, 2688 | "outputs": [ 2689 | { 2690 | "output_type": "execute_result", 2691 | "data": { 2692 | "text/plain": [ 2693 | "6" 2694 | ] 2695 | }, 2696 | "metadata": {}, 2697 | "execution_count": 107 2698 | } 2699 | ] 2700 | }, 2701 | { 2702 | "cell_type": "code", 2703 | "source": [ 2704 | "# Tuples are like lists but are immutable.\n", 2705 | "tup = (1, 2, 3)\n", 2706 | "tup[0] # => 1" 2707 | ], 2708 | "metadata": { 2709 | "colab": { 2710 | "base_uri": "https://localhost:8080/" 2711 | }, 2712 | "id": "SJm3i0f_gFPp", 2713 | "outputId": "795ba7c9-73f1-4c32-9d51-c1868015e550" 2714 | }, 2715 | "execution_count": null, 2716 | "outputs": [ 2717 | { 2718 | "output_type": "execute_result", 2719 | "data": { 2720 | "text/plain": [ 2721 | "1" 2722 | ] 2723 | }, 2724 | "metadata": {}, 2725 | "execution_count": 108 2726 | } 2727 | ] 2728 | }, 2729 | { 2730 | "cell_type": "code", 2731 | "source": [ 2732 | "tup[0] = 3 # Raises a TypeError" 2733 | ], 2734 | "metadata": { 2735 | "colab": { 2736 | "base_uri": "https://localhost:8080/", 2737 | "height": 182 2738 | }, 2739 | "id": "C5aF8hqugHjU", 2740 | "outputId": "d5bbc6f3-b0af-473f-9be9-b8d511d6d33a" 2741 | }, 2742 | "execution_count": null, 2743 | "outputs": [ 2744 | { 2745 | "output_type": "error", 2746 | "ename": "TypeError", 2747 | "evalue": "ignored", 2748 | "traceback": [ 2749 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 2750 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 2751 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtup\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3\u001b[0m \u001b[0;31m# Raises a TypeError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 2752 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 2753 | ] 2754 | } 2755 | ] 2756 | }, 2757 | { 2758 | "cell_type": "code", 2759 | "source": [ 2760 | "# Note that a tuple of length one has to have a comma after the last element but\n", 2761 | "# tuples of other lengths, even zero, do not.\n", 2762 | "type((1)) # => " 2763 | ], 2764 | "metadata": { 2765 | "colab": { 2766 | "base_uri": "https://localhost:8080/" 2767 | }, 2768 | "id": "44gIT5E5gJHt", 2769 | "outputId": "d3f86744-6f61-4307-f5fc-c8c42d79c0e9" 2770 | }, 2771 | "execution_count": null, 2772 | "outputs": [ 2773 | { 2774 | "output_type": "execute_result", 2775 | "data": { 2776 | "text/plain": [ 2777 | "int" 2778 | ] 2779 | }, 2780 | "metadata": {}, 2781 | "execution_count": 110 2782 | } 2783 | ] 2784 | }, 2785 | { 2786 | "cell_type": "code", 2787 | "source": [ 2788 | "type((1,)) # => " 2789 | ], 2790 | "metadata": { 2791 | "colab": { 2792 | "base_uri": "https://localhost:8080/" 2793 | }, 2794 | "id": "pqipkg6bgLmE", 2795 | "outputId": "49027986-ddf9-4c5c-fef8-d08e6f8e8500" 2796 | }, 2797 | "execution_count": null, 2798 | "outputs": [ 2799 | { 2800 | "output_type": "execute_result", 2801 | "data": { 2802 | "text/plain": [ 2803 | "tuple" 2804 | ] 2805 | }, 2806 | "metadata": {}, 2807 | "execution_count": 111 2808 | } 2809 | ] 2810 | }, 2811 | { 2812 | "cell_type": "code", 2813 | "source": [ 2814 | "type(()) # => " 2815 | ], 2816 | "metadata": { 2817 | "colab": { 2818 | "base_uri": "https://localhost:8080/" 2819 | }, 2820 | "id": "7a71Rkw3gQx6", 2821 | "outputId": "0f2394ff-4d30-4f16-f68b-39aef61c422f" 2822 | }, 2823 | "execution_count": null, 2824 | "outputs": [ 2825 | { 2826 | "output_type": "execute_result", 2827 | "data": { 2828 | "text/plain": [ 2829 | "tuple" 2830 | ] 2831 | }, 2832 | "metadata": {}, 2833 | "execution_count": 112 2834 | } 2835 | ] 2836 | }, 2837 | { 2838 | "cell_type": "code", 2839 | "source": [ 2840 | "# You can do most of the list operations on tuples too\n", 2841 | "len(tup) # => 3" 2842 | ], 2843 | "metadata": { 2844 | "colab": { 2845 | "base_uri": "https://localhost:8080/" 2846 | }, 2847 | "id": "igA5Ua0XgSwG", 2848 | "outputId": "5ede08e5-f3df-4f81-ddab-092ea90d37b3" 2849 | }, 2850 | "execution_count": null, 2851 | "outputs": [ 2852 | { 2853 | "output_type": "execute_result", 2854 | "data": { 2855 | "text/plain": [ 2856 | "3" 2857 | ] 2858 | }, 2859 | "metadata": {}, 2860 | "execution_count": 113 2861 | } 2862 | ] 2863 | }, 2864 | { 2865 | "cell_type": "code", 2866 | "source": [ 2867 | "tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6)" 2868 | ], 2869 | "metadata": { 2870 | "colab": { 2871 | "base_uri": "https://localhost:8080/" 2872 | }, 2873 | "id": "lZFkOk8HgUnz", 2874 | "outputId": "3eedda45-0c42-42c2-c18e-2797089464f4" 2875 | }, 2876 | "execution_count": null, 2877 | "outputs": [ 2878 | { 2879 | "output_type": "execute_result", 2880 | "data": { 2881 | "text/plain": [ 2882 | "(1, 2, 3, 4, 5, 6)" 2883 | ] 2884 | }, 2885 | "metadata": {}, 2886 | "execution_count": 114 2887 | } 2888 | ] 2889 | }, 2890 | { 2891 | "cell_type": "code", 2892 | "source": [ 2893 | "tup[:2] # => (1, 2)" 2894 | ], 2895 | "metadata": { 2896 | "colab": { 2897 | "base_uri": "https://localhost:8080/" 2898 | }, 2899 | "id": "4UXr_o4rgUrC", 2900 | "outputId": "d8f02639-1fd2-4eb6-8aa1-39cf4547cf36" 2901 | }, 2902 | "execution_count": null, 2903 | "outputs": [ 2904 | { 2905 | "output_type": "execute_result", 2906 | "data": { 2907 | "text/plain": [ 2908 | "(1, 2)" 2909 | ] 2910 | }, 2911 | "metadata": {}, 2912 | "execution_count": 115 2913 | } 2914 | ] 2915 | }, 2916 | { 2917 | "cell_type": "code", 2918 | "source": [ 2919 | "2 in tup # => True" 2920 | ], 2921 | "metadata": { 2922 | "colab": { 2923 | "base_uri": "https://localhost:8080/" 2924 | }, 2925 | "id": "0XdxLHaUgXdS", 2926 | "outputId": "3f550315-69e0-4ae5-cff6-39b24afb4d0b" 2927 | }, 2928 | "execution_count": null, 2929 | "outputs": [ 2930 | { 2931 | "output_type": "execute_result", 2932 | "data": { 2933 | "text/plain": [ 2934 | "True" 2935 | ] 2936 | }, 2937 | "metadata": {}, 2938 | "execution_count": 116 2939 | } 2940 | ] 2941 | }, 2942 | { 2943 | "cell_type": "code", 2944 | "source": [ 2945 | "# You can unpack tuples (or lists) into variables\n", 2946 | "a, b, c = (1, 2, 3) # a is now 1, b is now 2 and c is now 3" 2947 | ], 2948 | "metadata": { 2949 | "id": "aTQkFIyWgZVC" 2950 | }, 2951 | "execution_count": null, 2952 | "outputs": [] 2953 | }, 2954 | { 2955 | "cell_type": "code", 2956 | "source": [ 2957 | "# You can also do extended unpacking\n", 2958 | "a, *b, c = (1, 2, 3, 4) # a is now 1, b is now [2, 3] and c is now 4" 2959 | ], 2960 | "metadata": { 2961 | "id": "YfsKRvJkgZnR" 2962 | }, 2963 | "execution_count": null, 2964 | "outputs": [] 2965 | }, 2966 | { 2967 | "cell_type": "code", 2968 | "source": [ 2969 | "# Tuples are created by default if you leave out the parentheses\n", 2970 | "d, e, f = 4, 5, 6 # tuple 4, 5, 6 is unpacked into variables d, e and f" 2971 | ], 2972 | "metadata": { 2973 | "id": "Lcg3OgivgZqN" 2974 | }, 2975 | "execution_count": null, 2976 | "outputs": [] 2977 | }, 2978 | { 2979 | "cell_type": "code", 2980 | "source": [ 2981 | "# respectively such that d = 4, e = 5 and f = 6\n", 2982 | "# Now look how easy it is to swap two values\n", 2983 | "e, d = d, e # d is now 5 and e is now 4\n", 2984 | "\n", 2985 | "x, y = y, x" 2986 | ], 2987 | "metadata": { 2988 | "id": "FqMxek9vgZxs" 2989 | }, 2990 | "execution_count": null, 2991 | "outputs": [] 2992 | }, 2993 | { 2994 | "cell_type": "code", 2995 | "source": [ 2996 | "# Dictionaries store mappings from keys to values\n", 2997 | "empty_dict = {}\n", 2998 | "# Here is a prefilled dictionary\n", 2999 | "filled_dict = {\"one\": 1, \"two\": 2, \"three\": 3}" 3000 | ], 3001 | "metadata": { 3002 | "id": "IHFPcGahgmhW" 3003 | }, 3004 | "execution_count": null, 3005 | "outputs": [] 3006 | }, 3007 | { 3008 | "cell_type": "code", 3009 | "source": [ 3010 | "# Note keys for dictionaries have to be immutable types. This is to ensure that\n", 3011 | "# the key can be converted to a constant hash value for quick look-ups.\n", 3012 | "# Immutable types include ints, floats, strings, tuples.\n", 3013 | "invalid_dict = {[1,2,3]: \"123\"} # => Yield a TypeError: unhashable type: 'list'" 3014 | ], 3015 | "metadata": { 3016 | "colab": { 3017 | "base_uri": "https://localhost:8080/", 3018 | "height": 219 3019 | }, 3020 | "id": "uh1EQk7kgokp", 3021 | "outputId": "e3945acc-e319-4fbc-f5d0-f412acb49233" 3022 | }, 3023 | "execution_count": null, 3024 | "outputs": [ 3025 | { 3026 | "output_type": "error", 3027 | "ename": "TypeError", 3028 | "evalue": "ignored", 3029 | "traceback": [ 3030 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3031 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 3032 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# the key can be converted to a constant hash value for quick look-ups.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Immutable types include ints, floats, strings, tuples.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0minvalid_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"123\"\u001b[0m\u001b[0;34m}\u001b[0m \u001b[0;31m# => Yield a TypeError: unhashable type: 'list'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 3033 | "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" 3034 | ] 3035 | } 3036 | ] 3037 | }, 3038 | { 3039 | "cell_type": "code", 3040 | "source": [ 3041 | "valid_dict = {(1,2,3):[1,2,3]} # Values can be of any type, however." 3042 | ], 3043 | "metadata": { 3044 | "id": "fDyfd9Cfgslm" 3045 | }, 3046 | "execution_count": null, 3047 | "outputs": [] 3048 | }, 3049 | { 3050 | "cell_type": "code", 3051 | "source": [ 3052 | "# Look up values with []\n", 3053 | "filled_dict[\"one\"] # => 1\n" 3054 | ], 3055 | "metadata": { 3056 | "colab": { 3057 | "base_uri": "https://localhost:8080/" 3058 | }, 3059 | "id": "Soww2NPOU4XL", 3060 | "outputId": "d26c55bc-e7ef-4c12-8ea9-471ecd31afcc" 3061 | }, 3062 | "execution_count": null, 3063 | "outputs": [ 3064 | { 3065 | "output_type": "execute_result", 3066 | "data": { 3067 | "text/plain": [ 3068 | "1" 3069 | ] 3070 | }, 3071 | "metadata": {}, 3072 | "execution_count": 124 3073 | } 3074 | ] 3075 | }, 3076 | { 3077 | "cell_type": "code", 3078 | "source": [ 3079 | "# Get all keys as an iterable with \"keys()\". We need to wrap the call in list()\n", 3080 | "# to turn it into a list. We'll talk about those later. Note - for Python\n", 3081 | "# versions <3.7, dictionary key ordering is not guaranteed. Your results might\n", 3082 | "# not match the example below exactly. However, as of Python 3.7, dictionary\n", 3083 | "# items maintain the order at which they are inserted into the dictionary.\n", 3084 | "#list(filled_dict.keys()) # => [\"three\", \"two\", \"one\"] in Python <3.7\n", 3085 | "list(filled_dict.keys()) # => [\"one\", \"two\", \"three\"] in Python 3.7+\n" 3086 | ], 3087 | "metadata": { 3088 | "colab": { 3089 | "base_uri": "https://localhost:8080/" 3090 | }, 3091 | "id": "5BZd325tWiN3", 3092 | "outputId": "d169e6a7-c779-4527-e82d-b491e15bd93d" 3093 | }, 3094 | "execution_count": null, 3095 | "outputs": [ 3096 | { 3097 | "output_type": "execute_result", 3098 | "data": { 3099 | "text/plain": [ 3100 | "['one', 'two', 'three']" 3101 | ] 3102 | }, 3103 | "metadata": {}, 3104 | "execution_count": 125 3105 | } 3106 | ] 3107 | }, 3108 | { 3109 | "cell_type": "code", 3110 | "source": [ 3111 | "# Get all values as an iterable with \"values()\". Once again we need to wrap it\n", 3112 | "# in list() to get it out of the iterable. Note - Same as above regarding key\n", 3113 | "# ordering.\n", 3114 | "#list(filled_dict.values()) # => [3, 2, 1] in Python <3.7\n", 3115 | "list(filled_dict.values()) # => [1, 2, 3] in Python 3.7+\n" 3116 | ], 3117 | "metadata": { 3118 | "colab": { 3119 | "base_uri": "https://localhost:8080/" 3120 | }, 3121 | "id": "xoUAOU3nWrbC", 3122 | "outputId": "190592a6-cfa5-4917-8c95-48db90baa541" 3123 | }, 3124 | "execution_count": null, 3125 | "outputs": [ 3126 | { 3127 | "output_type": "execute_result", 3128 | "data": { 3129 | "text/plain": [ 3130 | "[1, 2, 3]" 3131 | ] 3132 | }, 3133 | "metadata": {}, 3134 | "execution_count": 126 3135 | } 3136 | ] 3137 | }, 3138 | { 3139 | "cell_type": "code", 3140 | "source": [ 3141 | "# Check for existence of keys in a dictionary with \"in\"\n", 3142 | "\"one\" in filled_dict # => True\n" 3143 | ], 3144 | "metadata": { 3145 | "colab": { 3146 | "base_uri": "https://localhost:8080/" 3147 | }, 3148 | "id": "9SJp_dBrWxtp", 3149 | "outputId": "8a616072-2880-4a1a-afcf-8c49b2af8a07" 3150 | }, 3151 | "execution_count": null, 3152 | "outputs": [ 3153 | { 3154 | "output_type": "execute_result", 3155 | "data": { 3156 | "text/plain": [ 3157 | "True" 3158 | ] 3159 | }, 3160 | "metadata": {}, 3161 | "execution_count": 127 3162 | } 3163 | ] 3164 | }, 3165 | { 3166 | "cell_type": "code", 3167 | "source": [ 3168 | "1 in filled_dict # => False" 3169 | ], 3170 | "metadata": { 3171 | "colab": { 3172 | "base_uri": "https://localhost:8080/" 3173 | }, 3174 | "id": "D7bUrII0W1JT", 3175 | "outputId": "c8038e62-a10a-4408-d3dc-02eb6436a0ea" 3176 | }, 3177 | "execution_count": null, 3178 | "outputs": [ 3179 | { 3180 | "output_type": "execute_result", 3181 | "data": { 3182 | "text/plain": [ 3183 | "False" 3184 | ] 3185 | }, 3186 | "metadata": {}, 3187 | "execution_count": 128 3188 | } 3189 | ] 3190 | }, 3191 | { 3192 | "cell_type": "code", 3193 | "source": [ 3194 | "# Looking up a non-existing key is a KeyError\n", 3195 | "filled_dict[\"four\"] # KeyError\n" 3196 | ], 3197 | "metadata": { 3198 | "colab": { 3199 | "base_uri": "https://localhost:8080/", 3200 | "height": 200 3201 | }, 3202 | "id": "R1UdScNhW532", 3203 | "outputId": "8ecb830e-d60c-486a-d48a-f5d226d85792" 3204 | }, 3205 | "execution_count": null, 3206 | "outputs": [ 3207 | { 3208 | "output_type": "error", 3209 | "ename": "KeyError", 3210 | "evalue": "ignored", 3211 | "traceback": [ 3212 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3213 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 3214 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Looking up a non-existing key is a KeyError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfilled_dict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"four\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# KeyError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 3215 | "\u001b[0;31mKeyError\u001b[0m: 'four'" 3216 | ] 3217 | } 3218 | ] 3219 | }, 3220 | { 3221 | "cell_type": "code", 3222 | "source": [ 3223 | "# Use \"get()\" method to avoid the KeyError\n", 3224 | "filled_dict.get(\"one\") # => 1\n" 3225 | ], 3226 | "metadata": { 3227 | "colab": { 3228 | "base_uri": "https://localhost:8080/" 3229 | }, 3230 | "id": "xIY1tA-CW8jP", 3231 | "outputId": "24d76ee7-d2cd-40e9-d319-1b72560512f0" 3232 | }, 3233 | "execution_count": null, 3234 | "outputs": [ 3235 | { 3236 | "output_type": "execute_result", 3237 | "data": { 3238 | "text/plain": [ 3239 | "1" 3240 | ] 3241 | }, 3242 | "metadata": {}, 3243 | "execution_count": 130 3244 | } 3245 | ] 3246 | }, 3247 | { 3248 | "cell_type": "code", 3249 | "source": [ 3250 | "filled_dict.get(\"four\") # => None\n" 3251 | ], 3252 | "metadata": { 3253 | "id": "hQzHsYFvXDvX" 3254 | }, 3255 | "execution_count": null, 3256 | "outputs": [] 3257 | }, 3258 | { 3259 | "cell_type": "code", 3260 | "source": [ 3261 | "# The get method supports a default argument when the value is missing\n", 3262 | "filled_dict.get(\"one\", 4) # => 1" 3263 | ], 3264 | "metadata": { 3265 | "colab": { 3266 | "base_uri": "https://localhost:8080/" 3267 | }, 3268 | "id": "WziLF2JXXBcv", 3269 | "outputId": "20433f4b-7013-48a0-e80d-e0545d70bf89" 3270 | }, 3271 | "execution_count": null, 3272 | "outputs": [ 3273 | { 3274 | "output_type": "execute_result", 3275 | "data": { 3276 | "text/plain": [ 3277 | "1" 3278 | ] 3279 | }, 3280 | "metadata": {}, 3281 | "execution_count": 132 3282 | } 3283 | ] 3284 | }, 3285 | { 3286 | "cell_type": "code", 3287 | "source": [ 3288 | "filled_dict.get(\"four\", 4) # => 4\n" 3289 | ], 3290 | "metadata": { 3291 | "colab": { 3292 | "base_uri": "https://localhost:8080/" 3293 | }, 3294 | "id": "qYwCw2fSXNLU", 3295 | "outputId": "3bc93b38-a90a-45ea-a337-a3cd93170c37" 3296 | }, 3297 | "execution_count": null, 3298 | "outputs": [ 3299 | { 3300 | "output_type": "execute_result", 3301 | "data": { 3302 | "text/plain": [ 3303 | "4" 3304 | ] 3305 | }, 3306 | "metadata": {}, 3307 | "execution_count": 133 3308 | } 3309 | ] 3310 | }, 3311 | { 3312 | "cell_type": "code", 3313 | "source": [ 3314 | "# \"setdefault()\" inserts into a dictionary only if the given key isn't present\n", 3315 | "filled_dict.setdefault(\"five\", 5) # filled_dict[\"five\"] is set to 5" 3316 | ], 3317 | "metadata": { 3318 | "colab": { 3319 | "base_uri": "https://localhost:8080/" 3320 | }, 3321 | "id": "cCXT4m9FXdrc", 3322 | "outputId": "13d9b3e5-321e-49eb-8343-73f098befd2b" 3323 | }, 3324 | "execution_count": null, 3325 | "outputs": [ 3326 | { 3327 | "output_type": "execute_result", 3328 | "data": { 3329 | "text/plain": [ 3330 | "5" 3331 | ] 3332 | }, 3333 | "metadata": {}, 3334 | "execution_count": 134 3335 | } 3336 | ] 3337 | }, 3338 | { 3339 | "cell_type": "code", 3340 | "source": [ 3341 | "filled_dict.setdefault(\"five\", 6) # filled_dict[\"five\"] is still 5\n" 3342 | ], 3343 | "metadata": { 3344 | "colab": { 3345 | "base_uri": "https://localhost:8080/" 3346 | }, 3347 | "id": "uYYB8_llXd2D", 3348 | "outputId": "44a6ec53-875d-4133-87cc-9690f7a1f190" 3349 | }, 3350 | "execution_count": null, 3351 | "outputs": [ 3352 | { 3353 | "output_type": "execute_result", 3354 | "data": { 3355 | "text/plain": [ 3356 | "5" 3357 | ] 3358 | }, 3359 | "metadata": {}, 3360 | "execution_count": 135 3361 | } 3362 | ] 3363 | }, 3364 | { 3365 | "cell_type": "code", 3366 | "source": [ 3367 | "# Adding to a dictionary\n", 3368 | "filled_dict.update({\"four\":4}) # => {\"one\": 1, \"two\": 2, \"three\": 3, \"four\": 4}\n" 3369 | ], 3370 | "metadata": { 3371 | "id": "Umb6J9uZXd-x" 3372 | }, 3373 | "execution_count": null, 3374 | "outputs": [] 3375 | }, 3376 | { 3377 | "cell_type": "code", 3378 | "source": [ 3379 | "filled_dict[\"four\"] = 4 # another way to add to dict\n" 3380 | ], 3381 | "metadata": { 3382 | "id": "Zrr9JGgIXeA_" 3383 | }, 3384 | "execution_count": null, 3385 | "outputs": [] 3386 | }, 3387 | { 3388 | "cell_type": "code", 3389 | "source": [ 3390 | "# Remove keys from a dictionary with del\n", 3391 | "del filled_dict[\"one\"] # Removes the key \"one\" from filled dict\n" 3392 | ], 3393 | "metadata": { 3394 | "id": "5sp7VAuEXgT_" 3395 | }, 3396 | "execution_count": null, 3397 | "outputs": [] 3398 | }, 3399 | { 3400 | "cell_type": "code", 3401 | "source": [ 3402 | "# From Python 3.5 you can also use the additional unpacking options\n", 3403 | "{'a': 1, **{'b': 2}} # => {'a': 1, 'b': 2}" 3404 | ], 3405 | "metadata": { 3406 | "colab": { 3407 | "base_uri": "https://localhost:8080/" 3408 | }, 3409 | "id": "9-wPDIcHXgih", 3410 | "outputId": "1b353d28-9229-4e26-9e84-e6b586762fd2" 3411 | }, 3412 | "execution_count": null, 3413 | "outputs": [ 3414 | { 3415 | "output_type": "execute_result", 3416 | "data": { 3417 | "text/plain": [ 3418 | "{'a': 1, 'b': 2}" 3419 | ] 3420 | }, 3421 | "metadata": {}, 3422 | "execution_count": 139 3423 | } 3424 | ] 3425 | }, 3426 | { 3427 | "cell_type": "code", 3428 | "source": [ 3429 | "{'a': 1, **{'a': 2}} # => {'a': 2}\n" 3430 | ], 3431 | "metadata": { 3432 | "colab": { 3433 | "base_uri": "https://localhost:8080/" 3434 | }, 3435 | "id": "3U-Pw2TgX2_j", 3436 | "outputId": "9d3e8507-578f-4876-82ab-6f982bb68b99" 3437 | }, 3438 | "execution_count": null, 3439 | "outputs": [ 3440 | { 3441 | "output_type": "execute_result", 3442 | "data": { 3443 | "text/plain": [ 3444 | "{'a': 2}" 3445 | ] 3446 | }, 3447 | "metadata": {}, 3448 | "execution_count": 140 3449 | } 3450 | ] 3451 | }, 3452 | { 3453 | "cell_type": "code", 3454 | "source": [ 3455 | "# Sets store ... well sets\n", 3456 | "empty_set = set()\n" 3457 | ], 3458 | "metadata": { 3459 | "id": "Ng5GqSUZX3Bu" 3460 | }, 3461 | "execution_count": null, 3462 | "outputs": [] 3463 | }, 3464 | { 3465 | "cell_type": "code", 3466 | "source": [ 3467 | "# Initialize a set with a bunch of values.\n", 3468 | "some_set = {1, 1, 2, 2, 3, 4} # some_set is now {1, 2, 3, 4}\n" 3469 | ], 3470 | "metadata": { 3471 | "id": "u4rcoNtMX3FN" 3472 | }, 3473 | "execution_count": null, 3474 | "outputs": [] 3475 | }, 3476 | { 3477 | "cell_type": "code", 3478 | "source": [ 3479 | "# Similar to keys of a dictionary, elements of a set have to be immutable.\n", 3480 | "invalid_set = {[1], 1} # => Raises a TypeError: unhashable type: 'list'\n" 3481 | ], 3482 | "metadata": { 3483 | "colab": { 3484 | "base_uri": "https://localhost:8080/", 3485 | "height": 200 3486 | }, 3487 | "id": "jD2uoqyxYD0B", 3488 | "outputId": "cbd5af7e-dc97-445c-f613-11695d5389fa" 3489 | }, 3490 | "execution_count": null, 3491 | "outputs": [ 3492 | { 3493 | "output_type": "error", 3494 | "ename": "TypeError", 3495 | "evalue": "ignored", 3496 | "traceback": [ 3497 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3498 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 3499 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Similar to keys of a dictionary, elements of a set have to be immutable.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0minvalid_set\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m}\u001b[0m \u001b[0;31m# => Raises a TypeError: unhashable type: 'list'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 3500 | "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" 3501 | ] 3502 | } 3503 | ] 3504 | }, 3505 | { 3506 | "cell_type": "code", 3507 | "source": [ 3508 | "valid_set = {(1,), 1}\n" 3509 | ], 3510 | "metadata": { 3511 | "id": "ns2xrBWjYD9X" 3512 | }, 3513 | "execution_count": null, 3514 | "outputs": [] 3515 | }, 3516 | { 3517 | "cell_type": "code", 3518 | "source": [ 3519 | "# Add one more item to the set\n", 3520 | "filled_set = some_set\n" 3521 | ], 3522 | "metadata": { 3523 | "id": "iDA_DkyPYWUE" 3524 | }, 3525 | "execution_count": null, 3526 | "outputs": [] 3527 | }, 3528 | { 3529 | "cell_type": "code", 3530 | "source": [ 3531 | "filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}" 3532 | ], 3533 | "metadata": { 3534 | "id": "Oyql15LvYeZf" 3535 | }, 3536 | "execution_count": null, 3537 | "outputs": [] 3538 | }, 3539 | { 3540 | "cell_type": "code", 3541 | "source": [ 3542 | "# Sets do not have duplicate elements\n", 3543 | "filled_set.add(5) # it remains as before {1, 2, 3, 4, 5}\n" 3544 | ], 3545 | "metadata": { 3546 | "id": "zVKcA5uiYguo" 3547 | }, 3548 | "execution_count": null, 3549 | "outputs": [] 3550 | }, 3551 | { 3552 | "cell_type": "code", 3553 | "source": [ 3554 | "# Do set intersection with &\n", 3555 | "other_set = {3, 4, 5, 6}\n", 3556 | "filled_set & other_set # => {3, 4, 5}\n" 3557 | ], 3558 | "metadata": { 3559 | "colab": { 3560 | "base_uri": "https://localhost:8080/" 3561 | }, 3562 | "id": "ENneJTClYg09", 3563 | "outputId": "c6462368-d4a5-4b47-cebb-2706826eb068" 3564 | }, 3565 | "execution_count": null, 3566 | "outputs": [ 3567 | { 3568 | "output_type": "execute_result", 3569 | "data": { 3570 | "text/plain": [ 3571 | "{3, 4, 5}" 3572 | ] 3573 | }, 3574 | "metadata": {}, 3575 | "execution_count": 148 3576 | } 3577 | ] 3578 | }, 3579 | { 3580 | "cell_type": "code", 3581 | "source": [ 3582 | "# Do set union with |\n", 3583 | "filled_set | other_set # => {1, 2, 3, 4, 5, 6}\n" 3584 | ], 3585 | "metadata": { 3586 | "colab": { 3587 | "base_uri": "https://localhost:8080/" 3588 | }, 3589 | "id": "oujRvCqCYhEo", 3590 | "outputId": "127435b9-2247-40a2-8cf2-d2fa8a578b4f" 3591 | }, 3592 | "execution_count": null, 3593 | "outputs": [ 3594 | { 3595 | "output_type": "execute_result", 3596 | "data": { 3597 | "text/plain": [ 3598 | "{1, 2, 3, 4, 5, 6}" 3599 | ] 3600 | }, 3601 | "metadata": {}, 3602 | "execution_count": 149 3603 | } 3604 | ] 3605 | }, 3606 | { 3607 | "cell_type": "code", 3608 | "source": [ 3609 | "# Do set difference with -\n", 3610 | "{1, 2, 3, 4} - {2, 3, 5} # => {1, 4}\n" 3611 | ], 3612 | "metadata": { 3613 | "colab": { 3614 | "base_uri": "https://localhost:8080/" 3615 | }, 3616 | "id": "ABFCilmtYhQv", 3617 | "outputId": "dcb45eea-b530-496f-ae63-8fe9de0f5c07" 3618 | }, 3619 | "execution_count": null, 3620 | "outputs": [ 3621 | { 3622 | "output_type": "execute_result", 3623 | "data": { 3624 | "text/plain": [ 3625 | "{1, 4}" 3626 | ] 3627 | }, 3628 | "metadata": {}, 3629 | "execution_count": 150 3630 | } 3631 | ] 3632 | }, 3633 | { 3634 | "cell_type": "code", 3635 | "source": [ 3636 | "# Do set symmetric difference with ^\n", 3637 | "{1, 2, 3, 4} ^ {2, 3, 5} # => {1, 4, 5}\n" 3638 | ], 3639 | "metadata": { 3640 | "colab": { 3641 | "base_uri": "https://localhost:8080/" 3642 | }, 3643 | "id": "HB3sgsybYwN2", 3644 | "outputId": "682dfa40-cb13-4f3d-fa5d-9ec9de53dbe3" 3645 | }, 3646 | "execution_count": null, 3647 | "outputs": [ 3648 | { 3649 | "output_type": "execute_result", 3650 | "data": { 3651 | "text/plain": [ 3652 | "{1, 4, 5}" 3653 | ] 3654 | }, 3655 | "metadata": {}, 3656 | "execution_count": 151 3657 | } 3658 | ] 3659 | }, 3660 | { 3661 | "cell_type": "code", 3662 | "source": [ 3663 | "# Check if set on the left is a superset of set on the right\n", 3664 | "{1, 2} >= {1, 2, 3} # => False" 3665 | ], 3666 | "metadata": { 3667 | "colab": { 3668 | "base_uri": "https://localhost:8080/" 3669 | }, 3670 | "id": "33IOlpIPYwUS", 3671 | "outputId": "4ff641f9-8732-4c8d-e215-34dd7b72b370" 3672 | }, 3673 | "execution_count": null, 3674 | "outputs": [ 3675 | { 3676 | "output_type": "execute_result", 3677 | "data": { 3678 | "text/plain": [ 3679 | "False" 3680 | ] 3681 | }, 3682 | "metadata": {}, 3683 | "execution_count": 152 3684 | } 3685 | ] 3686 | }, 3687 | { 3688 | "cell_type": "code", 3689 | "source": [ 3690 | "# Check if set on the left is a subset of set on the right\n", 3691 | "{1, 2} <= {1, 2, 3} # => True\n" 3692 | ], 3693 | "metadata": { 3694 | "colab": { 3695 | "base_uri": "https://localhost:8080/" 3696 | }, 3697 | "id": "p5Ak17PTYwZB", 3698 | "outputId": "955d748f-b230-4f96-a596-5930cd315e2a" 3699 | }, 3700 | "execution_count": null, 3701 | "outputs": [ 3702 | { 3703 | "output_type": "execute_result", 3704 | "data": { 3705 | "text/plain": [ 3706 | "True" 3707 | ] 3708 | }, 3709 | "metadata": {}, 3710 | "execution_count": 153 3711 | } 3712 | ] 3713 | }, 3714 | { 3715 | "cell_type": "code", 3716 | "source": [ 3717 | "# Check for existence in a set with in\n", 3718 | "2 in filled_set # => True\n", 3719 | "10 in filled_set # => False\n", 3720 | "\n", 3721 | "# Make a one layer deep copy\n", 3722 | "filled_set = some_set.copy() # filled_set is {1, 2, 3, 4, 5}\n", 3723 | "filled_set is some_set # => False\n" 3724 | ], 3725 | "metadata": { 3726 | "colab": { 3727 | "base_uri": "https://localhost:8080/" 3728 | }, 3729 | "id": "uTkqg3GGYwe8", 3730 | "outputId": "91e2909a-6da7-4a76-86ef-eaab27f3c3e7" 3731 | }, 3732 | "execution_count": null, 3733 | "outputs": [ 3734 | { 3735 | "output_type": "execute_result", 3736 | "data": { 3737 | "text/plain": [ 3738 | "False" 3739 | ] 3740 | }, 3741 | "metadata": {}, 3742 | "execution_count": 154 3743 | } 3744 | ] 3745 | }, 3746 | { 3747 | "cell_type": "markdown", 3748 | "source": [ 3749 | "####################################################\n", 3750 | "## 3. Control Flow and Iterables\n", 3751 | "####################################################" 3752 | ], 3753 | "metadata": { 3754 | "id": "TfRMaR9yZBhj" 3755 | } 3756 | }, 3757 | { 3758 | "cell_type": "code", 3759 | "source": [ 3760 | "# Let's just make a variable\n", 3761 | "some_var = 5\n", 3762 | "\n", 3763 | "# Here is an if statement. Indentation is significant in Python!\n", 3764 | "# Convention is to use four spaces, not tabs.\n", 3765 | "# This prints \"some_var is smaller than 10\"\n", 3766 | "if some_var > 10:\n", 3767 | " print(\"some_var is totally bigger than 10.\")\n", 3768 | "elif some_var < 10: # This elif clause is optional.\n", 3769 | " print(\"some_var is smaller than 10.\")\n", 3770 | "else: # This is optional too.\n", 3771 | " print(\"some_var is indeed 10.\")\n" 3772 | ], 3773 | "metadata": { 3774 | "colab": { 3775 | "base_uri": "https://localhost:8080/" 3776 | }, 3777 | "id": "uUW52euNZFxy", 3778 | "outputId": "1e9acfed-58b1-47d1-f9a7-6644dc1b5e6c" 3779 | }, 3780 | "execution_count": null, 3781 | "outputs": [ 3782 | { 3783 | "output_type": "stream", 3784 | "name": "stdout", 3785 | "text": [ 3786 | "some_var is smaller than 10.\n" 3787 | ] 3788 | } 3789 | ] 3790 | }, 3791 | { 3792 | "cell_type": "code", 3793 | "source": [ 3794 | "\"\"\"\n", 3795 | "For loops iterate over lists\n", 3796 | "prints:\n", 3797 | " dog is a mammal\n", 3798 | " cat is a mammal\n", 3799 | " mouse is a mammal\n", 3800 | "\"\"\"\n", 3801 | "for animal in [\"dog\", \"cat\", \"mouse\"]:\n", 3802 | " # You can use format() to interpolate formatted strings\n", 3803 | " print(\"{} is a mammal\".format(animal))\n", 3804 | "\n" 3805 | ], 3806 | "metadata": { 3807 | "colab": { 3808 | "base_uri": "https://localhost:8080/" 3809 | }, 3810 | "id": "mfySasYHZF0n", 3811 | "outputId": "6eefa528-ae19-4450-d01a-3dfd3bcb0b87" 3812 | }, 3813 | "execution_count": null, 3814 | "outputs": [ 3815 | { 3816 | "output_type": "stream", 3817 | "name": "stdout", 3818 | "text": [ 3819 | "dog is a mammal\n", 3820 | "cat is a mammal\n", 3821 | "mouse is a mammal\n" 3822 | ] 3823 | } 3824 | ] 3825 | }, 3826 | { 3827 | "cell_type": "code", 3828 | "source": [ 3829 | "\"\"\"\n", 3830 | "\"range(number)\" returns an iterable of numbers\n", 3831 | "from zero up to (but excluding) the given number\n", 3832 | "prints:\n", 3833 | " 0\n", 3834 | " 1\n", 3835 | " 2\n", 3836 | " 3\n", 3837 | "\"\"\"\n", 3838 | "for i in range(4):\n", 3839 | " print(i)\n" 3840 | ], 3841 | "metadata": { 3842 | "colab": { 3843 | "base_uri": "https://localhost:8080/" 3844 | }, 3845 | "id": "X8zO_KZ4ZF4C", 3846 | "outputId": "ad596c44-db6c-4eea-d828-5e8a21cdfa78" 3847 | }, 3848 | "execution_count": null, 3849 | "outputs": [ 3850 | { 3851 | "output_type": "stream", 3852 | "name": "stdout", 3853 | "text": [ 3854 | "0\n", 3855 | "1\n", 3856 | "2\n", 3857 | "3\n" 3858 | ] 3859 | } 3860 | ] 3861 | }, 3862 | { 3863 | "cell_type": "code", 3864 | "source": [ 3865 | "\"\"\"\n", 3866 | "\"range(lower, upper)\" returns an iterable of numbers\n", 3867 | "from the lower number to the upper number\n", 3868 | "prints:\n", 3869 | " 4\n", 3870 | " 5\n", 3871 | " 6\n", 3872 | " 7\n", 3873 | "\"\"\"\n", 3874 | "for i in range(4, 8):\n", 3875 | " print(i)\n" 3876 | ], 3877 | "metadata": { 3878 | "colab": { 3879 | "base_uri": "https://localhost:8080/" 3880 | }, 3881 | "id": "jTvCVsBJZF7Z", 3882 | "outputId": "75b3051b-7b46-49da-af81-ebfe3662b5cd" 3883 | }, 3884 | "execution_count": null, 3885 | "outputs": [ 3886 | { 3887 | "output_type": "stream", 3888 | "name": "stdout", 3889 | "text": [ 3890 | "4\n", 3891 | "5\n", 3892 | "6\n", 3893 | "7\n" 3894 | ] 3895 | } 3896 | ] 3897 | }, 3898 | { 3899 | "cell_type": "code", 3900 | "source": [ 3901 | "\"\"\"\n", 3902 | "\"range(lower, upper, step)\" returns an iterable of numbers\n", 3903 | "from the lower number to the upper number, while incrementing\n", 3904 | "by step. If step is not indicated, the default value is 1.\n", 3905 | "prints:\n", 3906 | " 4\n", 3907 | " 6\n", 3908 | "\"\"\"\n", 3909 | "for i in range(4, 8, 2):\n", 3910 | " print(i)\n" 3911 | ], 3912 | "metadata": { 3913 | "colab": { 3914 | "base_uri": "https://localhost:8080/" 3915 | }, 3916 | "id": "3p9YzWL7ZF-9", 3917 | "outputId": "e8a6a7d4-bfc3-49b6-fc06-c539ea696af1" 3918 | }, 3919 | "execution_count": null, 3920 | "outputs": [ 3921 | { 3922 | "output_type": "stream", 3923 | "name": "stdout", 3924 | "text": [ 3925 | "4\n", 3926 | "6\n" 3927 | ] 3928 | } 3929 | ] 3930 | }, 3931 | { 3932 | "cell_type": "code", 3933 | "source": [ 3934 | "\"\"\"\n", 3935 | "Loop over a list to retrieve both the index and the value of each list item:\n", 3936 | " 0 dog\n", 3937 | " 1 cat\n", 3938 | " 2 mouse\n", 3939 | "\"\"\"\n", 3940 | "animals = [\"dog\", \"cat\", \"mouse\"]\n", 3941 | "for i, value in enumerate(animals):\n", 3942 | " print(i, value)\n" 3943 | ], 3944 | "metadata": { 3945 | "colab": { 3946 | "base_uri": "https://localhost:8080/" 3947 | }, 3948 | "id": "mRIE3Q_0ZGDe", 3949 | "outputId": "6c47e586-73ab-423d-a4ca-0ba417afb52c" 3950 | }, 3951 | "execution_count": null, 3952 | "outputs": [ 3953 | { 3954 | "output_type": "stream", 3955 | "name": "stdout", 3956 | "text": [ 3957 | "0 dog\n", 3958 | "1 cat\n", 3959 | "2 mouse\n" 3960 | ] 3961 | } 3962 | ] 3963 | }, 3964 | { 3965 | "cell_type": "code", 3966 | "source": [ 3967 | "\"\"\"\n", 3968 | "While loops go until a condition is no longer met.\n", 3969 | "prints:\n", 3970 | " 0\n", 3971 | " 1\n", 3972 | " 2\n", 3973 | " 3\n", 3974 | "\"\"\"\n", 3975 | "x = 0\n", 3976 | "while x < 4:\n", 3977 | " print(x)\n", 3978 | " x += 1 # Shorthand for x = x + 1\n" 3979 | ], 3980 | "metadata": { 3981 | "colab": { 3982 | "base_uri": "https://localhost:8080/" 3983 | }, 3984 | "id": "UAwLItIUZc4n", 3985 | "outputId": "98d547c4-da96-42fc-e087-359c2f6f7033" 3986 | }, 3987 | "execution_count": null, 3988 | "outputs": [ 3989 | { 3990 | "output_type": "stream", 3991 | "name": "stdout", 3992 | "text": [ 3993 | "0\n", 3994 | "1\n", 3995 | "2\n", 3996 | "3\n" 3997 | ] 3998 | } 3999 | ] 4000 | }, 4001 | { 4002 | "cell_type": "code", 4003 | "source": [ 4004 | "# Handle exceptions with a try/except block\n", 4005 | "try:\n", 4006 | " # Use \"raise\" to raise an error\n", 4007 | "\n", 4008 | " raise IndexError(\"This is an index error\")\n", 4009 | "\n", 4010 | "except IndexError as e:\n", 4011 | " print(\"HHAHHAHHA we are making joke\")\n", 4012 | " print(e)\n", 4013 | " pass # Refrain from this, provide a recovery (next example).\n", 4014 | "except (TypeError, NameError):\n", 4015 | " pass # Multiple exceptions can be processed jointly.\n", 4016 | "else: # Optional clause to the try/except block. Must follow\n", 4017 | " # all except blocks.\n", 4018 | " print(\"All good!\") # Runs only if the code in try raises no exceptions\n", 4019 | "finally: # Execute under all circumstances\n", 4020 | " print(\"We can clean up resources here\")\n" 4021 | ], 4022 | "metadata": { 4023 | "colab": { 4024 | "base_uri": "https://localhost:8080/" 4025 | }, 4026 | "id": "kUW7BxpvZhla", 4027 | "outputId": "97161301-f0a4-46aa-8611-336b8b54a965" 4028 | }, 4029 | "execution_count": null, 4030 | "outputs": [ 4031 | { 4032 | "output_type": "stream", 4033 | "name": "stdout", 4034 | "text": [ 4035 | "HHAHHAHHA we are making joke\n", 4036 | "This is an index error\n", 4037 | "We can clean up resources here\n" 4038 | ] 4039 | } 4040 | ] 4041 | }, 4042 | { 4043 | "cell_type": "code", 4044 | "source": [], 4045 | "metadata": { 4046 | "id": "uhFbwyK053o1" 4047 | }, 4048 | "execution_count": null, 4049 | "outputs": [] 4050 | }, 4051 | { 4052 | "cell_type": "code", 4053 | "source": [ 4054 | "try:\n", 4055 | " raise Exception('spam', 'eggs')\n", 4056 | "except Exception as inst:\n", 4057 | " print(type(inst)) # the exception type\n", 4058 | " print(inst.args) # arguments stored in .args\n", 4059 | " print(inst) # __str__ allows args to be printed directly,\n", 4060 | " # but may be overridden in exception subclasses\n", 4061 | " x, y = inst.args # unpack args\n", 4062 | " print('x =', x)\n", 4063 | " print('y =', y)" 4064 | ], 4065 | "metadata": { 4066 | "colab": { 4067 | "base_uri": "https://localhost:8080/" 4068 | }, 4069 | "id": "gS14hSTjbXRS", 4070 | "outputId": "c8c407f9-886a-4943-9cf6-4c1eee7f1cc3" 4071 | }, 4072 | "execution_count": null, 4073 | "outputs": [ 4074 | { 4075 | "output_type": "stream", 4076 | "name": "stdout", 4077 | "text": [ 4078 | "\n", 4079 | "('spam', 'eggs')\n", 4080 | "('spam', 'eggs')\n", 4081 | "x = spam\n", 4082 | "y = eggs\n" 4083 | ] 4084 | } 4085 | ] 4086 | }, 4087 | { 4088 | "cell_type": "code", 4089 | "source": [ 4090 | "# Writing to a file\n", 4091 | "contents = {\"aa\": 12, \"bb\": 21} #\n", 4092 | "with open(\"myfile1.txt\", \"w+\") as file:\n", 4093 | " file.write(str(contents)) # writes a string to a file\n" 4094 | ], 4095 | "metadata": { 4096 | "id": "oKtjMjcMcXPi" 4097 | }, 4098 | "execution_count": null, 4099 | "outputs": [] 4100 | }, 4101 | { 4102 | "cell_type": "code", 4103 | "source": [ 4104 | "# Instead of try/finally to cleanup resources you can use a with statement\n", 4105 | "with open(\"myfile.txt\") as f:\n", 4106 | " for line in f:\n", 4107 | " print(line)" 4108 | ], 4109 | "metadata": { 4110 | "colab": { 4111 | "base_uri": "https://localhost:8080/", 4112 | "height": 237 4113 | }, 4114 | "id": "Nf4OwKJNZh1R", 4115 | "outputId": "ae155021-65e7-4fde-d0d4-6dfb9899f694" 4116 | }, 4117 | "execution_count": null, 4118 | "outputs": [ 4119 | { 4120 | "output_type": "error", 4121 | "ename": "FileNotFoundError", 4122 | "evalue": "ignored", 4123 | "traceback": [ 4124 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 4125 | "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 4126 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Instead of try/finally to cleanup resources you can use a with statement\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"myfile.txt\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 4127 | "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'myfile.txt'" 4128 | ] 4129 | } 4130 | ] 4131 | }, 4132 | { 4133 | "cell_type": "code", 4134 | "source": [ 4135 | "import json\n", 4136 | "with open(\"myfile2.txt\", \"w+\") as file:\n", 4137 | " file.write(json.dumps(contents)) # writes an object to a file" 4138 | ], 4139 | "metadata": { 4140 | "id": "5kg47GQrZiFR" 4141 | }, 4142 | "execution_count": null, 4143 | "outputs": [] 4144 | }, 4145 | { 4146 | "cell_type": "code", 4147 | "source": [ 4148 | "# Reading from a file\n", 4149 | "with open('myfile1.txt', \"r+\") as file:\n", 4150 | " contents = file.read() # reads a string from a file\n", 4151 | "print(contents)" 4152 | ], 4153 | "metadata": { 4154 | "colab": { 4155 | "base_uri": "https://localhost:8080/" 4156 | }, 4157 | "id": "gm9jpljFkFj4", 4158 | "outputId": "0e786ac4-781b-4ed3-93fc-b4f76ac09553" 4159 | }, 4160 | "execution_count": null, 4161 | "outputs": [ 4162 | { 4163 | "output_type": "stream", 4164 | "name": "stdout", 4165 | "text": [ 4166 | "{'aa': 12, 'bb': 21}\n" 4167 | ] 4168 | } 4169 | ] 4170 | }, 4171 | { 4172 | "cell_type": "code", 4173 | "source": [ 4174 | "# print: {\"aa\": 12, \"bb\": 21}\n", 4175 | "\n", 4176 | "with open('myfile2.txt', \"r+\") as file:\n", 4177 | " contents = json.load(file) # reads a json object from a file\n", 4178 | "print(contents)" 4179 | ], 4180 | "metadata": { 4181 | "colab": { 4182 | "base_uri": "https://localhost:8080/" 4183 | }, 4184 | "id": "IQaVhvBDkIVz", 4185 | "outputId": "9c698f8c-934b-4900-946d-3080d55686a8" 4186 | }, 4187 | "execution_count": null, 4188 | "outputs": [ 4189 | { 4190 | "output_type": "stream", 4191 | "name": "stdout", 4192 | "text": [ 4193 | "{'aa': 12, 'bb': 21}\n" 4194 | ] 4195 | } 4196 | ] 4197 | }, 4198 | { 4199 | "cell_type": "code", 4200 | "source": [ 4201 | "# print: {\"aa\": 12, \"bb\": 21}\n", 4202 | "\n", 4203 | "\n", 4204 | "# Python offers a fundamental abstraction called the Iterable.\n", 4205 | "# An iterable is an object that can be treated as a sequence.\n", 4206 | "# The object returned by the range function, is an iterable.\n", 4207 | "\n", 4208 | "filled_dict = {\"one\": 1, \"two\": 2, \"three\": 3}\n", 4209 | "our_iterable = filled_dict.keys()\n", 4210 | "print(our_iterable) # => dict_keys(['one', 'two', 'three']). This is an object\n", 4211 | " # that implements our Iterable interface." 4212 | ], 4213 | "metadata": { 4214 | "colab": { 4215 | "base_uri": "https://localhost:8080/" 4216 | }, 4217 | "id": "C2COyTawkNra", 4218 | "outputId": "dbe2e2cd-e732-4162-9399-f3db0a945afd" 4219 | }, 4220 | "execution_count": null, 4221 | "outputs": [ 4222 | { 4223 | "output_type": "stream", 4224 | "name": "stdout", 4225 | "text": [ 4226 | "dict_keys(['one', 'two', 'three'])\n" 4227 | ] 4228 | } 4229 | ] 4230 | }, 4231 | { 4232 | "cell_type": "code", 4233 | "source": [ 4234 | "# We can loop over it.\n", 4235 | "for i in our_iterable:\n", 4236 | " print(i) # Prints one, two, three" 4237 | ], 4238 | "metadata": { 4239 | "colab": { 4240 | "base_uri": "https://localhost:8080/" 4241 | }, 4242 | "id": "1ZrczFuukV4X", 4243 | "outputId": "a6d0ff6b-c228-485c-99c7-8577889296a3" 4244 | }, 4245 | "execution_count": null, 4246 | "outputs": [ 4247 | { 4248 | "output_type": "stream", 4249 | "name": "stdout", 4250 | "text": [ 4251 | "one\n", 4252 | "two\n", 4253 | "three\n" 4254 | ] 4255 | } 4256 | ] 4257 | }, 4258 | { 4259 | "cell_type": "code", 4260 | "source": [ 4261 | "# However we cannot address elements by index.\n", 4262 | "our_iterable[1] # Raises a TypeError" 4263 | ], 4264 | "metadata": { 4265 | "colab": { 4266 | "base_uri": "https://localhost:8080/", 4267 | "height": 200 4268 | }, 4269 | "id": "1Egc2kcnkZX2", 4270 | "outputId": "f16fd921-dc8d-4658-f471-b9db70603d66" 4271 | }, 4272 | "execution_count": null, 4273 | "outputs": [ 4274 | { 4275 | "output_type": "error", 4276 | "ename": "TypeError", 4277 | "evalue": "ignored", 4278 | "traceback": [ 4279 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 4280 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 4281 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# However we cannot address elements by index.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mour_iterable\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# Raises a TypeError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 4282 | "\u001b[0;31mTypeError\u001b[0m: 'dict_keys' object is not subscriptable" 4283 | ] 4284 | } 4285 | ] 4286 | }, 4287 | { 4288 | "cell_type": "code", 4289 | "source": [ 4290 | "# An iterable is an object that knows how to create an iterator.\n", 4291 | "our_iterator = iter(our_iterable)" 4292 | ], 4293 | "metadata": { 4294 | "id": "Ae4wyv5TkpSd" 4295 | }, 4296 | "execution_count": null, 4297 | "outputs": [] 4298 | }, 4299 | { 4300 | "cell_type": "code", 4301 | "source": [ 4302 | "# Our iterator is an object that can remember the state as we traverse through\n", 4303 | "# it. We get the next object with \"next()\".\n", 4304 | "next(our_iterator) # => \"one\"" 4305 | ], 4306 | "metadata": { 4307 | "colab": { 4308 | "base_uri": "https://localhost:8080/", 4309 | "height": 36 4310 | }, 4311 | "id": "g4fUuQvfkrWo", 4312 | "outputId": "fda5ef5f-9ea3-4718-bd18-f66248d4dd7f" 4313 | }, 4314 | "execution_count": null, 4315 | "outputs": [ 4316 | { 4317 | "output_type": "execute_result", 4318 | "data": { 4319 | "text/plain": [ 4320 | "'one'" 4321 | ], 4322 | "application/vnd.google.colaboratory.intrinsic+json": { 4323 | "type": "string" 4324 | } 4325 | }, 4326 | "metadata": {}, 4327 | "execution_count": 172 4328 | } 4329 | ] 4330 | }, 4331 | { 4332 | "cell_type": "code", 4333 | "source": [ 4334 | "# It maintains state as we iterate.\n", 4335 | "next(our_iterator) # => \"two\"" 4336 | ], 4337 | "metadata": { 4338 | "colab": { 4339 | "base_uri": "https://localhost:8080/", 4340 | "height": 36 4341 | }, 4342 | "id": "jtmI8DGjla03", 4343 | "outputId": "309720c1-fab4-4604-c9a2-1e3a94532bd7" 4344 | }, 4345 | "execution_count": null, 4346 | "outputs": [ 4347 | { 4348 | "output_type": "execute_result", 4349 | "data": { 4350 | "text/plain": [ 4351 | "'two'" 4352 | ], 4353 | "application/vnd.google.colaboratory.intrinsic+json": { 4354 | "type": "string" 4355 | } 4356 | }, 4357 | "metadata": {}, 4358 | "execution_count": 173 4359 | } 4360 | ] 4361 | }, 4362 | { 4363 | "cell_type": "code", 4364 | "source": [ 4365 | "next(our_iterator) # => \"three\"" 4366 | ], 4367 | "metadata": { 4368 | "colab": { 4369 | "base_uri": "https://localhost:8080/", 4370 | "height": 36 4371 | }, 4372 | "id": "a-gLDX94le6l", 4373 | "outputId": "ab4f82bc-d4e5-4e64-89ea-5c92e88cf23d" 4374 | }, 4375 | "execution_count": null, 4376 | "outputs": [ 4377 | { 4378 | "output_type": "execute_result", 4379 | "data": { 4380 | "text/plain": [ 4381 | "'three'" 4382 | ], 4383 | "application/vnd.google.colaboratory.intrinsic+json": { 4384 | "type": "string" 4385 | } 4386 | }, 4387 | "metadata": {}, 4388 | "execution_count": 174 4389 | } 4390 | ] 4391 | }, 4392 | { 4393 | "cell_type": "code", 4394 | "source": [ 4395 | "# After the iterator has returned all of its data, it raises a\n", 4396 | "# StopIteration exception\n", 4397 | "next(our_iterator) # Raises StopIteration\n" 4398 | ], 4399 | "metadata": { 4400 | "colab": { 4401 | "base_uri": "https://localhost:8080/", 4402 | "height": 219 4403 | }, 4404 | "id": "t48Ocx6QliJY", 4405 | "outputId": "e9c88da9-068e-4c26-b156-5e1eeb0d2bb5" 4406 | }, 4407 | "execution_count": null, 4408 | "outputs": [ 4409 | { 4410 | "output_type": "error", 4411 | "ename": "StopIteration", 4412 | "evalue": "ignored", 4413 | "traceback": [ 4414 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 4415 | "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", 4416 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# After the iterator has returned all of its data, it raises a\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# StopIteration exception\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mour_iterator\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Raises StopIteration\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 4417 | "\u001b[0;31mStopIteration\u001b[0m: " 4418 | ] 4419 | } 4420 | ] 4421 | }, 4422 | { 4423 | "cell_type": "code", 4424 | "source": [ 4425 | "# We can also loop over it, in fact, \"for\" does this implicitly!\n", 4426 | "our_iterator = iter(our_iterable)\n", 4427 | "for i in our_iterator:\n", 4428 | " print(i) # Prints one, two, three\n" 4429 | ], 4430 | "metadata": { 4431 | "colab": { 4432 | "base_uri": "https://localhost:8080/" 4433 | }, 4434 | "id": "8p6qAmtoltZn", 4435 | "outputId": "dfb1df49-8528-496a-9e88-92c37a94d1b2" 4436 | }, 4437 | "execution_count": null, 4438 | "outputs": [ 4439 | { 4440 | "output_type": "stream", 4441 | "name": "stdout", 4442 | "text": [ 4443 | "one\n", 4444 | "two\n", 4445 | "three\n" 4446 | ] 4447 | } 4448 | ] 4449 | }, 4450 | { 4451 | "cell_type": "code", 4452 | "source": [ 4453 | "# You can grab all the elements of an iterable or iterator by call of list().\n", 4454 | "list(our_iterable) # => Returns [\"one\", \"two\", \"three\"]\n", 4455 | "list(our_iterator) # => Returns [] because state is saved\n" 4456 | ], 4457 | "metadata": { 4458 | "colab": { 4459 | "base_uri": "https://localhost:8080/" 4460 | }, 4461 | "id": "tq0i4jwumE2Q", 4462 | "outputId": "2a248c6e-af48-4af8-e080-e45aacf59bdf" 4463 | }, 4464 | "execution_count": null, 4465 | "outputs": [ 4466 | { 4467 | "output_type": "execute_result", 4468 | "data": { 4469 | "text/plain": [ 4470 | "[]" 4471 | ] 4472 | }, 4473 | "metadata": {}, 4474 | "execution_count": 177 4475 | } 4476 | ] 4477 | }, 4478 | { 4479 | "cell_type": "markdown", 4480 | "source": [ 4481 | "\n", 4482 | "####################################################\n", 4483 | "## 4. Functions\n", 4484 | "####################################################" 4485 | ], 4486 | "metadata": { 4487 | "id": "tYPfSI3OmGc6" 4488 | } 4489 | }, 4490 | { 4491 | "cell_type": "code", 4492 | "source": [ 4493 | "# Use \"def\" to create new functions\n", 4494 | "def add(x, y):\n", 4495 | " print(\"x is {} and y is {}\".format(x, y))\n", 4496 | " return x + y # Return values with a return statement\n" 4497 | ], 4498 | "metadata": { 4499 | "id": "kzVuc9lTmJLS" 4500 | }, 4501 | "execution_count": null, 4502 | "outputs": [] 4503 | }, 4504 | { 4505 | "cell_type": "code", 4506 | "source": [ 4507 | "# Calling functions with parameters\n", 4508 | "add(5, 6) # => prints out \"x is 5 and y is 6\" and returns 11\n" 4509 | ], 4510 | "metadata": { 4511 | "colab": { 4512 | "base_uri": "https://localhost:8080/" 4513 | }, 4514 | "id": "iTjWIORQmLoX", 4515 | "outputId": "e19be61b-ba91-4941-9a0e-e9f68835188d" 4516 | }, 4517 | "execution_count": null, 4518 | "outputs": [ 4519 | { 4520 | "output_type": "stream", 4521 | "name": "stdout", 4522 | "text": [ 4523 | "x is 5 and y is 6\n" 4524 | ] 4525 | }, 4526 | { 4527 | "output_type": "execute_result", 4528 | "data": { 4529 | "text/plain": [ 4530 | "11" 4531 | ] 4532 | }, 4533 | "metadata": {}, 4534 | "execution_count": 179 4535 | } 4536 | ] 4537 | }, 4538 | { 4539 | "cell_type": "code", 4540 | "source": [ 4541 | "# Another way to call functions is with keyword arguments\n", 4542 | "add(y=6, x=5) # Keyword arguments can arrive in any order.\n" 4543 | ], 4544 | "metadata": { 4545 | "colab": { 4546 | "base_uri": "https://localhost:8080/" 4547 | }, 4548 | "id": "-UNaoReqmQCH", 4549 | "outputId": "2d01162b-09b0-4cb5-c7aa-7615ae7df340" 4550 | }, 4551 | "execution_count": null, 4552 | "outputs": [ 4553 | { 4554 | "output_type": "stream", 4555 | "name": "stdout", 4556 | "text": [ 4557 | "x is 5 and y is 6\n" 4558 | ] 4559 | }, 4560 | { 4561 | "output_type": "execute_result", 4562 | "data": { 4563 | "text/plain": [ 4564 | "11" 4565 | ] 4566 | }, 4567 | "metadata": {}, 4568 | "execution_count": 180 4569 | } 4570 | ] 4571 | }, 4572 | { 4573 | "cell_type": "code", 4574 | "source": [ 4575 | "# You can define functions that take a variable number of\n", 4576 | "# positional arguments\n", 4577 | "def varargs(*args):\n", 4578 | " return args\n", 4579 | "\n", 4580 | "varargs(1, 2, 3) # => (1, 2, 3)\n" 4581 | ], 4582 | "metadata": { 4583 | "colab": { 4584 | "base_uri": "https://localhost:8080/" 4585 | }, 4586 | "id": "buTVkWy8mT2n", 4587 | "outputId": "426054e3-1cf0-4e8d-a188-da3299ca951d" 4588 | }, 4589 | "execution_count": null, 4590 | "outputs": [ 4591 | { 4592 | "output_type": "execute_result", 4593 | "data": { 4594 | "text/plain": [ 4595 | "(1, 2, 3)" 4596 | ] 4597 | }, 4598 | "metadata": {}, 4599 | "execution_count": 181 4600 | } 4601 | ] 4602 | }, 4603 | { 4604 | "cell_type": "code", 4605 | "source": [ 4606 | "# You can define functions that take a variable number of\n", 4607 | "# keyword arguments, as well\n", 4608 | "def keyword_args(**kwargs):\n", 4609 | " return kwargs\n", 4610 | "\n", 4611 | "# Let's call it to see what happens\n", 4612 | "keyword_args(big=\"foot\", loch=\"ness\") # => {\"big\": \"foot\", \"loch\": \"ness\"}\n" 4613 | ], 4614 | "metadata": { 4615 | "colab": { 4616 | "base_uri": "https://localhost:8080/" 4617 | }, 4618 | "id": "U1LD_aq6mXlV", 4619 | "outputId": "49f61607-62f3-467d-9ab6-37d82f9a2d0d" 4620 | }, 4621 | "execution_count": null, 4622 | "outputs": [ 4623 | { 4624 | "output_type": "execute_result", 4625 | "data": { 4626 | "text/plain": [ 4627 | "{'big': 'foot', 'loch': 'ness'}" 4628 | ] 4629 | }, 4630 | "metadata": {}, 4631 | "execution_count": 182 4632 | } 4633 | ] 4634 | }, 4635 | { 4636 | "cell_type": "code", 4637 | "source": [ 4638 | "# You can do both at once, if you like\n", 4639 | "def all_the_args(*args, **kwargs):\n", 4640 | " print(args)\n", 4641 | " print(kwargs)\n", 4642 | "# \"\"\"\n", 4643 | "# all_the_args(1, 2, a=3, b=4) prints:\n", 4644 | "# (1, 2)\n", 4645 | "# {\"a\": 3, \"b\": 4}\n", 4646 | "# \"\"\"\n" 4647 | ], 4648 | "metadata": { 4649 | "id": "qofzfxlamc_D" 4650 | }, 4651 | "execution_count": null, 4652 | "outputs": [] 4653 | }, 4654 | { 4655 | "cell_type": "code", 4656 | "source": [ 4657 | "# When calling functions, you can do the opposite of args/kwargs!\n", 4658 | "# Use * to expand args (tuples) and use ** to expand kwargs (dictionaries).\n", 4659 | "args = (1, 2, 3, 4)\n", 4660 | "kwargs = {\"a\": 3, \"b\": 4}\n", 4661 | "all_the_args(*args) # equivalent: all_the_args(1, 2, 3, 4)\n", 4662 | "all_the_args(**kwargs) # equivalent: all_the_args(a=3, b=4)\n", 4663 | "all_the_args(*args, **kwargs) # equivalent: all_the_args(1, 2, 3, 4, a=3, b=4)" 4664 | ], 4665 | "metadata": { 4666 | "colab": { 4667 | "base_uri": "https://localhost:8080/" 4668 | }, 4669 | "id": "Gedo1v4amhpo", 4670 | "outputId": "4f2b5ab6-5976-4df9-90ae-2b1899924885" 4671 | }, 4672 | "execution_count": null, 4673 | "outputs": [ 4674 | { 4675 | "output_type": "stream", 4676 | "name": "stdout", 4677 | "text": [ 4678 | "(1, 2, 3, 4)\n", 4679 | "{}\n", 4680 | "()\n", 4681 | "{'a': 3, 'b': 4}\n", 4682 | "(1, 2, 3, 4)\n", 4683 | "{'a': 3, 'b': 4}\n" 4684 | ] 4685 | } 4686 | ] 4687 | }, 4688 | { 4689 | "cell_type": "code", 4690 | "source": [ 4691 | "# Returning multiple values (with tuple assignments)\n", 4692 | "def swap(x, y):\n", 4693 | " return y, x # Return multiple values as a tuple without the parenthesis.\n", 4694 | " # (Note: parenthesis have been excluded but can be included)\n", 4695 | "\n", 4696 | "x = 1\n", 4697 | "y = 2\n", 4698 | "x, y = swap(x, y) # => x = 2, y = 1\n", 4699 | "# (x, y) = swap(x,y) # Again the use of parenthesis is optional.\n" 4700 | ], 4701 | "metadata": { 4702 | "id": "aOVR0T_2mzXn" 4703 | }, 4704 | "execution_count": null, 4705 | "outputs": [] 4706 | }, 4707 | { 4708 | "cell_type": "code", 4709 | "source": [ 4710 | "# global scope\n", 4711 | "x = 5\n", 4712 | "\n", 4713 | "def set_x(num):\n", 4714 | " # local scope begins here\n", 4715 | " # local var x not the same as global var x\n", 4716 | " x = num # => 43\n", 4717 | " print(x) # => 43" 4718 | ], 4719 | "metadata": { 4720 | "id": "rxoX5W9Bm2z6" 4721 | }, 4722 | "execution_count": null, 4723 | "outputs": [] 4724 | }, 4725 | { 4726 | "cell_type": "code", 4727 | "source": [ 4728 | "def set_global_x(num):\n", 4729 | " # global indicates that particular var lives in the global scope\n", 4730 | " global x\n", 4731 | " print(x) # => 5\n", 4732 | " x = num # global var x is now set to 6\n", 4733 | " print(x) # => 6" 4734 | ], 4735 | "metadata": { 4736 | "id": "qrCahrpenAQ6" 4737 | }, 4738 | "execution_count": null, 4739 | "outputs": [] 4740 | }, 4741 | { 4742 | "cell_type": "code", 4743 | "source": [ 4744 | "\n", 4745 | "\n", 4746 | "# \"\"\"\n", 4747 | "# prints:\n", 4748 | "# 43\n", 4749 | "# 5\n", 4750 | "# 6\n", 4751 | "# \"\"\"\n", 4752 | "set_x(43)\n", 4753 | "set_global_x(6)" 4754 | ], 4755 | "metadata": { 4756 | "colab": { 4757 | "base_uri": "https://localhost:8080/" 4758 | }, 4759 | "id": "wk43DcgUnBV4", 4760 | "outputId": "e64587bf-6330-466e-aff3-95af50ae253a" 4761 | }, 4762 | "execution_count": null, 4763 | "outputs": [ 4764 | { 4765 | "output_type": "stream", 4766 | "name": "stdout", 4767 | "text": [ 4768 | "43\n", 4769 | "5\n", 4770 | "6\n" 4771 | ] 4772 | } 4773 | ] 4774 | }, 4775 | { 4776 | "cell_type": "code", 4777 | "source": [ 4778 | "# Python has first class functions\n", 4779 | "def create_adder(x):\n", 4780 | " def adder(y):\n", 4781 | " return x + y\n", 4782 | " return adder\n", 4783 | "\n", 4784 | "add_10 = create_adder(10)\n", 4785 | "add_10(3) # => 13\n" 4786 | ], 4787 | "metadata": { 4788 | "colab": { 4789 | "base_uri": "https://localhost:8080/" 4790 | }, 4791 | "id": "JrDlYy95nLdP", 4792 | "outputId": "ab835201-8921-40e8-ec2f-543ff254d5c5" 4793 | }, 4794 | "execution_count": null, 4795 | "outputs": [ 4796 | { 4797 | "output_type": "execute_result", 4798 | "data": { 4799 | "text/plain": [ 4800 | "13" 4801 | ] 4802 | }, 4803 | "metadata": {}, 4804 | "execution_count": 195 4805 | } 4806 | ] 4807 | }, 4808 | { 4809 | "cell_type": "code", 4810 | "source": [ 4811 | "# There are also anonymous functions\n", 4812 | "(lambda x: x > 2)(3) # => True\n", 4813 | "\n" 4814 | ], 4815 | "metadata": { 4816 | "colab": { 4817 | "base_uri": "https://localhost:8080/" 4818 | }, 4819 | "id": "Mx8c6KGTnPCR", 4820 | "outputId": "6270aeff-1151-4e80-e5ef-4c2134114e2b" 4821 | }, 4822 | "execution_count": null, 4823 | "outputs": [ 4824 | { 4825 | "output_type": "execute_result", 4826 | "data": { 4827 | "text/plain": [ 4828 | "True" 4829 | ] 4830 | }, 4831 | "metadata": {}, 4832 | "execution_count": 197 4833 | } 4834 | ] 4835 | }, 4836 | { 4837 | "cell_type": "code", 4838 | "source": [ 4839 | "(lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5" 4840 | ], 4841 | "metadata": { 4842 | "colab": { 4843 | "base_uri": "https://localhost:8080/" 4844 | }, 4845 | "id": "en0ZKIvAnV57", 4846 | "outputId": "defbce8e-f604-4242-9e2f-a14278327edb" 4847 | }, 4848 | "execution_count": null, 4849 | "outputs": [ 4850 | { 4851 | "output_type": "execute_result", 4852 | "data": { 4853 | "text/plain": [ 4854 | "5" 4855 | ] 4856 | }, 4857 | "metadata": {}, 4858 | "execution_count": 198 4859 | } 4860 | ] 4861 | }, 4862 | { 4863 | "cell_type": "code", 4864 | "source": [ 4865 | "# There are built-in higher order functions\n", 4866 | "list(map(add_10, [1, 2, 3])) # => [11, 12, 13]" 4867 | ], 4868 | "metadata": { 4869 | "colab": { 4870 | "base_uri": "https://localhost:8080/" 4871 | }, 4872 | "id": "Yjg9lwuInXBi", 4873 | "outputId": "2673467a-7b93-4174-9aba-29a9e69a695c" 4874 | }, 4875 | "execution_count": null, 4876 | "outputs": [ 4877 | { 4878 | "output_type": "execute_result", 4879 | "data": { 4880 | "text/plain": [ 4881 | "[11, 12, 13]" 4882 | ] 4883 | }, 4884 | "metadata": {}, 4885 | "execution_count": 200 4886 | } 4887 | ] 4888 | }, 4889 | { 4890 | "cell_type": "code", 4891 | "source": [ 4892 | "list(map(max, [1, 2, 3], [4, 2, 1])) # => [4, 2, 3]" 4893 | ], 4894 | "metadata": { 4895 | "colab": { 4896 | "base_uri": "https://localhost:8080/" 4897 | }, 4898 | "id": "NgYlo-pTnjSZ", 4899 | "outputId": "1b5dcc78-8368-491e-849a-1e76aab06d83" 4900 | }, 4901 | "execution_count": null, 4902 | "outputs": [ 4903 | { 4904 | "output_type": "execute_result", 4905 | "data": { 4906 | "text/plain": [ 4907 | "[4, 2, 3]" 4908 | ] 4909 | }, 4910 | "metadata": {}, 4911 | "execution_count": 201 4912 | } 4913 | ] 4914 | }, 4915 | { 4916 | "cell_type": "code", 4917 | "source": [ 4918 | "list(filter(lambda x: x > 5, [3, 4, 5, 6, 7])) # => [6, 7]" 4919 | ], 4920 | "metadata": { 4921 | "colab": { 4922 | "base_uri": "https://localhost:8080/" 4923 | }, 4924 | "id": "3mk9toppnl15", 4925 | "outputId": "3509cfd5-ee96-4838-b1f2-891fb7476b6b" 4926 | }, 4927 | "execution_count": null, 4928 | "outputs": [ 4929 | { 4930 | "output_type": "execute_result", 4931 | "data": { 4932 | "text/plain": [ 4933 | "[6, 7]" 4934 | ] 4935 | }, 4936 | "metadata": {}, 4937 | "execution_count": 202 4938 | } 4939 | ] 4940 | }, 4941 | { 4942 | "cell_type": "code", 4943 | "source": [ 4944 | "# We can use list comprehensions for nice maps and filters\n", 4945 | "# List comprehension stores the output as a list (which itself may be nested).\n", 4946 | "[add_10(i) for i in [1, 2, 3]] # => [11, 12, 13]" 4947 | ], 4948 | "metadata": { 4949 | "colab": { 4950 | "base_uri": "https://localhost:8080/" 4951 | }, 4952 | "id": "py9SNotUnrRt", 4953 | "outputId": "855e6446-cbc6-4cf5-b1f6-b277bb14b43b" 4954 | }, 4955 | "execution_count": null, 4956 | "outputs": [ 4957 | { 4958 | "output_type": "execute_result", 4959 | "data": { 4960 | "text/plain": [ 4961 | "[11, 12, 13]" 4962 | ] 4963 | }, 4964 | "metadata": {}, 4965 | "execution_count": 203 4966 | } 4967 | ] 4968 | }, 4969 | { 4970 | "cell_type": "code", 4971 | "source": [ 4972 | "[x for x in [3, 4, 5, 6, 7] if x > 5] # => [6, 7]\n" 4973 | ], 4974 | "metadata": { 4975 | "colab": { 4976 | "base_uri": "https://localhost:8080/" 4977 | }, 4978 | "id": "G-Im0ugenuyz", 4979 | "outputId": "512d55c1-9f03-44d6-b7f5-8fe4479b186a" 4980 | }, 4981 | "execution_count": null, 4982 | "outputs": [ 4983 | { 4984 | "output_type": "execute_result", 4985 | "data": { 4986 | "text/plain": [ 4987 | "[6, 7]" 4988 | ] 4989 | }, 4990 | "metadata": {}, 4991 | "execution_count": 204 4992 | } 4993 | ] 4994 | }, 4995 | { 4996 | "cell_type": "code", 4997 | "source": [ 4998 | "# You can construct set and dict comprehensions as well.\n", 4999 | "{x for x in 'abcddeef' if x not in 'abc'} # => {'d', 'e', 'f'}" 5000 | ], 5001 | "metadata": { 5002 | "colab": { 5003 | "base_uri": "https://localhost:8080/" 5004 | }, 5005 | "id": "Oo6-amXZnxoB", 5006 | "outputId": "b4179b2e-34ec-4bb3-b4b7-efebf44ef1d4" 5007 | }, 5008 | "execution_count": null, 5009 | "outputs": [ 5010 | { 5011 | "output_type": "execute_result", 5012 | "data": { 5013 | "text/plain": [ 5014 | "{'d', 'e', 'f'}" 5015 | ] 5016 | }, 5017 | "metadata": {}, 5018 | "execution_count": 205 5019 | } 5020 | ] 5021 | }, 5022 | { 5023 | "cell_type": "code", 5024 | "source": [ 5025 | "{x: x**2 for x in range(5)} # => {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}" 5026 | ], 5027 | "metadata": { 5028 | "colab": { 5029 | "base_uri": "https://localhost:8080/" 5030 | }, 5031 | "id": "heLpNcd8n1aj", 5032 | "outputId": "30452b79-5919-4ba3-f75d-bf69d1c6555c" 5033 | }, 5034 | "execution_count": null, 5035 | "outputs": [ 5036 | { 5037 | "output_type": "execute_result", 5038 | "data": { 5039 | "text/plain": [ 5040 | "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}" 5041 | ] 5042 | }, 5043 | "metadata": {}, 5044 | "execution_count": 206 5045 | } 5046 | ] 5047 | }, 5048 | { 5049 | "cell_type": "markdown", 5050 | "source": [ 5051 | "####################################################\n", 5052 | "## 5. Modules\n", 5053 | "####################################################" 5054 | ], 5055 | "metadata": { 5056 | "id": "Vs7CA8MSn8fo" 5057 | } 5058 | }, 5059 | { 5060 | "cell_type": "code", 5061 | "source": [ 5062 | "# You can import modules\n", 5063 | "import math\n", 5064 | "print(math.sqrt(16)) # => 4.0" 5065 | ], 5066 | "metadata": { 5067 | "colab": { 5068 | "base_uri": "https://localhost:8080/" 5069 | }, 5070 | "id": "0nz-ZmwJn5sI", 5071 | "outputId": "4ade6531-5dc7-4922-d46f-db2489db871f" 5072 | }, 5073 | "execution_count": null, 5074 | "outputs": [ 5075 | { 5076 | "output_type": "stream", 5077 | "name": "stdout", 5078 | "text": [ 5079 | "4.0\n" 5080 | ] 5081 | } 5082 | ] 5083 | }, 5084 | { 5085 | "cell_type": "code", 5086 | "source": [ 5087 | "# You can get specific functions from a module\n", 5088 | "from math import ceil, floor\n", 5089 | "print(ceil(3.7)) # => 4\n" 5090 | ], 5091 | "metadata": { 5092 | "colab": { 5093 | "base_uri": "https://localhost:8080/" 5094 | }, 5095 | "id": "QDl7HgfFoHL2", 5096 | "outputId": "81a87948-9ee5-47dd-a6c5-d87408c8d0fe" 5097 | }, 5098 | "execution_count": null, 5099 | "outputs": [ 5100 | { 5101 | "output_type": "stream", 5102 | "name": "stdout", 5103 | "text": [ 5104 | "4\n" 5105 | ] 5106 | } 5107 | ] 5108 | }, 5109 | { 5110 | "cell_type": "code", 5111 | "source": [ 5112 | "print(floor(3.7)) # => 3" 5113 | ], 5114 | "metadata": { 5115 | "colab": { 5116 | "base_uri": "https://localhost:8080/" 5117 | }, 5118 | "id": "6ITExlwdoNSA", 5119 | "outputId": "fae1842c-a2fe-4e52-fe7d-4f1a1b1e274b" 5120 | }, 5121 | "execution_count": null, 5122 | "outputs": [ 5123 | { 5124 | "output_type": "stream", 5125 | "name": "stdout", 5126 | "text": [ 5127 | "3\n" 5128 | ] 5129 | } 5130 | ] 5131 | }, 5132 | { 5133 | "cell_type": "code", 5134 | "source": [ 5135 | "# You can import all functions from a module.\n", 5136 | "# Warning: this is not recommended\n", 5137 | "from math import *" 5138 | ], 5139 | "metadata": { 5140 | "id": "f3VGJkvNoQXU" 5141 | }, 5142 | "execution_count": null, 5143 | "outputs": [] 5144 | }, 5145 | { 5146 | "cell_type": "code", 5147 | "source": [ 5148 | "# You can shorten module names\n", 5149 | "import math as m\n", 5150 | "math.sqrt(16) == m.sqrt(16) # => True" 5151 | ], 5152 | "metadata": { 5153 | "colab": { 5154 | "base_uri": "https://localhost:8080/" 5155 | }, 5156 | "id": "9QbKl8LioUgJ", 5157 | "outputId": "b828131f-f753-4ff9-aa5d-458109a797a7" 5158 | }, 5159 | "execution_count": null, 5160 | "outputs": [ 5161 | { 5162 | "output_type": "execute_result", 5163 | "data": { 5164 | "text/plain": [ 5165 | "True" 5166 | ] 5167 | }, 5168 | "metadata": {}, 5169 | "execution_count": 212 5170 | } 5171 | ] 5172 | }, 5173 | { 5174 | "cell_type": "code", 5175 | "source": [ 5176 | "# Python modules are just ordinary Python files. You\n", 5177 | "# can write your own, and import them. The name of the\n", 5178 | "# module is the same as the name of the file.\n", 5179 | "\n", 5180 | "# You can find out which functions and attributes\n", 5181 | "# are defined in a module.\n", 5182 | "import math\n", 5183 | "dir(math)\n", 5184 | "\n", 5185 | "\n", 5186 | "# If you have a Python script named math.py in the same\n", 5187 | "# folder as your current script, the file math.py will\n", 5188 | "# be loaded instead of the built-in Python module.\n", 5189 | "# This happens because the local folder has priority\n", 5190 | "# over Python's built-in libraries." 5191 | ], 5192 | "metadata": { 5193 | "colab": { 5194 | "base_uri": "https://localhost:8080/" 5195 | }, 5196 | "id": "jJ7YlFXfocOp", 5197 | "outputId": "d9c1c338-0e2b-4bee-db59-1cc07ff0a92e" 5198 | }, 5199 | "execution_count": null, 5200 | "outputs": [ 5201 | { 5202 | "output_type": "execute_result", 5203 | "data": { 5204 | "text/plain": [ 5205 | "['__doc__',\n", 5206 | " '__loader__',\n", 5207 | " '__name__',\n", 5208 | " '__package__',\n", 5209 | " '__spec__',\n", 5210 | " 'acos',\n", 5211 | " 'acosh',\n", 5212 | " 'asin',\n", 5213 | " 'asinh',\n", 5214 | " 'atan',\n", 5215 | " 'atan2',\n", 5216 | " 'atanh',\n", 5217 | " 'ceil',\n", 5218 | " 'comb',\n", 5219 | " 'copysign',\n", 5220 | " 'cos',\n", 5221 | " 'cosh',\n", 5222 | " 'degrees',\n", 5223 | " 'dist',\n", 5224 | " 'e',\n", 5225 | " 'erf',\n", 5226 | " 'erfc',\n", 5227 | " 'exp',\n", 5228 | " 'expm1',\n", 5229 | " 'fabs',\n", 5230 | " 'factorial',\n", 5231 | " 'floor',\n", 5232 | " 'fmod',\n", 5233 | " 'frexp',\n", 5234 | " 'fsum',\n", 5235 | " 'gamma',\n", 5236 | " 'gcd',\n", 5237 | " 'hypot',\n", 5238 | " 'inf',\n", 5239 | " 'isclose',\n", 5240 | " 'isfinite',\n", 5241 | " 'isinf',\n", 5242 | " 'isnan',\n", 5243 | " 'isqrt',\n", 5244 | " 'lcm',\n", 5245 | " 'ldexp',\n", 5246 | " 'lgamma',\n", 5247 | " 'log',\n", 5248 | " 'log10',\n", 5249 | " 'log1p',\n", 5250 | " 'log2',\n", 5251 | " 'modf',\n", 5252 | " 'nan',\n", 5253 | " 'nextafter',\n", 5254 | " 'perm',\n", 5255 | " 'pi',\n", 5256 | " 'pow',\n", 5257 | " 'prod',\n", 5258 | " 'radians',\n", 5259 | " 'remainder',\n", 5260 | " 'sin',\n", 5261 | " 'sinh',\n", 5262 | " 'sqrt',\n", 5263 | " 'tan',\n", 5264 | " 'tanh',\n", 5265 | " 'tau',\n", 5266 | " 'trunc',\n", 5267 | " 'ulp']" 5268 | ] 5269 | }, 5270 | "metadata": {}, 5271 | "execution_count": 213 5272 | } 5273 | ] 5274 | }, 5275 | { 5276 | "cell_type": "markdown", 5277 | "source": [ 5278 | "\n", 5279 | "####################################################\n", 5280 | "## 6. Classes\n", 5281 | "####################################################\n" 5282 | ], 5283 | "metadata": { 5284 | "id": "awUUWZRColSF" 5285 | } 5286 | }, 5287 | { 5288 | "cell_type": "code", 5289 | "source": [ 5290 | "# One of the most important method for a class is the Constructor.\n", 5291 | "# In Python, it has name of __init__ by default.\n", 5292 | "# The first parameter to this function (in its definition) is self which refers to the current object that is being instantiated.\n", 5293 | "\n", 5294 | "# Rest of the parameters can be used to initialize the values of all attributes.\n", 5295 | "\n", 5296 | "# There are certain methods that can be considered as standard methods within the Python language. For example, getting a string representation of the object or a length function (if it can be applied to a particular object).\n", 5297 | "# __len__ for length\n", 5298 | "# __str__ for String representation\n", 5299 | "\n", 5300 | "\n", 5301 | "\n", 5302 | "class BankAccount:\n", 5303 | "\n", 5304 | " acctCount=0 #class object method\n", 5305 | "\n", 5306 | " def __init__(self, name, balance=0): #constructor\n", 5307 | "\n", 5308 | " BankAccount.acctCount+=1 #incrementing class object method\n", 5309 | " self.name= name # instance attribute to store name of bankaccount\n", 5310 | " self.balance= balance # instance attribute to store balance of bank account\n", 5311 | " #you can add more attributes here and initialize them, (using parameter\n", 5312 | " #or some default value\n", 5313 | "\n", 5314 | " def displayCount(): #static method\n", 5315 | " '''Method to display number of accounts created'''\n", 5316 | " print(\"Total Accounts\", BankAccount.acctCount) #using class name\n", 5317 | "\n", 5318 | " def displayName(self): #instance method\n", 5319 | " '''Method to print name of the account'''\n", 5320 | " print(\"Name: \",self.name)\n", 5321 | "\n", 5322 | " def Credit(self, amount):\n", 5323 | " '''Method to deduce credit from the account'''\n", 5324 | " self.balance-=amount\n", 5325 | "\n", 5326 | " def Debit(self, amount):\n", 5327 | " '''Method to increase debit into the account'''\n", 5328 | " self.balance+=amount\n", 5329 | "\n", 5330 | " def getBalance(self):\n", 5331 | " '''Method to get balance of the account'''\n", 5332 | " return self.balance\n", 5333 | "\n", 5334 | "\n", 5335 | "\n", 5336 | " def __str__(self):\n", 5337 | " '''Method to return string representation of the account'''\n", 5338 | " return \"Name: \" + self.name + \", Balance: \"+ str(self.balance)" 5339 | ], 5340 | "metadata": { 5341 | "id": "hwg8AjWFpDOz" 5342 | }, 5343 | "execution_count": null, 5344 | "outputs": [] 5345 | }, 5346 | { 5347 | "cell_type": "code", 5348 | "source": [ 5349 | "#create an bankaccount Ahmed with balance of 1000\n", 5350 | "acct1= BankAccount('Ahmed',2000)\n", 5351 | "#create an bankaccount Asad with balance of 5000\n", 5352 | "acct2= BankAccount('Asad',5000)\n" 5353 | ], 5354 | "metadata": { 5355 | "id": "Xt65bZZRzLU6" 5356 | }, 5357 | "execution_count": null, 5358 | "outputs": [] 5359 | }, 5360 | { 5361 | "cell_type": "code", 5362 | "source": [ 5363 | "#calling instance function\n", 5364 | "acct1.displayName()\n", 5365 | "\n", 5366 | "acct1.Credit(25)\n", 5367 | "\n", 5368 | "acct1.getBalance()" 5369 | ], 5370 | "metadata": { 5371 | "id": "LRW292dnyA3a", 5372 | "colab": { 5373 | "base_uri": "https://localhost:8080/" 5374 | }, 5375 | "outputId": "d1eb013b-f102-4e45-c51e-8530318de314" 5376 | }, 5377 | "execution_count": null, 5378 | "outputs": [ 5379 | { 5380 | "output_type": "stream", 5381 | "name": "stdout", 5382 | "text": [ 5383 | "Name: Ahmed\n" 5384 | ] 5385 | }, 5386 | { 5387 | "output_type": "execute_result", 5388 | "data": { 5389 | "text/plain": [ 5390 | "1975" 5391 | ] 5392 | }, 5393 | "metadata": {}, 5394 | "execution_count": 3 5395 | } 5396 | ] 5397 | }, 5398 | { 5399 | "cell_type": "code", 5400 | "source": [ 5401 | "#calling __str__ function\n", 5402 | "print(acct1)" 5403 | ], 5404 | "metadata": { 5405 | "colab": { 5406 | "base_uri": "https://localhost:8080/" 5407 | }, 5408 | "id": "xm5MDEUizOdR", 5409 | "outputId": "1f11fa3c-d496-44aa-e9bb-bae402d028be" 5410 | }, 5411 | "execution_count": null, 5412 | "outputs": [ 5413 | { 5414 | "output_type": "stream", 5415 | "name": "stdout", 5416 | "text": [ 5417 | "Name: Ahmed, Balance: 1975\n" 5418 | ] 5419 | } 5420 | ] 5421 | }, 5422 | { 5423 | "cell_type": "code", 5424 | "source": [], 5425 | "metadata": { 5426 | "id": "8GAxPpbszQft" 5427 | }, 5428 | "execution_count": null, 5429 | "outputs": [] 5430 | } 5431 | ] 5432 | } --------------------------------------------------------------------------------