├── README.md ├── Projects ├── anagram_guessing_game │ ├── game │ │ ├── __init__.py │ │ ├── player.py │ │ ├── word_provider.py │ │ ├── user.py │ │ └── game_manager.py │ ├── main.py │ ├── docker-compose.yml │ ├── Dockerfile │ └── data │ │ ├── users.json │ │ └── words.txt ├── console_calculator │ ├── docker-compose.yml │ ├── Dockerfile │ └── main.py └── number_guessing_game │ ├── game.py │ └── main.py ├── scripts ├── factorial.py ├── factorial_recursion.py └── commutative_sum.py ├── list.py ├── .gitignore ├── Loop.ipynb ├── Function.ipynb ├── ConditionalStatements.ipynb ├── modules ├── os_module.ipynb └── deque.ipynb ├── Variable.ipynb ├── lambda_function.ipynb └── python_basics.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Learning-Python 2 | -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/game/__init__.py: -------------------------------------------------------------------------------- 1 | from .game_manager import GameManager 2 | 3 | __all__ = ["GameManager"] 4 | -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/main.py: -------------------------------------------------------------------------------- 1 | from game import GameManager 2 | 3 | if __name__ == '__main__': 4 | game = GameManager(max_attempts=5) 5 | game.start_game() 6 | -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/game/player.py: -------------------------------------------------------------------------------- 1 | class Player: 2 | def __init__(self): 3 | pass 4 | 5 | @staticmethod 6 | def get_guess(): 7 | return input("Your guess:") 8 | -------------------------------------------------------------------------------- /Projects/console_calculator/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | calculator: 5 | build: . 6 | container_name: console-calculator-container 7 | stdin_open: true 8 | tty: true 9 | command: 10 | - python main.py -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | 4 | services: 5 | anagram_guessing_game: 6 | build: . 7 | container_name: anagram-guessing-game-container 8 | stdin_open: true 9 | tty: true 10 | command: 11 | - python main.py -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/Dockerfile: -------------------------------------------------------------------------------- 1 | # python runtime 2 | FROM python:3 3 | 4 | # working directory in container 5 | WORKDIR app 6 | 7 | # copy the current directory contents into the container at /app 8 | COPY . /app 9 | 10 | # set the command to run 11 | CMD ["python", "main.py"] -------------------------------------------------------------------------------- /Projects/console_calculator/Dockerfile: -------------------------------------------------------------------------------- 1 | # official python runtime 2 | FROM python:3 3 | 4 | # working directory in container 5 | WORKDIR /app 6 | 7 | # copy the current directory contents into the container at /app 8 | COPY . /app 9 | 10 | # set the command to run 11 | CMD ["python", "main.py"] 12 | -------------------------------------------------------------------------------- /scripts/factorial.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n == 0: 3 | return 1 4 | 5 | fact = 1 6 | for i in range(1, n + 1): 7 | fact *= i 8 | return fact 9 | 10 | 11 | if __name__ == '__main__': 12 | print(factorial(0)) 13 | print(factorial(1)) 14 | print(factorial(3)) 15 | print(factorial(5)) 16 | print(factorial(10)) -------------------------------------------------------------------------------- /scripts/factorial_recursion.py: -------------------------------------------------------------------------------- 1 | # calculate factorial using recursion 2 | 3 | def factorial(n): 4 | if n == 0: 5 | return 1 6 | 7 | return n * factorial(n - 1) 8 | 9 | 10 | if __name__ == '__main__': 11 | print(factorial(0)) 12 | print(factorial(1)) 13 | print(factorial(3)) 14 | print(factorial(5)) 15 | print(factorial(10)) 16 | -------------------------------------------------------------------------------- /scripts/commutative_sum.py: -------------------------------------------------------------------------------- 1 | def commutative_sum(nums: list[int]) -> list[int]: 2 | n = len(nums) 3 | ans = [0] * n 4 | ans[0] = nums[0] 5 | for i in range(1, n): 6 | ans[i] = nums[i] + ans[i - 1] 7 | return ans 8 | 9 | 10 | if __name__ == '__main__': 11 | nums: list[int] = [1, 2, 3, 4, 5] 12 | 13 | print(commutative_sum(nums)) 14 | -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/game/word_provider.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | class WordProvider: 5 | def __init__(self, filepath="data/words.txt"): 6 | self.filepath = filepath 7 | 8 | def get_random_word(self): 9 | with open(self.filepath, 'r') as file: 10 | words = file.read().splitlines() 11 | return random.choice(words) 12 | -------------------------------------------------------------------------------- /Projects/number_guessing_game/game.py: -------------------------------------------------------------------------------- 1 | class Game: 2 | def welcome(self): 3 | game_description = '''The main objective of this game is to guess a secret number within a guessing limit. 4 | There are three levels(Easy, Medium, Hard) in this game. Choose your desired level. 5 | Guess limit for each level: 6 | Easy-> 10 times 7 | Medium-> 5 times 8 | Hard-> 3 times 9 | 10 | Try your luck!!! Happy gaming!!! 11 | ''' 12 | print('Welcome to my guessing game!!!\n') 13 | print(game_description) -------------------------------------------------------------------------------- /Projects/number_guessing_game/main.py: -------------------------------------------------------------------------------- 1 | # Welcome the player 2 | # Take permission to start or exit the game 3 | # If the user starts the game, take input for game level 4 | # Levels: easy, medium, hard 5 | # In this game the player have to guess the correct number within a guess limit 6 | # Guess limit: easy-> 10 times, medium-> 5 times, hard-> 3 times 7 | 8 | from Projects.number_guessing_game.game import Game 9 | 10 | 11 | def main(): 12 | game = Game() 13 | game.welcome() 14 | 15 | 16 | if __name__ == '__main__': 17 | main() 18 | -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/data/users.json: -------------------------------------------------------------------------------- 1 | { 2 | "wasik": { 3 | "password": "5f567bf73fa1d9ca30d6fb7700f2cd76a3ad909ef5ee16a89f5e77205079b0a7", 4 | "wins": 2, 5 | "losses": 2 6 | }, 7 | "apon": { 8 | "password": "5ccffa4964dc16be06771f4cb898907dfd22e486fca7a620d67214a978c9dc34", 9 | "wins": 0, 10 | "losses": 1 11 | }, 12 | "gg": { 13 | "password": "cbd3cfb9b9f51bbbfbf08759e243f5b3519cbf6ecc219ee95fe7c667e32c0a8d", 14 | "wins": 1, 15 | "losses": 2 16 | }, 17 | "sumaya": { 18 | "password": "2e44e08484ab33f1a1ae6c5b975fdc1e2884fddf1715c4ba051db952857b20c6", 19 | "wins": 1, 20 | "losses": 0 21 | }, 22 | "sazzad": { 23 | "password": "88f1821b3122366261d5778a084a366579103e32615662f76518460a0eb3e96f", 24 | "wins": 0, 25 | "losses": 1 26 | } 27 | } -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/data/words.txt: -------------------------------------------------------------------------------- 1 | angel 2 | listen 3 | enlist 4 | below 5 | stressed 6 | evil 7 | heart 8 | admirer 9 | dusty 10 | night 11 | brag 12 | flow 13 | cider 14 | alert 15 | caret 16 | cried 17 | study 18 | rescue 19 | fried 20 | save 21 | least 22 | glean 23 | cinema 24 | dog 25 | apt 26 | forty 27 | inch 28 | brave 29 | chin 30 | arc 31 | cat 32 | bored 33 | scar 34 | bowl 35 | beard 36 | forty 37 | gallery 38 | car 39 | dart 40 | date 41 | dice 42 | finder 43 | frosty 44 | grown 45 | hug 46 | idol 47 | last 48 | made 49 | mate 50 | melon 51 | neat 52 | pace 53 | paint 54 | part 55 | pat 56 | peach 57 | pearl 58 | pole 59 | race 60 | ram 61 | reins 62 | satin 63 | smart 64 | snap 65 | straw 66 | tab 67 | tame 68 | tar 69 | taste 70 | team 71 | ten 72 | time 73 | top 74 | trace 75 | train 76 | trap 77 | vase 78 | wane 79 | war 80 | west 81 | who 82 | wins 83 | wolf 84 | worn 85 | dear 86 | drawer 87 | earth 88 | ear 89 | fear 90 | fowl 91 | garden 92 | gate 93 | grab 94 | large 95 | male 96 | pat 97 | part 98 | plates 99 | rail 100 | read 101 | reap 102 | singer 103 | solemn 104 | spare 105 | stale 106 | star 107 | thing -------------------------------------------------------------------------------- /list.py: -------------------------------------------------------------------------------- 1 | # creating a list 2 | List = ['this','is', 'a', 'list'] 3 | print(List) 4 | 5 | # creating a multidimentional list 6 | m_list = [['this', 'is'], 'a', 'multidimentional', ['list']] 7 | print(m_list) 8 | 9 | # Method Description 10 | # append() Adds an element at the end of the list 11 | # clear() Removes all the elements from the list 12 | # copy() Returns a copy of the list 13 | # count() Returns the number of elements with the specified value 14 | # extend() Add the elements of a list (or any iterable), to the end of the current list 15 | # index() Returns the index of the first element with the specified value 16 | # insert() Adds an element at the specified position 17 | # pop() Removes the element at the specified position 18 | # remove() Removes the first item with the specified value 19 | # reverse() Reverses the order of the list 20 | # sort() Sorts the list 21 | 22 | len(List) 23 | List.append(69) 24 | print(List) 25 | List_b = List.copy() 26 | print(List_b) 27 | List_b.clear() 28 | print(List_b) 29 | List.extend(m_list) 30 | print(List) 31 | for item in m_list: 32 | List.remove(item) 33 | print(List) 34 | List.insert(10,0) 35 | print(List) 36 | List.remove(0) 37 | print(List) 38 | List.reverse() 39 | print(List) 40 | List.sort() # TypeError: '<' not supported between instances of 'int' and 'str' 41 | -------------------------------------------------------------------------------- /Projects/console_calculator/main.py: -------------------------------------------------------------------------------- 1 | def add(x, y): 2 | return x + y 3 | 4 | 5 | def sub(x, y): 6 | return x - y 7 | 8 | 9 | def mul(x, y): 10 | return x * y 11 | 12 | 13 | def div(x, y): 14 | if y == 0: 15 | return "Error: Division by zero!" 16 | else: 17 | return x / y 18 | 19 | 20 | def showOptions(): 21 | print('1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit') 22 | choice = int(input('Please choose an option: ')) 23 | return choice 24 | 25 | 26 | def take_input(): 27 | x = int(input('Please enter 1st number: ')) 28 | y = int(input('Please enter 2nd number: ')) 29 | return x, y 30 | 31 | 32 | print('Welcome to the Calculator!!!') 33 | while True: 34 | option = showOptions() 35 | if option == 5: 36 | print('Thank you for using my calculator!!!') 37 | break 38 | x, y = take_input() 39 | if option == 1: 40 | result = add(x, y) 41 | print("Sum of {} and {} is {}".format(x, y, result)) 42 | elif option == 2: 43 | result = sub(x, y) 44 | print("Subtraction of {} and {} is {}".format(x, y, result)) 45 | elif option == 3: 46 | result = mul(x, y) 47 | print("Multiplication of {} and {} is {}".format(x, y, result)) 48 | elif option == 4: 49 | result = div(x, y) 50 | print("Division of {} and {} is {}".format(x, y, result)) 51 | -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/game/user.py: -------------------------------------------------------------------------------- 1 | import json 2 | import hashlib 3 | 4 | 5 | class User: 6 | def __init__(self, username, password, wins=0, losses=0): 7 | self.username = username 8 | self.password = self._hash_password(password) 9 | self.wins = wins 10 | self.losses = losses 11 | 12 | @staticmethod 13 | def _hash_password(password): 14 | return hashlib.sha256(password.encode()).hexdigest() 15 | 16 | def check_password(self, password): 17 | return self.password == self._hash_password(password) 18 | 19 | def update_stat(self, won): 20 | if won: 21 | self.wins += 1 22 | else: 23 | self.losses += 1 24 | 25 | def save_user(self, filepath="data/users.json"): 26 | users = self._load_user(filepath) 27 | users[self.username] = { 28 | "password": self.password, 29 | "wins": self.wins, 30 | "losses": self.losses 31 | } 32 | with open(filepath, 'w') as file: 33 | json.dump(users, file, indent=4) 34 | 35 | @staticmethod 36 | def _load_user(filepath): 37 | try: 38 | with open(filepath, 'r') as file: 39 | return json.load(file) 40 | except FileNotFoundError: 41 | return {} 42 | 43 | @classmethod 44 | def authenticate(cls, username, password, filepath="data/users.json"): 45 | users = cls._load_user(filepath) 46 | if username in users and users[username]['password'] == cls._hash_password(password): 47 | return cls(username, password, users[username]['wins'], users[username]['losses']) 48 | else: 49 | return None 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | .idea/ 161 | 162 | -------------------------------------------------------------------------------- /Loop.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "609e9624-2d94-41f1-b5fe-1c848d2ad6c6", 6 | "metadata": {}, 7 | "source": [ 8 | "# Loops in Python" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "88535bdc-56b2-4aca-93b2-01a849727658", 14 | "metadata": {}, 15 | "source": [ 16 | "- **`while` loop**\n", 17 | "- **`for` loop**" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "id": "fd140068-c9ea-425b-ade1-a860ef775248", 23 | "metadata": {}, 24 | "source": [ 25 | "### while loop" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "id": "3ba2d4ec-95f3-45cd-af39-90e211d5b468", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "# while condition:\n", 36 | " # Code block to be executed as long as the condition " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 5, 42 | "id": "01b33a45-af1f-41ee-8dd3-7b6b6f6dd3c7", 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "1 2 3 4 5 6 7 8 9 10 " 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "x = 1\n", 55 | "while x <= 10:\n", 56 | " print(x, end=' ')\n", 57 | " x += 1" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 6, 63 | "id": "52d54fa3-28e2-479a-891d-5fe8cea6d5a8", 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "Sum of numbers from 1 to 100: 5050\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "# Calculate the sum of numbers from 1 to 100\n", 76 | "\n", 77 | "total = 0\n", 78 | "current_number = 1\n", 79 | "while current_number <= 100:\n", 80 | " total += current_number\n", 81 | " current_number += 1\n", 82 | "print(\"Sum of numbers from 1 to 100: \", total)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 7, 88 | "id": "62199f5c-d329-4972-a308-8bd5d022e245", 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdin", 93 | "output_type": "stream", 94 | "text": [ 95 | "Enter a positive number: -18\n", 96 | "Enter a positive number: 20\n" 97 | ] 98 | }, 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "You entered: 20\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "# Get a positive number from the user\n", 109 | "user_input = -1\n", 110 | "\n", 111 | "while user_input <= 0:\n", 112 | " user_input = int(input(\"Enter a positive number: \"))\n", 113 | " \n", 114 | "print(\"You entered:\", user_input)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 10, 120 | "id": "b05a5566-4c49-4c37-bd9d-814766f19129", 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "name": "stdin", 125 | "output_type": "stream", 126 | "text": [ 127 | "Enter a positive number: 5\n" 128 | ] 129 | }, 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "Factorial: 120\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "# Calculate the factorial of a number\n", 140 | "\n", 141 | "number = int(input(\"Enter a positive number: \"))\n", 142 | "result = 1\n", 143 | "while number > 0:\n", 144 | " result *= number\n", 145 | " number -= 1\n", 146 | "print(\"Factorial:\", result)" 147 | ] 148 | } 149 | ], 150 | "metadata": { 151 | "kernelspec": { 152 | "display_name": "Python 3 (ipykernel)", 153 | "language": "python", 154 | "name": "python3" 155 | }, 156 | "language_info": { 157 | "codemirror_mode": { 158 | "name": "ipython", 159 | "version": 3 160 | }, 161 | "file_extension": ".py", 162 | "mimetype": "text/x-python", 163 | "name": "python", 164 | "nbconvert_exporter": "python", 165 | "pygments_lexer": "ipython3", 166 | "version": "3.12.0" 167 | } 168 | }, 169 | "nbformat": 4, 170 | "nbformat_minor": 5 171 | } 172 | -------------------------------------------------------------------------------- /Projects/anagram_guessing_game/game/game_manager.py: -------------------------------------------------------------------------------- 1 | from .word_provider import WordProvider 2 | from .player import Player 3 | from .user import User 4 | 5 | 6 | class GameManager: 7 | def __init__(self, max_attempts=3): 8 | self.word_provider = WordProvider() 9 | self.player = Player() 10 | self.max_attempts = max_attempts 11 | self.attempts = 0 12 | self.user = None 13 | 14 | def start_game(self): 15 | self.authenticate_user() 16 | 17 | while True: 18 | print("\n--- Main Menu ---") 19 | print("1. Play Game") 20 | print("2. View Profile") 21 | print("3. Update Profile") 22 | print("4. Logout") 23 | choice = input("Select an option: ").strip() 24 | 25 | if choice == '1': 26 | self.play_game() 27 | elif choice == '2': 28 | self.view_profile() 29 | elif choice == '3': 30 | self.update_profile() 31 | elif choice == '4': 32 | print("Logging out...") 33 | self.user = None 34 | self.authenticate_user() 35 | else: 36 | print("Invalid option. Please choose again.") 37 | 38 | def play_game(self): 39 | word = self.word_provider.get_random_word() 40 | anagram = ''.join(sorted(word)) 41 | 42 | print("Welcome to the Anagram Guessing Game!") 43 | print(f"Here is your anagram: {anagram}") 44 | print(f"You have {self.max_attempts} attempts to guess the correct word") 45 | 46 | self.attempts = 0 47 | while self.attempts < self.max_attempts: 48 | guess = self.player.get_guess() 49 | self.attempts += 1 50 | if self.check_guess(guess, word): 51 | print("Congratulations!!! You've guessed the word correctly.") 52 | self.user.update_stat(won=True) 53 | self.user.save_user() 54 | return # Exit the loop if the user wins 55 | else: 56 | remaining_attempts = self.max_attempts - self.attempts 57 | if remaining_attempts > 0: 58 | print(f"Incorrect guess! You have {remaining_attempts} attempts left. Try again.") 59 | else: 60 | print("Incorrect guess! You have no attempts left.") 61 | 62 | print(f"You've failed! The correct word was: {word}") 63 | self.user.update_stat(won=False) 64 | self.user.save_user() 65 | 66 | def view_profile(self): 67 | print("\n--- Profile Details ---") 68 | print(f"Username: {self.user.username}") 69 | print(f"Wins: {self.user.wins}") 70 | print(f"Losses: {self.user.losses}") 71 | 72 | def update_profile(self): 73 | print("\n--- Update Profile ---") 74 | new_username = input("Enter new username (leave blank to keep current): ").strip() 75 | new_password = input("Enter new password (leave blank to keep current): ").strip() 76 | 77 | if new_username: 78 | self.user.username = new_username 79 | if new_password: 80 | self.user.password = User._hash_password(new_password) 81 | 82 | self.user.save_user() 83 | print("Profile updated successfully!") 84 | 85 | @staticmethod 86 | def check_guess(guess, word): 87 | return guess.lower() == word.lower() 88 | 89 | def authenticate_user(self): 90 | print("Please log in or sign up:") 91 | while True: 92 | choice = input("Do you have an account? (yes/no): ").strip().lower() 93 | if choice == 'yes': 94 | username = input("Enter your username: ").strip() 95 | password = input("Enter your password: ").strip() 96 | user = User.authenticate(username, password) 97 | if user: 98 | print("Login successful!") 99 | self.user = user 100 | break 101 | else: 102 | print("Incorrect username or password. Please try again.") 103 | elif choice == 'no': 104 | username = input("Choose a username: ").strip() 105 | password = input("Choose a password: ").strip() 106 | user = User(username, password) 107 | user.save_user() 108 | print("Account created successfully!") 109 | self.user = user 110 | break 111 | else: 112 | print("Invalid choice. Please enter 'yes' or 'no'.") 113 | -------------------------------------------------------------------------------- /Function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "source": [ 6 | "# Functions in Python" 7 | ], 8 | "metadata": { 9 | "collapsed": false 10 | }, 11 | "id": "3a580c817581d99c" 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "source": [ 16 | "**Define new function using `def()` keyword**" 17 | ], 18 | "metadata": { 19 | "collapsed": false 20 | }, 21 | "id": "d0652570c3a4aa61" 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "Welcome!\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "def welcome():\n", 37 | " print(\"Welcome!\")\n", 38 | " \n", 39 | "welcome()" 40 | ], 41 | "metadata": { 42 | "collapsed": false, 43 | "ExecuteTime": { 44 | "end_time": "2023-10-26T13:30:42.363908Z", 45 | "start_time": "2023-10-26T13:30:42.352957Z" 46 | } 47 | }, 48 | "id": "e99ef0ad5db5d5e0" 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "source": [ 53 | "**_parameter (argument)_ - a value passed into a function for use within the function**" 54 | ], 55 | "metadata": { 56 | "collapsed": false 57 | }, 58 | "id": "1b389846edbc6aeb" 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 2, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "Welcome Wasik\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "def welcome(name):\n", 74 | " print(\"Welcome \" + name)\n", 75 | "\n", 76 | "welcome(\"Wasik\")" 77 | ], 78 | "metadata": { 79 | "collapsed": false, 80 | "ExecuteTime": { 81 | "end_time": "2023-10-26T13:30:42.388847Z", 82 | "start_time": "2023-10-26T13:30:42.356296Z" 83 | } 84 | }, 85 | "id": "f6b03232de4240ce" 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "source": [ 90 | "**_return value_ - the value or variable returned as the end result of a function**" 91 | ], 92 | "metadata": { 93 | "collapsed": false 94 | }, 95 | "id": "40eaa47d315336e7" 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 3, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "30\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "def sum(x, y):\n", 111 | " return x + y\n", 112 | "\n", 113 | "print(sum(10, 20))" 114 | ], 115 | "metadata": { 116 | "collapsed": false, 117 | "ExecuteTime": { 118 | "end_time": "2023-10-26T13:30:42.391120Z", 119 | "start_time": "2023-10-26T13:30:42.362474Z" 120 | } 121 | }, 122 | "id": "4be104c2cd7c30c5" 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "source": [ 127 | "**return multiple values**" 128 | ], 129 | "metadata": { 130 | "collapsed": false 131 | }, 132 | "id": "4c19261889ec82bb" 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 4, 137 | "outputs": [ 138 | { 139 | "name": "stdout", 140 | "output_type": "stream", 141 | "text": [ 142 | "10 20 30\n" 143 | ] 144 | } 145 | ], 146 | "source": [ 147 | "def get_values():\n", 148 | " return 10, 20, 30\n", 149 | "\n", 150 | "value1, value2, value3 = get_values() # storing values in multiple variables\n", 151 | "\n", 152 | "print(value1, value2, value3)" 153 | ], 154 | "metadata": { 155 | "collapsed": false, 156 | "ExecuteTime": { 157 | "end_time": "2023-10-26T13:30:42.391684Z", 158 | "start_time": "2023-10-26T13:30:42.367623Z" 159 | } 160 | }, 161 | "id": "dda641112b76010c" 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "source": [ 166 | "### Create two functions that convert miles to kilometers and kilometers to miles." 167 | ], 168 | "metadata": { 169 | "collapsed": false 170 | }, 171 | "id": "9f4854019cc08808" 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 5, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "length in km: 80.45\n", 182 | "length in mile: 31.05\n" 183 | ] 184 | } 185 | ], 186 | "source": [ 187 | "def convert_km_to_mile(km):\n", 188 | " return km * 0.621\n", 189 | "\n", 190 | "def convert_mile_to_km(mile):\n", 191 | " return mile * 1.609\n", 192 | "\n", 193 | "length = 50\n", 194 | "print(\"length in km:\", convert_mile_to_km(length))\n", 195 | "print(\"length in mile:\",convert_km_to_mile(length))" 196 | ], 197 | "metadata": { 198 | "collapsed": false, 199 | "ExecuteTime": { 200 | "end_time": "2023-10-26T13:30:42.392634Z", 201 | "start_time": "2023-10-26T13:30:42.373200Z" 202 | } 203 | }, 204 | "id": "f72c9c020b4363a5" 205 | } 206 | ], 207 | "metadata": { 208 | "kernelspec": { 209 | "display_name": "Python 3", 210 | "language": "python", 211 | "name": "python3" 212 | }, 213 | "language_info": { 214 | "codemirror_mode": { 215 | "name": "ipython", 216 | "version": 2 217 | }, 218 | "file_extension": ".py", 219 | "mimetype": "text/x-python", 220 | "name": "python", 221 | "nbconvert_exporter": "python", 222 | "pygments_lexer": "ipython2", 223 | "version": "2.7.6" 224 | } 225 | }, 226 | "nbformat": 4, 227 | "nbformat_minor": 5 228 | } 229 | -------------------------------------------------------------------------------- /ConditionalStatements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c9b8ae8a-a85c-4899-a52c-b2600615cc61", 6 | "metadata": {}, 7 | "source": [ 8 | "# Conditional Statements" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "7321e0e9-ead1-4cdc-9f6c-138bd97faf1e", 14 | "metadata": {}, 15 | "source": [ 16 | "- **`if statement` -> used to test specific condition and executes a block of code if true**\n", 17 | "- **`if-else` statement -> else statement will execute if the `if` statement if false**\n", 18 | "- **`elif` statement -> enables us to check multiple conditions and executes the block depending upon the true condition**" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "id": "1de0e8d0-b733-44d9-938f-a23e28efd3e9", 24 | "metadata": {}, 25 | "source": [ 26 | "### `if` statement" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 3, 32 | "id": "8ca0d182-0746-483a-9ec9-ed14c7e76f50", 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "a is less than b\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "a = 10\n", 45 | "b = 20\n", 46 | "\n", 47 | "if a == b:\n", 48 | " print(\"a is equal to b\")\n", 49 | "\n", 50 | "if a > b:\n", 51 | " print(\"a is greater than b\")\n", 52 | "\n", 53 | "if a < b:\n", 54 | " print(\"a is less than b\")" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "f7d561db-8bfc-40dd-8b0e-35453da017d6", 60 | "metadata": {}, 61 | "source": [ 62 | "### `else` statement" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 8, 68 | "id": "15ef8b19-7ae3-411c-bf0f-e3b1ddc6855c", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdin", 73 | "output_type": "stream", 74 | "text": [ 75 | "Please enter a number: 11\n" 76 | ] 77 | }, 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "11 is odd number\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "# determine if a given number is even or odd\n", 88 | "\n", 89 | "a = int(input(\"Please enter a number: \"))\n", 90 | "\n", 91 | "if a % 2 == 0:\n", 92 | " print(f\"{a} is even number\")\n", 93 | "else:\n", 94 | " print(f\"{a} is odd number\")" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "id": "38ec8120-f4c8-4bd8-9268-1e198f40f5ff", 100 | "metadata": {}, 101 | "source": [ 102 | "### `elif` statement" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 10, 108 | "id": "b0e0c222-a84b-48e1-97ce-b205f5d7b408", 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdin", 113 | "output_type": "stream", 114 | "text": [ 115 | "Please enter a number: 25\n" 116 | ] 117 | }, 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "25 is positive number\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "# determine if a given number is positive or negative\n", 128 | "\n", 129 | "x = int(input(\"Please enter a number: \"))\n", 130 | "\n", 131 | "if x == 0:\n", 132 | " print(f\"{x} is neither positive nor negative\")\n", 133 | "elif x > 0:\n", 134 | " print(f\"{x} is positive number\")\n", 135 | "else:\n", 136 | " print(f\"{x} is negative number\")" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "id": "18391c07-211f-46df-902c-351889264247", 142 | "metadata": {}, 143 | "source": [ 144 | "**Create a program that takes a student's score as input and prints their grade.**" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 16, 150 | "id": "549a88c2-69ec-41bc-8a93-0ffcb56fd7ba", 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdin", 155 | "output_type": "stream", 156 | "text": [ 157 | "Please enter your score: 80\n" 158 | ] 159 | }, 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "B+\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "score = int(input(\"Please enter your score: \"))\n", 170 | "\n", 171 | "# As per AIUB grading system\n", 172 | "\n", 173 | "if score > 100 or score < 0:\n", 174 | " print(\"Invalid input\")\n", 175 | "elif score >= 90:\n", 176 | " print(\"A+\")\n", 177 | "elif score >= 85:\n", 178 | " print(\"A\")\n", 179 | "elif score >= 80:\n", 180 | " print(\"B+\")\n", 181 | "elif score >= 75:\n", 182 | " print(\"B\")\n", 183 | "elif score >= 70:\n", 184 | " print(\"C+\")\n", 185 | "elif score >= 65:\n", 186 | " print(\"C\")\n", 187 | "elif score >= 60:\n", 188 | " print(\"D+\")\n", 189 | "elif score >= 50:\n", 190 | " print(\"D\")\n", 191 | "else:\n", 192 | " print(\"F\")" 193 | ] 194 | } 195 | ], 196 | "metadata": { 197 | "kernelspec": { 198 | "display_name": "Python 3 (ipykernel)", 199 | "language": "python", 200 | "name": "python3" 201 | }, 202 | "language_info": { 203 | "codemirror_mode": { 204 | "name": "ipython", 205 | "version": 3 206 | }, 207 | "file_extension": ".py", 208 | "mimetype": "text/x-python", 209 | "name": "python", 210 | "nbconvert_exporter": "python", 211 | "pygments_lexer": "ipython3", 212 | "version": "3.12.0" 213 | } 214 | }, 215 | "nbformat": 4, 216 | "nbformat_minor": 5 217 | } 218 | -------------------------------------------------------------------------------- /modules/os_module.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "source": [ 6 | "# Python OS Module\n", 7 | "The **OS module** in Python provides functions for interacting with the operating system." 8 | ], 9 | "metadata": { 10 | "collapsed": false 11 | }, 12 | "id": "91e25ea38d7e944d" 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "source": [ 17 | "## Importing module" 18 | ], 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "id": "3a5e4d399795e9bc" 23 | }, 24 | { 25 | "cell_type": "code", 26 | "outputs": [ 27 | { 28 | "data": { 29 | "text/plain": "" 30 | }, 31 | "execution_count": 1, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "import os\n", 38 | "\n", 39 | "os" 40 | ], 41 | "metadata": { 42 | "collapsed": false, 43 | "ExecuteTime": { 44 | "end_time": "2024-07-01T16:56:43.386938Z", 45 | "start_time": "2024-07-01T16:56:43.379376Z" 46 | } 47 | }, 48 | "id": "74b20f2166d7230c", 49 | "execution_count": 1 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "source": [ 54 | "## 1. File and Directory Operations\n", 55 | "- `os.open()`\n", 56 | "- `os.close()`\n", 57 | "- `os.closerange()`\n", 58 | "- `os.listdir()`\n", 59 | "- `os.mkdir()`\n", 60 | "- `os.makedirs()`\n", 61 | "- `os.rmdir()`\n", 62 | "- `os.removedirs()`\n", 63 | "- `os.rename()`\n", 64 | "- `os.replace()`\n", 65 | "- `os.renames()`\n", 66 | "- `os.remove()`\n", 67 | "- `os.unlink()`\n", 68 | "- `os.path.exists()`\n", 69 | "- `os.path.isdir()`\n", 70 | "- `os.path.isfile()`\n", 71 | "- `os.path.islink()`\n", 72 | "- `os.path.ismount()`\n", 73 | "- `os.path.join()`\n", 74 | "- `os.path.split()`\n", 75 | "- `os.path.splitext()`\n", 76 | "- `os.path.basename()`\n", 77 | "- `os.path.dirname()`\n", 78 | "- `os.path.getsize()`\n", 79 | "- `os.path.getmtime()`\n", 80 | "- `os.path.getatime()`\n", 81 | "- `os.path.getctime()`" 82 | ], 83 | "metadata": { 84 | "collapsed": false 85 | }, 86 | "id": "d60bf26c44e35a9c" 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "source": [ 91 | "### 1.1 Creating Directories" 92 | ], 93 | "metadata": { 94 | "collapsed": false 95 | }, 96 | "id": "993f69b3a85b918" 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "source": [ 101 | "Checking path existence" 102 | ], 103 | "metadata": { 104 | "collapsed": false 105 | }, 106 | "id": "1765e1d2a94db05b" 107 | }, 108 | { 109 | "cell_type": "code", 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": "True" 114 | }, 115 | "execution_count": 2, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "directory = 'os_module_test_dir'\n", 122 | "os.path.exists(directory)" 123 | ], 124 | "metadata": { 125 | "collapsed": false, 126 | "ExecuteTime": { 127 | "end_time": "2024-07-01T16:56:43.392055Z", 128 | "start_time": "2024-07-01T16:56:43.389029Z" 129 | } 130 | }, 131 | "id": "68425b526225a120", 132 | "execution_count": 2 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "source": [ 137 | "Creating a single directory\n", 138 | "\n", 139 | "`os.mkdir()` creates a single directory." 140 | ], 141 | "metadata": { 142 | "collapsed": false 143 | }, 144 | "id": "51891101162eed8e" 145 | }, 146 | { 147 | "cell_type": "code", 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "Directory already exists.\n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "directory = 'os_module_test_dir'\n", 159 | "if not os.path.exists(directory):\n", 160 | " os.mkdir(directory)\n", 161 | "else:\n", 162 | " print(f'Directory already exists.')" 163 | ], 164 | "metadata": { 165 | "collapsed": false, 166 | "ExecuteTime": { 167 | "end_time": "2024-07-01T16:56:43.398195Z", 168 | "start_time": "2024-07-01T16:56:43.393958Z" 169 | } 170 | }, 171 | "id": "c7fdd3db69975288", 172 | "execution_count": 3 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "source": [ 177 | "Creating multiple directories\n", 178 | "\n", 179 | "`os.makedirs()` creates multiple directories, including parent directories if they don't exist." 180 | ], 181 | "metadata": { 182 | "collapsed": false 183 | }, 184 | "id": "91fe817b14df1f1f" 185 | }, 186 | { 187 | "cell_type": "code", 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "Directory already exists.\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "directory = 'os_module_test_dir/parent_dir/child_dir'\n", 199 | "if not os.path.exists(directory):\n", 200 | " os.makedirs(directory)\n", 201 | "else:\n", 202 | " print('Directory already exists.')" 203 | ], 204 | "metadata": { 205 | "collapsed": false, 206 | "ExecuteTime": { 207 | "end_time": "2024-07-01T16:56:43.404058Z", 208 | "start_time": "2024-07-01T16:56:43.401129Z" 209 | } 210 | }, 211 | "id": "6646139fc9463aaf", 212 | "execution_count": 4 213 | } 214 | ], 215 | "metadata": { 216 | "kernelspec": { 217 | "display_name": "Python 3", 218 | "language": "python", 219 | "name": "python3" 220 | }, 221 | "language_info": { 222 | "codemirror_mode": { 223 | "name": "ipython", 224 | "version": 2 225 | }, 226 | "file_extension": ".py", 227 | "mimetype": "text/x-python", 228 | "name": "python", 229 | "nbconvert_exporter": "python", 230 | "pygments_lexer": "ipython2", 231 | "version": "2.7.6" 232 | } 233 | }, 234 | "nbformat": 4, 235 | "nbformat_minor": 5 236 | } 237 | -------------------------------------------------------------------------------- /Variable.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "02ff4ef5-b8cf-4cc5-bb93-a8711bfd8dfc", 6 | "metadata": {}, 7 | "source": [ 8 | "# Python Variables" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "7f015424-9b1a-4383-acae-3374b1f75965", 14 | "metadata": {}, 15 | "source": [ 16 | "Variables are containers for storing data values." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "88a2735c-9d1a-4fcf-b3c6-99c188a9a34e", 22 | "metadata": { 23 | "tags": [] 24 | }, 25 | "source": [ 26 | "### Declaring Variables\n", 27 | "Python has no command for declaring a variable." 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "id": "db9f6a1c-f4bd-40b1-b730-cff6d0a160a3", 34 | "metadata": { 35 | "tags": [] 36 | }, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "4\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "a = 4 # int\n", 48 | "print(a)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "c5a690fe-1557-414d-a4dd-f811d81d783c", 54 | "metadata": { 55 | "tags": [] 56 | }, 57 | "source": [ 58 | "### Print the datatype of variable using type() function" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "id": "fe7f010a-ec48-4c3f-a9ff-13c8686d20a5", 65 | "metadata": { 66 | "tags": [] 67 | }, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "print(type(a))" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 5, 84 | "id": "215fde13-416a-414f-96c0-66ae43f5ce41", 85 | "metadata": { 86 | "tags": [] 87 | }, 88 | "outputs": [], 89 | "source": [ 90 | "b = 6.9 # decimal\n", 91 | "c = True # boolean\n", 92 | "d = \"10\" # string \n", 93 | "# String variables can be declared either by using single or double quotes" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 6, 99 | "id": "27d7ce44-9ea5-4cc7-a8e2-3b1b3042d9da", 100 | "metadata": { 101 | "tags": [] 102 | }, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "\n", 109 | "\n", 110 | "\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "print(type(b))\n", 116 | "print(type(c))\n", 117 | "print(type(d))" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "id": "bf405a9f-6bb3-4b9c-b65d-2a5d9d5a913a", 123 | "metadata": {}, 124 | "source": [ 125 | "## Casting" 126 | ] 127 | }, 128 | { 129 | "cell_type": "raw", 130 | "id": "f9f444d3-df22-4175-add9-dab9b30a3ec8", 131 | "metadata": {}, 132 | "source": [ 133 | "Casting, also known as type conversion, is a process that converts a variable's data type into another data type.\n", 134 | "Specify the data type of a variable using casting." 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "id": "1e3cbce4-7407-4744-87d0-fa7445c67a29", 140 | "metadata": {}, 141 | "source": [ 142 | "##### Integer to string" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 7, 148 | "id": "92389106-d1a8-4d31-84a8-c289b457da94", 149 | "metadata": { 150 | "tags": [] 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "4\n", 158 | "\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "x = str(a) # a = 4\n", 164 | "print(x)\n", 165 | "print(type(x))" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "id": "ce50fad6-a1c2-494b-a864-cbeaff2c674e", 171 | "metadata": {}, 172 | "source": [ 173 | "##### string to integer" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 8, 179 | "id": "18f9ed26-9c4e-4c13-8225-27e55acdc916", 180 | "metadata": { 181 | "tags": [] 182 | }, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "10\n", 189 | "\n" 190 | ] 191 | } 192 | ], 193 | "source": [ 194 | "y = int(d) # d = \"10\"\n", 195 | "print(y)\n", 196 | "print(type(y))" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "id": "b03ad0fe-9114-4e86-a693-fbc627b0c72f", 202 | "metadata": {}, 203 | "source": [ 204 | "##### float to integer" 205 | ] 206 | }, 207 | { 208 | "cell_type": "raw", 209 | "id": "4c2cd08f-6db0-4c42-9e81-1079d4fc07d0", 210 | "metadata": {}, 211 | "source": [ 212 | "Convert a float to an int always results in a data loss." 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "id": "77d5b7f7-da35-4d3c-9bc4-03f25ee9db9d", 219 | "metadata": { 220 | "tags": [] 221 | }, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "6\n", 228 | "\n" 229 | ] 230 | } 231 | ], 232 | "source": [ 233 | "z = int(6.9)\n", 234 | "print(z)\n", 235 | "print(type(z))" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "id": "7d993267-a2f5-4011-a3a6-b67adc335300", 241 | "metadata": {}, 242 | "source": [ 243 | "##### integer to float" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 11, 249 | "id": "3af7ca8b-ca5c-465d-a8d6-071c34077add", 250 | "metadata": { 251 | "tags": [] 252 | }, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "10.0\n", 259 | "\n" 260 | ] 261 | } 262 | ], 263 | "source": [ 264 | "f = float(10)\n", 265 | "print(f)\n", 266 | "print(type(f))" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "id": "262da6ee-16e7-4367-848f-19127b5fe145", 272 | "metadata": {}, 273 | "source": [ 274 | "## Variable Names\n", 275 | "- A variable name must start with a letter or the underscore character\n", 276 | "- A variable name cannot start with a number\n", 277 | "- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )\n", 278 | "- Variable names are case-sensitive\n", 279 | "- A variable name cannot be any of the Python keywords." 280 | ] 281 | }, 282 | { 283 | "cell_type": "markdown", 284 | "id": "bfc262f3-3851-47df-9c0b-d573ac467600", 285 | "metadata": { 286 | "tags": [] 287 | }, 288 | "source": [ 289 | "## Multi Words Variable Names\n", 290 | "- Camel Case : myVariableName = \"John\"\n", 291 | "- Pascal Case : MyVariableName = \"John\"\n", 292 | "- Snake Case : my_variable_name = \"John\"" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "id": "522900d7-5d06-4e07-8100-22fea9d96b51", 298 | "metadata": { 299 | "tags": [] 300 | }, 301 | "source": [ 302 | "## Assign Multiple Values" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "id": "c76d66a7-b12c-4c42-a8e5-560feb7d67c9", 308 | "metadata": { 309 | "tags": [] 310 | }, 311 | "source": [ 312 | "#### Many values to multiple variables" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 15, 318 | "id": "f9f92c6f-46ed-4d1d-b5ef-763f6333311f", 319 | "metadata": { 320 | "tags": [] 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "James\n", 328 | "Bond\n", 329 | "7\n" 330 | ] 331 | } 332 | ], 333 | "source": [ 334 | "m, n, o = \"James\", \"Bond\", 7\n", 335 | "print(m)\n", 336 | "print(n)\n", 337 | "print(o)" 338 | ] 339 | }, 340 | { 341 | "cell_type": "markdown", 342 | "id": "e59cd570-06e4-4de8-a296-57f07981b8fb", 343 | "metadata": { 344 | "tags": [] 345 | }, 346 | "source": [ 347 | "#### One value to multiple variables" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 17, 353 | "id": "f498196d-ec18-485b-a834-0f6202f91e85", 354 | "metadata": { 355 | "tags": [] 356 | }, 357 | "outputs": [ 358 | { 359 | "name": "stdout", 360 | "output_type": "stream", 361 | "text": [ 362 | "Hello, World!\n", 363 | "Hello, World!\n", 364 | "Hello, World!\n" 365 | ] 366 | } 367 | ], 368 | "source": [ 369 | "m = n = o = \"Hello, World!\"\n", 370 | "print(m)\n", 371 | "print(n)\n", 372 | "print(o)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "id": "cb8782cf-f891-4689-9fbb-c94c127e9210", 378 | "metadata": {}, 379 | "source": [ 380 | "#### Unpack a Collection" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 18, 386 | "id": "aa38041b-9ce7-43cf-bfa2-094b403518a8", 387 | "metadata": { 388 | "tags": [] 389 | }, 390 | "outputs": [ 391 | { 392 | "name": "stdout", 393 | "output_type": "stream", 394 | "text": [ 395 | "wasik\n", 396 | "ahmed\n", 397 | "apon\n" 398 | ] 399 | } 400 | ], 401 | "source": [ 402 | "names = [\"wasik\", \"ahmed\", \"apon\"]\n", 403 | "m, n, o = names\n", 404 | "print(m)\n", 405 | "print(n)\n", 406 | "print(o)" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "id": "e567fe1b-be06-4829-adf0-a348435c1822", 412 | "metadata": {}, 413 | "source": [ 414 | "## Global variables\n", 415 | "Variables that are created outside of a function are known as global variables." 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 19, 421 | "id": "77fc9a1f-c90b-4f7d-a04f-e49d344ddd62", 422 | "metadata": { 423 | "tags": [] 424 | }, 425 | "outputs": [ 426 | { 427 | "name": "stdout", 428 | "output_type": "stream", 429 | "text": [ 430 | "HelloWorld\n" 431 | ] 432 | } 433 | ], 434 | "source": [ 435 | "x = \"World\"\n", 436 | "\n", 437 | "def myFunction():\n", 438 | " print(\"Hello\" + x)\n", 439 | "\n", 440 | "myFunction()" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 22, 446 | "id": "d40f0b9e-c4e3-4d53-9305-531b1853ae4a", 447 | "metadata": { 448 | "tags": [] 449 | }, 450 | "outputs": [ 451 | { 452 | "name": "stdout", 453 | "output_type": "stream", 454 | "text": [ 455 | "Hello Apon\n", 456 | "Hello Wasik\n" 457 | ] 458 | } 459 | ], 460 | "source": [ 461 | "x = \"Wasik\" # global\n", 462 | "\n", 463 | "def myFunction():\n", 464 | " x = \"Apon\" # local\n", 465 | " print(\"Hello\" , x)\n", 466 | "\n", 467 | "myFunction()\n", 468 | "\n", 469 | "print(\"Hello\" , x)" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "id": "993fb02c-dd22-4b51-a730-eecfebdd4d89", 475 | "metadata": {}, 476 | "source": [ 477 | "To change the value of a global variable inside a function, use the global keyword" 478 | ] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "execution_count": 25, 483 | "id": "f8f9d1db-ac01-4207-af8f-959b7778de2a", 484 | "metadata": { 485 | "tags": [] 486 | }, 487 | "outputs": [ 488 | { 489 | "name": "stdout", 490 | "output_type": "stream", 491 | "text": [ 492 | "Hello Wasik\n" 493 | ] 494 | } 495 | ], 496 | "source": [ 497 | "x = \"World\"\n", 498 | "\n", 499 | "def myFunction():\n", 500 | " global x\n", 501 | " x = \"Wasik\"\n", 502 | "\n", 503 | "myFunction()\n", 504 | " \n", 505 | "print(\"Hello \" + x)" 506 | ] 507 | } 508 | ], 509 | "metadata": { 510 | "kernelspec": { 511 | "display_name": "Python 3 (ipykernel)", 512 | "language": "python", 513 | "name": "python3" 514 | }, 515 | "language_info": { 516 | "codemirror_mode": { 517 | "name": "ipython", 518 | "version": 3 519 | }, 520 | "file_extension": ".py", 521 | "mimetype": "text/x-python", 522 | "name": "python", 523 | "nbconvert_exporter": "python", 524 | "pygments_lexer": "ipython3", 525 | "version": "3.11.3" 526 | } 527 | }, 528 | "nbformat": 4, 529 | "nbformat_minor": 5 530 | } 531 | -------------------------------------------------------------------------------- /lambda_function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "metadata": {}, 5 | "cell_type": "markdown", 6 | "source": "# Python Lambda Function", 7 | "id": "de0658f197fd30a7" 8 | }, 9 | { 10 | "metadata": {}, 11 | "cell_type": "markdown", 12 | "source": [ 13 | "## What is a Lambda Function?\n", 14 | "\n", 15 | "A lambda function is a small anonymous function.\n", 16 | "\n", 17 | "A lambda function can take any number of arguments, but can only have one expression.\n", 18 | "\n", 19 | "Lambda functions are efficient whenever you want to create a function that will only contain simple expressions – that is, expressions that are usually a single line of a statement. They're also useful when you want to use the function once." 20 | ], 21 | "id": "f237d9f33ad26566" 22 | }, 23 | { 24 | "metadata": {}, 25 | "cell_type": "markdown", 26 | "source": [ 27 | "## Defining a Lambda Function\n", 28 | "\n", 29 | "Syntax:\n", 30 | "```\n", 31 | "lambda argument(s) : expression\n", 32 | "```\n", 33 | "\n", 34 | "1. `lambda`: keyword in Python for defining the anonymous function.\n", 35 | "2. `argument(s)`: a placeholder, that is a variable that will be used to hold the value you want to pass into the function expression.\n", 36 | "3. `expression`: the code that you want to execute in the lambda function." 37 | ], 38 | "id": "7ed0d467e78f3fc3" 39 | }, 40 | { 41 | "metadata": { 42 | "ExecuteTime": { 43 | "end_time": "2024-07-30T16:23:26.933181Z", 44 | "start_time": "2024-07-30T16:23:26.927583Z" 45 | } 46 | }, 47 | "cell_type": "code", 48 | "source": [ 49 | "# Regular function that returns twice the number that is passed into the function.\n", 50 | "def double(x):\n", 51 | " return x * 2\n", 52 | "\n", 53 | "double(5)" 54 | ], 55 | "id": "6acc9f8183cd33e7", 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "10" 61 | ] 62 | }, 63 | "execution_count": 1, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "execution_count": 1 69 | }, 70 | { 71 | "metadata": { 72 | "ExecuteTime": { 73 | "end_time": "2024-07-30T16:23:26.937865Z", 74 | "start_time": "2024-07-30T16:23:26.934553Z" 75 | } 76 | }, 77 | "cell_type": "code", 78 | "source": [ 79 | "# using a lambda function\n", 80 | "double_lambda = lambda x: x * 2\n", 81 | "double_lambda(5)" 82 | ], 83 | "id": "29b661dac229ff59", 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/plain": [ 88 | "10" 89 | ] 90 | }, 91 | "execution_count": 2, 92 | "metadata": {}, 93 | "output_type": "execute_result" 94 | } 95 | ], 96 | "execution_count": 2 97 | }, 98 | { 99 | "metadata": { 100 | "ExecuteTime": { 101 | "end_time": "2024-07-30T16:23:26.941468Z", 102 | "start_time": "2024-07-30T16:23:26.938596Z" 103 | } 104 | }, 105 | "cell_type": "code", 106 | "source": "(lambda x: x * 2)(5)", 107 | "id": "f4079cd7ae1e6347", 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "10" 113 | ] 114 | }, 115 | "execution_count": 3, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "execution_count": 3 121 | }, 122 | { 123 | "metadata": { 124 | "ExecuteTime": { 125 | "end_time": "2024-07-30T16:23:26.945726Z", 126 | "start_time": "2024-07-30T16:23:26.942535Z" 127 | } 128 | }, 129 | "cell_type": "code", 130 | "source": [ 131 | "# using lambda function to add 3 numbers\n", 132 | "add = lambda x, y, z: x + y + z\n", 133 | "add(10, 20, 30)" 134 | ], 135 | "id": "9ce6aab2c5da0da8", 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/plain": [ 140 | "60" 141 | ] 142 | }, 143 | "execution_count": 4, 144 | "metadata": {}, 145 | "output_type": "execute_result" 146 | } 147 | ], 148 | "execution_count": 4 149 | }, 150 | { 151 | "metadata": {}, 152 | "cell_type": "markdown", 153 | "source": [ 154 | "## Common Use Cases for Lambda Functions\n", 155 | "\n", 156 | "### Using a Lambda Function with Iterables\n", 157 | "\n", 158 | "Lambda functions can be used in conjunction with two common functions: `filter()` and `map()`." 159 | ], 160 | "id": "d97b0472f4a72d09" 161 | }, 162 | { 163 | "metadata": {}, 164 | "cell_type": "markdown", 165 | "source": [ 166 | "### `filter()` function\n", 167 | "\n", 168 | "syntax:\n", 169 | "```\n", 170 | "filter(function, iterable)\n", 171 | "```\n", 172 | "a filter function requires another function that contains the expression or operations that will be performed on the iterable." 173 | ], 174 | "id": "36feb3ed139e23d2" 175 | }, 176 | { 177 | "metadata": { 178 | "ExecuteTime": { 179 | "end_time": "2024-07-30T16:23:26.950378Z", 180 | "start_time": "2024-07-30T16:23:26.947447Z" 181 | } 182 | }, 183 | "cell_type": "code", 184 | "source": [ 185 | "# using lambda function to filter out the even numbers in a list\n", 186 | "nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 187 | "\n", 188 | "even_nums = filter(lambda x: x % 2 == 0, nums)\n", 189 | "even_nums" 190 | ], 191 | "id": "6ac7ca23ae9aade0", 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "" 197 | ] 198 | }, 199 | "execution_count": 5, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "execution_count": 5 205 | }, 206 | { 207 | "metadata": { 208 | "ExecuteTime": { 209 | "end_time": "2024-07-30T16:23:26.954189Z", 210 | "start_time": "2024-07-30T16:23:26.951228Z" 211 | } 212 | }, 213 | "cell_type": "code", 214 | "source": [ 215 | "# converting filter object to list\n", 216 | "even_nums = list(even_nums)\n", 217 | "even_nums" 218 | ], 219 | "id": "4580ff5a91ac8425", 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "[2, 4, 6, 8, 10]" 225 | ] 226 | }, 227 | "execution_count": 6, 228 | "metadata": {}, 229 | "output_type": "execute_result" 230 | } 231 | ], 232 | "execution_count": 6 233 | }, 234 | { 235 | "metadata": {}, 236 | "cell_type": "markdown", 237 | "source": [ 238 | "### `map()` function\n", 239 | "\n", 240 | "syntax:\n", 241 | "```\n", 242 | "map(function, iterable)\n", 243 | "```" 244 | ], 245 | "id": "1fcf549cfeebb90" 246 | }, 247 | { 248 | "metadata": { 249 | "ExecuteTime": { 250 | "end_time": "2024-07-30T16:23:26.957663Z", 251 | "start_time": "2024-07-30T16:23:26.954868Z" 252 | } 253 | }, 254 | "cell_type": "code", 255 | "source": [ 256 | "# using lambda function to modify every value in a list\n", 257 | "nums = [1, 2, 3 ,4 ,5]\n", 258 | "\n", 259 | "# raising all values in the list to the power of 2\n", 260 | "squared_nums = list(map(lambda x: pow(x, 2), nums))\n", 261 | "squared_nums" 262 | ], 263 | "id": "344660a7f5a1d6ce", 264 | "outputs": [ 265 | { 266 | "data": { 267 | "text/plain": [ 268 | "[1, 4, 9, 16, 25]" 269 | ] 270 | }, 271 | "execution_count": 7, 272 | "metadata": {}, 273 | "output_type": "execute_result" 274 | } 275 | ], 276 | "execution_count": 7 277 | }, 278 | { 279 | "metadata": {}, 280 | "cell_type": "markdown", 281 | "source": [ 282 | "### Pandas Series\n", 283 | "\n", 284 | "A series is a data frame column in pandas. \n", 285 | "Using lambda function all the values is a series can be manipulated." 286 | ], 287 | "id": "9ec41f6b26e69010" 288 | }, 289 | { 290 | "metadata": { 291 | "ExecuteTime": { 292 | "end_time": "2024-07-30T16:23:27.454713Z", 293 | "start_time": "2024-07-30T16:23:26.958305Z" 294 | } 295 | }, 296 | "cell_type": "code", 297 | "source": [ 298 | "# importing pandas module\n", 299 | "import pandas as pd\n", 300 | "\n", 301 | "df = pd.DataFrame({\n", 302 | " 'name': ['WASIK', 'AHMED', 'APON'],\n", 303 | " 'score': [20, 30, 40]\n", 304 | "})\n", 305 | "df" 306 | ], 307 | "id": "4ada8a90df4c7f6", 308 | "outputs": [ 309 | { 310 | "data": { 311 | "text/plain": [ 312 | " name score\n", 313 | "0 WASIK 20\n", 314 | "1 AHMED 30\n", 315 | "2 APON 40" 316 | ], 317 | "text/html": [ 318 | "
\n", 319 | "\n", 332 | "\n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | "
namescore
0WASIK20
1AHMED30
2APON40
\n", 358 | "
" 359 | ] 360 | }, 361 | "execution_count": 8, 362 | "metadata": {}, 363 | "output_type": "execute_result" 364 | } 365 | ], 366 | "execution_count": 8 367 | }, 368 | { 369 | "metadata": { 370 | "ExecuteTime": { 371 | "end_time": "2024-07-30T16:23:27.459288Z", 372 | "start_time": "2024-07-30T16:23:27.455396Z" 373 | } 374 | }, 375 | "cell_type": "code", 376 | "source": [ 377 | "# applying lambda function to convert all names to lowercase\n", 378 | "df['lower_case_name'] = df['name'].apply(lambda x: x.lower())\n", 379 | "df" 380 | ], 381 | "id": "50d1abd159ec437c", 382 | "outputs": [ 383 | { 384 | "data": { 385 | "text/plain": [ 386 | " name score lower_case_name\n", 387 | "0 WASIK 20 wasik\n", 388 | "1 AHMED 30 ahmed\n", 389 | "2 APON 40 apon" 390 | ], 391 | "text/html": [ 392 | "
\n", 393 | "\n", 406 | "\n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | "
namescorelower_case_name
0WASIK20wasik
1AHMED30ahmed
2APON40apon
\n", 436 | "
" 437 | ] 438 | }, 439 | "execution_count": 9, 440 | "metadata": {}, 441 | "output_type": "execute_result" 442 | } 443 | ], 444 | "execution_count": 9 445 | } 446 | ], 447 | "metadata": { 448 | "kernelspec": { 449 | "display_name": "Python 3", 450 | "language": "python", 451 | "name": "python3" 452 | }, 453 | "language_info": { 454 | "codemirror_mode": { 455 | "name": "ipython", 456 | "version": 2 457 | }, 458 | "file_extension": ".py", 459 | "mimetype": "text/x-python", 460 | "name": "python", 461 | "nbconvert_exporter": "python", 462 | "pygments_lexer": "ipython2", 463 | "version": "2.7.6" 464 | } 465 | }, 466 | "nbformat": 4, 467 | "nbformat_minor": 5 468 | } 469 | -------------------------------------------------------------------------------- /modules/deque.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "metadata": {}, 5 | "cell_type": "markdown", 6 | "source": [ 7 | "# **collections.deque** Module\n", 8 | "\n", 9 | "A list-like sequence optimized for data accesses near its endpoints." 10 | ], 11 | "id": "3876854808bc4b47" 12 | }, 13 | { 14 | "metadata": {}, 15 | "cell_type": "markdown", 16 | "source": "## Import module", 17 | "id": "7c46cb85da521297" 18 | }, 19 | { 20 | "metadata": { 21 | "ExecuteTime": { 22 | "end_time": "2024-07-21T17:16:37.763214Z", 23 | "start_time": "2024-07-21T17:16:37.760884Z" 24 | } 25 | }, 26 | "cell_type": "code", 27 | "source": "from collections import deque", 28 | "id": "a1538441d213482", 29 | "outputs": [], 30 | "execution_count": 1 31 | }, 32 | { 33 | "metadata": {}, 34 | "cell_type": "markdown", 35 | "source": [ 36 | "## Methods\n", 37 | "\n", 38 | "- **`append(...)`** -> Add an element to the right side of the deque.\n", 39 | "- **`appendleft(...)`** -> Add an element to the left side of the deque.\n", 40 | "- **`clear(...)`** -> Remove all elements from the deque.\n", 41 | "- **`copy(...)`** -> Return a shallow copy of a deque.\n", 42 | "- **`count(...)`** -> D.count(value) -- return number of occurrences of value.\n", 43 | "- **`extend(...)`** -> Extend the right side of the deque with elements from the iterable.\n", 44 | "- **`extendleft(...)`** -> Extend the left side of the deque with elements from the iterable\n", 45 | "- **`index(...)`** -> D.index(value, [start, [stop]]) -- return first index of value. Raises ValueError if the value is not present.\n", 46 | "- **`insert(...)`** -> D.insert(index, object) -- insert object before index.\n", 47 | "- **`pop(...)`** -> Remove and return the rightmost element.\n", 48 | "- **`popleft(...)`** -> Remove and return the leftmost element.\n", 49 | "- **`remove(...)`** -> D.remove(value) -- remove first occurrence of value.\n", 50 | "- **`reverse(...)`** -> D.reverse() -- reverse _IN PLACE_.\n", 51 | "- **`rotate(...)`** -> Rotate the deque n steps to the right (default n=1). If n is negative, rotates left." 52 | ], 53 | "id": "fcffd08ca70da376" 54 | }, 55 | { 56 | "metadata": {}, 57 | "cell_type": "markdown", 58 | "source": "## Creating **deque**", 59 | "id": "8b9ad95f442725dc" 60 | }, 61 | { 62 | "metadata": { 63 | "ExecuteTime": { 64 | "end_time": "2024-07-21T17:16:37.767355Z", 65 | "start_time": "2024-07-21T17:16:37.764133Z" 66 | } 67 | }, 68 | "cell_type": "code", 69 | "source": [ 70 | "d = deque()\n", 71 | "d, type(d)" 72 | ], 73 | "id": "ec5aa89465e83fb2", 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "(deque([]), collections.deque)" 79 | ] 80 | }, 81 | "execution_count": 2, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "execution_count": 2 87 | }, 88 | { 89 | "metadata": { 90 | "ExecuteTime": { 91 | "end_time": "2024-07-21T17:16:37.770039Z", 92 | "start_time": "2024-07-21T17:16:37.767872Z" 93 | } 94 | }, 95 | "cell_type": "code", 96 | "source": [ 97 | "# creating deque from list\n", 98 | "l = [10, 20, 30, 40, 50]\n", 99 | "d = deque(l)\n", 100 | "d, type(d)" 101 | ], 102 | "id": "ba265e561ad28937", 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "(deque([10, 20, 30, 40, 50]), collections.deque)" 108 | ] 109 | }, 110 | "execution_count": 3, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "execution_count": 3 116 | }, 117 | { 118 | "metadata": { 119 | "ExecuteTime": { 120 | "end_time": "2024-07-21T17:16:37.773109Z", 121 | "start_time": "2024-07-21T17:16:37.770781Z" 122 | } 123 | }, 124 | "cell_type": "code", 125 | "source": [ 126 | "# creating deque from dict.keys()\n", 127 | "dictionary = {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}\n", 128 | "d = deque(dictionary)\n", 129 | "d" 130 | ], 131 | "id": "834ee8aad7f5d4c6", 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "deque(['a', 'b', 'c', 'd', 'e'])" 137 | ] 138 | }, 139 | "execution_count": 4, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "execution_count": 4 145 | }, 146 | { 147 | "metadata": { 148 | "ExecuteTime": { 149 | "end_time": "2024-07-21T17:16:37.777287Z", 150 | "start_time": "2024-07-21T17:16:37.774437Z" 151 | } 152 | }, 153 | "cell_type": "code", 154 | "source": [ 155 | "# creating deque from dict.values()\n", 156 | "dictionary = {'a': 60, 'b': 20, 'c': 30, 'd': 40, 'e': 50}\n", 157 | "d = deque(dictionary.values())\n", 158 | "d" 159 | ], 160 | "id": "542c5811446efe6d", 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "deque([60, 20, 30, 40, 50])" 166 | ] 167 | }, 168 | "execution_count": 5, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "execution_count": 5 174 | }, 175 | { 176 | "metadata": { 177 | "ExecuteTime": { 178 | "end_time": "2024-07-21T17:16:37.779947Z", 179 | "start_time": "2024-07-21T17:16:37.777871Z" 180 | } 181 | }, 182 | "cell_type": "code", 183 | "source": [ 184 | "# creating deque from set\n", 185 | "s = {10, 20, 30, 40, 50, 100, 200}\n", 186 | "d = deque(s)\n", 187 | "s, d" 188 | ], 189 | "id": "1d5c7b5af267ce08", 190 | "outputs": [ 191 | { 192 | "data": { 193 | "text/plain": [ 194 | "({10, 20, 30, 40, 50, 100, 200}, deque([50, 100, 20, 40, 10, 30, 200]))" 195 | ] 196 | }, 197 | "execution_count": 6, 198 | "metadata": {}, 199 | "output_type": "execute_result" 200 | } 201 | ], 202 | "execution_count": 6 203 | }, 204 | { 205 | "metadata": { 206 | "ExecuteTime": { 207 | "end_time": "2024-07-21T17:16:37.782910Z", 208 | "start_time": "2024-07-21T17:16:37.780573Z" 209 | } 210 | }, 211 | "cell_type": "code", 212 | "source": [ 213 | "# creating deque from tuple\n", 214 | "t = (10, 20)\n", 215 | "d = deque(t)\n", 216 | "d" 217 | ], 218 | "id": "11c6f38fafe21c6f", 219 | "outputs": [ 220 | { 221 | "data": { 222 | "text/plain": [ 223 | "deque([10, 20])" 224 | ] 225 | }, 226 | "execution_count": 7, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "execution_count": 7 232 | }, 233 | { 234 | "metadata": { 235 | "ExecuteTime": { 236 | "end_time": "2024-07-21T17:16:37.786216Z", 237 | "start_time": "2024-07-21T17:16:37.784048Z" 238 | } 239 | }, 240 | "cell_type": "code", 241 | "source": [ 242 | "# creating deque from list of tuples\n", 243 | "l = [(10, 20), (30, 40), (50, 60)]\n", 244 | "d = deque(l)\n", 245 | "d" 246 | ], 247 | "id": "b11c26a57c2a0621", 248 | "outputs": [ 249 | { 250 | "data": { 251 | "text/plain": [ 252 | "deque([(10, 20), (30, 40), (50, 60)])" 253 | ] 254 | }, 255 | "execution_count": 8, 256 | "metadata": {}, 257 | "output_type": "execute_result" 258 | } 259 | ], 260 | "execution_count": 8 261 | }, 262 | { 263 | "metadata": {}, 264 | "cell_type": "markdown", 265 | "source": "## **`append(...)`**", 266 | "id": "7f91bedb70cb4af0" 267 | }, 268 | { 269 | "metadata": { 270 | "ExecuteTime": { 271 | "end_time": "2024-07-21T17:16:37.789367Z", 272 | "start_time": "2024-07-21T17:16:37.787067Z" 273 | } 274 | }, 275 | "cell_type": "code", 276 | "source": [ 277 | "d.append(10)\n", 278 | "d.append(20)\n", 279 | "d.append(30)\n", 280 | "d" 281 | ], 282 | "id": "22275ba17df49d12", 283 | "outputs": [ 284 | { 285 | "data": { 286 | "text/plain": [ 287 | "deque([(10, 20), (30, 40), (50, 60), 10, 20, 30])" 288 | ] 289 | }, 290 | "execution_count": 9, 291 | "metadata": {}, 292 | "output_type": "execute_result" 293 | } 294 | ], 295 | "execution_count": 9 296 | }, 297 | { 298 | "metadata": {}, 299 | "cell_type": "markdown", 300 | "source": "## **`appendleft(...)`**", 301 | "id": "7ae57b432affe301" 302 | }, 303 | { 304 | "metadata": { 305 | "ExecuteTime": { 306 | "end_time": "2024-07-21T17:16:37.821254Z", 307 | "start_time": "2024-07-21T17:16:37.818821Z" 308 | } 309 | }, 310 | "cell_type": "code", 311 | "source": [ 312 | "d = deque()\n", 313 | "d.appendleft(40)\n", 314 | "d.appendleft(50)\n", 315 | "d.appendleft(60)\n", 316 | "d" 317 | ], 318 | "id": "ee6f7e9f1c98df8", 319 | "outputs": [ 320 | { 321 | "data": { 322 | "text/plain": [ 323 | "deque([60, 50, 40])" 324 | ] 325 | }, 326 | "execution_count": 10, 327 | "metadata": {}, 328 | "output_type": "execute_result" 329 | } 330 | ], 331 | "execution_count": 10 332 | }, 333 | { 334 | "metadata": {}, 335 | "cell_type": "markdown", 336 | "source": "## **`clear(...)`**", 337 | "id": "973fa2d7a879b324" 338 | }, 339 | { 340 | "metadata": { 341 | "ExecuteTime": { 342 | "end_time": "2024-07-21T17:16:37.839129Z", 343 | "start_time": "2024-07-21T17:16:37.835656Z" 344 | } 345 | }, 346 | "cell_type": "code", 347 | "source": [ 348 | "d = deque([20, 40, 60])\n", 349 | "print(d)\n", 350 | "d.clear()\n", 351 | "d" 352 | ], 353 | "id": "91dae33e07dac35b", 354 | "outputs": [ 355 | { 356 | "name": "stdout", 357 | "output_type": "stream", 358 | "text": [ 359 | "deque([20, 40, 60])\n" 360 | ] 361 | }, 362 | { 363 | "data": { 364 | "text/plain": [ 365 | "deque([])" 366 | ] 367 | }, 368 | "execution_count": 11, 369 | "metadata": {}, 370 | "output_type": "execute_result" 371 | } 372 | ], 373 | "execution_count": 11 374 | }, 375 | { 376 | "metadata": {}, 377 | "cell_type": "markdown", 378 | "source": "## **`copy(...)`**", 379 | "id": "9ead9316e4237989" 380 | }, 381 | { 382 | "metadata": { 383 | "ExecuteTime": { 384 | "end_time": "2024-07-21T17:16:37.845259Z", 385 | "start_time": "2024-07-21T17:16:37.842671Z" 386 | } 387 | }, 388 | "cell_type": "code", 389 | "source": [ 390 | "d = deque([100, 200, 300, 400, 500])\n", 391 | "copy_d = d.copy()\n", 392 | "d, copy_d" 393 | ], 394 | "id": "c296947c295c0cf3", 395 | "outputs": [ 396 | { 397 | "data": { 398 | "text/plain": [ 399 | "(deque([100, 200, 300, 400, 500]), deque([100, 200, 300, 400, 500]))" 400 | ] 401 | }, 402 | "execution_count": 12, 403 | "metadata": {}, 404 | "output_type": "execute_result" 405 | } 406 | ], 407 | "execution_count": 12 408 | }, 409 | { 410 | "metadata": {}, 411 | "cell_type": "markdown", 412 | "source": "## **`count(...)`**", 413 | "id": "7275cca8813bb8ac" 414 | }, 415 | { 416 | "metadata": { 417 | "ExecuteTime": { 418 | "end_time": "2024-07-21T17:16:37.914826Z", 419 | "start_time": "2024-07-21T17:16:37.912361Z" 420 | } 421 | }, 422 | "cell_type": "code", 423 | "source": [ 424 | "d = deque([10, 20, 30, 10, 40, 10, 50, 10])\n", 425 | "d.count(10)" 426 | ], 427 | "id": "c4f88c3e084e1dc7", 428 | "outputs": [ 429 | { 430 | "data": { 431 | "text/plain": [ 432 | "4" 433 | ] 434 | }, 435 | "execution_count": 13, 436 | "metadata": {}, 437 | "output_type": "execute_result" 438 | } 439 | ], 440 | "execution_count": 13 441 | }, 442 | { 443 | "metadata": {}, 444 | "cell_type": "markdown", 445 | "source": "## **`extend(...)`**", 446 | "id": "ab4a42744c4b7c9" 447 | }, 448 | { 449 | "metadata": { 450 | "ExecuteTime": { 451 | "end_time": "2024-07-21T17:16:37.930243Z", 452 | "start_time": "2024-07-21T17:16:37.927736Z" 453 | } 454 | }, 455 | "cell_type": "code", 456 | "source": [ 457 | "d = deque([10, 20, 30])\n", 458 | "d.extend([40, 50, 60])\n", 459 | "d" 460 | ], 461 | "id": "d0485c507c65360e", 462 | "outputs": [ 463 | { 464 | "data": { 465 | "text/plain": [ 466 | "deque([10, 20, 30, 40, 50, 60])" 467 | ] 468 | }, 469 | "execution_count": 14, 470 | "metadata": {}, 471 | "output_type": "execute_result" 472 | } 473 | ], 474 | "execution_count": 14 475 | }, 476 | { 477 | "metadata": {}, 478 | "cell_type": "markdown", 479 | "source": "## **`extendleft(...)`**", 480 | "id": "5ef9755be235e12e" 481 | }, 482 | { 483 | "metadata": { 484 | "ExecuteTime": { 485 | "end_time": "2024-07-21T17:16:37.953006Z", 486 | "start_time": "2024-07-21T17:16:37.950562Z" 487 | } 488 | }, 489 | "cell_type": "code", 490 | "source": [ 491 | "d = deque([10, 20, 30])\n", 492 | "d.extendleft([40, 50, 60])\n", 493 | "d" 494 | ], 495 | "id": "16df251a5f4fcd1f", 496 | "outputs": [ 497 | { 498 | "data": { 499 | "text/plain": [ 500 | "deque([60, 50, 40, 10, 20, 30])" 501 | ] 502 | }, 503 | "execution_count": 15, 504 | "metadata": {}, 505 | "output_type": "execute_result" 506 | } 507 | ], 508 | "execution_count": 15 509 | }, 510 | { 511 | "metadata": {}, 512 | "cell_type": "markdown", 513 | "source": "## **`index(...)`**", 514 | "id": "9a82238d592e4e8d" 515 | }, 516 | { 517 | "metadata": { 518 | "ExecuteTime": { 519 | "end_time": "2024-07-21T17:16:37.963403Z", 520 | "start_time": "2024-07-21T17:16:37.960465Z" 521 | } 522 | }, 523 | "cell_type": "code", 524 | "source": [ 525 | "d = deque([10, 20, 30, 40, 20])\n", 526 | "d.index(20)" 527 | ], 528 | "id": "c00000eb4e0f9922", 529 | "outputs": [ 530 | { 531 | "data": { 532 | "text/plain": [ 533 | "1" 534 | ] 535 | }, 536 | "execution_count": 16, 537 | "metadata": {}, 538 | "output_type": "execute_result" 539 | } 540 | ], 541 | "execution_count": 16 542 | }, 543 | { 544 | "metadata": {}, 545 | "cell_type": "markdown", 546 | "source": "## **`insert(...)`**", 547 | "id": "5ef4b8ef52c5b171" 548 | }, 549 | { 550 | "metadata": { 551 | "ExecuteTime": { 552 | "end_time": "2024-07-21T17:16:37.967993Z", 553 | "start_time": "2024-07-21T17:16:37.965011Z" 554 | } 555 | }, 556 | "cell_type": "code", 557 | "source": [ 558 | "d = deque([10, 20, 30, 40])\n", 559 | "d.insert(4, 100)\n", 560 | "d.insert(0, 200)\n", 561 | "d" 562 | ], 563 | "id": "b70911fba37f64b3", 564 | "outputs": [ 565 | { 566 | "data": { 567 | "text/plain": [ 568 | "deque([200, 10, 20, 30, 40, 100])" 569 | ] 570 | }, 571 | "execution_count": 17, 572 | "metadata": {}, 573 | "output_type": "execute_result" 574 | } 575 | ], 576 | "execution_count": 17 577 | }, 578 | { 579 | "metadata": {}, 580 | "cell_type": "markdown", 581 | "source": "## **`pop(...)`**", 582 | "id": "b74686fb222f7a5b" 583 | }, 584 | { 585 | "metadata": { 586 | "ExecuteTime": { 587 | "end_time": "2024-07-21T17:16:37.977603Z", 588 | "start_time": "2024-07-21T17:16:37.974537Z" 589 | } 590 | }, 591 | "cell_type": "code", 592 | "source": [ 593 | "d = deque([10, 20, 30])\n", 594 | "popped = d.pop()\n", 595 | "d, popped" 596 | ], 597 | "id": "243fb0e6e5441fed", 598 | "outputs": [ 599 | { 600 | "data": { 601 | "text/plain": [ 602 | "(deque([10, 20]), 30)" 603 | ] 604 | }, 605 | "execution_count": 18, 606 | "metadata": {}, 607 | "output_type": "execute_result" 608 | } 609 | ], 610 | "execution_count": 18 611 | }, 612 | { 613 | "metadata": {}, 614 | "cell_type": "markdown", 615 | "source": "## **`popleft(...)`**", 616 | "id": "a912018ede25db9f" 617 | }, 618 | { 619 | "metadata": { 620 | "ExecuteTime": { 621 | "end_time": "2024-07-21T17:16:38.024554Z", 622 | "start_time": "2024-07-21T17:16:38.022323Z" 623 | } 624 | }, 625 | "cell_type": "code", 626 | "source": [ 627 | "d = deque([10, 20, 30])\n", 628 | "popped = d.popleft()\n", 629 | "d, popped" 630 | ], 631 | "id": "a3b23d851baf1967", 632 | "outputs": [ 633 | { 634 | "data": { 635 | "text/plain": [ 636 | "(deque([20, 30]), 10)" 637 | ] 638 | }, 639 | "execution_count": 19, 640 | "metadata": {}, 641 | "output_type": "execute_result" 642 | } 643 | ], 644 | "execution_count": 19 645 | }, 646 | { 647 | "metadata": {}, 648 | "cell_type": "markdown", 649 | "source": "## **`remove(...)`**", 650 | "id": "ad594b03cae55b68" 651 | }, 652 | { 653 | "metadata": { 654 | "ExecuteTime": { 655 | "end_time": "2024-07-21T17:16:38.036344Z", 656 | "start_time": "2024-07-21T17:16:38.034188Z" 657 | } 658 | }, 659 | "cell_type": "code", 660 | "source": [ 661 | "d = deque([10, 20, 30, 10])\n", 662 | "d.remove(10)\n", 663 | "d" 664 | ], 665 | "id": "d6e191fa086d55ca", 666 | "outputs": [ 667 | { 668 | "data": { 669 | "text/plain": [ 670 | "deque([20, 30, 10])" 671 | ] 672 | }, 673 | "execution_count": 20, 674 | "metadata": {}, 675 | "output_type": "execute_result" 676 | } 677 | ], 678 | "execution_count": 20 679 | }, 680 | { 681 | "metadata": {}, 682 | "cell_type": "markdown", 683 | "source": "## **`reverse(...)`**", 684 | "id": "9baea30b0b612079" 685 | }, 686 | { 687 | "metadata": { 688 | "ExecuteTime": { 689 | "end_time": "2024-07-21T17:16:38.048191Z", 690 | "start_time": "2024-07-21T17:16:38.046054Z" 691 | } 692 | }, 693 | "cell_type": "code", 694 | "source": [ 695 | "d = deque([10, 20, 30, 40])\n", 696 | "d.reverse()\n", 697 | "d" 698 | ], 699 | "id": "f653cdef4576d61d", 700 | "outputs": [ 701 | { 702 | "data": { 703 | "text/plain": [ 704 | "deque([40, 30, 20, 10])" 705 | ] 706 | }, 707 | "execution_count": 21, 708 | "metadata": {}, 709 | "output_type": "execute_result" 710 | } 711 | ], 712 | "execution_count": 21 713 | }, 714 | { 715 | "metadata": {}, 716 | "cell_type": "markdown", 717 | "source": "## **`rotate(...)`**", 718 | "id": "80d31c87bb3600ef" 719 | }, 720 | { 721 | "metadata": { 722 | "ExecuteTime": { 723 | "end_time": "2024-07-21T17:16:38.068443Z", 724 | "start_time": "2024-07-21T17:16:38.065777Z" 725 | } 726 | }, 727 | "cell_type": "code", 728 | "source": [ 729 | "d = deque([10, 20, 30, 40, 50])\n", 730 | "d.rotate(1)\n", 731 | "d" 732 | ], 733 | "id": "731ad517d7381a52", 734 | "outputs": [ 735 | { 736 | "data": { 737 | "text/plain": [ 738 | "deque([50, 10, 20, 30, 40])" 739 | ] 740 | }, 741 | "execution_count": 22, 742 | "metadata": {}, 743 | "output_type": "execute_result" 744 | } 745 | ], 746 | "execution_count": 22 747 | }, 748 | { 749 | "metadata": { 750 | "ExecuteTime": { 751 | "end_time": "2024-07-21T17:16:38.072412Z", 752 | "start_time": "2024-07-21T17:16:38.069954Z" 753 | } 754 | }, 755 | "cell_type": "code", 756 | "source": [ 757 | "d = deque([10, 20, 30, 40, 50])\n", 758 | "d.rotate(3)\n", 759 | "d\n" 760 | ], 761 | "id": "cfad5534312d7ed5", 762 | "outputs": [ 763 | { 764 | "data": { 765 | "text/plain": [ 766 | "deque([30, 40, 50, 10, 20])" 767 | ] 768 | }, 769 | "execution_count": 23, 770 | "metadata": {}, 771 | "output_type": "execute_result" 772 | } 773 | ], 774 | "execution_count": 23 775 | }, 776 | { 777 | "metadata": { 778 | "ExecuteTime": { 779 | "end_time": "2024-07-21T17:16:38.082279Z", 780 | "start_time": "2024-07-21T17:16:38.079594Z" 781 | } 782 | }, 783 | "cell_type": "code", 784 | "source": [ 785 | "d = deque([10, 20, 30, 40, 50])\n", 786 | "d.rotate(-2)\n", 787 | "d\n" 788 | ], 789 | "id": "affb45033f75e113", 790 | "outputs": [ 791 | { 792 | "data": { 793 | "text/plain": [ 794 | "deque([30, 40, 50, 10, 20])" 795 | ] 796 | }, 797 | "execution_count": 24, 798 | "metadata": {}, 799 | "output_type": "execute_result" 800 | } 801 | ], 802 | "execution_count": 24 803 | } 804 | ], 805 | "metadata": { 806 | "kernelspec": { 807 | "display_name": "Python 3", 808 | "language": "python", 809 | "name": "python3" 810 | }, 811 | "language_info": { 812 | "codemirror_mode": { 813 | "name": "ipython", 814 | "version": 2 815 | }, 816 | "file_extension": ".py", 817 | "mimetype": "text/x-python", 818 | "name": "python", 819 | "nbconvert_exporter": "python", 820 | "pygments_lexer": "ipython2", 821 | "version": "2.7.6" 822 | } 823 | }, 824 | "nbformat": 4, 825 | "nbformat_minor": 5 826 | } 827 | -------------------------------------------------------------------------------- /python_basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [ 7 | { 8 | "file_id": "https://github.com/WasikAhmed/Learning-Python/blob/main/python_basics.ipynb", 9 | "timestamp": 1717751852084 10 | } 11 | ], 12 | "authorship_tag": "ABX9TyM+yvSYLg1JzIHfXZQIdZj6" 13 | }, 14 | "kernelspec": { 15 | "name": "python3", 16 | "language": "python", 17 | "display_name": "Python 3 (ipykernel)" 18 | }, 19 | "language_info": { 20 | "name": "python" 21 | } 22 | }, 23 | "cells": [ 24 | { 25 | "cell_type": "markdown", 26 | "source": [ 27 | "# Python Basics" 28 | ], 29 | "metadata": { 30 | "id": "KqarRX2RfUov" 31 | } 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "source": [ 36 | "## Hello world" 37 | ], 38 | "metadata": { 39 | "id": "lOMkC6Wyfmb-" 40 | } 41 | }, 42 | { 43 | "cell_type": "code", 44 | "source": [ 45 | "print(\"Hello, World!\")" 46 | ], 47 | "metadata": { 48 | "colab": { 49 | "base_uri": "https://localhost:8080/" 50 | }, 51 | "id": "4w8pnwqFftMN", 52 | "executionInfo": { 53 | "status": "ok", 54 | "timestamp": 1717751658143, 55 | "user_tz": -360, 56 | "elapsed": 64, 57 | "user": { 58 | "displayName": "", 59 | "userId": "" 60 | } 61 | }, 62 | "outputId": "26d65df0-75fd-45a4-982c-e1ab8ca1569a", 63 | "ExecuteTime": { 64 | "end_time": "2024-06-08T21:00:14.104703Z", 65 | "start_time": "2024-06-08T21:00:14.099351Z" 66 | } 67 | }, 68 | "execution_count": 1, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "Hello, World!\n" 75 | ] 76 | } 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "source": [ 82 | "## Checking python version" 83 | ], 84 | "metadata": { 85 | "id": "NJCouLVrgs29" 86 | } 87 | }, 88 | { 89 | "cell_type": "code", 90 | "source": [ 91 | "! python3 --version" 92 | ], 93 | "metadata": { 94 | "colab": { 95 | "base_uri": "https://localhost:8080/" 96 | }, 97 | "id": "o_Y1xMhVgxuh", 98 | "executionInfo": { 99 | "status": "ok", 100 | "timestamp": 1717751658143, 101 | "user_tz": -360, 102 | "elapsed": 62, 103 | "user": { 104 | "displayName": "", 105 | "userId": "" 106 | } 107 | }, 108 | "outputId": "b65d0d09-7470-4006-f47f-d8880d5d504a", 109 | "ExecuteTime": { 110 | "end_time": "2024-06-08T21:00:15.204068Z", 111 | "start_time": "2024-06-08T21:00:15.071469Z" 112 | } 113 | }, 114 | "execution_count": 2, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "Python 3.11.7\r\n" 121 | ] 122 | } 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "source": [ 128 | "**`!` character is used to run shell commands directly from the notebook's cell. This is a feature of IPython, which Jupyter Notebooks are built on.**\n", 129 | "\n", 130 | "**_NOTE:_** IPython stands for Interactive Python. It is an enhanced interactive shell that provides a rich toolkit for interactive computing in Python." 131 | ], 132 | "metadata": { 133 | "collapsed": false 134 | } 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "source": [ 139 | "## Variables\n", 140 | "Containers for storing data.\n", 141 | "Don't require to be declared with any particular type." 142 | ], 143 | "metadata": { 144 | "id": "0uy_2n_di4IK" 145 | } 146 | }, 147 | { 148 | "cell_type": "code", 149 | "source": [ 150 | "x = 10\n", 151 | "y = 20\n", 152 | "x, y" 153 | ], 154 | "metadata": { 155 | "colab": { 156 | "base_uri": "https://localhost:8080/" 157 | }, 158 | "id": "oP8Bm3p9i6Y7", 159 | "executionInfo": { 160 | "status": "ok", 161 | "timestamp": 1717751658143, 162 | "user_tz": -360, 163 | "elapsed": 58, 164 | "user": { 165 | "displayName": "", 166 | "userId": "" 167 | } 168 | }, 169 | "outputId": "72977d57-7e01-4aef-8cc1-48317062e311", 170 | "ExecuteTime": { 171 | "end_time": "2024-06-08T21:00:15.208085Z", 172 | "start_time": "2024-06-08T21:00:15.205146Z" 173 | } 174 | }, 175 | "execution_count": 3, 176 | "outputs": [ 177 | { 178 | "data": { 179 | "text/plain": "(10, 20)" 180 | }, 181 | "execution_count": 3, 182 | "metadata": {}, 183 | "output_type": "execute_result" 184 | } 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "source": [ 190 | "a, b, c = 55, 6.66, -77\n", 191 | "a, b, c" 192 | ], 193 | "metadata": { 194 | "colab": { 195 | "base_uri": "https://localhost:8080/" 196 | }, 197 | "id": "WX5MYMNhjji2", 198 | "executionInfo": { 199 | "status": "ok", 200 | "timestamp": 1717751658143, 201 | "user_tz": -360, 202 | "elapsed": 55, 203 | "user": { 204 | "displayName": "", 205 | "userId": "" 206 | } 207 | }, 208 | "outputId": "e92ee43f-1dad-4fa1-b989-57af15d1c6c4", 209 | "ExecuteTime": { 210 | "end_time": "2024-06-08T21:00:15.210814Z", 211 | "start_time": "2024-06-08T21:00:15.208621Z" 212 | } 213 | }, 214 | "execution_count": 4, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": "(55, 6.66, -77)" 219 | }, 220 | "execution_count": 4, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "source": [ 229 | "m, n, o= 'Hello', 'w', True\n", 230 | "m, n, o" 231 | ], 232 | "metadata": { 233 | "colab": { 234 | "base_uri": "https://localhost:8080/" 235 | }, 236 | "id": "Q-S17IiDkJxb", 237 | "executionInfo": { 238 | "status": "ok", 239 | "timestamp": 1717751658143, 240 | "user_tz": -360, 241 | "elapsed": 53, 242 | "user": { 243 | "displayName": "", 244 | "userId": "" 245 | } 246 | }, 247 | "outputId": "3bf6b69f-ed3e-491f-ecd7-fe50990f8240", 248 | "ExecuteTime": { 249 | "end_time": "2024-06-08T21:00:15.223437Z", 250 | "start_time": "2024-06-08T21:00:15.221400Z" 251 | } 252 | }, 253 | "execution_count": 5, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": "('Hello', 'w', True)" 258 | }, 259 | "execution_count": 5, 260 | "metadata": {}, 261 | "output_type": "execute_result" 262 | } 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "source": [ 268 | "## Data types\n", 269 | "Built-in data types\n", 270 | "\n", 271 | "- Text Type: \tstr\n", 272 | "- Numeric Types: \tint, float, complex\n", 273 | "- Sequence Types: \tlist, tuple, range\n", 274 | "- Mapping Type: \tdict\n", 275 | "- Set Types: \tset, frozenset\n", 276 | "- Boolean Type: \tbool\n", 277 | "- Binary Types: \tbytes, bytearray, memoryview\n", 278 | "- None Type: \tNoneType" 279 | ], 280 | "metadata": { 281 | "id": "FrkAkkP0kz32" 282 | } 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "source": [ 287 | "### Checking data types\n", 288 | "Python's built-in `type()` function returns the data type of variable or object." 289 | ], 290 | "metadata": { 291 | "id": "YJBUwcI-lvWf" 292 | } 293 | }, 294 | { 295 | "cell_type": "code", 296 | "source": [ 297 | "a, b, c, d, e, f, g = 'hello', 5, 2.32, 3+2j, [1, 2, 3], True, None\n", 298 | "type(a), type(b), type(c), type(d), type(e), type(f), type(g)" 299 | ], 300 | "metadata": { 301 | "colab": { 302 | "base_uri": "https://localhost:8080/" 303 | }, 304 | "id": "HCxlG-Gmk3qY", 305 | "executionInfo": { 306 | "status": "ok", 307 | "timestamp": 1717751658143, 308 | "user_tz": -360, 309 | "elapsed": 50, 310 | "user": { 311 | "displayName": "", 312 | "userId": "" 313 | } 314 | }, 315 | "outputId": "b1e55e5a-7a63-441b-dde7-1901d682bdd1", 316 | "ExecuteTime": { 317 | "end_time": "2024-06-08T21:00:15.281371Z", 318 | "start_time": "2024-06-08T21:00:15.278881Z" 319 | } 320 | }, 321 | "execution_count": 6, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": "(str, int, float, complex, list, bool, NoneType)" 326 | }, 327 | "execution_count": 6, 328 | "metadata": {}, 329 | "output_type": "execute_result" 330 | } 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "source": [ 336 | "## Numbers\n", 337 | "There are three numeric types in Python\n", 338 | "- int\n", 339 | "- float\n", 340 | "- complex" 341 | ], 342 | "metadata": { 343 | "id": "WC091Cp8sk-k" 344 | } 345 | }, 346 | { 347 | "cell_type": "code", 348 | "source": [ 349 | "x = 5\n", 350 | "y = 6.78\n", 351 | "z = 5+10j\n", 352 | "type(x), type(y), type(z)" 353 | ], 354 | "metadata": { 355 | "colab": { 356 | "base_uri": "https://localhost:8080/" 357 | }, 358 | "id": "do6BGCBfnafW", 359 | "executionInfo": { 360 | "status": "ok", 361 | "timestamp": 1717751658143, 362 | "user_tz": -360, 363 | "elapsed": 48, 364 | "user": { 365 | "displayName": "", 366 | "userId": "" 367 | } 368 | }, 369 | "outputId": "e8b6d665-e586-4222-fce0-18fe96dd7a16", 370 | "ExecuteTime": { 371 | "end_time": "2024-06-08T21:00:15.416023Z", 372 | "start_time": "2024-06-08T21:00:15.413548Z" 373 | } 374 | }, 375 | "execution_count": 7, 376 | "outputs": [ 377 | { 378 | "data": { 379 | "text/plain": "(int, float, complex)" 380 | }, 381 | "execution_count": 7, 382 | "metadata": {}, 383 | "output_type": "execute_result" 384 | } 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "source": [ 390 | "## Casting\n", 391 | "Specify a type on to a variable using casting." 392 | ], 393 | "metadata": { 394 | "id": "479Hktaith36" 395 | } 396 | }, 397 | { 398 | "cell_type": "code", 399 | "source": [ 400 | "a, b, c, d = 3, 5.66, '5', 7+8j" 401 | ], 402 | "metadata": { 403 | "id": "UJkO8kreu-Lo", 404 | "executionInfo": { 405 | "status": "ok", 406 | "timestamp": 1717751658143, 407 | "user_tz": -360, 408 | "elapsed": 47, 409 | "user": { 410 | "displayName": "", 411 | "userId": "" 412 | } 413 | }, 414 | "ExecuteTime": { 415 | "end_time": "2024-06-08T21:00:15.447271Z", 416 | "start_time": "2024-06-08T21:00:15.445552Z" 417 | } 418 | }, 419 | "execution_count": 8, 420 | "outputs": [] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "source": [ 425 | "**Integers**" 426 | ], 427 | "metadata": { 428 | "id": "ym434StDt9Js" 429 | } 430 | }, 431 | { 432 | "cell_type": "code", 433 | "source": [ 434 | "int(a), int(b), int(c)\n", 435 | "# A complex number cannot be represented as an integer.\n", 436 | "# int(d) will raise an error" 437 | ], 438 | "metadata": { 439 | "colab": { 440 | "base_uri": "https://localhost:8080/" 441 | }, 442 | "id": "VhV1AkdztHd2", 443 | "executionInfo": { 444 | "status": "ok", 445 | "timestamp": 1717751658143, 446 | "user_tz": -360, 447 | "elapsed": 46, 448 | "user": { 449 | "displayName": "", 450 | "userId": "" 451 | } 452 | }, 453 | "outputId": "3fd356cf-7b14-49ab-86db-603cd8943ca9", 454 | "ExecuteTime": { 455 | "end_time": "2024-06-08T21:00:15.515956Z", 456 | "start_time": "2024-06-08T21:00:15.513885Z" 457 | } 458 | }, 459 | "execution_count": 9, 460 | "outputs": [ 461 | { 462 | "data": { 463 | "text/plain": "(3, 5, 5)" 464 | }, 465 | "execution_count": 9, 466 | "metadata": {}, 467 | "output_type": "execute_result" 468 | } 469 | ] 470 | }, 471 | { 472 | "cell_type": "markdown", 473 | "source": [ 474 | "**Floats**" 475 | ], 476 | "metadata": { 477 | "id": "_aQWQCIpu31H" 478 | } 479 | }, 480 | { 481 | "cell_type": "code", 482 | "source": [ 483 | "float(a), float(b), float(c)\n", 484 | "# A complex number cannot be represented as an float.\n", 485 | "# float(d) will raise an error" 486 | ], 487 | "metadata": { 488 | "colab": { 489 | "base_uri": "https://localhost:8080/" 490 | }, 491 | "id": "agTIEu9uuObs", 492 | "executionInfo": { 493 | "status": "ok", 494 | "timestamp": 1717751658143, 495 | "user_tz": -360, 496 | "elapsed": 45, 497 | "user": { 498 | "displayName": "", 499 | "userId": "" 500 | } 501 | }, 502 | "outputId": "872b5d44-6ae2-4bf6-b7fa-514d35a358f1", 503 | "ExecuteTime": { 504 | "end_time": "2024-06-08T21:00:15.548128Z", 505 | "start_time": "2024-06-08T21:00:15.546052Z" 506 | } 507 | }, 508 | "execution_count": 10, 509 | "outputs": [ 510 | { 511 | "data": { 512 | "text/plain": "(3.0, 5.66, 5.0)" 513 | }, 514 | "execution_count": 10, 515 | "metadata": {}, 516 | "output_type": "execute_result" 517 | } 518 | ] 519 | }, 520 | { 521 | "cell_type": "markdown", 522 | "source": [ 523 | "**Strings**" 524 | ], 525 | "metadata": { 526 | "id": "m14wzd39vM1H" 527 | } 528 | }, 529 | { 530 | "cell_type": "code", 531 | "source": [ 532 | "str(a), str(b), str(c), str(d)" 533 | ], 534 | "metadata": { 535 | "colab": { 536 | "base_uri": "https://localhost:8080/" 537 | }, 538 | "id": "zM0LeAqVvFjQ", 539 | "executionInfo": { 540 | "status": "ok", 541 | "timestamp": 1717751658143, 542 | "user_tz": -360, 543 | "elapsed": 43, 544 | "user": { 545 | "displayName": "", 546 | "userId": "" 547 | } 548 | }, 549 | "outputId": "b4f6bc8d-64a6-42fb-a414-0b9067655f08", 550 | "ExecuteTime": { 551 | "end_time": "2024-06-08T21:00:15.602489Z", 552 | "start_time": "2024-06-08T21:00:15.600315Z" 553 | } 554 | }, 555 | "execution_count": 11, 556 | "outputs": [ 557 | { 558 | "data": { 559 | "text/plain": "('3', '5.66', '5', '(7+8j)')" 560 | }, 561 | "execution_count": 11, 562 | "metadata": {}, 563 | "output_type": "execute_result" 564 | } 565 | ] 566 | }, 567 | { 568 | "cell_type": "markdown", 569 | "source": [ 570 | "## Strings\n", 571 | "Strings are used for representing textual data. A string is a sequence of characters enclosed in either single quotes ('') or double quotes (“”)." 572 | ], 573 | "metadata": { 574 | "id": "WBZVqIeHwDN0" 575 | } 576 | }, 577 | { 578 | "cell_type": "code", 579 | "source": [ 580 | "string = \"This is a string\"\n", 581 | "string" 582 | ], 583 | "metadata": { 584 | "colab": { 585 | "base_uri": "https://localhost:8080/", 586 | "height": 36 587 | }, 588 | "id": "6tTGB8UsvYWz", 589 | "executionInfo": { 590 | "status": "ok", 591 | "timestamp": 1717751658144, 592 | "user_tz": -360, 593 | "elapsed": 43, 594 | "user": { 595 | "displayName": "", 596 | "userId": "" 597 | } 598 | }, 599 | "outputId": "8d41a231-1d04-457e-93e5-b3169d4a53c0", 600 | "ExecuteTime": { 601 | "end_time": "2024-06-08T21:00:15.645222Z", 602 | "start_time": "2024-06-08T21:00:15.642908Z" 603 | } 604 | }, 605 | "execution_count": 12, 606 | "outputs": [ 607 | { 608 | "data": { 609 | "text/plain": "'This is a string'" 610 | }, 611 | "execution_count": 12, 612 | "metadata": {}, 613 | "output_type": "execute_result" 614 | } 615 | ] 616 | }, 617 | { 618 | "cell_type": "markdown", 619 | "source": [ 620 | "**Multiline Strings**\n", 621 | "\n", 622 | "Triple quote marks “”\" or ''' creates multi-line strings." 623 | ], 624 | "metadata": { 625 | "id": "qBCFIceLwpFJ" 626 | } 627 | }, 628 | { 629 | "cell_type": "code", 630 | "source": [ 631 | "multiline_string = \"\"\"This is a\n", 632 | "multiline\n", 633 | "string\n", 634 | "\"\"\"\n", 635 | "multiline_string" 636 | ], 637 | "metadata": { 638 | "colab": { 639 | "base_uri": "https://localhost:8080/", 640 | "height": 36 641 | }, 642 | "id": "RIzRtF_wwame", 643 | "executionInfo": { 644 | "status": "ok", 645 | "timestamp": 1717751658144, 646 | "user_tz": -360, 647 | "elapsed": 42, 648 | "user": { 649 | "displayName": "", 650 | "userId": "" 651 | } 652 | }, 653 | "outputId": "99f87841-be17-4e33-b44e-9c483b3bd2b9", 654 | "ExecuteTime": { 655 | "end_time": "2024-06-08T21:00:15.707660Z", 656 | "start_time": "2024-06-08T21:00:15.705567Z" 657 | } 658 | }, 659 | "execution_count": 13, 660 | "outputs": [ 661 | { 662 | "data": { 663 | "text/plain": "'This is a\\nmultiline\\nstring\\n'" 664 | }, 665 | "execution_count": 13, 666 | "metadata": {}, 667 | "output_type": "execute_result" 668 | } 669 | ] 670 | }, 671 | { 672 | "cell_type": "markdown", 673 | "source": [ 674 | "**Looping Through a String**" 675 | ], 676 | "metadata": { 677 | "id": "IQHDON_dxWn3" 678 | } 679 | }, 680 | { 681 | "cell_type": "code", 682 | "source": [ 683 | "string = 'string'\n", 684 | "for char in string:\n", 685 | " print(char)" 686 | ], 687 | "metadata": { 688 | "colab": { 689 | "base_uri": "https://localhost:8080/" 690 | }, 691 | "id": "s8pOKXf4xKB0", 692 | "executionInfo": { 693 | "status": "ok", 694 | "timestamp": 1717751658144, 695 | "user_tz": -360, 696 | "elapsed": 41, 697 | "user": { 698 | "displayName": "", 699 | "userId": "" 700 | } 701 | }, 702 | "outputId": "52250010-1e86-4734-c776-21575f8a7735", 703 | "ExecuteTime": { 704 | "end_time": "2024-06-08T21:00:15.736523Z", 705 | "start_time": "2024-06-08T21:00:15.734756Z" 706 | } 707 | }, 708 | "execution_count": 14, 709 | "outputs": [ 710 | { 711 | "name": "stdout", 712 | "output_type": "stream", 713 | "text": [ 714 | "s\n", 715 | "t\n", 716 | "r\n", 717 | "i\n", 718 | "n\n", 719 | "g\n" 720 | ] 721 | } 722 | ] 723 | }, 724 | { 725 | "cell_type": "markdown", 726 | "source": [ 727 | "**String Length**\n", 728 | "\n", 729 | "`len()` function returns the length of a string." 730 | ], 731 | "metadata": { 732 | "id": "6JYDYqf3xlBY" 733 | } 734 | }, 735 | { 736 | "cell_type": "code", 737 | "source": [ 738 | "string = \"Hello, World!\"\n", 739 | "len(string)" 740 | ], 741 | "metadata": { 742 | "colab": { 743 | "base_uri": "https://localhost:8080/" 744 | }, 745 | "id": "ld5w6Dj_xgwQ", 746 | "executionInfo": { 747 | "status": "ok", 748 | "timestamp": 1717751658144, 749 | "user_tz": -360, 750 | "elapsed": 39, 751 | "user": { 752 | "displayName": "", 753 | "userId": "" 754 | } 755 | }, 756 | "outputId": "76da3200-5fe8-469c-931d-66e95d4b43df", 757 | "ExecuteTime": { 758 | "end_time": "2024-06-08T21:00:15.809840Z", 759 | "start_time": "2024-06-08T21:00:15.808016Z" 760 | } 761 | }, 762 | "execution_count": 15, 763 | "outputs": [ 764 | { 765 | "data": { 766 | "text/plain": "13" 767 | }, 768 | "execution_count": 15, 769 | "metadata": {}, 770 | "output_type": "execute_result" 771 | } 772 | ] 773 | }, 774 | { 775 | "cell_type": "markdown", 776 | "source": [ 777 | "### Slicing\n", 778 | "\n", 779 | "`string[start:stop:step]`" 780 | ], 781 | "metadata": { 782 | "id": "2TGK0lcDyeda" 783 | } 784 | }, 785 | { 786 | "cell_type": "code", 787 | "source": [ 788 | "s = \"Python is fun!!!\"" 789 | ], 790 | "metadata": { 791 | "id": "kl_JC0hoyOmJ", 792 | "executionInfo": { 793 | "status": "ok", 794 | "timestamp": 1717751658144, 795 | "user_tz": -360, 796 | "elapsed": 37, 797 | "user": { 798 | "displayName": "", 799 | "userId": "" 800 | } 801 | }, 802 | "ExecuteTime": { 803 | "end_time": "2024-06-08T21:00:15.839920Z", 804 | "start_time": "2024-06-08T21:00:15.838350Z" 805 | } 806 | }, 807 | "execution_count": 16, 808 | "outputs": [] 809 | }, 810 | { 811 | "cell_type": "markdown", 812 | "source": [ 813 | "**Slice From the Start**" 814 | ], 815 | "metadata": { 816 | "id": "vpHYCj3MzVNB" 817 | } 818 | }, 819 | { 820 | "cell_type": "code", 821 | "source": [ 822 | "s[:6]" 823 | ], 824 | "metadata": { 825 | "colab": { 826 | "base_uri": "https://localhost:8080/", 827 | "height": 36 828 | }, 829 | "id": "OPfKEot_zr07", 830 | "executionInfo": { 831 | "status": "ok", 832 | "timestamp": 1717751658144, 833 | "user_tz": -360, 834 | "elapsed": 37, 835 | "user": { 836 | "displayName": "", 837 | "userId": "" 838 | } 839 | }, 840 | "outputId": "e8275c0c-730e-4a6f-8a5e-c0f50d4bb4e9", 841 | "ExecuteTime": { 842 | "end_time": "2024-06-08T21:00:15.900406Z", 843 | "start_time": "2024-06-08T21:00:15.898189Z" 844 | } 845 | }, 846 | "execution_count": 17, 847 | "outputs": [ 848 | { 849 | "data": { 850 | "text/plain": "'Python'" 851 | }, 852 | "execution_count": 17, 853 | "metadata": {}, 854 | "output_type": "execute_result" 855 | } 856 | ] 857 | }, 858 | { 859 | "cell_type": "markdown", 860 | "source": [ 861 | "**Slice To the End**" 862 | ], 863 | "metadata": { 864 | "id": "3LRvwGCpz6F9" 865 | } 866 | }, 867 | { 868 | "cell_type": "code", 869 | "source": [ 870 | "s[7:]" 871 | ], 872 | "metadata": { 873 | "colab": { 874 | "base_uri": "https://localhost:8080/", 875 | "height": 36 876 | }, 877 | "id": "FRJHdc7Ryw9L", 878 | "executionInfo": { 879 | "status": "ok", 880 | "timestamp": 1717751658144, 881 | "user_tz": -360, 882 | "elapsed": 36, 883 | "user": { 884 | "displayName": "", 885 | "userId": "" 886 | } 887 | }, 888 | "outputId": "9c0277e0-2475-4812-fd67-dff5258037ce", 889 | "ExecuteTime": { 890 | "end_time": "2024-06-08T21:00:15.934326Z", 891 | "start_time": "2024-06-08T21:00:15.932392Z" 892 | } 893 | }, 894 | "execution_count": 18, 895 | "outputs": [ 896 | { 897 | "data": { 898 | "text/plain": "'is fun!!!'" 899 | }, 900 | "execution_count": 18, 901 | "metadata": {}, 902 | "output_type": "execute_result" 903 | } 904 | ] 905 | }, 906 | { 907 | "cell_type": "markdown", 908 | "source": [ 909 | "**Negative Indexing**" 910 | ], 911 | "metadata": { 912 | "id": "WEazyGXkz_z0" 913 | } 914 | }, 915 | { 916 | "cell_type": "code", 917 | "source": [ 918 | "s[:-3]" 919 | ], 920 | "metadata": { 921 | "colab": { 922 | "base_uri": "https://localhost:8080/", 923 | "height": 36 924 | }, 925 | "id": "z_m-SWlYzekT", 926 | "executionInfo": { 927 | "status": "ok", 928 | "timestamp": 1717751658144, 929 | "user_tz": -360, 930 | "elapsed": 36, 931 | "user": { 932 | "displayName": "", 933 | "userId": "" 934 | } 935 | }, 936 | "outputId": "08359af4-39ce-44fa-8c2e-18130c99943a", 937 | "ExecuteTime": { 938 | "end_time": "2024-06-08T21:00:15.985186Z", 939 | "start_time": "2024-06-08T21:00:15.983253Z" 940 | } 941 | }, 942 | "execution_count": 19, 943 | "outputs": [ 944 | { 945 | "data": { 946 | "text/plain": "'Python is fun'" 947 | }, 948 | "execution_count": 19, 949 | "metadata": {}, 950 | "output_type": "execute_result" 951 | } 952 | ] 953 | }, 954 | { 955 | "cell_type": "markdown", 956 | "source": [ 957 | "### Modify Strings" 958 | ], 959 | "metadata": { 960 | "id": "bOFuPOkz1C9B" 961 | } 962 | }, 963 | { 964 | "cell_type": "markdown", 965 | "source": [ 966 | "**Upper Case**" 967 | ], 968 | "metadata": { 969 | "id": "fDska9Iv1cYY" 970 | } 971 | }, 972 | { 973 | "cell_type": "code", 974 | "source": [ 975 | "string = 'Hello, World!'\n", 976 | "string.upper()" 977 | ], 978 | "metadata": { 979 | "colab": { 980 | "base_uri": "https://localhost:8080/", 981 | "height": 36 982 | }, 983 | "id": "IgqB3zVy0hG7", 984 | "executionInfo": { 985 | "status": "ok", 986 | "timestamp": 1717751658144, 987 | "user_tz": -360, 988 | "elapsed": 35, 989 | "user": { 990 | "displayName": "", 991 | "userId": "" 992 | } 993 | }, 994 | "outputId": "c10034ca-48cb-4b7e-f560-c301efb3cf53", 995 | "ExecuteTime": { 996 | "end_time": "2024-06-08T21:00:16.002091Z", 997 | "start_time": "2024-06-08T21:00:16.000172Z" 998 | } 999 | }, 1000 | "execution_count": 20, 1001 | "outputs": [ 1002 | { 1003 | "data": { 1004 | "text/plain": "'HELLO, WORLD!'" 1005 | }, 1006 | "execution_count": 20, 1007 | "metadata": {}, 1008 | "output_type": "execute_result" 1009 | } 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "markdown", 1014 | "source": [ 1015 | "**Lower Case**" 1016 | ], 1017 | "metadata": { 1018 | "id": "3Lb0dw_916Zo" 1019 | } 1020 | }, 1021 | { 1022 | "cell_type": "code", 1023 | "source": [ 1024 | "string = 'Hello, World!'\n", 1025 | "string.lower()" 1026 | ], 1027 | "metadata": { 1028 | "colab": { 1029 | "base_uri": "https://localhost:8080/", 1030 | "height": 36 1031 | }, 1032 | "id": "cN6FGXn_13pd", 1033 | "executionInfo": { 1034 | "status": "ok", 1035 | "timestamp": 1717751658144, 1036 | "user_tz": -360, 1037 | "elapsed": 35, 1038 | "user": { 1039 | "displayName": "", 1040 | "userId": "" 1041 | } 1042 | }, 1043 | "outputId": "323e74c5-327d-4acd-fc01-3021a1b0641c", 1044 | "ExecuteTime": { 1045 | "end_time": "2024-06-08T21:00:16.023035Z", 1046 | "start_time": "2024-06-08T21:00:16.020637Z" 1047 | } 1048 | }, 1049 | "execution_count": 21, 1050 | "outputs": [ 1051 | { 1052 | "data": { 1053 | "text/plain": "'hello, world!'" 1054 | }, 1055 | "execution_count": 21, 1056 | "metadata": {}, 1057 | "output_type": "execute_result" 1058 | } 1059 | ] 1060 | }, 1061 | { 1062 | "cell_type": "markdown", 1063 | "source": [ 1064 | "**Capitalize**" 1065 | ], 1066 | "metadata": { 1067 | "id": "MCdrbd812Gic" 1068 | } 1069 | }, 1070 | { 1071 | "cell_type": "code", 1072 | "source": [ 1073 | "string = 'this is capitalized string!'\n", 1074 | "string.capitalize()" 1075 | ], 1076 | "metadata": { 1077 | "colab": { 1078 | "base_uri": "https://localhost:8080/", 1079 | "height": 36 1080 | }, 1081 | "id": "1UGrSlRE2BFh", 1082 | "executionInfo": { 1083 | "status": "ok", 1084 | "timestamp": 1717751658144, 1085 | "user_tz": -360, 1086 | "elapsed": 34, 1087 | "user": { 1088 | "displayName": "", 1089 | "userId": "" 1090 | } 1091 | }, 1092 | "outputId": "bab79fe3-6cb7-44bf-e7c6-89ca51d3088e", 1093 | "ExecuteTime": { 1094 | "end_time": "2024-06-08T21:00:16.105680Z", 1095 | "start_time": "2024-06-08T21:00:16.103626Z" 1096 | } 1097 | }, 1098 | "execution_count": 22, 1099 | "outputs": [ 1100 | { 1101 | "data": { 1102 | "text/plain": "'This is capitalized string!'" 1103 | }, 1104 | "execution_count": 22, 1105 | "metadata": {}, 1106 | "output_type": "execute_result" 1107 | } 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "markdown", 1112 | "source": [ 1113 | "**Remove Whitespace**" 1114 | ], 1115 | "metadata": { 1116 | "id": "Lu2j77ZV2ie9" 1117 | } 1118 | }, 1119 | { 1120 | "cell_type": "code", 1121 | "source": [ 1122 | "string = \" Hello, World! \"\n", 1123 | "string.strip()" 1124 | ], 1125 | "metadata": { 1126 | "colab": { 1127 | "base_uri": "https://localhost:8080/", 1128 | "height": 36 1129 | }, 1130 | "id": "UoPj6d8B2T45", 1131 | "executionInfo": { 1132 | "status": "ok", 1133 | "timestamp": 1717751658144, 1134 | "user_tz": -360, 1135 | "elapsed": 34, 1136 | "user": { 1137 | "displayName": "", 1138 | "userId": "" 1139 | } 1140 | }, 1141 | "outputId": "842a1f0f-d9f1-4062-900e-868dbcc23d2d", 1142 | "ExecuteTime": { 1143 | "end_time": "2024-06-08T21:00:16.174113Z", 1144 | "start_time": "2024-06-08T21:00:16.171891Z" 1145 | } 1146 | }, 1147 | "execution_count": 23, 1148 | "outputs": [ 1149 | { 1150 | "data": { 1151 | "text/plain": "'Hello, World!'" 1152 | }, 1153 | "execution_count": 23, 1154 | "metadata": {}, 1155 | "output_type": "execute_result" 1156 | } 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "markdown", 1161 | "source": [ 1162 | "**Replace**" 1163 | ], 1164 | "metadata": { 1165 | "id": "HtfxcSld2yYJ" 1166 | } 1167 | }, 1168 | { 1169 | "cell_type": "code", 1170 | "source": [ 1171 | "string = 'Hello, World!'\n", 1172 | "string.replace('H', 'J')" 1173 | ], 1174 | "metadata": { 1175 | "colab": { 1176 | "base_uri": "https://localhost:8080/", 1177 | "height": 36 1178 | }, 1179 | "id": "0Ew62Gen2qTc", 1180 | "executionInfo": { 1181 | "status": "ok", 1182 | "timestamp": 1717751658144, 1183 | "user_tz": -360, 1184 | "elapsed": 34, 1185 | "user": { 1186 | "displayName": "", 1187 | "userId": "" 1188 | } 1189 | }, 1190 | "outputId": "4a07539f-5902-4ad3-cb01-af3e9704fd56", 1191 | "ExecuteTime": { 1192 | "end_time": "2024-06-08T21:00:16.205196Z", 1193 | "start_time": "2024-06-08T21:00:16.203062Z" 1194 | } 1195 | }, 1196 | "execution_count": 24, 1197 | "outputs": [ 1198 | { 1199 | "data": { 1200 | "text/plain": "'Jello, World!'" 1201 | }, 1202 | "execution_count": 24, 1203 | "metadata": {}, 1204 | "output_type": "execute_result" 1205 | } 1206 | ] 1207 | }, 1208 | { 1209 | "cell_type": "markdown", 1210 | "source": [ 1211 | "**Split**" 1212 | ], 1213 | "metadata": { 1214 | "id": "74DGOuaT3CbM" 1215 | } 1216 | }, 1217 | { 1218 | "cell_type": "code", 1219 | "source": [ 1220 | "string = 'This.sentence.will.be.split.by.a.dot'\n", 1221 | "string.split('.')" 1222 | ], 1223 | "metadata": { 1224 | "colab": { 1225 | "base_uri": "https://localhost:8080/" 1226 | }, 1227 | "id": "y_U_n3NX26XM", 1228 | "executionInfo": { 1229 | "status": "ok", 1230 | "timestamp": 1717751658144, 1231 | "user_tz": -360, 1232 | "elapsed": 34, 1233 | "user": { 1234 | "displayName": "", 1235 | "userId": "" 1236 | } 1237 | }, 1238 | "outputId": "78531eed-7ae9-42d3-a012-a91d712640b2", 1239 | "ExecuteTime": { 1240 | "end_time": "2024-06-08T21:00:16.277619Z", 1241 | "start_time": "2024-06-08T21:00:16.275740Z" 1242 | } 1243 | }, 1244 | "execution_count": 25, 1245 | "outputs": [ 1246 | { 1247 | "data": { 1248 | "text/plain": "['This', 'sentence', 'will', 'be', 'split', 'by', 'a', 'dot']" 1249 | }, 1250 | "execution_count": 25, 1251 | "metadata": {}, 1252 | "output_type": "execute_result" 1253 | } 1254 | ] 1255 | }, 1256 | { 1257 | "cell_type": "markdown", 1258 | "source": [ 1259 | "### Concatenation\n", 1260 | "Concatenate or combine two string using `+` operator." 1261 | ], 1262 | "metadata": { 1263 | "id": "hZnb8jVnpg3l" 1264 | } 1265 | }, 1266 | { 1267 | "cell_type": "code", 1268 | "source": [ 1269 | "first_part = 'To be'\n", 1270 | "second_part = 'or not to be.'\n", 1271 | "full_string = first_part + ' '+ second_part\n", 1272 | "full_string" 1273 | ], 1274 | "metadata": { 1275 | "colab": { 1276 | "base_uri": "https://localhost:8080/", 1277 | "height": 36 1278 | }, 1279 | "id": "dBKIKhONom0A", 1280 | "executionInfo": { 1281 | "status": "ok", 1282 | "timestamp": 1717751658145, 1283 | "user_tz": -360, 1284 | "elapsed": 33, 1285 | "user": { 1286 | "displayName": "", 1287 | "userId": "" 1288 | } 1289 | }, 1290 | "outputId": "6a6ad99a-691c-4a0e-b6c9-bab2caf4fdfc", 1291 | "ExecuteTime": { 1292 | "end_time": "2024-06-08T21:00:16.302761Z", 1293 | "start_time": "2024-06-08T21:00:16.300644Z" 1294 | } 1295 | }, 1296 | "execution_count": 26, 1297 | "outputs": [ 1298 | { 1299 | "data": { 1300 | "text/plain": "'To be or not to be.'" 1301 | }, 1302 | "execution_count": 26, 1303 | "metadata": {}, 1304 | "output_type": "execute_result" 1305 | } 1306 | ] 1307 | }, 1308 | { 1309 | "cell_type": "markdown", 1310 | "source": [ 1311 | "### Format Strings\n", 1312 | "using `f-strings` or `format()` method." 1313 | ], 1314 | "metadata": { 1315 | "id": "fh1zGW_9rG5S" 1316 | } 1317 | }, 1318 | { 1319 | "cell_type": "code", 1320 | "source": [ 1321 | "name = 'Alice'\n", 1322 | "age = 30\n", 1323 | "city = 'Wonderland'" 1324 | ], 1325 | "metadata": { 1326 | "id": "gvxT2Va3qh_Z", 1327 | "executionInfo": { 1328 | "status": "ok", 1329 | "timestamp": 1717751658145, 1330 | "user_tz": -360, 1331 | "elapsed": 32, 1332 | "user": { 1333 | "displayName": "", 1334 | "userId": "" 1335 | } 1336 | }, 1337 | "ExecuteTime": { 1338 | "end_time": "2024-06-08T21:00:16.380748Z", 1339 | "start_time": "2024-06-08T21:00:16.379164Z" 1340 | } 1341 | }, 1342 | "execution_count": 27, 1343 | "outputs": [] 1344 | }, 1345 | { 1346 | "cell_type": "code", 1347 | "source": [ 1348 | "formatted_string = 'My name is {}. I am {} years old and I live in {}.'.format(name, age, city)\n", 1349 | "formatted_string" 1350 | ], 1351 | "metadata": { 1352 | "colab": { 1353 | "base_uri": "https://localhost:8080/", 1354 | "height": 36 1355 | }, 1356 | "id": "lkwXnig5rzQY", 1357 | "executionInfo": { 1358 | "status": "ok", 1359 | "timestamp": 1717751658145, 1360 | "user_tz": -360, 1361 | "elapsed": 32, 1362 | "user": { 1363 | "displayName": "", 1364 | "userId": "" 1365 | } 1366 | }, 1367 | "outputId": "50b2ab2b-17ef-46bf-b8fc-ede96d4484e0", 1368 | "ExecuteTime": { 1369 | "end_time": "2024-06-08T21:00:16.408129Z", 1370 | "start_time": "2024-06-08T21:00:16.406138Z" 1371 | } 1372 | }, 1373 | "execution_count": 28, 1374 | "outputs": [ 1375 | { 1376 | "data": { 1377 | "text/plain": "'My name is Alice. I am 30 years old and I live in Wonderland.'" 1378 | }, 1379 | "execution_count": 28, 1380 | "metadata": {}, 1381 | "output_type": "execute_result" 1382 | } 1383 | ] 1384 | }, 1385 | { 1386 | "cell_type": "code", 1387 | "source": [ 1388 | "formatted_string = f'My name is {name}. I am {age} years old and I live in {city}.'\n", 1389 | "formatted_string" 1390 | ], 1391 | "metadata": { 1392 | "colab": { 1393 | "base_uri": "https://localhost:8080/", 1394 | "height": 36 1395 | }, 1396 | "id": "kuX-iM6PsJRj", 1397 | "executionInfo": { 1398 | "status": "ok", 1399 | "timestamp": 1717751658145, 1400 | "user_tz": -360, 1401 | "elapsed": 31, 1402 | "user": { 1403 | "displayName": "", 1404 | "userId": "" 1405 | } 1406 | }, 1407 | "outputId": "c75babe8-85dd-4e6b-b021-8d9549c315d3", 1408 | "ExecuteTime": { 1409 | "end_time": "2024-06-08T21:00:16.464158Z", 1410 | "start_time": "2024-06-08T21:00:16.462147Z" 1411 | } 1412 | }, 1413 | "execution_count": 29, 1414 | "outputs": [ 1415 | { 1416 | "data": { 1417 | "text/plain": "'My name is Alice. I am 30 years old and I live in Wonderland.'" 1418 | }, 1419 | "execution_count": 29, 1420 | "metadata": {}, 1421 | "output_type": "execute_result" 1422 | } 1423 | ] 1424 | }, 1425 | { 1426 | "cell_type": "markdown", 1427 | "source": [ 1428 | "### Escape Characters" 1429 | ], 1430 | "metadata": { 1431 | "id": "OalA4JLrtIQO" 1432 | } 1433 | }, 1434 | { 1435 | "cell_type": "markdown", 1436 | "source": [ 1437 | "**single quote escape character**" 1438 | ], 1439 | "metadata": { 1440 | "id": "9_kTxmF44Vbz" 1441 | } 1442 | }, 1443 | { 1444 | "cell_type": "code", 1445 | "source": [ 1446 | "sentence_with_single_quote = 'It\\'s a beautiful day!'\n", 1447 | "print(sentence_with_single_quote)" 1448 | ], 1449 | "metadata": { 1450 | "colab": { 1451 | "base_uri": "https://localhost:8080/" 1452 | }, 1453 | "id": "6GLj0zKA4bIh", 1454 | "executionInfo": { 1455 | "status": "ok", 1456 | "timestamp": 1717751658145, 1457 | "user_tz": -360, 1458 | "elapsed": 31, 1459 | "user": { 1460 | "displayName": "", 1461 | "userId": "" 1462 | } 1463 | }, 1464 | "outputId": "71f852e1-c0e5-47d5-a915-1fe499b7d6ab", 1465 | "ExecuteTime": { 1466 | "end_time": "2024-06-08T21:00:16.490831Z", 1467 | "start_time": "2024-06-08T21:00:16.489224Z" 1468 | } 1469 | }, 1470 | "execution_count": 30, 1471 | "outputs": [ 1472 | { 1473 | "name": "stdout", 1474 | "output_type": "stream", 1475 | "text": [ 1476 | "It's a beautiful day!\n" 1477 | ] 1478 | } 1479 | ] 1480 | }, 1481 | { 1482 | "cell_type": "markdown", 1483 | "source": [ 1484 | "**new line escape character**" 1485 | ], 1486 | "metadata": { 1487 | "id": "ns6VGhi95DiI" 1488 | } 1489 | }, 1490 | { 1491 | "cell_type": "code", 1492 | "source": [ 1493 | "sentence_with_new_line = 'Hello,\\nWelcome to the party!'\n", 1494 | "print(sentence_with_new_line)" 1495 | ], 1496 | "metadata": { 1497 | "colab": { 1498 | "base_uri": "https://localhost:8080/" 1499 | }, 1500 | "id": "h89MClZr5I7V", 1501 | "executionInfo": { 1502 | "status": "ok", 1503 | "timestamp": 1717751658145, 1504 | "user_tz": -360, 1505 | "elapsed": 29, 1506 | "user": { 1507 | "displayName": "", 1508 | "userId": "" 1509 | } 1510 | }, 1511 | "outputId": "385b148c-1777-4672-ff3e-867d3b1ea455", 1512 | "ExecuteTime": { 1513 | "end_time": "2024-06-08T21:00:16.522358Z", 1514 | "start_time": "2024-06-08T21:00:16.520618Z" 1515 | } 1516 | }, 1517 | "execution_count": 31, 1518 | "outputs": [ 1519 | { 1520 | "name": "stdout", 1521 | "output_type": "stream", 1522 | "text": [ 1523 | "Hello,\n", 1524 | "Welcome to the party!\n" 1525 | ] 1526 | } 1527 | ] 1528 | }, 1529 | { 1530 | "cell_type": "markdown", 1531 | "source": [ 1532 | "**backslash escape character**" 1533 | ], 1534 | "metadata": { 1535 | "id": "PWRMCS4k3QbX" 1536 | } 1537 | }, 1538 | { 1539 | "cell_type": "code", 1540 | "source": [ 1541 | "sentence_with_backslash = 'This is a backslash: \\\\'\n", 1542 | "print(sentence_with_backslash)" 1543 | ], 1544 | "metadata": { 1545 | "colab": { 1546 | "base_uri": "https://localhost:8080/" 1547 | }, 1548 | "id": "aflrs__KsVrF", 1549 | "executionInfo": { 1550 | "status": "ok", 1551 | "timestamp": 1717751658145, 1552 | "user_tz": -360, 1553 | "elapsed": 29, 1554 | "user": { 1555 | "displayName": "", 1556 | "userId": "" 1557 | } 1558 | }, 1559 | "outputId": "b4fb712f-412b-4e57-f910-3eb715a20bc8", 1560 | "ExecuteTime": { 1561 | "end_time": "2024-06-08T21:00:16.563734Z", 1562 | "start_time": "2024-06-08T21:00:16.562203Z" 1563 | } 1564 | }, 1565 | "execution_count": 32, 1566 | "outputs": [ 1567 | { 1568 | "name": "stdout", 1569 | "output_type": "stream", 1570 | "text": [ 1571 | "This is a backslash: \\\n" 1572 | ] 1573 | } 1574 | ] 1575 | }, 1576 | { 1577 | "cell_type": "markdown", 1578 | "source": [ 1579 | "**tab escape character**" 1580 | ], 1581 | "metadata": { 1582 | "id": "zxFsu9L23L7a" 1583 | } 1584 | }, 1585 | { 1586 | "cell_type": "code", 1587 | "source": [ 1588 | "sentence_with_tab = \"Name:\\tAlice\"\n", 1589 | "print(sentence_with_tab)" 1590 | ], 1591 | "metadata": { 1592 | "colab": { 1593 | "base_uri": "https://localhost:8080/" 1594 | }, 1595 | "id": "B1pxt_3LyY39", 1596 | "executionInfo": { 1597 | "status": "ok", 1598 | "timestamp": 1717751658145, 1599 | "user_tz": -360, 1600 | "elapsed": 27, 1601 | "user": { 1602 | "displayName": "", 1603 | "userId": "" 1604 | } 1605 | }, 1606 | "outputId": "846d5d43-54e2-4d97-f636-10af7ccc73b4", 1607 | "ExecuteTime": { 1608 | "end_time": "2024-06-08T21:00:16.640705Z", 1609 | "start_time": "2024-06-08T21:00:16.639127Z" 1610 | } 1611 | }, 1612 | "execution_count": 33, 1613 | "outputs": [ 1614 | { 1615 | "name": "stdout", 1616 | "output_type": "stream", 1617 | "text": [ 1618 | "Name:\tAlice\n" 1619 | ] 1620 | } 1621 | ] 1622 | }, 1623 | { 1624 | "cell_type": "markdown", 1625 | "source": [ 1626 | "### More string methods\n", 1627 | "- [GeeksforGeeks](https://www.geeksforgeeks.org/python-string-methods)\n", 1628 | "- [W3Schools](https://www.w3schools.com/python/python_strings_methods.asp)\n" 1629 | ], 1630 | "metadata": { 1631 | "id": "cB7fJkuY7NbW" 1632 | } 1633 | }, 1634 | { 1635 | "cell_type": "markdown", 1636 | "source": [ 1637 | "## Booleans\n", 1638 | "- Booleans are a data type that can hold one of two values: `True` or `False`.\n", 1639 | "- Used to represent truth values.\n", 1640 | "- Typically the result of comparisons or logical operations.\n", 1641 | "\n", 1642 | "**Boolean Values**\n", 1643 | "- `True`: Represents a truth value.\n", 1644 | "- `False`: Represents a false value.\n", 1645 | "\n", 1646 | "`0` and `1` are integer values, but they can also be interpreted as boolean values." 1647 | ], 1648 | "metadata": { 1649 | "id": "m3uBUwKx_Gp6" 1650 | } 1651 | }, 1652 | { 1653 | "cell_type": "code", 1654 | "source": [ 1655 | "is_sunny = True\n", 1656 | "is_raining = False\n", 1657 | "\n", 1658 | "is_sunny, is_raining" 1659 | ], 1660 | "metadata": { 1661 | "colab": { 1662 | "base_uri": "https://localhost:8080/" 1663 | }, 1664 | "id": "5jYJ1KqRDayu", 1665 | "executionInfo": { 1666 | "status": "ok", 1667 | "timestamp": 1717751658145, 1668 | "user_tz": -360, 1669 | "elapsed": 26, 1670 | "user": { 1671 | "displayName": "", 1672 | "userId": "" 1673 | } 1674 | }, 1675 | "outputId": "d0737e1b-2cd7-4173-cb9d-ba44f45998f2", 1676 | "ExecuteTime": { 1677 | "end_time": "2024-06-08T21:00:16.673015Z", 1678 | "start_time": "2024-06-08T21:00:16.670593Z" 1679 | } 1680 | }, 1681 | "execution_count": 34, 1682 | "outputs": [ 1683 | { 1684 | "data": { 1685 | "text/plain": "(True, False)" 1686 | }, 1687 | "execution_count": 34, 1688 | "metadata": {}, 1689 | "output_type": "execute_result" 1690 | } 1691 | ] 1692 | }, 1693 | { 1694 | "cell_type": "markdown", 1695 | "source": [ 1696 | "### Booleans in Conditions" 1697 | ], 1698 | "metadata": { 1699 | "id": "M-tKGgg0DwO9" 1700 | } 1701 | }, 1702 | { 1703 | "cell_type": "code", 1704 | "source": [ 1705 | "is_hungry = True\n", 1706 | "\n", 1707 | "if is_hungry:\n", 1708 | " print(\"Time to eat!\")\n", 1709 | "else:\n", 1710 | " print(\"Not hungry right now.\")" 1711 | ], 1712 | "metadata": { 1713 | "colab": { 1714 | "base_uri": "https://localhost:8080/" 1715 | }, 1716 | "id": "OZ8xUpOFD4a9", 1717 | "executionInfo": { 1718 | "status": "ok", 1719 | "timestamp": 1717751658145, 1720 | "user_tz": -360, 1721 | "elapsed": 25, 1722 | "user": { 1723 | "displayName": "", 1724 | "userId": "" 1725 | } 1726 | }, 1727 | "outputId": "91a9e5d0-b896-4ec6-c71d-b10013d89a12", 1728 | "ExecuteTime": { 1729 | "end_time": "2024-06-08T21:00:16.745493Z", 1730 | "start_time": "2024-06-08T21:00:16.743745Z" 1731 | } 1732 | }, 1733 | "execution_count": 35, 1734 | "outputs": [ 1735 | { 1736 | "name": "stdout", 1737 | "output_type": "stream", 1738 | "text": [ 1739 | "Time to eat!\n" 1740 | ] 1741 | } 1742 | ] 1743 | }, 1744 | { 1745 | "cell_type": "markdown", 1746 | "source": [ 1747 | "### Comparisons Resulting in Booleans" 1748 | ], 1749 | "metadata": { 1750 | "id": "Vl-iqUO7DAGk" 1751 | } 1752 | }, 1753 | { 1754 | "cell_type": "code", 1755 | "source": [ 1756 | "a = 10\n", 1757 | "b = 20\n", 1758 | "\n", 1759 | "print(a == b)\n", 1760 | "print(a != b)\n", 1761 | "print(a > b)\n", 1762 | "print(a < b)\n", 1763 | "print(a >= b)\n", 1764 | "print(a <= b)" 1765 | ], 1766 | "metadata": { 1767 | "colab": { 1768 | "base_uri": "https://localhost:8080/" 1769 | }, 1770 | "id": "tDZuz7mcDEJn", 1771 | "executionInfo": { 1772 | "status": "ok", 1773 | "timestamp": 1717751658145, 1774 | "user_tz": -360, 1775 | "elapsed": 24, 1776 | "user": { 1777 | "displayName": "", 1778 | "userId": "" 1779 | } 1780 | }, 1781 | "outputId": "9ef20877-4bea-496c-bfce-2ecb922cbe35", 1782 | "ExecuteTime": { 1783 | "end_time": "2024-06-08T21:00:16.781147Z", 1784 | "start_time": "2024-06-08T21:00:16.778974Z" 1785 | } 1786 | }, 1787 | "execution_count": 36, 1788 | "outputs": [ 1789 | { 1790 | "name": "stdout", 1791 | "output_type": "stream", 1792 | "text": [ 1793 | "False\n", 1794 | "True\n", 1795 | "False\n", 1796 | "True\n", 1797 | "False\n", 1798 | "True\n" 1799 | ] 1800 | } 1801 | ] 1802 | }, 1803 | { 1804 | "cell_type": "markdown", 1805 | "source": [ 1806 | "### Logical Operations with `0` and `1`" 1807 | ], 1808 | "metadata": { 1809 | "id": "9Rk4PVTQCvnU" 1810 | } 1811 | }, 1812 | { 1813 | "cell_type": "code", 1814 | "source": [ 1815 | "a = 0\n", 1816 | "b = 1\n", 1817 | "\n", 1818 | "print(a and b)\n", 1819 | "print(a or b)" 1820 | ], 1821 | "metadata": { 1822 | "colab": { 1823 | "base_uri": "https://localhost:8080/" 1824 | }, 1825 | "id": "AHqtsc1J-Qi3", 1826 | "executionInfo": { 1827 | "status": "ok", 1828 | "timestamp": 1717751658145, 1829 | "user_tz": -360, 1830 | "elapsed": 23, 1831 | "user": { 1832 | "displayName": "", 1833 | "userId": "" 1834 | } 1835 | }, 1836 | "outputId": "1cdcd16d-52ec-4aef-b4ff-0bac98045f0c", 1837 | "ExecuteTime": { 1838 | "end_time": "2024-06-08T21:00:16.842560Z", 1839 | "start_time": "2024-06-08T21:00:16.840746Z" 1840 | } 1841 | }, 1842 | "execution_count": 37, 1843 | "outputs": [ 1844 | { 1845 | "name": "stdout", 1846 | "output_type": "stream", 1847 | "text": [ 1848 | "0\n", 1849 | "1\n" 1850 | ] 1851 | } 1852 | ] 1853 | }, 1854 | { 1855 | "cell_type": "markdown", 1856 | "source": [ 1857 | "### Integer and Float to Boolean Convertion\n", 1858 | "\n", 1859 | "Any number is `True`, except `0`." 1860 | ], 1861 | "metadata": { 1862 | "id": "nq9-zlZtCR5n" 1863 | } 1864 | }, 1865 | { 1866 | "cell_type": "code", 1867 | "source": [ 1868 | "bool(0), bool(1), bool(2), bool(20), bool(-100), bool(5.5)" 1869 | ], 1870 | "metadata": { 1871 | "colab": { 1872 | "base_uri": "https://localhost:8080/" 1873 | }, 1874 | "id": "ULi-wGZJB-wa", 1875 | "executionInfo": { 1876 | "status": "ok", 1877 | "timestamp": 1717751658145, 1878 | "user_tz": -360, 1879 | "elapsed": 22, 1880 | "user": { 1881 | "displayName": "", 1882 | "userId": "" 1883 | } 1884 | }, 1885 | "outputId": "da6533b0-e649-43d6-eef8-f96cf18b69a2", 1886 | "ExecuteTime": { 1887 | "end_time": "2024-06-08T21:00:16.877732Z", 1888 | "start_time": "2024-06-08T21:00:16.874857Z" 1889 | } 1890 | }, 1891 | "execution_count": 38, 1892 | "outputs": [ 1893 | { 1894 | "data": { 1895 | "text/plain": "(False, True, True, True, True, True)" 1896 | }, 1897 | "execution_count": 38, 1898 | "metadata": {}, 1899 | "output_type": "execute_result" 1900 | } 1901 | ] 1902 | }, 1903 | { 1904 | "cell_type": "markdown", 1905 | "source": [ 1906 | "### More Resources\n", 1907 | "- [GeeksforGeeks](https://www.geeksforgeeks.org/boolean-data-type-in-python/)\n", 1908 | "- [W3Schools](https://www.w3schools.com/python/python_booleans.asp)\n", 1909 | "- [Python Booleans: Use Truth Values in Your Code](https://realpython.com/python-boolean/)" 1910 | ], 1911 | "metadata": { 1912 | "id": "3MeBm22-FGSu" 1913 | } 1914 | }, 1915 | { 1916 | "cell_type": "markdown", 1917 | "source": [ 1918 | "## List\n", 1919 | "- Store collections of items in a single variable.\n", 1920 | "- Items can be of any data type (integers, floats, strings, etc.) and can even be other lists.\n", 1921 | "- Lists can have items with the same value.\n", 1922 | "- Lists are indexed.\n", 1923 | "\n", 1924 | "**_NOTE:_** There are 4 built-in data structures in python. (List, Dictionary, Tuple, Set)." 1925 | ], 1926 | "metadata": { 1927 | "collapsed": false 1928 | } 1929 | }, 1930 | { 1931 | "cell_type": "markdown", 1932 | "source": [ 1933 | "### List Creation\n", 1934 | "Create a list by placing elements inside square brackets `[]`, separated by commas." 1935 | ], 1936 | "metadata": { 1937 | "collapsed": false 1938 | } 1939 | }, 1940 | { 1941 | "cell_type": "code", 1942 | "outputs": [ 1943 | { 1944 | "data": { 1945 | "text/plain": "([1, 2, 3, 1, 2, 3],\n ['John', 'Bob', 'Thomas'],\n [1, 'hello', 3.22, [55, 66, 77], True])" 1946 | }, 1947 | "execution_count": 39, 1948 | "metadata": {}, 1949 | "output_type": "execute_result" 1950 | } 1951 | ], 1952 | "source": [ 1953 | "nums = [1, 2, 3, 1, 2, 3]\n", 1954 | "names = ['John', 'Bob', 'Thomas']\n", 1955 | "mixed_list = [1, 'hello', 3.22, [55, 66, 77], True]\n", 1956 | "nums, names, mixed_list" 1957 | ], 1958 | "metadata": { 1959 | "collapsed": false, 1960 | "ExecuteTime": { 1961 | "end_time": "2024-06-08T21:00:16.950202Z", 1962 | "start_time": "2024-06-08T21:00:16.947614Z" 1963 | } 1964 | }, 1965 | "execution_count": 39 1966 | }, 1967 | { 1968 | "cell_type": "markdown", 1969 | "source": [ 1970 | "**Check list length**" 1971 | ], 1972 | "metadata": { 1973 | "collapsed": false 1974 | } 1975 | }, 1976 | { 1977 | "cell_type": "code", 1978 | "outputs": [ 1979 | { 1980 | "data": { 1981 | "text/plain": "(6, 3, 5)" 1982 | }, 1983 | "execution_count": 40, 1984 | "metadata": {}, 1985 | "output_type": "execute_result" 1986 | } 1987 | ], 1988 | "source": [ 1989 | "len(nums), len(names), len(mixed_list)" 1990 | ], 1991 | "metadata": { 1992 | "collapsed": false, 1993 | "ExecuteTime": { 1994 | "end_time": "2024-06-08T21:00:16.968141Z", 1995 | "start_time": "2024-06-08T21:00:16.966321Z" 1996 | } 1997 | }, 1998 | "execution_count": 40 1999 | }, 2000 | { 2001 | "cell_type": "markdown", 2002 | "source": [ 2003 | "### Accessing Elements\n", 2004 | "List elements can be accessed using index. Indexing starts at `0`." 2005 | ], 2006 | "metadata": { 2007 | "collapsed": false 2008 | } 2009 | }, 2010 | { 2011 | "cell_type": "code", 2012 | "outputs": [ 2013 | { 2014 | "data": { 2015 | "text/plain": "[1, 2, [3, 4, 5], 6, 7]" 2016 | }, 2017 | "execution_count": 41, 2018 | "metadata": {}, 2019 | "output_type": "execute_result" 2020 | } 2021 | ], 2022 | "source": [ 2023 | "nums = [1, 2, [3, 4, 5], 6, 7]\n", 2024 | "nums" 2025 | ], 2026 | "metadata": { 2027 | "collapsed": false, 2028 | "ExecuteTime": { 2029 | "end_time": "2024-06-08T21:00:16.996124Z", 2030 | "start_time": "2024-06-08T21:00:16.993760Z" 2031 | } 2032 | }, 2033 | "execution_count": 41 2034 | }, 2035 | { 2036 | "cell_type": "code", 2037 | "outputs": [ 2038 | { 2039 | "name": "stdout", 2040 | "output_type": "stream", 2041 | "text": [ 2042 | "1\n", 2043 | "[3, 4, 5]\n", 2044 | "4\n", 2045 | "7\n" 2046 | ] 2047 | } 2048 | ], 2049 | "source": [ 2050 | "print(nums[0])\n", 2051 | "print(nums[2])\n", 2052 | "print(nums[2][1])\n", 2053 | "print(nums[-1]) # negative indexing. (starts at -1)" 2054 | ], 2055 | "metadata": { 2056 | "collapsed": false, 2057 | "ExecuteTime": { 2058 | "end_time": "2024-06-08T21:00:17.062474Z", 2059 | "start_time": "2024-06-08T21:00:17.060133Z" 2060 | } 2061 | }, 2062 | "execution_count": 42 2063 | }, 2064 | { 2065 | "cell_type": "markdown", 2066 | "source": [ 2067 | "### Modify a List" 2068 | ], 2069 | "metadata": { 2070 | "collapsed": false 2071 | } 2072 | }, 2073 | { 2074 | "cell_type": "code", 2075 | "outputs": [ 2076 | { 2077 | "data": { 2078 | "text/plain": "[1, 2, 3, [4, 5, 6], 7, 8, 9]" 2079 | }, 2080 | "execution_count": 43, 2081 | "metadata": {}, 2082 | "output_type": "execute_result" 2083 | } 2084 | ], 2085 | "source": [ 2086 | "nums = [1, 2, 3, [4, 5, 6], 7, 8, 9]\n", 2087 | "nums" 2088 | ], 2089 | "metadata": { 2090 | "collapsed": false, 2091 | "ExecuteTime": { 2092 | "end_time": "2024-06-08T21:00:17.085016Z", 2093 | "start_time": "2024-06-08T21:00:17.082955Z" 2094 | } 2095 | }, 2096 | "execution_count": 43 2097 | }, 2098 | { 2099 | "cell_type": "markdown", 2100 | "source": [ 2101 | "**Change element**" 2102 | ], 2103 | "metadata": { 2104 | "collapsed": false 2105 | } 2106 | }, 2107 | { 2108 | "cell_type": "code", 2109 | "outputs": [ 2110 | { 2111 | "data": { 2112 | "text/plain": "[1, 100, 3, [4, 5, 6], 7, 8, 9]" 2113 | }, 2114 | "execution_count": 44, 2115 | "metadata": {}, 2116 | "output_type": "execute_result" 2117 | } 2118 | ], 2119 | "source": [ 2120 | "nums[1] = 100\n", 2121 | "nums" 2122 | ], 2123 | "metadata": { 2124 | "collapsed": false, 2125 | "ExecuteTime": { 2126 | "end_time": "2024-06-08T21:00:17.148161Z", 2127 | "start_time": "2024-06-08T21:00:17.146079Z" 2128 | } 2129 | }, 2130 | "execution_count": 44 2131 | }, 2132 | { 2133 | "cell_type": "markdown", 2134 | "source": [ 2135 | "**Add elements**" 2136 | ], 2137 | "metadata": { 2138 | "collapsed": false 2139 | } 2140 | }, 2141 | { 2142 | "cell_type": "code", 2143 | "outputs": [ 2144 | { 2145 | "data": { 2146 | "text/plain": "[1, 100, 3, [4, 5, 6], 7, 8, 9, 500, 600]" 2147 | }, 2148 | "execution_count": 45, 2149 | "metadata": {}, 2150 | "output_type": "execute_result" 2151 | } 2152 | ], 2153 | "source": [ 2154 | "nums.append(500)\n", 2155 | "nums.append(600)\n", 2156 | "nums" 2157 | ], 2158 | "metadata": { 2159 | "collapsed": false, 2160 | "ExecuteTime": { 2161 | "end_time": "2024-06-08T21:00:17.182837Z", 2162 | "start_time": "2024-06-08T21:00:17.180751Z" 2163 | } 2164 | }, 2165 | "execution_count": 45 2166 | }, 2167 | { 2168 | "cell_type": "markdown", 2169 | "source": [ 2170 | "**Insert elements**" 2171 | ], 2172 | "metadata": { 2173 | "collapsed": false 2174 | } 2175 | }, 2176 | { 2177 | "cell_type": "code", 2178 | "outputs": [ 2179 | { 2180 | "data": { 2181 | "text/plain": "[99, 1, 100, 3, [4, 5, 6], 7, 8, 9, 500, 600]" 2182 | }, 2183 | "execution_count": 46, 2184 | "metadata": {}, 2185 | "output_type": "execute_result" 2186 | } 2187 | ], 2188 | "source": [ 2189 | "nums.insert(0, 99)\n", 2190 | "nums" 2191 | ], 2192 | "metadata": { 2193 | "collapsed": false, 2194 | "ExecuteTime": { 2195 | "end_time": "2024-06-08T21:00:17.249912Z", 2196 | "start_time": "2024-06-08T21:00:17.247216Z" 2197 | } 2198 | }, 2199 | "execution_count": 46 2200 | }, 2201 | { 2202 | "cell_type": "markdown", 2203 | "source": [ 2204 | "### Removing elements" 2205 | ], 2206 | "metadata": { 2207 | "collapsed": false 2208 | } 2209 | }, 2210 | { 2211 | "cell_type": "code", 2212 | "outputs": [ 2213 | { 2214 | "data": { 2215 | "text/plain": "[19.99, 5.49, 3.99, 49.95, 23.5]" 2216 | }, 2217 | "execution_count": 47, 2218 | "metadata": {}, 2219 | "output_type": "execute_result" 2220 | } 2221 | ], 2222 | "source": [ 2223 | "prices = [19.99, 5.49, 3.99, 49.95, 23.50]\n", 2224 | "prices" 2225 | ], 2226 | "metadata": { 2227 | "collapsed": false, 2228 | "ExecuteTime": { 2229 | "end_time": "2024-06-08T21:00:17.289381Z", 2230 | "start_time": "2024-06-08T21:00:17.287085Z" 2231 | } 2232 | }, 2233 | "execution_count": 47 2234 | }, 2235 | { 2236 | "cell_type": "markdown", 2237 | "source": [ 2238 | "**Remove by value**" 2239 | ], 2240 | "metadata": { 2241 | "collapsed": false 2242 | } 2243 | }, 2244 | { 2245 | "cell_type": "code", 2246 | "outputs": [ 2247 | { 2248 | "data": { 2249 | "text/plain": "[19.99, 5.49, 49.95, 23.5]" 2250 | }, 2251 | "execution_count": 48, 2252 | "metadata": {}, 2253 | "output_type": "execute_result" 2254 | } 2255 | ], 2256 | "source": [ 2257 | "prices.remove(3.99)\n", 2258 | "prices" 2259 | ], 2260 | "metadata": { 2261 | "collapsed": false, 2262 | "ExecuteTime": { 2263 | "end_time": "2024-06-08T21:00:17.345206Z", 2264 | "start_time": "2024-06-08T21:00:17.343139Z" 2265 | } 2266 | }, 2267 | "execution_count": 48 2268 | }, 2269 | { 2270 | "cell_type": "markdown", 2271 | "source": [ 2272 | "**Remove by index**" 2273 | ], 2274 | "metadata": { 2275 | "collapsed": false 2276 | } 2277 | }, 2278 | { 2279 | "cell_type": "code", 2280 | "outputs": [ 2281 | { 2282 | "data": { 2283 | "text/plain": "[5.49, 49.95, 23.5]" 2284 | }, 2285 | "execution_count": 49, 2286 | "metadata": {}, 2287 | "output_type": "execute_result" 2288 | } 2289 | ], 2290 | "source": [ 2291 | "prices.pop(0)\n", 2292 | "prices" 2293 | ], 2294 | "metadata": { 2295 | "collapsed": false, 2296 | "ExecuteTime": { 2297 | "end_time": "2024-06-08T21:00:17.376662Z", 2298 | "start_time": "2024-06-08T21:00:17.374666Z" 2299 | } 2300 | }, 2301 | "execution_count": 49 2302 | }, 2303 | { 2304 | "cell_type": "markdown", 2305 | "source": [ 2306 | "### Slicing\n", 2307 | "- Access a range of elements in a list using slicing.\n", 2308 | "```\n", 2309 | "list[ start: stop: step ]\n", 2310 | "```" 2311 | ], 2312 | "metadata": { 2313 | "collapsed": false 2314 | } 2315 | }, 2316 | { 2317 | "cell_type": "code", 2318 | "outputs": [ 2319 | { 2320 | "data": { 2321 | "text/plain": "[72, 85, 78, 90, 67, 31, 44, 100]" 2322 | }, 2323 | "execution_count": 50, 2324 | "metadata": {}, 2325 | "output_type": "execute_result" 2326 | } 2327 | ], 2328 | "source": [ 2329 | "temperatures = [72, 85, 78, 90, 67, 31, 44, 100]\n", 2330 | "temperatures" 2331 | ], 2332 | "metadata": { 2333 | "collapsed": false, 2334 | "ExecuteTime": { 2335 | "end_time": "2024-06-08T21:00:17.461441Z", 2336 | "start_time": "2024-06-08T21:00:17.459298Z" 2337 | } 2338 | }, 2339 | "execution_count": 50 2340 | }, 2341 | { 2342 | "cell_type": "code", 2343 | "outputs": [ 2344 | { 2345 | "data": { 2346 | "text/plain": "[78, 90, 67, 31, 44, 100]" 2347 | }, 2348 | "execution_count": 51, 2349 | "metadata": {}, 2350 | "output_type": "execute_result" 2351 | } 2352 | ], 2353 | "source": [ 2354 | "temperatures[2:]" 2355 | ], 2356 | "metadata": { 2357 | "collapsed": false, 2358 | "ExecuteTime": { 2359 | "end_time": "2024-06-08T21:00:17.481893Z", 2360 | "start_time": "2024-06-08T21:00:17.479871Z" 2361 | } 2362 | }, 2363 | "execution_count": 51 2364 | }, 2365 | { 2366 | "cell_type": "code", 2367 | "outputs": [ 2368 | { 2369 | "data": { 2370 | "text/plain": "[72, 85, 78, 90, 67, 31, 44]" 2371 | }, 2372 | "execution_count": 52, 2373 | "metadata": {}, 2374 | "output_type": "execute_result" 2375 | } 2376 | ], 2377 | "source": [ 2378 | "temperatures[:7]" 2379 | ], 2380 | "metadata": { 2381 | "collapsed": false, 2382 | "ExecuteTime": { 2383 | "end_time": "2024-06-08T21:00:17.545216Z", 2384 | "start_time": "2024-06-08T21:00:17.543199Z" 2385 | } 2386 | }, 2387 | "execution_count": 52 2388 | }, 2389 | { 2390 | "cell_type": "code", 2391 | "outputs": [ 2392 | { 2393 | "data": { 2394 | "text/plain": "[78, 90, 67, 31, 44]" 2395 | }, 2396 | "execution_count": 53, 2397 | "metadata": {}, 2398 | "output_type": "execute_result" 2399 | } 2400 | ], 2401 | "source": [ 2402 | "temperatures[2:7]" 2403 | ], 2404 | "metadata": { 2405 | "collapsed": false, 2406 | "ExecuteTime": { 2407 | "end_time": "2024-06-08T21:00:17.562840Z", 2408 | "start_time": "2024-06-08T21:00:17.560872Z" 2409 | } 2410 | }, 2411 | "execution_count": 53 2412 | }, 2413 | { 2414 | "cell_type": "code", 2415 | "outputs": [ 2416 | { 2417 | "data": { 2418 | "text/plain": "[78, 67, 44]" 2419 | }, 2420 | "execution_count": 54, 2421 | "metadata": {}, 2422 | "output_type": "execute_result" 2423 | } 2424 | ], 2425 | "source": [ 2426 | "temperatures[2:8:2]" 2427 | ], 2428 | "metadata": { 2429 | "collapsed": false, 2430 | "ExecuteTime": { 2431 | "end_time": "2024-06-08T21:00:17.590269Z", 2432 | "start_time": "2024-06-08T21:00:17.588204Z" 2433 | } 2434 | }, 2435 | "execution_count": 54 2436 | }, 2437 | { 2438 | "cell_type": "markdown", 2439 | "source": [ 2440 | "### More on List" 2441 | ], 2442 | "metadata": { 2443 | "collapsed": false 2444 | } 2445 | }, 2446 | { 2447 | "cell_type": "markdown", 2448 | "source": [ 2449 | "**Counting the total number of occurrences of a specified value**" 2450 | ], 2451 | "metadata": { 2452 | "collapsed": false 2453 | } 2454 | }, 2455 | { 2456 | "cell_type": "code", 2457 | "outputs": [ 2458 | { 2459 | "name": "stdout", 2460 | "output_type": "stream", 2461 | "text": [ 2462 | "[165, 180, 190, 180, 150, 170]\n", 2463 | "2\n" 2464 | ] 2465 | } 2466 | ], 2467 | "source": [ 2468 | "heights = [165, 180, 190, 180, 150, 170]\n", 2469 | "print(heights)\n", 2470 | "print(heights.count(180))" 2471 | ], 2472 | "metadata": { 2473 | "collapsed": false, 2474 | "ExecuteTime": { 2475 | "end_time": "2024-06-08T21:00:17.646683Z", 2476 | "start_time": "2024-06-08T21:00:17.644845Z" 2477 | } 2478 | }, 2479 | "execution_count": 55 2480 | }, 2481 | { 2482 | "cell_type": "markdown", 2483 | "source": [ 2484 | "**Sorting Lists**" 2485 | ], 2486 | "metadata": { 2487 | "collapsed": false 2488 | } 2489 | }, 2490 | { 2491 | "cell_type": "code", 2492 | "outputs": [ 2493 | { 2494 | "name": "stdout", 2495 | "output_type": "stream", 2496 | "text": [ 2497 | "[88, 92, 75, 64, 100]\n", 2498 | "[64, 75, 88, 92, 100]\n", 2499 | "[100, 92, 88, 75, 64]\n" 2500 | ] 2501 | } 2502 | ], 2503 | "source": [ 2504 | "scores = [88, 92, 75, 64, 100]\n", 2505 | "print(scores)\n", 2506 | "\n", 2507 | "scores.sort()\n", 2508 | "print(scores)\n", 2509 | "\n", 2510 | "scores.sort(reverse=True) # by default reverse=False\n", 2511 | "print(scores)" 2512 | ], 2513 | "metadata": { 2514 | "collapsed": false, 2515 | "ExecuteTime": { 2516 | "end_time": "2024-06-08T21:00:17.738097Z", 2517 | "start_time": "2024-06-08T21:00:17.735970Z" 2518 | } 2519 | }, 2520 | "execution_count": 56 2521 | }, 2522 | { 2523 | "cell_type": "markdown", 2524 | "source": [ 2525 | "**Adding up values**" 2526 | ], 2527 | "metadata": { 2528 | "collapsed": false 2529 | } 2530 | }, 2531 | { 2532 | "cell_type": "code", 2533 | "outputs": [ 2534 | { 2535 | "data": { 2536 | "text/plain": "318.17999999999995" 2537 | }, 2538 | "execution_count": 57, 2539 | "metadata": {}, 2540 | "output_type": "execute_result" 2541 | } 2542 | ], 2543 | "source": [ 2544 | "bills = [22.5, 100.39, 55.7, 88.93, 50.66]\n", 2545 | "total_bill = sum(bills)\n", 2546 | "total_bill" 2547 | ], 2548 | "metadata": { 2549 | "collapsed": false, 2550 | "ExecuteTime": { 2551 | "end_time": "2024-06-08T21:00:17.766666Z", 2552 | "start_time": "2024-06-08T21:00:17.763930Z" 2553 | } 2554 | }, 2555 | "execution_count": 57 2556 | }, 2557 | { 2558 | "cell_type": "markdown", 2559 | "source": [ 2560 | "**Checking if an item exists in a List**" 2561 | ], 2562 | "metadata": { 2563 | "collapsed": false 2564 | } 2565 | }, 2566 | { 2567 | "cell_type": "code", 2568 | "outputs": [ 2569 | { 2570 | "name": "stdout", 2571 | "output_type": "stream", 2572 | "text": [ 2573 | "Yes, apple is in the fruits list\n" 2574 | ] 2575 | } 2576 | ], 2577 | "source": [ 2578 | "fruits = ['apple', 'banana', 'cherry']\n", 2579 | "\n", 2580 | "fruit = 'apple'\n", 2581 | "if fruit in fruits:\n", 2582 | " print(f'Yes, {fruit} is in the fruits list')" 2583 | ], 2584 | "metadata": { 2585 | "collapsed": false, 2586 | "ExecuteTime": { 2587 | "end_time": "2024-06-08T21:00:17.814942Z", 2588 | "start_time": "2024-06-08T21:00:17.812564Z" 2589 | } 2590 | }, 2591 | "execution_count": 58 2592 | }, 2593 | { 2594 | "cell_type": "markdown", 2595 | "source": [ 2596 | "**Additional Resources**\n", 2597 | "1. Python documentation: [More on Lists¶](https://docs.python.org/3/tutorial/datastructures.html)\n", 2598 | "2. W3Schools: [Python Lists](https://www.w3schools.com/python/python_lists.asp)\n", 2599 | "3. Programiz: [Python List](https://www.programiz.com/python-programming/list)" 2600 | ], 2601 | "metadata": { 2602 | "collapsed": false 2603 | } 2604 | }, 2605 | { 2606 | "cell_type": "markdown", 2607 | "source": [ 2608 | "## Conditional Statements\n", 2609 | "Conditional statements allow to execute certain blocks of code based on whether a condition is true or false. They are essential for decision-making in programming.\n", 2610 | "\n", 2611 | "**Types of Conditional Statements**\n", 2612 | "1. if statement\n", 2613 | "2. if-else statement\n", 2614 | "3. if-elif-else statement\n", 2615 | "4. Nested if statements" 2616 | ], 2617 | "metadata": { 2618 | "id": "bwqkSa0VI2jT" 2619 | } 2620 | }, 2621 | { 2622 | "cell_type": "markdown", 2623 | "source": [ 2624 | "### `if` Statement\n", 2625 | "The statement evaluates a condition and executes the indented block of code if the condition is true.\n", 2626 | "\n", 2627 | "```\n", 2628 | "if condition:\n", 2629 | " # code block\n", 2630 | "```" 2631 | ], 2632 | "metadata": { 2633 | "id": "ldu7ZObBwTnb" 2634 | } 2635 | }, 2636 | { 2637 | "cell_type": "code", 2638 | "source": [ 2639 | "age = 18\n", 2640 | "\n", 2641 | "if age >= 18:\n", 2642 | " print(\"You are an adult.\")" 2643 | ], 2644 | "metadata": { 2645 | "id": "j0jAC3yeCI3K", 2646 | "executionInfo": { 2647 | "status": "ok", 2648 | "timestamp": 1717751658146, 2649 | "user_tz": -360, 2650 | "elapsed": 22, 2651 | "user": { 2652 | "displayName": "", 2653 | "userId": "" 2654 | } 2655 | }, 2656 | "colab": { 2657 | "base_uri": "https://localhost:8080/" 2658 | }, 2659 | "outputId": "be41b354-338f-4ef1-9cc4-ad2c5380ea33", 2660 | "ExecuteTime": { 2661 | "end_time": "2024-06-08T21:00:17.878631Z", 2662 | "start_time": "2024-06-08T21:00:17.876991Z" 2663 | } 2664 | }, 2665 | "execution_count": 59, 2666 | "outputs": [ 2667 | { 2668 | "name": "stdout", 2669 | "output_type": "stream", 2670 | "text": [ 2671 | "You are an adult.\n" 2672 | ] 2673 | } 2674 | ] 2675 | }, 2676 | { 2677 | "cell_type": "markdown", 2678 | "source": [ 2679 | "### `if-else` Statement\n", 2680 | "The `else` statement is used to execute a block of code if the condition in the `if` statement is false.\n", 2681 | "```\n", 2682 | "if condition:\n", 2683 | " # block of code if condition is true\n", 2684 | "else:\n", 2685 | " # block of code if condition is false\n", 2686 | "\n", 2687 | "```" 2688 | ], 2689 | "metadata": { 2690 | "id": "dvpyLT9h0NwU" 2691 | } 2692 | }, 2693 | { 2694 | "cell_type": "code", 2695 | "source": [ 2696 | "age = 16\n", 2697 | "if age >= 18:\n", 2698 | " print(\"You are an adult.\")\n", 2699 | "else:\n", 2700 | " print(\"You are a minor.\")" 2701 | ], 2702 | "metadata": { 2703 | "colab": { 2704 | "base_uri": "https://localhost:8080/" 2705 | }, 2706 | "id": "WzXazlO5xBUJ", 2707 | "executionInfo": { 2708 | "status": "ok", 2709 | "timestamp": 1717751658146, 2710 | "user_tz": -360, 2711 | "elapsed": 21, 2712 | "user": { 2713 | "displayName": "", 2714 | "userId": "" 2715 | } 2716 | }, 2717 | "outputId": "9da6c9ab-53b5-4f56-8153-022618c3acaa", 2718 | "ExecuteTime": { 2719 | "end_time": "2024-06-08T21:00:17.943630Z", 2720 | "start_time": "2024-06-08T21:00:17.941673Z" 2721 | } 2722 | }, 2723 | "execution_count": 60, 2724 | "outputs": [ 2725 | { 2726 | "name": "stdout", 2727 | "output_type": "stream", 2728 | "text": [ 2729 | "You are a minor.\n" 2730 | ] 2731 | } 2732 | ] 2733 | }, 2734 | { 2735 | "cell_type": "markdown", 2736 | "source": [ 2737 | "### `elif` Statement\n", 2738 | "The `if-elif-else` statement allows checking multiple conditions. The first true condition's code block gets executed.\n", 2739 | "\n", 2740 | "```\n", 2741 | "if condition1:\n", 2742 | " # code block for condition1\n", 2743 | "elif condition2:\n", 2744 | " # code block for condition2\n", 2745 | "else:\n", 2746 | " # code block if none of the above conditions are true\n", 2747 | "```" 2748 | ], 2749 | "metadata": { 2750 | "id": "5SfUDpyb0rcC" 2751 | } 2752 | }, 2753 | { 2754 | "cell_type": "code", 2755 | "source": [ 2756 | "score = 75\n", 2757 | "if score >= 90:\n", 2758 | " print(\"Grade: A\")\n", 2759 | "elif score >= 80:\n", 2760 | " print(\"Grade: B\")\n", 2761 | "elif score >= 70:\n", 2762 | " print(\"Grade: C\")\n", 2763 | "elif score >= 50:\n", 2764 | " print(\"Grade: D\")\n", 2765 | "else:\n", 2766 | " print(\"Grade: F\")" 2767 | ], 2768 | "metadata": { 2769 | "colab": { 2770 | "base_uri": "https://localhost:8080/" 2771 | }, 2772 | "id": "S5AFFE-j0mdo", 2773 | "executionInfo": { 2774 | "status": "ok", 2775 | "timestamp": 1717751658146, 2776 | "user_tz": -360, 2777 | "elapsed": 20, 2778 | "user": { 2779 | "displayName": "", 2780 | "userId": "" 2781 | } 2782 | }, 2783 | "outputId": "245eee52-338e-4784-d2b3-004d7e133033", 2784 | "ExecuteTime": { 2785 | "end_time": "2024-06-08T21:00:17.977496Z", 2786 | "start_time": "2024-06-08T21:00:17.975612Z" 2787 | } 2788 | }, 2789 | "execution_count": 61, 2790 | "outputs": [ 2791 | { 2792 | "name": "stdout", 2793 | "output_type": "stream", 2794 | "text": [ 2795 | "Grade: C\n" 2796 | ] 2797 | } 2798 | ] 2799 | }, 2800 | { 2801 | "cell_type": "markdown", 2802 | "source": [ 2803 | "### Nested `if` Statements\n", 2804 | " `if`, `elif`, and `else` statements can be nested to create more complex conditions.\n", 2805 | "\n", 2806 | " ```\n", 2807 | "if condition1:\n", 2808 | " if condition2:\n", 2809 | " # block of code if condition1 and condition2 are true\n", 2810 | " else:\n", 2811 | " # block of code if condition1 is true but condition2 is false\n", 2812 | "else:\n", 2813 | " # block of code if condition1 is false\n", 2814 | " ```" 2815 | ], 2816 | "metadata": { 2817 | "id": "Aj2gRDEs1tTL" 2818 | } 2819 | }, 2820 | { 2821 | "cell_type": "code", 2822 | "source": [ 2823 | "num = 10\n", 2824 | "if num > 0:\n", 2825 | " print(\"The number is positive.\")\n", 2826 | " if num % 2 == 0:\n", 2827 | " print(\"The number is even.\")\n", 2828 | " else:\n", 2829 | " print(\"The number is odd.\")\n", 2830 | "else:\n", 2831 | " print(\"The number is negative or zero.\")" 2832 | ], 2833 | "metadata": { 2834 | "colab": { 2835 | "base_uri": "https://localhost:8080/" 2836 | }, 2837 | "id": "4S9yFCEJ1WFw", 2838 | "executionInfo": { 2839 | "status": "ok", 2840 | "timestamp": 1717751658146, 2841 | "user_tz": -360, 2842 | "elapsed": 19, 2843 | "user": { 2844 | "displayName": "", 2845 | "userId": "" 2846 | } 2847 | }, 2848 | "outputId": "40713408-4347-495a-8c05-5747980fbf32", 2849 | "ExecuteTime": { 2850 | "end_time": "2024-06-08T21:00:18.037205Z", 2851 | "start_time": "2024-06-08T21:00:18.035121Z" 2852 | } 2853 | }, 2854 | "execution_count": 62, 2855 | "outputs": [ 2856 | { 2857 | "name": "stdout", 2858 | "output_type": "stream", 2859 | "text": [ 2860 | "The number is positive.\n", 2861 | "The number is even.\n" 2862 | ] 2863 | } 2864 | ] 2865 | }, 2866 | { 2867 | "cell_type": "markdown", 2868 | "source": [ 2869 | "**Additional Resources**\n", 2870 | "\n", 2871 | "1. Official Python Documentation: [Control Flow - if statements](https://docs.python.org/3/tutorial/controlflow.html#if-statements)\n", 2872 | "2. Programiz: [Python if...else Statement](https://www.programiz.com/python-programming/if-elif-else)\n", 2873 | "3. Real Python: [Real Python - Conditional Statements](https://realpython.com/python-conditional-statements/)" 2874 | ], 2875 | "metadata": { 2876 | "id": "hOE7N8XR3hNF" 2877 | } 2878 | }, 2879 | { 2880 | "cell_type": "markdown", 2881 | "source": [ 2882 | "## Loops\n", 2883 | "Loops are used to execute a block of code repeatedly until a certain condition is met. They are essential for automating repetitive tasks.\n", 2884 | "\n", 2885 | "**Types of Loops**\n", 2886 | "1. `for` loop\n", 2887 | "2. `while` loop" 2888 | ], 2889 | "metadata": { 2890 | "id": "l3sxcL606SAW" 2891 | } 2892 | }, 2893 | { 2894 | "cell_type": "markdown", 2895 | "source": [ 2896 | "### `for` loop\n", 2897 | "The `for` loop is used to iterate over a sequence (such as a list, tuple, dictionary, set, or string) or other iterable objects. It repeats the block of code for each element in the sequence.\n", 2898 | "```\n", 2899 | "for variable in sequence:\n", 2900 | " # block of code\n", 2901 | "```" 2902 | ], 2903 | "metadata": { 2904 | "id": "tj5ad4yA8PrX" 2905 | } 2906 | }, 2907 | { 2908 | "cell_type": "markdown", 2909 | "source": [ 2910 | "**Iterating over a list**" 2911 | ], 2912 | "metadata": { 2913 | "collapsed": false 2914 | } 2915 | }, 2916 | { 2917 | "cell_type": "code", 2918 | "outputs": [ 2919 | { 2920 | "name": "stdout", 2921 | "output_type": "stream", 2922 | "text": [ 2923 | "11\n", 2924 | "22\n", 2925 | "33\n", 2926 | "44\n", 2927 | "55\n" 2928 | ] 2929 | } 2930 | ], 2931 | "source": [ 2932 | "nums = [11, 22, 33, 44, 55]\n", 2933 | "for num in nums:\n", 2934 | " print(num)" 2935 | ], 2936 | "metadata": { 2937 | "collapsed": false, 2938 | "ExecuteTime": { 2939 | "end_time": "2024-06-08T21:00:18.068417Z", 2940 | "start_time": "2024-06-08T21:00:18.066743Z" 2941 | } 2942 | }, 2943 | "execution_count": 63 2944 | }, 2945 | { 2946 | "cell_type": "markdown", 2947 | "source": [ 2948 | "**Iterating over a string**" 2949 | ], 2950 | "metadata": { 2951 | "collapsed": false 2952 | } 2953 | }, 2954 | { 2955 | "cell_type": "code", 2956 | "outputs": [ 2957 | { 2958 | "name": "stdout", 2959 | "output_type": "stream", 2960 | "text": [ 2961 | "H\n", 2962 | "e\n", 2963 | "l\n", 2964 | "l\n", 2965 | "o\n" 2966 | ] 2967 | } 2968 | ], 2969 | "source": [ 2970 | "string = \"Hello\"\n", 2971 | "for char in string:\n", 2972 | " print(char)" 2973 | ], 2974 | "metadata": { 2975 | "collapsed": false, 2976 | "ExecuteTime": { 2977 | "end_time": "2024-06-08T21:00:18.235716Z", 2978 | "start_time": "2024-06-08T21:00:18.233618Z" 2979 | } 2980 | }, 2981 | "execution_count": 64 2982 | }, 2983 | { 2984 | "cell_type": "markdown", 2985 | "source": [ 2986 | "**Iterating over a range of numbers**\n", 2987 | "\n", 2988 | "`range()` function generates a sequence of numbers.\n", 2989 | "\n", 2990 | "`range(start, stop, step)`" 2991 | ], 2992 | "metadata": { 2993 | "collapsed": false 2994 | } 2995 | }, 2996 | { 2997 | "cell_type": "code", 2998 | "outputs": [ 2999 | { 3000 | "name": "stdout", 3001 | "output_type": "stream", 3002 | "text": [ 3003 | "Hello 0\n", 3004 | "Hello 1\n", 3005 | "Hello 2\n", 3006 | "Hello 3\n", 3007 | "Hello 4\n" 3008 | ] 3009 | } 3010 | ], 3011 | "source": [ 3012 | "for i in range(5):\n", 3013 | " print('Hello', i)" 3014 | ], 3015 | "metadata": { 3016 | "collapsed": false, 3017 | "ExecuteTime": { 3018 | "end_time": "2024-06-08T21:00:18.252567Z", 3019 | "start_time": "2024-06-08T21:00:18.250370Z" 3020 | } 3021 | }, 3022 | "execution_count": 65 3023 | }, 3024 | { 3025 | "cell_type": "code", 3026 | "outputs": [ 3027 | { 3028 | "name": "stdout", 3029 | "output_type": "stream", 3030 | "text": [ 3031 | "10\n", 3032 | "11\n", 3033 | "12\n", 3034 | "13\n", 3035 | "14\n" 3036 | ] 3037 | } 3038 | ], 3039 | "source": [ 3040 | "for i in range(10, 15):\n", 3041 | " print(i)" 3042 | ], 3043 | "metadata": { 3044 | "collapsed": false, 3045 | "ExecuteTime": { 3046 | "end_time": "2024-06-08T21:00:18.297685Z", 3047 | "start_time": "2024-06-08T21:00:18.295725Z" 3048 | } 3049 | }, 3050 | "execution_count": 66 3051 | }, 3052 | { 3053 | "cell_type": "code", 3054 | "outputs": [ 3055 | { 3056 | "name": "stdout", 3057 | "output_type": "stream", 3058 | "text": [ 3059 | "0\n", 3060 | "2\n", 3061 | "4\n", 3062 | "6\n", 3063 | "8\n", 3064 | "10\n", 3065 | "12\n", 3066 | "14\n", 3067 | "16\n", 3068 | "18\n" 3069 | ] 3070 | } 3071 | ], 3072 | "source": [ 3073 | "# print all the even numbers between 0 and 20\n", 3074 | "for i in range(0, 20, 2):\n", 3075 | " print(i)" 3076 | ], 3077 | "metadata": { 3078 | "collapsed": false, 3079 | "ExecuteTime": { 3080 | "end_time": "2024-06-08T21:00:18.370683Z", 3081 | "start_time": "2024-06-08T21:00:18.368827Z" 3082 | } 3083 | }, 3084 | "execution_count": 67 3085 | }, 3086 | { 3087 | "cell_type": "markdown", 3088 | "source": [ 3089 | "### `while` loop\n", 3090 | "A `while` loop repeats as long as a specified condition is true. *`while` loop is used when the number of iterations is not known in advance*.\n", 3091 | "\n", 3092 | "```\n", 3093 | "while condition:\n", 3094 | " # block of code\n", 3095 | "```" 3096 | ], 3097 | "metadata": { 3098 | "collapsed": false 3099 | } 3100 | }, 3101 | { 3102 | "cell_type": "code", 3103 | "outputs": [ 3104 | { 3105 | "name": "stdout", 3106 | "output_type": "stream", 3107 | "text": [ 3108 | "0\n", 3109 | "1\n", 3110 | "2\n", 3111 | "3\n", 3112 | "4\n" 3113 | ] 3114 | } 3115 | ], 3116 | "source": [ 3117 | "count = 0\n", 3118 | "while count < 5:\n", 3119 | " print(count)\n", 3120 | " count += 1" 3121 | ], 3122 | "metadata": { 3123 | "collapsed": false, 3124 | "ExecuteTime": { 3125 | "end_time": "2024-06-08T21:00:18.423179Z", 3126 | "start_time": "2024-06-08T21:00:18.421656Z" 3127 | } 3128 | }, 3129 | "execution_count": 68 3130 | }, 3131 | { 3132 | "cell_type": "markdown", 3133 | "source": [ 3134 | "**Enter a positive number**\n", 3135 | "In this program, a `while` loop repeatedly asks the user to input a positive number. The loop continues until the user enters a positive number." 3136 | ], 3137 | "metadata": { 3138 | "collapsed": false 3139 | } 3140 | }, 3141 | { 3142 | "cell_type": "code", 3143 | "outputs": [], 3144 | "source": [ 3145 | "# num = -1\n", 3146 | "# \n", 3147 | "# while num <= 0:\n", 3148 | "# num = int(input('Please enter a positive number: '))\n", 3149 | "# \n", 3150 | "# if num <= 0:\n", 3151 | "# print(f'The number {num} is not a positive number. Try again.')\n", 3152 | "# \n", 3153 | "# print(f'You entered a positive number!!!\\nEntered number: {num}')" 3154 | ], 3155 | "metadata": { 3156 | "collapsed": false, 3157 | "ExecuteTime": { 3158 | "end_time": "2024-06-08T21:00:18.463885Z", 3159 | "start_time": "2024-06-08T21:00:18.462424Z" 3160 | } 3161 | }, 3162 | "execution_count": 69 3163 | }, 3164 | { 3165 | "cell_type": "markdown", 3166 | "source": [ 3167 | "### Loop Control Statements" 3168 | ], 3169 | "metadata": { 3170 | "collapsed": false 3171 | } 3172 | }, 3173 | { 3174 | "cell_type": "markdown", 3175 | "source": [ 3176 | "**`break` Statement**\n", 3177 | "The `break` statement is used to exit the loop prematurely when a certain condition is met." 3178 | ], 3179 | "metadata": { 3180 | "collapsed": false 3181 | } 3182 | }, 3183 | { 3184 | "cell_type": "code", 3185 | "outputs": [ 3186 | { 3187 | "name": "stdout", 3188 | "output_type": "stream", 3189 | "text": [ 3190 | "0\n", 3191 | "1\n", 3192 | "2\n", 3193 | "3\n", 3194 | "4\n" 3195 | ] 3196 | } 3197 | ], 3198 | "source": [ 3199 | "for num in range(10):\n", 3200 | " if num == 5:\n", 3201 | " break\n", 3202 | " print(num)" 3203 | ], 3204 | "metadata": { 3205 | "collapsed": false, 3206 | "ExecuteTime": { 3207 | "end_time": "2024-06-08T21:00:18.539408Z", 3208 | "start_time": "2024-06-08T21:00:18.537728Z" 3209 | } 3210 | }, 3211 | "execution_count": 70 3212 | }, 3213 | { 3214 | "cell_type": "markdown", 3215 | "source": [ 3216 | "**`continue` Statement**\n", 3217 | "The `continue` statement skips the current iteration and moves to the next iteration of the loop." 3218 | ], 3219 | "metadata": { 3220 | "collapsed": false 3221 | } 3222 | }, 3223 | { 3224 | "cell_type": "code", 3225 | "outputs": [ 3226 | { 3227 | "name": "stdout", 3228 | "output_type": "stream", 3229 | "text": [ 3230 | "0\n", 3231 | "1\n", 3232 | "2\n", 3233 | "4\n" 3234 | ] 3235 | } 3236 | ], 3237 | "source": [ 3238 | "for num in range(5):\n", 3239 | " if num == 3:\n", 3240 | " continue\n", 3241 | " print(num)" 3242 | ], 3243 | "metadata": { 3244 | "collapsed": false, 3245 | "ExecuteTime": { 3246 | "end_time": "2024-06-08T21:00:18.566792Z", 3247 | "start_time": "2024-06-08T21:00:18.565010Z" 3248 | } 3249 | }, 3250 | "execution_count": 71 3251 | }, 3252 | { 3253 | "cell_type": "markdown", 3254 | "source": [ 3255 | "**More Resources**\n", 3256 | "1. Python Documentation:\n", 3257 | " - [for Statements](https://docs.python.org/3/tutorial/controlflow.html#for-statements)\n", 3258 | " - [While Loop](https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming)\n", 3259 | "2. GeeksforGeeks: \n", 3260 | " - [Loops in Python – For, While and Nested Loops](https://www.geeksforgeeks.org/loops-in-python/)\n", 3261 | " - [Python While Loop](https://www.geeksforgeeks.org/python-while-loop/)\n", 3262 | "3. Coursera: \n", 3263 | " - [How to Use For Loops in Python: Step by Step](https://www.coursera.org/tutorials/for-loop-python)\n", 3264 | " - [How to Write and Use Python While Loops](https://www.coursera.org/tutorials/python-while-loop)" 3265 | ], 3266 | "metadata": { 3267 | "collapsed": false 3268 | } 3269 | }, 3270 | { 3271 | "cell_type": "markdown", 3272 | "source": [ 3273 | "## Functions\n", 3274 | "A set of instructions that perform a specific task. Can be reused whenever needed." 3275 | ], 3276 | "metadata": { 3277 | "collapsed": false 3278 | } 3279 | }, 3280 | { 3281 | "cell_type": "markdown", 3282 | "source": [ 3283 | "### Defining Functions\n", 3284 | "Functions are defined using the `def` keyword.\n", 3285 | "\n", 3286 | "```\n", 3287 | "def function_name(parameters):\n", 3288 | " # function body\n", 3289 | " return value\n", 3290 | "```\n", 3291 | "\n", 3292 | "**_NOTE:_** Parameters are optional." 3293 | ], 3294 | "metadata": { 3295 | "collapsed": false 3296 | } 3297 | }, 3298 | { 3299 | "cell_type": "code", 3300 | "outputs": [], 3301 | "source": [ 3302 | "def say_hello():\n", 3303 | " print('Hello, World!')" 3304 | ], 3305 | "metadata": { 3306 | "collapsed": false, 3307 | "ExecuteTime": { 3308 | "end_time": "2024-06-08T21:00:18.627588Z", 3309 | "start_time": "2024-06-08T21:00:18.625960Z" 3310 | } 3311 | }, 3312 | "execution_count": 72 3313 | }, 3314 | { 3315 | "cell_type": "markdown", 3316 | "source": [ 3317 | "### Calling Functions\n", 3318 | "Functions are called by its name followed by parentheses." 3319 | ], 3320 | "metadata": { 3321 | "collapsed": false 3322 | } 3323 | }, 3324 | { 3325 | "cell_type": "code", 3326 | "outputs": [ 3327 | { 3328 | "name": "stdout", 3329 | "output_type": "stream", 3330 | "text": [ 3331 | "Hello, World!\n" 3332 | ] 3333 | } 3334 | ], 3335 | "source": [ 3336 | "say_hello()" 3337 | ], 3338 | "metadata": { 3339 | "collapsed": false, 3340 | "ExecuteTime": { 3341 | "end_time": "2024-06-08T21:00:18.648784Z", 3342 | "start_time": "2024-06-08T21:00:18.647204Z" 3343 | } 3344 | }, 3345 | "execution_count": 73 3346 | }, 3347 | { 3348 | "cell_type": "markdown", 3349 | "source": [ 3350 | "### Parameterized Functions\n", 3351 | "- Parameters allow to pass information into the function.\n", 3352 | "- Parameters are defined inside the parentheses.\n", 3353 | "- Parameters specify the type and number of values the function expects to receive when it's called.\n", 3354 | "\n", 3355 | "**_Arguments:_** _Actual values that is provided during a function call._ " 3356 | ], 3357 | "metadata": { 3358 | "collapsed": false 3359 | } 3360 | }, 3361 | { 3362 | "cell_type": "code", 3363 | "outputs": [], 3364 | "source": [ 3365 | "def say_hello(name):\n", 3366 | " print(f\"Hello, {name}!\")" 3367 | ], 3368 | "metadata": { 3369 | "collapsed": false, 3370 | "ExecuteTime": { 3371 | "end_time": "2024-06-08T21:00:18.690380Z", 3372 | "start_time": "2024-06-08T21:00:18.688530Z" 3373 | } 3374 | }, 3375 | "execution_count": 74 3376 | }, 3377 | { 3378 | "cell_type": "code", 3379 | "outputs": [ 3380 | { 3381 | "name": "stdout", 3382 | "output_type": "stream", 3383 | "text": [ 3384 | "Hello, Guido van Rossum!\n" 3385 | ] 3386 | } 3387 | ], 3388 | "source": [ 3389 | "say_hello('Guido van Rossum') # creator of the Python programming language" 3390 | ], 3391 | "metadata": { 3392 | "collapsed": false, 3393 | "ExecuteTime": { 3394 | "end_time": "2024-06-08T21:00:18.759283Z", 3395 | "start_time": "2024-06-08T21:00:18.757683Z" 3396 | } 3397 | }, 3398 | "execution_count": 75 3399 | }, 3400 | { 3401 | "cell_type": "markdown", 3402 | "source": [ 3403 | "### Return Values\n", 3404 | "A return value is the result that a function sends back to the place where it was called.\n", 3405 | "\n", 3406 | "The `return` statement is used to specify the return value of a function.\n" 3407 | ], 3408 | "metadata": { 3409 | "collapsed": false 3410 | } 3411 | }, 3412 | { 3413 | "cell_type": "code", 3414 | "outputs": [], 3415 | "source": [ 3416 | "def add(a, b):\n", 3417 | " return a + b" 3418 | ], 3419 | "metadata": { 3420 | "collapsed": false, 3421 | "ExecuteTime": { 3422 | "end_time": "2024-06-08T21:00:18.792625Z", 3423 | "start_time": "2024-06-08T21:00:18.791132Z" 3424 | } 3425 | }, 3426 | "execution_count": 76 3427 | }, 3428 | { 3429 | "cell_type": "code", 3430 | "outputs": [ 3431 | { 3432 | "data": { 3433 | "text/plain": "30" 3434 | }, 3435 | "execution_count": 77, 3436 | "metadata": {}, 3437 | "output_type": "execute_result" 3438 | } 3439 | ], 3440 | "source": [ 3441 | "result = add(10, 20)\n", 3442 | "result" 3443 | ], 3444 | "metadata": { 3445 | "collapsed": false, 3446 | "ExecuteTime": { 3447 | "end_time": "2024-06-08T21:00:18.873358Z", 3448 | "start_time": "2024-06-08T21:00:18.871445Z" 3449 | } 3450 | }, 3451 | "execution_count": 77 3452 | }, 3453 | { 3454 | "cell_type": "code", 3455 | "outputs": [ 3456 | { 3457 | "name": "stdout", 3458 | "output_type": "stream", 3459 | "text": [ 3460 | "20\n" 3461 | ] 3462 | } 3463 | ], 3464 | "source": [ 3465 | "def find_max(a, b):\n", 3466 | " if a>b:\n", 3467 | " return a\n", 3468 | " else:\n", 3469 | " return b\n", 3470 | "\n", 3471 | "print(find_max(10, 20))" 3472 | ], 3473 | "metadata": { 3474 | "collapsed": false, 3475 | "ExecuteTime": { 3476 | "end_time": "2024-06-08T21:00:18.941554Z", 3477 | "start_time": "2024-06-08T21:00:18.939746Z" 3478 | } 3479 | }, 3480 | "execution_count": 78 3481 | }, 3482 | { 3483 | "cell_type": "code", 3484 | "outputs": [ 3485 | { 3486 | "name": "stdout", 3487 | "output_type": "stream", 3488 | "text": [ 3489 | "True\n", 3490 | "False\n" 3491 | ] 3492 | } 3493 | ], 3494 | "source": [ 3495 | "def is_even(num):\n", 3496 | " return num % 2 == 0\n", 3497 | "\n", 3498 | "print(is_even(10))\n", 3499 | "print(is_even(7))" 3500 | ], 3501 | "metadata": { 3502 | "collapsed": false, 3503 | "ExecuteTime": { 3504 | "end_time": "2024-06-08T21:00:18.972127Z", 3505 | "start_time": "2024-06-08T21:00:18.970019Z" 3506 | } 3507 | }, 3508 | "execution_count": 79 3509 | }, 3510 | { 3511 | "cell_type": "markdown", 3512 | "source": [ 3513 | "**More Resources**\n", 3514 | "1. Python Documentation: [Defining Functions¶](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)\n", 3515 | "2. W3Schools: [Python Functions](https://www.w3schools.com/python/python_functions.asp)\n", 3516 | "3. GeeksforGeeks: [Python Functions](https://www.geeksforgeeks.org/python-functions/)" 3517 | ], 3518 | "metadata": { 3519 | "collapsed": false 3520 | } 3521 | }, 3522 | { 3523 | "cell_type": "markdown", 3524 | "source": [ 3525 | "**Happy Coding!!!**" 3526 | ], 3527 | "metadata": { 3528 | "collapsed": false 3529 | } 3530 | } 3531 | ] 3532 | } 3533 | --------------------------------------------------------------------------------