└── 01-Python ├── 01-Lec1 ├── .ipynb_checkpoints │ └── Lec1-checkpoint.ipynb ├── Lec1.ipynb ├── Untitled-2.ipynb └── img │ ├── frames.jpg │ ├── frames2.jpg │ ├── interpeter.jpg │ ├── intro.jpg │ ├── mc.png │ ├── n1.jpg │ └── van.jpg ├── 02-Lec2 ├── BIT_MATH.py ├── JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ.py ├── Lec24.ipynb ├── My_info.py ├── TimeMange.txt ├── __pycache__ │ ├── BIT_MATH.cpython-39.pyc │ ├── JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ.cpython-39.pyc │ └── My_info.cpython-39.pyc ├── check.py ├── code.asm ├── emp.txt ├── file1.txt ├── init.c ├── names.csv ├── output.txt ├── search.txt └── total.txt ├── 03-Lec3 ├── BMS.png ├── Lec3_1.ipynb ├── img1.png └── iti.png ├── Lec1 ├── .ipynb_checkpoints │ └── Lec1-checkpoint.ipynb ├── Lec1.ipynb ├── Untitled-2.ipynb └── img │ ├── frames.jpg │ ├── frames2.jpg │ ├── interpeter.jpg │ ├── intro.jpg │ ├── mc.png │ ├── n1.jpg │ └── van.jpg └── Lec2.ipynb /01-Python/01-Lec1/.ipynb_checkpoints/Lec1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "334e70fe", 6 | "metadata": {}, 7 | "source": [ 8 | "## Introduction" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "id": "26d96a00", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stderr", 19 | "output_type": "stream", 20 | "text": [ 21 | "'[title]' is not recognized as an internal or external command,\n", 22 | "operable program or batch file.\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "![title](\"img/intro.jpg\") \n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "id": "e5c3cbf8", 33 | "metadata": {}, 34 | "source": [] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 4, 39 | "id": "4fdfb8e5", 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "ename": "SyntaxError", 44 | "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"hello\")? (2253489653.py, line 1)", 45 | "output_type": "error", 46 | "traceback": [ 47 | "\u001b[1;36m Input \u001b[1;32mIn [4]\u001b[1;36m\u001b[0m\n\u001b[1;33m print \"hello\"\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"hello\")?\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "print (\"hello\")" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "id": "9332f8fe", 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "id": "d63059cd", 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "f1ebbe20", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "id": "b385835a", 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "b4b2ef1a", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "id": "ee7cdcba", 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "\n", 103 | "\n" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "a396e95b", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "id": "7728d8d3", 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "id": "6cbc9137", 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "id": "3a74190c", 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "id": "86a4e8b5", 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "id": "37d0bc91", 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "id": "5bf7f232", 158 | "metadata": {}, 159 | "outputs": [], 160 | "source": [] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "id": "b122efb4", 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [] 169 | } 170 | ], 171 | "metadata": { 172 | "kernelspec": { 173 | "display_name": "Python 3.10.0a1 64-bit", 174 | "language": "python", 175 | "name": "python3" 176 | }, 177 | "language_info": { 178 | "codemirror_mode": { 179 | "name": "ipython", 180 | "version": 3 181 | }, 182 | "file_extension": ".py", 183 | "mimetype": "text/x-python", 184 | "name": "python", 185 | "nbconvert_exporter": "python", 186 | "pygments_lexer": "ipython3", 187 | "version": "3.10.0" 188 | }, 189 | "vscode": { 190 | "interpreter": { 191 | "hash": "a0e8842e24919357e9d799e5407db1611799105107ef38b0e383b327aae76fad" 192 | } 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 5 197 | } 198 | -------------------------------------------------------------------------------- /01-Python/01-Lec1/Lec1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "334e70fe", 6 | "metadata": {}, 7 | "source": [ 8 | "## Introduction" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "26328eb1", 14 | "metadata": {}, 15 | "source": [ 16 | "## History of python \n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "26d96a00", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from IPython import display\n", 27 | "display.Image(\"img/n1.jpg\")\n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "id": "81cbda09", 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "from IPython import display\n", 38 | "display.Image(\"img/van.jpg\")" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "f337046e", 44 | "metadata": {}, 45 | "source": [ 46 | "### Python laid its foundation in the late 1980s. \n", 47 | "### The implementation of Python was started in December 1989 by Guido Van Rossum in Netherland.\n", 48 | "### Python 2.0 added new features such as list comprehensions, garbage collection systems.\n", 49 | "### It can be treated in a procedural way, an object-oriented way or a functional way\n", 50 | "### Python is an interpreted programming language, this means that as a developer\n", 51 | "### you write Python (.py) files in a text editor and then put those files into the python\n", 52 | "### interpreter to be executed" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "id": "e5c3cbf8", 58 | "metadata": {}, 59 | "source": [ 60 | "## Main usages of python \n", 61 | " \n" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "id": "88ef4ce3", 67 | "metadata": {}, 68 | "source": [ 69 | "#### Desktop Applications\n", 70 | "#### Web Applications \n", 71 | "#### Data Science\n", 72 | "#### Artificial Intelligence \n", 73 | "#### Machine Learning \n", 74 | "#### Scientific Computing \n", 75 | "#### Robotics \n", 76 | "#### Internet of Things (IoT) \n", 77 | "#### Gaming \n", 78 | "#### Mobile Apps\n", 79 | "#### Data Analysis and Preprocessing\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "a1453b7f", 85 | "metadata": {}, 86 | "source": [ 87 | "# Roles of Python in Real-time Embedded Systems\n", 88 | "\n", 89 | "\n", 90 | "## Role 1 – Device control and debugging\n", 91 | "\n", 92 | "- analyzing bus traffic such as USB, SPI or I2C. \n", 93 | "- control the bus analyzer and send messages to the embedded system.\n", 94 | "- control the tool but they also normally provide a way to develop scripts that can be used to control the tool as well.\n", 95 | "- Python is one scripting languages that is normally supported, sometimes exclusively, to interface with and control the tool.\n", 96 | "\n", 97 | "![](./img/frames.jpg) ![](./img/frames2.jpg)\n", 98 | "\n", 99 | "\n", 100 | "\n", 101 | "\n", 102 | "## Role 2 – Automating testing\n", 103 | "\n", 104 | "- ability to control tools that can send and receive messages from an embedded system through Python. \n", 105 | "- using Python to create automated tests that include regression testing.\n", 106 | "- Python scripts can be developed that set the embedded system into different states.\n", 107 | "- One advantage to using Python for automated testing is that regression tests can be developed that constantly test and exercise the system. Any code changes that result in bugs or non-conformances will be immediately discovered.\n", 108 | "\n", 109 | "## Role 3 – Data analysis\n", 110 | "\n", 111 | "- used to receive critical embedded system data that can then be stored in database or locally for analysis.\n", 112 | "- Developers can use Python to develop real-time visualizations that show critical parameters for later analysis. \n", 113 | "\n", 114 | "## Role 4 – Real-time Software\n", 115 | "\n", 116 | "- used in Real-Time ES through Micro Python port is designed to run on microcontrollers such as the ARM Cortex-M3/4.\n", 117 | "- Micro Python is not alone though. Companies such as Synapse and OpenMV are using either Micro Python or their own Python port within embedded systems. \n", 118 | "\n", 119 | "\n" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "id": "859d768c", 125 | "metadata": {}, 126 | "source": [ 127 | "# What is interperter\n", 128 | "- https://www.youtube.com/watch?v=e4ax90XmUBc\n", 129 | "\n", 130 | "\n", 131 | "![](./img/interpeter.jpg) \n", 132 | "\n" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "id": "4fdfb8e5", 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "# printing hello world \n", 143 | "print(\"Hello world \") " 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "id": "9332f8fe", 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "'''In python string is represented between\n", 154 | "double quotation “ This is a string “ or\n", 155 | "single quotation ‘ This is also a string ’'''\n", 156 | "print('This is also a string') " 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "id": "048f192a", 162 | "metadata": {}, 163 | "source": [ 164 | "## Lab1 \n", 165 | "Write a python code that will print your short \\\n", 166 | "biography. \\\n", 167 | "Full Name, Birth Year, Faculty, and \\\n", 168 | "graduation year " 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 3, 174 | "id": "d63059cd", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "I'm AlaaElnaggar\n", 182 | "My birth date is 10/4/1995\n", 183 | "I'm Graduated from HTI, Mechatronics engineering 2018\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "print (\"I'm AlaaElnaggar\") \n", 189 | "print (\"My birth date is 10/4/1995\") \n", 190 | "print (\"I'm Graduated from HTI, Mechatronics engineering 2018\") \n" 191 | ] 192 | }, 193 | { 194 | "cell_type": "markdown", 195 | "id": "f1ebbe20", 196 | "metadata": {}, 197 | "source": [ 198 | "# Variables in python \n", 199 | "Variable is a part from the memory, used to hold a piece of data. \\\n", 200 | "• Unlike other programming languages, Python has no command for declaring a\n", 201 | "variable. A variable is created the moment you first assign a value to it" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 4, 207 | "id": "b385835a", 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "100\n", 215 | "AlaaElnaggar\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "num_1 = 100 \n", 221 | "MyName=\"AlaaElnaggar\"\n", 222 | "print(num_1)\n", 223 | "print(MyName)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "id": "572a06bb", 229 | "metadata": {}, 230 | "source": [ 231 | "## Note\n", 232 | "Variables do not need to be\n", 233 | "declared with any particular\n", 234 | "type and can even change type\n", 235 | "after they have been set" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 5, 241 | "id": "b4b2ef1a", 242 | "metadata": {}, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "AlaaElnaggar\n", 249 | "100\n" 250 | ] 251 | } 252 | ], 253 | "source": [ 254 | "num_1 = 100 \n", 255 | "MyName=\"AlaaElnaggar\"\n", 256 | "num_1 = \"AlaaElnaggar\" \n", 257 | "MyName=100 \n", 258 | "print(num_1)\n", 259 | "print(MyName)" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "id": "a8fd4330", 265 | "metadata": {}, 266 | "source": [ 267 | "## Variable naming rules:\n", 268 | "Variable naming rules in Python: \\\n", 269 | "◆ A variable name must start with a letter or the underscore character \\\n", 270 | "◆ A variable name cannot start with a number \\\n", 271 | "◆ A variable name can only contain alphanumeric characters and \\\n", 272 | "underscores (A-z, 0-9, and _ ) \\\n", 273 | "◆ Variable names are case-sensitive (age, Age and AGE are three different \\\n", 274 | "variables)" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "id": "ee7cdcba", 280 | "metadata": {}, 281 | "source": [ 282 | "\n", 283 | "\n" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "id": "a396e95b", 289 | "metadata": {}, 290 | "source": [ 291 | "### Using comma operator to define multiple variables in same line:" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": null, 297 | "id": "7728d8d3", 298 | "metadata": {}, 299 | "outputs": [], 300 | "source": [ 301 | "\n", 302 | "A,B,F,S = 1 , 2 , 3 , 4\n", 303 | "\n", 304 | "print('First number is %d and second number is %d' % (F, S)) \n" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "id": "4594ad07", 310 | "metadata": {}, 311 | "source": [ 312 | "## Methods of injecting variables in string " 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "id": "fd60dd1d", 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "F,S = 5 ,10\n", 323 | "A,B = 3,4\n", 324 | "# Method 1 \n", 325 | "print(\"First number is {} and second number is {}\".format(A, B))\n", 326 | "# Method 2\n", 327 | "print(\"First number is {first} and number is {second}\".format(first=F, second=S)) \n", 328 | "# Method 3\n", 329 | "print('First number is', F, 'second number is', S) \n", 330 | "# Method 4\n", 331 | "print('First number %d and second number is %d' % (F, S))\n", 332 | "# Method 5\n", 333 | "print('First number is ' + str(F) + ' second number is '+ str(F))" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "id": "a0754d2d", 339 | "metadata": {}, 340 | "source": [ 341 | "# Input output " 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": null, 347 | "id": "6cbc9137", 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "MyName = input (\"Please Enter your name \") \n", 352 | "print (\"My name is %d\" %(MyName))" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": null, 358 | "id": "be6528d0", 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [] 362 | }, 363 | { 364 | "cell_type": "markdown", 365 | "id": "0046f0e8", 366 | "metadata": {}, 367 | "source": [ 368 | "# Lab2 \n", 369 | "Write a python code that ask about your name , age , facaulity " 370 | ] 371 | }, 372 | { 373 | "cell_type": "markdown", 374 | "id": "e5342a83", 375 | "metadata": {}, 376 | "source": [ 377 | "# Receiving more than one variable \n", 378 | " need to take the multiple inputs in a single line\n", 379 | "\n", 380 | " The split() method is useful for getting multiple inputs from users. The syntax is given below.\n", 381 | "\n", 382 | "Syntax -\n", 383 | "\n", 384 | "input().split(separator, maxsplit) \n", 385 | "The split() method is used to split the Python string, but we can use it to get the multiple values.\n", 386 | "\n", 387 | "# Why\n", 388 | "Very useful in embedded systems parsing of the records for the code from another file specifically in Bootloader " 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 7, 394 | "id": "3a74190c", 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "name": "stdout", 399 | "output_type": "stream", 400 | "text": [ 401 | "First number is 1, second number is 2 third is 3 and fourth is 4\n" 402 | ] 403 | } 404 | ], 405 | "source": [ 406 | "# taking two inputs at a time \n", 407 | "# a, b, c = input(\"Enter three values: \").split(\"M\",3) \n", 408 | "# print(\"val1: \", a) \n", 409 | "# print(\"val1: \", b) \n", 410 | "# print(\"val1: \", c) \n", 411 | "\n", 412 | "# print(\"Enter Your Last Name: \", b) \n", 413 | "# print(\"Enter Your Class: \", c) \n", 414 | "# print() \n", 415 | " \n", 416 | "# # taking three inputs at a time \n", 417 | "# x, y, z = input(\"Enter three values: \").split(\"p\",3) \n", 418 | "# print(\"Total number of students: \", x) \n", 419 | "# print(\"Number of passed student : \", y) \n", 420 | "# print(\"Number of failed student : \", z) \n", 421 | "\n", 422 | " \n", 423 | "# taking four inputs at a time with white space \n", 424 | "a, b, c, d = input(\"Enter four values: \").split() \n", 425 | "print(\"First number is {}, second number is {} third is {} and fourth is {}\".format(a, b, c, d)) \n", 426 | "# print() " 427 | ] 428 | }, 429 | { 430 | "cell_type": "markdown", 431 | "id": "86a4e8b5", 432 | "metadata": {}, 433 | "source": [ 434 | "# Type Cating in python \n", 435 | "#### • int() - constructs an integer number from an integer literal, a float literal (by\n", 436 | "#### rounding down to the previous whole number), or a string literal (providing the\n", 437 | "#### string represents a whole number)\n", 438 | "#### • float() - constructs a float number from an integer literal, a float literal or a string\n", 439 | "#### literal (providing the string represents a float or an integer)\n", 440 | "#### • str() - constructs a string from a wide variety of data types, including strings,\n", 441 | "#### integer literals and float literals" 442 | ] 443 | }, 444 | { 445 | "cell_type": "code", 446 | "execution_count": null, 447 | "id": "37d0bc91", 448 | "metadata": {}, 449 | "outputs": [], 450 | "source": [ 451 | "x = int(\"100\")\n", 452 | "print (x)\n", 453 | "x = str (100)\n", 454 | "print (x)\n", 455 | "x = float (100)\n", 456 | "print (x)\n", 457 | "# printing asci value \n", 458 | "x=ord('A') \n", 459 | "print (x)\n" 460 | ] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "id": "5bf7f232", 465 | "metadata": {}, 466 | "source": [ 467 | "# Operators in python " 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": null, 473 | "id": "f3366190", 474 | "metadata": {}, 475 | "outputs": [], 476 | "source": [ 477 | "# Arithmatic operators \n", 478 | "a = 90\n", 479 | "b = 20\n", 480 | "#additaddition \n", 481 | "print(a+b)\n", 482 | "#subtraction\n", 483 | "print(a-b)\n", 484 | "#multiplication\n", 485 | "print(a*b)\n", 486 | "#divison\n", 487 | "print(a/b)\n", 488 | "#Exponentiation\n", 489 | "print(a**b)\n", 490 | "#floor divison\n", 491 | "print(a//b)\n", 492 | "#Modulas\n", 493 | "print(a%b)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": null, 499 | "id": "b122efb4", 500 | "metadata": {}, 501 | "outputs": [], 502 | "source": [ 503 | "# relational operators \n", 504 | "a = 5\n", 505 | "b = 10\n", 506 | "\n", 507 | "print(a == b) #return False\n", 508 | "print(a != b) #return True\n", 509 | "print(a < b) #return True\n", 510 | "print(a <= b) #return True\n", 511 | "print(a > b) #return False\n", 512 | "print(a >= b) #return False" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": null, 518 | "id": "ba8aae8d", 519 | "metadata": {}, 520 | "outputs": [], 521 | "source": [ 522 | "# Bitwise operators in python \n", 523 | "\n", 524 | "a = 60 # 60 = 0011 1100 \n", 525 | "b = 13 # 13 = 0000 1101 \n", 526 | "c = 0\n", 527 | "\n", 528 | "c = a & b; # 12 = 0000 1100\n", 529 | "print (\"Line 1 - Value of c is \", c)\n", 530 | "\n", 531 | "c = a | b; # 61 = 0011 1101 \n", 532 | "print (\"Line 2 - Value of c is \", c)\n", 533 | "\n", 534 | "c = a ^ b; # 49 = 0011 0001\n", 535 | "print (\"Line 3 - Value of c is \", c)\n", 536 | "\n", 537 | "c = ~a; # -61 = 1100 0011\n", 538 | "print (\"Line 4 - Value of c is \", c)\n", 539 | "\n", 540 | "c = a << 2; # 240 = 1111 0000\n", 541 | "print (\"Line 5 - Value of c is \", c)\n", 542 | "\n", 543 | "c = a >> 2; # 15 = 0000 1111\n", 544 | "print (\"Line 6 - Value of c is \", c)" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": null, 550 | "id": "06f8229a", 551 | "metadata": {}, 552 | "outputs": [], 553 | "source": [ 554 | "\n", 555 | "# Assignment Operators\n", 556 | "a = 10\n", 557 | " \n", 558 | "# Assign value\n", 559 | "b = a\n", 560 | "print(b)\n", 561 | " \n", 562 | "# Add and assign value\n", 563 | "b += a\n", 564 | "print(b)\n", 565 | " \n", 566 | "# Subtract and assign value\n", 567 | "b -= a\n", 568 | "print(b)\n", 569 | " \n", 570 | "# multiply and assign\n", 571 | "b *= a\n", 572 | "print(b)\n", 573 | " \n", 574 | "# bitwise lishift operator\n", 575 | "b <<= a\n", 576 | "print(b)" 577 | ] 578 | }, 579 | { 580 | "cell_type": "markdown", 581 | "id": "10bda04a", 582 | "metadata": {}, 583 | "source": [ 584 | "#### Membership operators, Logical operators and Identity operators will be mentiond later " 585 | ] 586 | }, 587 | { 588 | "cell_type": "markdown", 589 | "id": "04b7aa68", 590 | "metadata": {}, 591 | "source": [ 592 | "### Complex type" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": null, 598 | "id": "9d0f7406", 599 | "metadata": {}, 600 | "outputs": [], 601 | "source": [ 602 | "# Python complex() function example \n", 603 | "# Calling function \n", 604 | "a = complex(1) # Passing single parameter \n", 605 | "b = complex(1,2) # Passing both parameters \n", 606 | "# Displaying result \n", 607 | "print(a) \n", 608 | "print(b) " 609 | ] 610 | }, 611 | { 612 | "cell_type": "markdown", 613 | "id": "178d5a7b", 614 | "metadata": {}, 615 | "source": [ 616 | "# Sequence Types in python [list , tuple , range]\n" 617 | ] 618 | }, 619 | { 620 | "cell_type": "markdown", 621 | "id": "c163686a", 622 | "metadata": {}, 623 | "source": [ 624 | "## List " 625 | ] 626 | }, 627 | { 628 | "cell_type": "markdown", 629 | "id": "8c7ded94", 630 | "metadata": {}, 631 | "source": [ 632 | "Lists are used to store multiple items in a single variable.\n", 633 | "\n", 634 | "Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.\n", 635 | "\n", 636 | "Lists are created using square brackets:" 637 | ] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": null, 642 | "id": "f6d6ceb6", 643 | "metadata": {}, 644 | "outputs": [], 645 | "source": [ 646 | "#Creating lists\n", 647 | "\n", 648 | "# my_list = ['Alaa', 1, 5.4, 'Elnaggar', 0.7]\n", 649 | "\n", 650 | "# print(my_list)\n", 651 | "\n", 652 | "# #Accessing list values\n", 653 | "# print(my_list[2])# 5.4\n", 654 | "\n", 655 | "# print(my_list[-1])# 0.7\n", 656 | "# print(my_list[:2]) # till element2\n", 657 | "\n", 658 | "# #Modifying lists\n", 659 | "# x = \"ITI\"\n", 660 | "# Y = \"567575I65767TI54667572\" \n", 661 | "# my_list.append(x) # append x to end of list\n", 662 | "\n", 663 | "# print(my_list)\n", 664 | "\n", 665 | "# my_list.extend(Y) # append all elements of Y to list\n", 666 | "# print(my_list)\n", 667 | "\n", 668 | "# my_list.insert(0, x) # insert x at index 1\n", 669 | "# print(my_list)\n", 670 | "\n", 671 | "# my_list.remove(x) # remove first occurance of x from list\n", 672 | "# print(my_list)\n", 673 | "\n", 674 | "# my_list.pop(3) # pop element at index i (defaults to end of list)\n", 675 | "# print(my_list)\n", 676 | "\n", 677 | "# my_list.clear() # delete all elements from the list\n", 678 | "# print(my_list)\n", 679 | "\n", 680 | "my_list = ['Alaa', 1, 5.4, 'Elnaggar', 0.7,'Elnaggar']\n", 681 | "x='Elnaggar'\n", 682 | "\n", 683 | "print(my_list.index(x)) # return index of element x\n", 684 | "\n", 685 | "\n", 686 | "print(my_list.count(x)) # return number of occurances of x in list\n", 687 | "\n", 688 | "my_list.reverse() # reverse elements of list in-place (no return)\n", 689 | "print(my_list)\n", 690 | "\n", 691 | "#my_list.sort(key=None, reverse=False) # sort list in-place\n", 692 | "#my_list.copy() # return a shallow copy of the list\n", 693 | "#my_nested_list = ['foobar', ['baz', 'qux'], [0]]\n" 694 | ] 695 | }, 696 | { 697 | "cell_type": "markdown", 698 | "id": "b2981485", 699 | "metadata": {}, 700 | "source": [ 701 | "# Tuple\n", 702 | "Tuples are used to store multiple items in a single variable.\n", 703 | "\n", 704 | "Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.\n", 705 | "\n", 706 | "A tuple is a collection which is ordered and unchangeable.\n", 707 | "\n", 708 | "Tuples are written with round brackets.\n", 709 | "\n", 710 | "#### Ordered\n", 711 | "When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.\n", 712 | "\n", 713 | "#### Unchangeable\n", 714 | "Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created\n", 715 | "#### Allow Duplicates\n", 716 | "Since tuples are indexed, they can have items with the same value:" 717 | ] 718 | }, 719 | { 720 | "cell_type": "code", 721 | "execution_count": null, 722 | "id": "66bb4de5", 723 | "metadata": {}, 724 | "outputs": [], 725 | "source": [ 726 | "thistuple = (\"apple\", 2, \"cherry\",2)\n", 727 | "print(thistuple)\n", 728 | "\n", 729 | "# to print length of tuple \n", 730 | "thistuple = (\"apple\", \"banana\", \"cherry\")\n", 731 | "\n", 732 | "print(len(thistuple))\n", 733 | "print(thistuple[1])\n", 734 | "#thistuple[1] = 1 # will produce error tuple is a constant \n", 735 | "\n", 736 | "# Tuble at least consist of two elements \n", 737 | "thistuple = (\"apple\",)\n", 738 | "print(type(thistuple))\n", 739 | "print(thistuple)\n", 740 | "\n", 741 | "\n", 742 | "\n", 743 | "thistuple = (\"apple\")\n", 744 | "print(type(thistuple) ) \n", 745 | "\n", 746 | "\n", 747 | "\n", 748 | "\n" 749 | ] 750 | }, 751 | { 752 | "cell_type": "markdown", 753 | "id": "e3f36f1e", 754 | "metadata": {}, 755 | "source": [ 756 | "# Range \n", 757 | "The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number \\\n", 758 | "range(start, stop, step)\n" 759 | ] 760 | }, 761 | { 762 | "cell_type": "code", 763 | "execution_count": null, 764 | "id": "6a23209d", 765 | "metadata": {}, 766 | "outputs": [], 767 | "source": [ 768 | "x = range(1, 102 , 20)\n", 769 | "\n", 770 | "for n in x:\n", 771 | " print(n)\n", 772 | "\n", 773 | " \n", 774 | "x = range(6)\n", 775 | "for n in x:\n", 776 | " print(n)" 777 | ] 778 | }, 779 | { 780 | "cell_type": "markdown", 781 | "id": "a65555e5", 782 | "metadata": {}, 783 | "source": [ 784 | "# Mapping type" 785 | ] 786 | }, 787 | { 788 | "cell_type": "markdown", 789 | "id": "977a9598", 790 | "metadata": {}, 791 | "source": [ 792 | "# dict\n", 793 | "Dictionary\n", 794 | "Dictionaries are used to store data values in key:value pairs.\n", 795 | "\n", 796 | "A dictionary is a collection which is ordered*, changeable and do not allow duplicates." 797 | ] 798 | }, 799 | { 800 | "cell_type": "markdown", 801 | "id": "6d339a51", 802 | "metadata": {}, 803 | "source": [] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": null, 808 | "id": "78df18a5", 809 | "metadata": {}, 810 | "outputs": [], 811 | "source": [ 812 | "# thisdict = {\n", 813 | "# \"brand\": \"New\",\n", 814 | "# \"model\": \"Bently\",\n", 815 | "# \"year\": 1964,\n", 816 | "# \"year\": 2020\n", 817 | "# }\n", 818 | "# print(thisdict)\n", 819 | "\n", 820 | "# # Print the number of items in the dictionary:\n", 821 | "# print(len(thisdict))\n", 822 | "\n", 823 | "#The values in dictionary items can be of any data type:\n", 824 | "thisdict = {\n", 825 | " \"brand\": \"Ford\",\n", 826 | " \"electric\": False,\n", 827 | " \"year\": 1964,\n", 828 | " \"colors\": [\"red\", \"white\", \"blue\"]\n", 829 | "}\n", 830 | "print(len(thisdict))\n", 831 | "\n", 832 | "# printing dict elements \n", 833 | "\n", 834 | "print(thisdict[\"year\"]) \n", 835 | "\n", 836 | "print(thisdict[\"colors\"][1]) " 837 | ] 838 | }, 839 | { 840 | "cell_type": "markdown", 841 | "id": "5c4e429a", 842 | "metadata": {}, 843 | "source": [ 844 | "# Set Types\n", 845 | " ### set , frozenset" 846 | ] 847 | }, 848 | { 849 | "cell_type": "markdown", 850 | "id": "160b29b3", 851 | "metadata": {}, 852 | "source": [ 853 | "## set \n", 854 | "A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).\n", 855 | "\n", 856 | " a set itself is mutable. We can add or remove items from it.\n" 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": null, 862 | "id": "1af1d72f", 863 | "metadata": {}, 864 | "outputs": [], 865 | "source": [ 866 | "# Different types of sets in Python\n", 867 | "# set of integers\n", 868 | "my_set = {1, 2, 3}\n", 869 | "print(my_set)\n", 870 | "\n", 871 | "# set of mixed datatypes\n", 872 | "my_set = {1.0, \"Hello\", (1, 2, 3)}\n", 873 | "print(my_set)\n", 874 | "print(my_set)\n", 875 | "\n", 876 | "# # set cannot have duplicates\n", 877 | "# # Output: {1, 2, 3, 4}\n", 878 | "# my_set = {1, 2, 3, 4, 3, 2}\n", 879 | "# print(my_set)\n", 880 | "\n", 881 | "# we can make set from a list\n", 882 | "# Output: {1, 2, 3}\n", 883 | "my_set = set([1, 2, 3, 2])\n", 884 | "print(my_set)\n", 885 | "\n", 886 | "\n", 887 | "# # set cannot have mutable items\n", 888 | "# # here [3, 4] is a mutable list\n", 889 | "# # this will cause an error.\n", 890 | "\n", 891 | "# my_set = {1, 2, [3, 4]}\n", 892 | "\n", 893 | "# initialize my_set\n", 894 | "# Output: set of unique elements\n", 895 | "my_set = set(\"HelloWorld\")\n", 896 | "print(my_set)\n", 897 | "\n", 898 | "my_set.add(100)\n", 899 | "print(my_set)\n", 900 | "\n", 901 | "# pop an element\n", 902 | "# Output: random element\n", 903 | "print(my_set.pop())\n", 904 | "\n", 905 | "# pop another element\n", 906 | "my_set.pop()\n", 907 | "print(my_set)\n", 908 | "\n", 909 | "# clear my_set\n", 910 | "# Output: set()\n", 911 | "my_set.clear()\n", 912 | "print(my_set)" 913 | ] 914 | }, 915 | { 916 | "cell_type": "markdown", 917 | "id": "6f550e3a", 918 | "metadata": {}, 919 | "source": [ 920 | "# The frozenset\n", 921 | " function returns an immutable frozenset object initialized with elements from the given iterable.\n", 922 | "\n", 923 | "Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation.\n", 924 | "\n", 925 | "Due to this, frozen sets can be used as keys in Dictionary or as elements of another set. But like sets, it is not ordered (the elements can be set at any index)." 926 | ] 927 | }, 928 | { 929 | "cell_type": "code", 930 | "execution_count": null, 931 | "id": "706575c6", 932 | "metadata": {}, 933 | "outputs": [], 934 | "source": [ 935 | "# tuple of vowels\n", 936 | "vowels = ('a', 'e', 'i', 'o', 'u')\n", 937 | "\n", 938 | "fSet = frozenset(vowels)\n", 939 | "print('The frozen set is:', fSet)\n", 940 | "\n", 941 | "# print('The frozen set is:', fSet[0]) # error 'frozenset' object is not subscriptable\n", 942 | "\n", 943 | "# frozensets are immutable\n", 944 | "# fSet.add('v') # error \n", 945 | "\n", 946 | "# fSet.remove(0) # allowed in normal set\n" 947 | ] 948 | }, 949 | { 950 | "cell_type": "markdown", 951 | "id": "af96ecd3", 952 | "metadata": {}, 953 | "source": [ 954 | "# bytes and bytearray\n", 955 | " \n", 956 | " are used for manipulating binary data.\n", 957 | "\n", 958 | "# The memoryview uses the buffer protocol to access the memory of other binary objects without needing to make a copy.\n", 959 | "\n", 960 | "Bytes objects are immutable sequences of single bytes. We should use them only when working with ASCII compatible data.\n", 961 | "\n", 962 | "The syntax for bytes literals is same as string literals, except that a 'b' prefix is added.\n", 963 | "\n", 964 | "bytearray objects are always created by calling the constructor bytearray(). These are mutable objects." 965 | ] 966 | }, 967 | { 968 | "cell_type": "code", 969 | "execution_count": null, 970 | "id": "37b7482e", 971 | "metadata": {}, 972 | "outputs": [], 973 | "source": [ 974 | "x = b'char_data'\n", 975 | "x = b\"char_data\"\n", 976 | " \n", 977 | "y = bytearray(5)\n", 978 | "print (y)\n", 979 | " \n", 980 | "z = memoryview(y )\n", 981 | " \n", 982 | "print(z)\n", 983 | "\n", 984 | "x = 2 \n", 985 | "y = 2*x/5\n", 986 | "\n", 987 | "x = y\n", 988 | "\n", 989 | "# print(x) # b'char_data'\n", 990 | "# print(y) # bytearray(b'\\x00\\x00\\x00\\x00\\x00')\n", 991 | "# print(z) # \n", 992 | "\n", 993 | "# bytestr = bytes(b'abc') \n", 994 | " \n", 995 | "# # initializing a string with b\n", 996 | "# # makes it a binary string\n", 997 | "# print('bytestr')\n", 998 | "# print(bytestr[0])\n", 999 | " \n", 1000 | "# print(b'\\xC2\\xA9\\x20\\xF0\\x9D\\x8C\\x86\\x20\\xE2\\x98\\x83'.decode('utf-8'))\n", 1001 | "# print(b'\\xC2\\xA9\\x20\\xF0\\x9D\\x8C\\x86\\x20\\xE2\\x98\\x83'.decode('latin-1'))\n", 1002 | "\n", 1003 | "\n", 1004 | "# bytesArr = bytearray(b'\\x00\\x0F')\n", 1005 | " \n", 1006 | "# # Bytearray allows modification\n", 1007 | "# bytesArr[0] = 255\n", 1008 | "# bytesArr.append(255)\n", 1009 | "# print(bytesArr)\n", 1010 | "\n" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "markdown", 1015 | "id": "088b5f25", 1016 | "metadata": {}, 1017 | "source": [ 1018 | "# bool types " 1019 | ] 1020 | }, 1021 | { 1022 | "cell_type": "code", 1023 | "execution_count": null, 1024 | "id": "80a93a5e", 1025 | "metadata": {}, 1026 | "outputs": [], 1027 | "source": [ 1028 | "# Python program to illustrate\n", 1029 | "# built-in method bool()\n", 1030 | " \n", 1031 | "# Returns False as x is False\n", 1032 | "x = False\n", 1033 | "print(bool(x))\n", 1034 | " \n", 1035 | "# Returns True as x is True\n", 1036 | "x = True\n", 1037 | "print(bool(x))\n", 1038 | " \n", 1039 | "# Returns False as x is not equal to y\n", 1040 | "x = 5\n", 1041 | "y = 10\n", 1042 | "print(bool(x == y))\n", 1043 | " \n", 1044 | "# Returns False as x is None\n", 1045 | "x = None\n", 1046 | "print(bool(x))\n", 1047 | " \n", 1048 | "# Returns False as x is an empty sequence\n", 1049 | "x = ()\n", 1050 | "print(bool(x))\n", 1051 | " \n", 1052 | "# Returns False as x is an empty mapping\n", 1053 | "x = {}\n", 1054 | "print(bool(x))\n", 1055 | " \n", 1056 | "# Returns False as x is 0\n", 1057 | "x = 0.0\n", 1058 | "print(bool(x))\n", 1059 | " \n", 1060 | "# Returns True as x is a non empty string\n", 1061 | "x = 'AlaaElnaggar'\n", 1062 | "print(bool(x))" 1063 | ] 1064 | }, 1065 | { 1066 | "cell_type": "markdown", 1067 | "id": "e1ff146b", 1068 | "metadata": {}, 1069 | "source": [] 1070 | }, 1071 | { 1072 | "cell_type": "code", 1073 | "execution_count": null, 1074 | "id": "c03bfa83", 1075 | "metadata": {}, 1076 | "outputs": [], 1077 | "source": [ 1078 | "print (10 > 9)\n", 1079 | "print (bool(\"Hello\"))\n", 1080 | "#empty string is used to chek if contains value or not\n", 1081 | "print (bool(\"\"))\n", 1082 | "#empty tuple is used to chek if contains value or not\n", 1083 | "print (bool())\n", 1084 | "#empty list is used to chek if contains value or not\n", 1085 | "print (bool([]))\n", 1086 | "#empty set is used to chek if contains value or not\n", 1087 | "print (bool({}))\n", 1088 | "print (type(int))" 1089 | ] 1090 | }, 1091 | { 1092 | "cell_type": "markdown", 1093 | "id": "4675157a", 1094 | "metadata": {}, 1095 | "source": [] 1096 | }, 1097 | { 1098 | "cell_type": "code", 1099 | "execution_count": null, 1100 | "id": "bebb88a5", 1101 | "metadata": {}, 1102 | "outputs": [], 1103 | "source": [ 1104 | "# bytes is a class which creates bytes\n", 1105 | "# bytes are constructor of class bytes\n", 1106 | "# when used create instance of bytes\n", 1107 | "# bytes are immutable const \n", 1108 | "empty_bytes = bytes(4)\n", 1109 | "print(empty_bytes) # b'\\x00\\x00\\x00\\x00'\n", 1110 | "print (type(empty_bytes)) # class \n", 1111 | "#empty_bytes[0]= 5 ; #'bytes' object does not support item assignment\n" 1112 | ] 1113 | }, 1114 | { 1115 | "cell_type": "code", 1116 | "execution_count": null, 1117 | "id": "a601bfa7", 1118 | "metadata": {}, 1119 | "outputs": [], 1120 | "source": [ 1121 | "\n", 1122 | "# list conversion into bytes \n", 1123 | "\n", 1124 | "# My_List =[1,2,3,2,5]\n", 1125 | "\n", 1126 | "# My_List_bytes = bytes(My_List)\n", 1127 | "\n", 1128 | "# print (My_List_bytes)\n", 1129 | "'''\n", 1130 | "# '''\n", 1131 | "str1 = 'Hello علاءworld'\n", 1132 | "\n", 1133 | "print (\"Byte Conversion :\" +str(bytes(str1,'ascii',errors = 'ignore')) )\n", 1134 | "#relace error with question martk\n", 1135 | "print (\"Byte Conversion :\" +str(bytes(str1,'ascii',errors = 'replace')) )\n", 1136 | "# stop excution \n", 1137 | "print (\"Byte Conversion :\" +str(bytes(str1,'ascii',errors = 'strict')) )" 1138 | ] 1139 | }, 1140 | { 1141 | "cell_type": "code", 1142 | "execution_count": null, 1143 | "id": "b4a04c77", 1144 | "metadata": {}, 1145 | "outputs": [], 1146 | "source": [ 1147 | "# parsing data from the user \n", 1148 | "\n", 1149 | "data = \"\\x68\\x65\\x6c\\x6c\\x6f\"\n", 1150 | "\n", 1151 | "print (data)\n", 1152 | "\n", 1153 | "print (\"Byte Conversion :\" +str(bytes(data,'ascii',errors = 'ignore')) )" 1154 | ] 1155 | }, 1156 | { 1157 | "cell_type": "code", 1158 | "execution_count": null, 1159 | "id": "962f57b5", 1160 | "metadata": {}, 1161 | "outputs": [], 1162 | "source": [ 1163 | "# byte array is a changeable array of bytes \n", 1164 | "\n", 1165 | "byte_array_1 = bytearray(5) \n", 1166 | "print (type(byte_array_1))\n", 1167 | "print (byte_array_1)\n", 1168 | "byte_array_1[0] = 5\n", 1169 | "print (byte_array_1)\n", 1170 | "byte_array_1.append(0xff)\n", 1171 | "print (byte_array_1)" 1172 | ] 1173 | }, 1174 | { 1175 | "cell_type": "code", 1176 | "execution_count": null, 1177 | "id": "690b93fc", 1178 | "metadata": {}, 1179 | "outputs": [], 1180 | "source": [ 1181 | "# Memory view is function which receiev\n", 1182 | "# array receive object of bytes or byte array \n", 1183 | "# Use python buffer protocol \n", 1184 | "# return object address \n", 1185 | "\n", 1186 | "byte_array = bytearray('AlaaElnaggar','utf-8')\n", 1187 | "\n", 1188 | "memoryview_1 = memoryview(byte_array)\n", 1189 | "memoryview_1[0] = 0xff\n", 1190 | "print (byte_array)\n", 1191 | "# print the adress of memory location for byte_array\n", 1192 | "\n", 1193 | "print (\"The address of byte_array = \" + str(memoryview_1) )" 1194 | ] 1195 | }, 1196 | { 1197 | "cell_type": "code", 1198 | "execution_count": null, 1199 | "id": "6e4a45c8", 1200 | "metadata": {}, 1201 | "outputs": [], 1202 | "source": [ 1203 | "# slicing create local copies \n", 1204 | "import time \n", 1205 | "# printing bytes \n", 1206 | "for n in (10000 , 20000 , 30000 , 40000 , 50000):\n", 1207 | "\tdata = 'A'*n\n", 1208 | "\tstart =time.time()\n", 1209 | "\twhile data:\n", 1210 | "\t\tdata = data[1:]\n", 1211 | "\tprint ('bytes ' , n ,time.time() - start)\n", 1212 | "\n", 1213 | "\n", 1214 | "for n in (10000 , 20000 , 30000 , 40000 , 50000):\n", 1215 | "\tdata = 'A'*n\n", 1216 | "\tstart =time.time()\n", 1217 | "\tdata = bytearray(data,'utf-8')\n", 1218 | "\t# no copies edit will occur in the same place \n", 1219 | "\t\n", 1220 | "\tdata = memoryview(data)\n", 1221 | "\t\n", 1222 | "\twhile data:\n", 1223 | "\t\tdata = data[1:]\n", 1224 | "\tprint ('memory view ' , n ,time.time() - start)\n" 1225 | ] 1226 | }, 1227 | { 1228 | "cell_type": "code", 1229 | "execution_count": null, 1230 | "id": "627774da", 1231 | "metadata": {}, 1232 | "outputs": [], 1233 | "source": [ 1234 | "# binary view \n", 1235 | "\n", 1236 | "print(bin(5))" 1237 | ] 1238 | }, 1239 | { 1240 | "cell_type": "code", 1241 | "execution_count": null, 1242 | "id": "4278ff78", 1243 | "metadata": {}, 1244 | "outputs": [], 1245 | "source": [ 1246 | "# membership operators \n", 1247 | "# in - not in - \n", 1248 | "a = 20 \n", 1249 | "b = 20 \n", 1250 | "\n", 1251 | "list = [1 , 2 , 3 , 4]\n", 1252 | "\n", 1253 | "if a in list:\n", 1254 | "\tprint (\"it exist\")\n", 1255 | "else:\n", 1256 | "\tprint (\"it's not exist \")\n", 1257 | "\n", 1258 | "if a not in list:\n", 1259 | "\tprint (\"Thanks\")\n", 1260 | "else:\n", 1261 | "\tprint (\"it exist\")\n", 1262 | "\n", 1263 | "if a is b :\n", 1264 | "\tprint (\"a= b\")\n", 1265 | "else:\n", 1266 | "\tprint (\"a!=b\")" 1267 | ] 1268 | }, 1269 | { 1270 | "cell_type": "code", 1271 | "execution_count": null, 1272 | "id": "b23c16ed", 1273 | "metadata": {}, 1274 | "outputs": [], 1275 | "source": [ 1276 | "# slicing\n", 1277 | "\n", 1278 | "nums = [10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 ]\n", 1279 | "\n", 1280 | "print(nums[:])\n", 1281 | "print(nums[0:])\n", 1282 | "print(nums[1:])\n", 1283 | "print(nums[2:5:])\n", 1284 | "print(nums[-1::])\n", 1285 | "print(nums[-1::-1])\n", 1286 | "print(nums[-1::2])\n", 1287 | "print(nums[-1:4:]) \n", 1288 | "print(nums[-1:-4:]) \n", 1289 | "print(nums[-1:-4:-1]) \n", 1290 | "print(nums[-5:-1:]) \n" 1291 | ] 1292 | }, 1293 | { 1294 | "cell_type": "code", 1295 | "execution_count": null, 1296 | "id": "439f0e6d", 1297 | "metadata": {}, 1298 | "outputs": [], 1299 | "source": [ 1300 | "# Statements \n", 1301 | "\n", 1302 | "a = 20\n", 1303 | "b = 20 \n", 1304 | "\n", 1305 | "if a == 10:\n", 1306 | "\tprint(\"a= 10\")\n", 1307 | "elif a == 20:\n", 1308 | "\tprint(\"a==20\")\n", 1309 | "else:\n", 1310 | "\tprint(\"Wrong Input \")" 1311 | ] 1312 | }, 1313 | { 1314 | "cell_type": "code", 1315 | "execution_count": null, 1316 | "id": "5edb19b6", 1317 | "metadata": {}, 1318 | "outputs": [], 1319 | "source": [ 1320 | "i = 1\n", 1321 | "while True:\n", 1322 | " print(i)\n", 1323 | " i = i + 1\n", 1324 | " if(i > 3):\n", 1325 | " break" 1326 | ] 1327 | }, 1328 | { 1329 | "cell_type": "code", 1330 | "execution_count": null, 1331 | "id": "5ff9da85", 1332 | "metadata": {}, 1333 | "outputs": [], 1334 | "source": [ 1335 | "\n", 1336 | "# checks if list still\n", 1337 | "# contains any element\n", 1338 | "a = [1, 2, 3, 4]\n", 1339 | " \n", 1340 | "while a:\n", 1341 | " print(a.pop())" 1342 | ] 1343 | }, 1344 | { 1345 | "cell_type": "code", 1346 | "execution_count": null, 1347 | "id": "b065eae3", 1348 | "metadata": {}, 1349 | "outputs": [], 1350 | "source": [ 1351 | "\n" 1352 | ] 1353 | } 1354 | ], 1355 | "metadata": { 1356 | "kernelspec": { 1357 | "display_name": "Python 3.9.1rc1 64-bit", 1358 | "language": "python", 1359 | "name": "python3" 1360 | }, 1361 | "language_info": { 1362 | "codemirror_mode": { 1363 | "name": "ipython", 1364 | "version": 3 1365 | }, 1366 | "file_extension": ".py", 1367 | "mimetype": "text/x-python", 1368 | "name": "python", 1369 | "nbconvert_exporter": "python", 1370 | "pygments_lexer": "ipython3", 1371 | "version": "3.9.1" 1372 | }, 1373 | "vscode": { 1374 | "interpreter": { 1375 | "hash": "43623726d6407856152d67d5465a38392b5160cd189b98f2f24e14daccd05be9" 1376 | } 1377 | } 1378 | }, 1379 | "nbformat": 4, 1380 | "nbformat_minor": 5 1381 | } 1382 | -------------------------------------------------------------------------------- /01-Python/01-Lec1/Untitled-2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "hi\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "print (\"hi\")" 18 | ] 19 | } 20 | ], 21 | "metadata": { 22 | "kernelspec": { 23 | "display_name": "Python 3.9.1rc1 64-bit", 24 | "language": "python", 25 | "name": "python3" 26 | }, 27 | "language_info": { 28 | "codemirror_mode": { 29 | "name": "ipython", 30 | "version": 3 31 | }, 32 | "file_extension": ".py", 33 | "mimetype": "text/x-python", 34 | "name": "python", 35 | "nbconvert_exporter": "python", 36 | "pygments_lexer": "ipython3", 37 | "version": "3.9.1rc1" 38 | }, 39 | "orig_nbformat": 4, 40 | "vscode": { 41 | "interpreter": { 42 | "hash": "43623726d6407856152d67d5465a38392b5160cd189b98f2f24e14daccd05be9" 43 | } 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 2 48 | } 49 | -------------------------------------------------------------------------------- /01-Python/01-Lec1/img/frames.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/01-Lec1/img/frames.jpg -------------------------------------------------------------------------------- /01-Python/01-Lec1/img/frames2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/01-Lec1/img/frames2.jpg -------------------------------------------------------------------------------- /01-Python/01-Lec1/img/interpeter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/01-Lec1/img/interpeter.jpg -------------------------------------------------------------------------------- /01-Python/01-Lec1/img/intro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/01-Lec1/img/intro.jpg -------------------------------------------------------------------------------- /01-Python/01-Lec1/img/mc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/01-Lec1/img/mc.png -------------------------------------------------------------------------------- /01-Python/01-Lec1/img/n1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/01-Lec1/img/n1.jpg -------------------------------------------------------------------------------- /01-Python/01-Lec1/img/van.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/01-Lec1/img/van.jpg -------------------------------------------------------------------------------- /01-Python/02-Lec2/BIT_MATH.py: -------------------------------------------------------------------------------- 1 | 2 | REG =0 3 | 4 | 5 | def SET_BIT(VAR,BIT): 6 | global REG 7 | VAR = int(VAR) 8 | BIT = int(BIT) 9 | result = VAR | (1 << BIT) 10 | REG = result 11 | 12 | print("Hello from BIT_MATH Module") 13 | 14 | return result 15 | 16 | def CLR_BIT(VAR,BIT): 17 | global REG 18 | VAR = int(VAR) 19 | BIT = int(BIT) 20 | result = VAR & (~(1 << BIT)) 21 | REG = result 22 | 23 | return result 24 | 25 | def GET_BIT(VAR,BIT): 26 | global REG 27 | VAR = int(VAR) 28 | BIT = int(BIT) 29 | result = (VAR >> BIT) & 1 30 | REG = result 31 | 32 | return result 33 | 34 | def TOG_BIT(VAR,BIT): 35 | global REG 36 | VAR = int(VAR) 37 | BIT = int(BIT) 38 | result =VAR ^ (1 << BIT) 39 | REG = result 40 | 41 | return result -------------------------------------------------------------------------------- /01-Python/02-Lec2/JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ.py: -------------------------------------------------------------------------------- 1 | def printModuleName(): 2 | print("Hello from JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ MODULE") -------------------------------------------------------------------------------- /01-Python/02-Lec2/My_info.py: -------------------------------------------------------------------------------- 1 | 2 | REG =0 3 | 4 | 5 | def SET_BIT(VAR,BIT): 6 | global REG 7 | VAR = int(VAR) 8 | BIT = int(BIT) 9 | result = VAR | (1 << BIT) 10 | REG = result 11 | 12 | print("Hello from BIT_MATH Module") 13 | 14 | return result 15 | 16 | def CLR_BIT(VAR,BIT): 17 | global REG 18 | VAR = int(VAR) 19 | BIT = int(BIT) 20 | result = VAR & (~(1 << BIT)) 21 | REG = result 22 | 23 | return result 24 | 25 | def GET_BIT(VAR,BIT): 26 | global REG 27 | VAR = int(VAR) 28 | BIT = int(BIT) 29 | result = (VAR >> BIT) & 1 30 | REG = result 31 | 32 | return result 33 | 34 | def TOG_BIT(VAR,BIT): 35 | global REG 36 | VAR = int(VAR) 37 | BIT = int(BIT) 38 | result =VAR ^ (1 << BIT) 39 | REG = result 40 | 41 | return result 42 | 43 | 44 | def PrintMyInfo(Info): 45 | print(Info) 46 | 47 | 48 | Data = {"Name":"AlaaElnaggar" , "City":"Benha"} 49 | -------------------------------------------------------------------------------- /01-Python/02-Lec2/TimeMange.txt: -------------------------------------------------------------------------------- 1 | 2 | '1' 3 | '1' 4 | '1' 5 | '1' -------------------------------------------------------------------------------- /01-Python/02-Lec2/__pycache__/BIT_MATH.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/02-Lec2/__pycache__/BIT_MATH.cpython-39.pyc -------------------------------------------------------------------------------- /01-Python/02-Lec2/__pycache__/JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/02-Lec2/__pycache__/JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ.cpython-39.pyc -------------------------------------------------------------------------------- /01-Python/02-Lec2/__pycache__/My_info.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/02-Lec2/__pycache__/My_info.cpython-39.pyc -------------------------------------------------------------------------------- /01-Python/02-Lec2/check.py: -------------------------------------------------------------------------------- 1 | from time import sleep 2 | 3 | # to show loading dots ..... 4 | def loading (): 5 | print("") 6 | print("LOADING") 7 | for i in range(1,30): 8 | print('.', end='',flush=True) 9 | sleep(.1) 10 | print("") 11 | 12 | 13 | # adding a dict which contains HR users and password 14 | Hr_members={'alaa':1234,'ahmed':5678,'ebrahim':9101} 15 | 16 | new_dict=dict() 17 | # set count as 1 to enfoce starting of the code 18 | count=1 19 | while(count>0): 20 | print("___________________welcome to the company system_________________________") 21 | # update the count 22 | count=select=int(input ("to access Hr system press 1\nto access employ system press 5\nto exit press 0\nYour choise is :")) 23 | # exit 24 | if (count==0): 25 | print("______________________________Thank u__________________________________\n\n") 26 | break 27 | # acessing employ system 28 | if (select==1): 29 | 30 | # enforcr loop starting 31 | x=y=3 32 | while(x>0): 33 | # need Hr acess rights 34 | Hr_name=str(input("please enter your user name:")) 35 | Hr_name=Hr_name.lower() 36 | Hr_name=Hr_name.strip() 37 | y=3 38 | # maximum time of 3 then get out from this loop 39 | while(y>0): 40 | if Hr_name in Hr_members: 41 | Hr_Pass=int (input("please enter your password")) 42 | 43 | if Hr_Pass ==Hr_members[Hr_name] : 44 | 45 | print(f"_____________________welcome mr {Hr_name} to HR system__________________") 46 | # able to acrss hr system now 47 | count=2 48 | y=x=0 49 | else: 50 | print(f"in correct password try again!!") 51 | y=y-1 52 | else: 53 | print(f"incorrect user name") 54 | y=0 55 | 56 | 57 | 58 | # HR system accessable after valid user and pass 59 | while(count==2): 60 | #print("______________________________________HR System __________________________________\n\n") 61 | count=int (input("to enter employ data press 2: \nto clear employ data press 3: \nto see all emp data press 4:\nto back press1\nYour select is : ")) 62 | # enter employ data 63 | if (count==2): 64 | new_dict1= dict() 65 | ID=(input("please enter your emp id : ")) 66 | name= (input("please enter your emp name : ")) 67 | sallary= (input("please enter your emp sallary : ")) 68 | # dict of of keu id contains dict 69 | new_dict[ID]=new_dict1 70 | new_dict1['sallary']=sallary 71 | new_dict1['name']=name 72 | # show dot lines 73 | loading () 74 | print("element added succesfuly") 75 | 76 | 77 | # clr emp data 78 | if (count==3): 79 | rejected_member_id=(input("enter the ID of emp u want to reject")) 80 | if rejected_member_id in new_dict: 81 | 82 | del new_dict[rejected_member_id] 83 | #Hr_members[id].clear(rejected_member) 84 | #Hr_members[id].clear(rejected_member) 85 | 86 | else : 87 | print("Sorry no member match " ,rejected_member_id) 88 | count=2 89 | elif count==4: 90 | 91 | loading () 92 | # showing new employ 93 | print(new_dict) 94 | count=2 95 | if count==5: 96 | emp_id=int(input ("please enter your id")) 97 | loading () 98 | if emp_id in new_dict: 99 | loading () 100 | print(" ") 101 | print(f"your data is :{new_dict[emp_id]}") 102 | else: 103 | print(f"your ID:{emp_id} don't exist") 104 | 105 | -------------------------------------------------------------------------------- /01-Python/02-Lec2/code.asm: -------------------------------------------------------------------------------- 1 | .code 2 | demomain: 3 | REPEAT 20 4 | switch rv(nrandom, 9) ; generate a number between 0 and 8 5 | mov ecx, 7 6 | case 0 7 | print "case 0" 8 | case ecx ; in contrast to most other programming languages, 9 | print "case 7" ; the Masm32 switch allows "variable cases" 10 | case 1 .. 3 11 | .if eax==1 12 | print "case 1" 13 | .elseif eax==2 14 | print "case 2" 15 | .else 16 | print "cases 1 to 3: other" 17 | .endif 18 | case 4, 6, 8 19 | print "cases 4, 6 or 8" 20 | default 21 | mov ebx, 19 ; print 20 stars 22 | .Repeat 23 | print "*" 24 | dec ebx 25 | .Until Sign? ; loop until the sign flag is set 26 | endsw 27 | print chr$(13, 10) 28 | ENDM 29 | exit 30 | end demomain -------------------------------------------------------------------------------- /01-Python/02-Lec2/emp.txt: -------------------------------------------------------------------------------- 1 | @alaa. #123 -------------------------------------------------------------------------------- /01-Python/02-Lec2/file1.txt: -------------------------------------------------------------------------------- 1 | Hello Welcome 2 | Welcome 3 | Welcome 4 | Welcome 5 | Welcome 6 | Welcome 7 | -------------------------------------------------------------------------------- /01-Python/02-Lec2/init.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/02-Lec2/init.c -------------------------------------------------------------------------------- /01-Python/02-Lec2/names.csv: -------------------------------------------------------------------------------- 1 | UART,Async,PeerToPeer 2 | SPI,Synch,SingleMasterMultiSlave 3 | I2C,Synch,SingleMasters -------------------------------------------------------------------------------- /01-Python/02-Lec2/output.txt: -------------------------------------------------------------------------------- 1 | | hafuia 2 | | 0xb34a47885262f9d8673dc77de7b583961134f09fb03620b29d282c32ee6932be 3 | | 0xD0b2612a6eE3111114b43b25322C6F08A251D38D 4 | | Total: 47.62874464666479$ 5 | | 6 | | 7 | | Tokens eth: 8 | | 20.608732$ MANA 9 | | 10 | | Protocols cro: 11 | | 17.840052$ VVS Finance 12 | | 8.953779$ V3S Finance 13 | ======= 14 | | asdf 15 | | 0x72e164aa187feaff7cb28a74b7ff800a0dfe916594c70f141069669e9df5a23b 16 | | 0xC7dFe558ed09F0f3b72eBb0A04e9d4e99af0bd0D 17 | | Total: 22.908481672796988$ 18 | | 19 | | 20 | | Tokens eth: 21 | | 22.376087$ SOS 22 | ======= 23 | | hafuia 24 | | 0xb34a47885262f9d8673dc77de7b583961134f09fb03620b29d282c32ee6932be 25 | | 0xD0b2612a6eE3111114b43b25322C6F08A251D38D 26 | | Total: 47.62874464666479$ 27 | | 28 | | 29 | | Tokens eth: 30 | | 20.608732$ MANA 31 | | 32 | | Protocols cro: 33 | | 17.840052$ VVS Finance 34 | | 8.953779$ V3S Finance 35 | ======= 36 | | asdf 37 | | 0x72e164aa187feaff7cb28a74b7ff800a0dfe916594c70f141069669e9df5a23b 38 | | 0xC7dFe558ed09F0f3b72eBb0A04e9d4e99af0bd0D 39 | | Total: 22.908481672796988$ 40 | | 41 | | 42 | | Tokens eth: 43 | | 22.376087$ SOS 44 | ======= 45 | -------------------------------------------------------------------------------- /01-Python/02-Lec2/search.txt: -------------------------------------------------------------------------------- 1 | hi hi hi hi -------------------------------------------------------------------------------- /01-Python/02-Lec2/total.txt: -------------------------------------------------------------------------------- 1 | ============ 2 | | hafuia 3 | | 0xb34a47885262f9d8673dc77de7b583961134f09fb03620b29d282c32ee6932be 4 | | 0xD0b2612a6eE3111114b43b25322C6F08A251D38D 5 | | Total: 47.62874464666479$ 6 | | 7 | | 8 | | Tokens eth: 9 | | 20.608732$ MANA 10 | | 11 | | Protocols cro: 12 | | 17.840052$ VVS Finance 13 | | 8.953779$ V3S Finance 14 | ============ 15 | | asdf 16 | | 0x72e164aa187feaff7cb28a74b7ff800a0dfe916594c70f141069669e9df5a23b 17 | | 0xC7dFe558ed09F0f3b72eBb0A04e9d4e99af0bd0D 18 | | Total: 22.908481672796988$ 19 | | 20 | | 21 | | Tokens eth: 22 | | 22.376087$ SOS 23 | ============ 24 | | asdf 25 | | 0xbce666bca3c862a2ee44651374f95aca677de16b4922c6d5e7d922cc0ac42a3d 26 | | 0x5870923a244f52fF2D119fbf5525421E32EC006e 27 | | Total: 9.077030269778557$ 28 | | 29 | | 30 | | Tokens eth: 31 | | 8.942218$ SOS 32 | ============ -------------------------------------------------------------------------------- /01-Python/03-Lec3/BMS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/03-Lec3/BMS.png -------------------------------------------------------------------------------- /01-Python/03-Lec3/Lec3_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tkinter" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "Python has a lot of GUI frameworks, but Tkinter is the only framework that’s built into the Python standard library. Tkinter has several strengths. It’s cross-platform, so the same code works on Windows, macOS, and Linux. Visual elements are rendered using native operating system elements, so applications built with Tkinter look like they belong on the platform where they’re run.\n", 15 | "\n", 16 | "Although Tkinter is considered the de facto Python GUI framework, it’s not without criticism. One notable criticism is that GUIs built with Tkinter look outdated. If you want a shiny, modern interface, then Tkinter may not be what you’re looking for.\n", 17 | "\n", 18 | "However, Tkinter is lightweight and relatively painless to use compared to other frameworks. This makes it a compelling choice for building GUI applications in Python, especially for applications where a modern sheen is unnecessary, and the top priority is to quickly build something that’s functional and cross-platform." 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "# Show your project link " 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 1, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "ename": "KeyboardInterrupt", 40 | "evalue": "", 41 | "output_type": "error", 42 | "traceback": [ 43 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 44 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 45 | "\u001b[1;32md:\\ITI_Embedded_Linux\\01-Python\\03-Lec3\\Lec3_1.ipynb Cell 5\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 23\u001b[0m label_1\u001b[39m.\u001b[39mpack(side \u001b[39m=\u001b[39m TOP)\n\u001b[0;32m 25\u001b[0m \u001b[39m# Call the main loop which is used when the application is ready to run to keep the code displaying \u001b[39;00m\n\u001b[1;32m---> 26\u001b[0m window_1\u001b[39m.\u001b[39;49mmainloop()\n", 46 | "File \u001b[1;32mc:\\Users\\AlaaElnagar\\AppData\\Local\\Programs\\Python\\Python39\\lib\\tkinter\\__init__.py:1421\u001b[0m, in \u001b[0;36mMisc.mainloop\u001b[1;34m(self, n)\u001b[0m\n\u001b[0;32m 1419\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mmainloop\u001b[39m(\u001b[39mself\u001b[39m, n\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m):\n\u001b[0;32m 1420\u001b[0m \u001b[39m\"\"\"Call the mainloop of Tk.\"\"\"\u001b[39;00m\n\u001b[1;32m-> 1421\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mtk\u001b[39m.\u001b[39;49mmainloop(n)\n", 47 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "################################Creating of a small window with lable ################################\n", 53 | "######################################################################################################\n", 54 | "\n", 55 | "\n", 56 | "\n", 57 | "# Import all methods of the module \n", 58 | "from tkinter import *\n", 59 | "\n", 60 | "# construct main window through calling TK()\n", 61 | "# window_1 = tk.Tk() for other python versions \n", 62 | "window_1 = Tk() \n", 63 | "\n", 64 | "# adding title to the window \n", 65 | "\n", 66 | "window_1.title(\"Hello From Tkinter \")\n", 67 | "\n", 68 | "# Adding lable to a specific window with a specific name \n", 69 | "\n", 70 | "label_1 =Label(window_1 , text = \"Label1\")\n", 71 | "\n", 72 | "# using geomertry function to set the lable in a specific place [BOTTOM , LEFT , RIGHT] \n", 73 | "\n", 74 | "label_1.pack(side = TOP)\n", 75 | "\n", 76 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 77 | "window_1.mainloop()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 2, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "################################Controlling window geometery#########################################\n", 94 | "#####################################################################################################\n", 95 | "\n", 96 | "\n", 97 | "# Import all methods of the module \n", 98 | "from tkinter import *\n", 99 | "\n", 100 | "# construct main window through calling TK()\n", 101 | "# window_1 = tk.Tk() for other python versions \n", 102 | "window_1 = Tk() \n", 103 | "\n", 104 | "# adding title to the window \n", 105 | "\n", 106 | "window_1.title(\"Hello From Tkinter \")\n", 107 | "\n", 108 | "# controlling window geometry in pixles \n", 109 | "window_1.geometry('100x200') # width X heoght \n", 110 | "\n", 111 | "# Adding lable to a specific window with a specific name \n", 112 | "\n", 113 | "label_1 =Label(window_1 , text = \"Label1\")\n", 114 | "\n", 115 | "# using geomertry function to set the lable in a specific place [BOTTOM , LEFT , RIGHT] \n", 116 | "\n", 117 | "label_1.pack(side = TOP)\n", 118 | "\n", 119 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 120 | "window_1.mainloop()" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 6, 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [ 129 | "################################Adding widget [button] to the window ###############################\n", 130 | "#####################################################################################################\n", 131 | "# Import all methods of the module \n", 132 | "from tkinter import *\n", 133 | "\n", 134 | "# construct main window through calling TK()\n", 135 | "# window_1 = tk.Tk() for other python versions \n", 136 | "window_1 = Tk() \n", 137 | "\n", 138 | "# adding title to the window \n", 139 | "\n", 140 | "window_1.title(\"Hello From Tkinter \")\n", 141 | "\n", 142 | "# controlling window geometry in pixles \n", 143 | "window_1.geometry('1000x500') # width X heoght \n", 144 | "\n", 145 | "# Adding lable to a specific window with a specific name \n", 146 | "\n", 147 | "label_1 =Label(window_1 , text = \"Anis\")\n", 148 | "\n", 149 | "# Adding button to a specific window with a specific name and specific button name \n", 150 | "\n", 151 | "b1 =Button(window_1 , text = \"AlaaElnaggar\")\n", 152 | "\n", 153 | "# using geomertry function to set the lable in a specific place [BOTTOM , LEFT , RIGHT] \n", 154 | "\n", 155 | "b1.pack(side = TOP)\n", 156 | "label_1.pack(side = BOTTOM)\n", 157 | "\n", 158 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 159 | "window_1.mainloop()" 160 | ] 161 | }, 162 | { 163 | "cell_type": "markdown", 164 | "metadata": {}, 165 | "source": [ 166 | "# Lab1 \n", 167 | "- write a python module that generate window with three packed buttons left, right and Top \n", 168 | "- Window size is 500x500\n", 169 | "- window title welcome tkinter " 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 7, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "################################Adding widget [button] to the window ###############################\n", 179 | "#####################################################################################################\n", 180 | "# Import all methods of the module \n", 181 | "from tkinter import *\n", 182 | "\n", 183 | "# construct main window through calling TK()\n", 184 | "# window_1 = tk.Tk() for other python versions \n", 185 | "window_1 = Tk() \n", 186 | "\n", 187 | "# adding title to the window \n", 188 | "\n", 189 | "window_1.title(\"welcome Tkinter \")\n", 190 | "\n", 191 | "# controlling window geometry in pixles \n", 192 | "window_1.geometry('1000x500') # width X heoght \n", 193 | "\n", 194 | "# Adding button to a specific window with a specific name and specific button name \n", 195 | "\n", 196 | "B_1 =Button(window_1 , text = \"B1\")\n", 197 | "B_1.pack(side = TOP)\n", 198 | "\n", 199 | "B_2 =Button(window_1 , text = \"B2\")\n", 200 | "B_2.pack(side = LEFT)\n", 201 | "\n", 202 | "B_3 =Button(window_1 , text = \"B3\")\n", 203 | "B_3.pack(side = RIGHT)\n", 204 | "\n", 205 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 206 | "window_1.mainloop()" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 13, 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [ 215 | "################################Linking widget [button] with actions ###############################\n", 216 | "#####################################################################################################\n", 217 | "\n", 218 | "from tkinter import *\n", 219 | "\n", 220 | "# construct main window through calling TK()\n", 221 | "# window_1 = tk.Tk() for other python versions \n", 222 | "window_1 = Tk() \n", 223 | "\n", 224 | "# adding title to the window \n", 225 | "\n", 226 | "window_1.title(\"welcome Tkinter \")\n", 227 | "\n", 228 | "# controlling window geometry in pixles \n", 229 | "window_1.geometry('1000x500') # width X heoght \n", 230 | "\n", 231 | "# Adding button to a specific window with a specific name and specific button name \n", 232 | "# bd border size\n", 233 | "# destroy : delete the application \n", 234 | "B_1 =Button(window_1 , text = \"Close the window\" , bd = '5' , command = window_1.destroy)\n", 235 | "B_1.pack(side = TOP)\n", 236 | "\n", 237 | "\n", 238 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 239 | "window_1.mainloop()" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 15, 245 | "metadata": {}, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "The button pressed 1\n", 252 | "The button pressed 2\n", 253 | "The button pressed 3\n", 254 | "The button pressed 4\n", 255 | "The button pressed 5\n", 256 | "The button pressed 6\n", 257 | "The button pressed 7\n", 258 | "The button pressed 8\n", 259 | "The button pressed 9\n", 260 | "The button pressed 10\n", 261 | "The button pressed 11\n", 262 | "The button pressed 12\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "################################Linking widget [button] with actions ###############################\n", 268 | "#####################################################################################################\n", 269 | "\n", 270 | "from tkinter import *\n", 271 | "\n", 272 | "def ButtonPressTracker():\n", 273 | " ButtonPressTracker.counter +=1 \n", 274 | " print(\"The button pressed\" , ButtonPressTracker.counter)\n", 275 | "ButtonPressTracker.counter =0\n", 276 | "\n", 277 | "# construct main window through calling TK()\n", 278 | "# window_1 = tk.Tk() for other python versions \n", 279 | "window_1 = Tk() \n", 280 | "\n", 281 | "# adding title to the window \n", 282 | "\n", 283 | "window_1.title(\"welcome Tkinter \")\n", 284 | "\n", 285 | "# controlling window geometry in pixles \n", 286 | "window_1.geometry('1000x500') # width X heoght \n", 287 | "\n", 288 | "\n", 289 | "B_1 =Button(window_1 , text = \"Increment The button\" , bd = '5' , command = ButtonPressTracker)\n", 290 | "B_1.pack(side = TOP)\n", 291 | "\n", 292 | "# Adding button to a specific window with a specific name and specific button name \n", 293 | "# bd border size\n", 294 | "# destroy : delete the application \n", 295 | "B_2 =Button(window_1 , text = \"Close the window\" , bd = '5' , command = window_1.destroy)\n", 296 | "B_2.pack(side = BOTTOM)\n", 297 | "\n", 298 | "\n", 299 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 300 | "window_1.mainloop()" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 25, 306 | "metadata": {}, 307 | "outputs": [], 308 | "source": [ 309 | "################################ Adding image to a button ##########################################\n", 310 | "#####################################################################################################\n", 311 | "\n", 312 | "from tkinter import *\n", 313 | "\n", 314 | "def ButtonPressTracker():\n", 315 | " ButtonPressTracker.counter +=1 \n", 316 | " print(\"The button pressed\" , ButtonPressTracker.counter)\n", 317 | "ButtonPressTracker.counter =0\n", 318 | "\n", 319 | "# construct main window through calling TK()\n", 320 | "# window_1 = tk.Tk() for other python versions \n", 321 | "window_1 = Tk() \n", 322 | "\n", 323 | "# adding title to the window \n", 324 | "\n", 325 | "window_1.title(\"welcome Tkinter \")\n", 326 | "\n", 327 | "# controlling window geometry in pixles \n", 328 | "window_1.geometry('500x500') # width X height \n", 329 | "\n", 330 | "# Adding lable widget to the window_1\n", 331 | "Label(window_1 , text = \"Image Button \" , font = ('Verdana', 30)).pack(side=TOP)\n", 332 | "\n", 333 | "# Adding a photo image object to use image \n", 334 | "photo_1 = PhotoImage(file='iti.png')\n", 335 | "\n", 336 | "# editing of the image resizing of it \n", 337 | "# resizing decreased by increasing the number \n", 338 | "photo_1 = photo_1.subsample(2,2)\n", 339 | "\n", 340 | "\n", 341 | "B_1 =Button(window_1 , text = \"Increment The button \" , bd = '5' ,image=photo_1, command = ButtonPressTracker)\n", 342 | "\n", 343 | "B_1.pack(side = TOP)\n", 344 | "\n", 345 | "# Adding button to a specific window with a specific name and specific button name \n", 346 | "# bd border size\n", 347 | "# destroy : delete the application \n", 348 | "B_2 =Button(window_1 , text = \"Close the window\" , bd = '5' , command = window_1.destroy)\n", 349 | "B_2.pack(side = BOTTOM)\n", 350 | "\n", 351 | "\n", 352 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 353 | "window_1.mainloop()" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 26, 359 | "metadata": {}, 360 | "outputs": [], 361 | "source": [ 362 | "################################ Usage of colours ##########################################\n", 363 | "#####################################################################################################\n", 364 | "\n", 365 | "from tkinter import *\n", 366 | "\n", 367 | "def ButtonPressTracker():\n", 368 | " ButtonPressTracker.counter +=1 \n", 369 | " print(\"The button pressed\" , ButtonPressTracker.counter)\n", 370 | "ButtonPressTracker.counter =0\n", 371 | "\n", 372 | "# construct main window through calling TK()\n", 373 | "# window_1 = tk.Tk() for other python versions \n", 374 | "window_1 = Tk() \n", 375 | "\n", 376 | "# adding title to the window \n", 377 | "\n", 378 | "window_1.title(\"welcome Tkinter \")\n", 379 | "\n", 380 | "# controlling window geometry in pixles \n", 381 | "window_1.geometry('500x500') # width X height \n", 382 | "\n", 383 | "# Adding lable widget to the window_1\n", 384 | "Label(window_1 , text = \"Image Button \" ,background=\"green\" , fg=\"red\" , font = ('Verdana', 15)).pack(side=TOP)\n", 385 | "\n", 386 | "# Adding a photo image object to use image \n", 387 | "photo_1 = PhotoImage(file='iti.png')\n", 388 | "\n", 389 | "# editing of the image resizing of it \n", 390 | "# resizing decreased by increasing the number \n", 391 | "photo_1 = photo_1.subsample(2,2)\n", 392 | "\n", 393 | "\n", 394 | "B_1 =Button(window_1 , text = \"Increment The button \" ,background=\"blue\" , fg=\"black\" , bd = '5' ,image=photo_1, command = ButtonPressTracker)\n", 395 | "\n", 396 | "B_1.pack(side = TOP)\n", 397 | "\n", 398 | "# Adding button to a specific window with a specific name and specific button name \n", 399 | "# bd border size\n", 400 | "# destroy : delete the application \n", 401 | "B_2 =Button(window_1 , text = \"Close the window\" ,background=\"green\" , fg=\"red\", bd = '5' , command = window_1.destroy)\n", 402 | "B_2.pack(side = BOTTOM)\n", 403 | "\n", 404 | "\n", 405 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 406 | "window_1.mainloop()" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 28, 412 | "metadata": {}, 413 | "outputs": [], 414 | "source": [ 415 | "################################ Adjusting the location ##########################################\n", 416 | "#####################################################################################################\n", 417 | "\n", 418 | "from tkinter import *\n", 419 | "\n", 420 | "def ButtonPressTracker():\n", 421 | " ButtonPressTracker.counter +=1 \n", 422 | " print(\"The button pressed\" , ButtonPressTracker.counter)\n", 423 | "ButtonPressTracker.counter =0\n", 424 | "\n", 425 | "# construct main window through calling TK()\n", 426 | "# window_1 = tk.Tk() for other python versions \n", 427 | "window_1 = Tk() \n", 428 | "\n", 429 | "# adding title to the window \n", 430 | "\n", 431 | "window_1.title(\"welcome Tkinter \")\n", 432 | "\n", 433 | "# controlling window geometry in pixles \n", 434 | "window_1.geometry('500x500') # width X height \n", 435 | "\n", 436 | "# Adding lable widget to the window_1\n", 437 | "Label(window_1 , text = \"Image Button \" ,background=\"green\" , fg=\"red\" , font = ('Verdana', 15)).pack(side=TOP)\n", 438 | "\n", 439 | "\n", 440 | "# Adding a photo image object to use image \n", 441 | "photo_1 = PhotoImage(file='iti.png')\n", 442 | "\n", 443 | "# editing of the image resizing of it \n", 444 | "# resizing decreased by increasing the number \n", 445 | "photo_1 = photo_1.subsample(2,2)\n", 446 | "\n", 447 | "\n", 448 | "B_1 =Button(window_1 , text = \"Increment The button \" ,background=\"blue\" , fg=\"black\" , bd = '5' ,image=photo_1, command = ButtonPressTracker)\n", 449 | "\n", 450 | "B_1.place(x= 50,y = 50)\n", 451 | "\n", 452 | "# Adding button to a specific window with a specific name and specific button name \n", 453 | "# bd border size\n", 454 | "# destroy : delete the application \n", 455 | "B_2 =Button(window_1 , text = \"Close the window\" ,background=\"green\" , fg=\"red\", bd = '5' , command = window_1.destroy)\n", 456 | "B_2.place(x = 300,y = 400 )\n", 457 | "\n", 458 | "\n", 459 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 460 | "window_1.mainloop()" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 1, 466 | "metadata": {}, 467 | "outputs": [], 468 | "source": [ 469 | "################################ Anchor ##########################################\n", 470 | "###################################################################################################\n", 471 | "\n", 472 | "from tkinter import *\n", 473 | "\n", 474 | "def ButtonPressTracker():\n", 475 | " ButtonPressTracker.counter +=1 \n", 476 | " print(\"The button pressed\" , ButtonPressTracker.counter)\n", 477 | "ButtonPressTracker.counter =0\n", 478 | "\n", 479 | "# construct main window through calling TK()\n", 480 | "# window_1 = tk.Tk() for other python versions \n", 481 | "window_1 = Tk() \n", 482 | "\n", 483 | "# adding title to the window \n", 484 | "\n", 485 | "window_1.title(\"welcome Tkinter \")\n", 486 | "\n", 487 | "# controlling window geometry in pixles \n", 488 | "window_1.geometry('200x200') # width X height \n", 489 | "\n", 490 | "B_2 =Button(window_1 , text = \"Close the window\" ,background=\"green\" , fg=\"red\", bd = '5' , command = window_1.destroy)\n", 491 | "\n", 492 | "# anchor [NW N NE W CENTER E SW S SE]\n", 493 | "B_2.place(relx = 0.5,rely = 0, anchor = CENTER )\n", 494 | "\n", 495 | "\n", 496 | "# Call the main loop which is used when the application is ready to run to keep the code displaying \n", 497 | "window_1.mainloop()" 498 | ] 499 | }, 500 | { 501 | "cell_type": "markdown", 502 | "metadata": {}, 503 | "source": [ 504 | "# Position widget\n", 505 | "- pack() organizes widgets in horizontal and vertical boxes that are limited to left, right, top, bottom positions. Each box is offset and relative to each other.\n", 506 | "- place() places widgets in a two dimensional grid using x and y absolute coordinates. \n", 507 | "- grid() locates widgets in a two dimensional grid using row and column absolute coordinates. \n", 508 | "- Important: pack(), place(), and grid() should not be combined in the same master window. Instead choose one and stick with it." 509 | ] 510 | }, 511 | { 512 | "cell_type": "markdown", 513 | "metadata": {}, 514 | "source": [ 515 | "## Vertical Positioning with Pack \n" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 35, 521 | "metadata": {}, 522 | "outputs": [], 523 | "source": [ 524 | "################################ Packing #########################################\n", 525 | "###################################################################################################\n", 526 | "from tkinter import *\n", 527 | "\n", 528 | "root = Tk()\n", 529 | "\n", 530 | "# adding title to the window \n", 531 | "\n", 532 | "root.title(\"welcome Tkinter \")\n", 533 | "\n", 534 | "# controlling window geometry in pixles \n", 535 | "root.geometry('200x200') # width X height \n", 536 | "test = Label(root, text=\"Red\",width=5,height=2 ,bg=\"red\", fg=\"white\")\n", 537 | "test.pack(side=TOP)\n", 538 | "test = Label(root, text=\"Green\", width=5,height=2 ,bg=\"green\", fg=\"white\")\n", 539 | "test.pack(side=TOP)\n", 540 | "test = Label(root, text=\"Purple\", width=5,height=2 ,bg=\"purple\", fg=\"white\")\n", 541 | "test.pack(side=TOP)\n", 542 | "\n", 543 | "mainloop()\n" 544 | ] 545 | }, 546 | { 547 | "cell_type": "markdown", 548 | "metadata": {}, 549 | "source": [ 550 | "## Side-by-Side Positioning with Pack\n" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 36, 556 | "metadata": {}, 557 | "outputs": [], 558 | "source": [ 559 | "################################ Packing #########################################\n", 560 | "###################################################################################################\n", 561 | "from tkinter import *\n", 562 | "\n", 563 | "root = Tk()\n", 564 | "\n", 565 | "# adding title to the window \n", 566 | "\n", 567 | "root.title(\"welcome Tkinter \")\n", 568 | "\n", 569 | "# controlling window geometry in pixles \n", 570 | "root.geometry('200x200') # width X height \n", 571 | "test = Label(root, text=\"Red\",width=5,height=2 ,bg=\"red\", fg=\"white\")\n", 572 | "test.pack(side=LEFT)\n", 573 | "test = Label(root, text=\"Green\", width=5,height=2 ,bg=\"green\", fg=\"white\")\n", 574 | "test.pack(side=LEFT)\n", 575 | "test = Label(root, text=\"Purple\", width=5,height=2 ,bg=\"purple\", fg=\"white\")\n", 576 | "test.pack(side=LEFT)\n", 577 | "\n", 578 | "mainloop()\n" 579 | ] 580 | }, 581 | { 582 | "cell_type": "markdown", 583 | "metadata": {}, 584 | "source": [ 585 | "#### place() lets you position a widget either with absolute x,y coordinates, or relative to another widget. " 586 | ] 587 | }, 588 | { 589 | "cell_type": "code", 590 | "execution_count": 2, 591 | "metadata": {}, 592 | "outputs": [], 593 | "source": [ 594 | "from tkinter import *\n", 595 | "root = Tk()\n", 596 | "# we can set the geometery as following \n", 597 | "root.geometry('250x200')\n", 598 | "Label(root, text=\"Position 1 : x=0, y=0\", bg=\"#FFFF00\", fg=\"black\").place(x=5, y=0)\n", 599 | "Label(root, text=\"Position 2 : x=50, y=40\", bg=\"#3300CC\", fg=\"white\").place(x=50, y=40)\n", 600 | "Label(root, text=\"Position 3 : x=75, y=80\", bg=\"#FF0099\", fg=\"white\").place(x=75, y=80)\n", 601 | "\n", 602 | "root.mainloop()" 603 | ] 604 | }, 605 | { 606 | "cell_type": "markdown", 607 | "metadata": {}, 608 | "source": [ 609 | "# Positioning Widgets With Grid Layout Manager \n", 610 | "- grid() positions widgets in a two dimensional grid of rows and columns similar to a spreadsheet." 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 11, 616 | "metadata": {}, 617 | "outputs": [ 618 | { 619 | "ename": "TclError", 620 | "evalue": "cannot use geometry manager pack inside . which already has slaves managed by grid", 621 | "output_type": "error", 622 | "traceback": [ 623 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 624 | "\u001b[1;31mTclError\u001b[0m Traceback (most recent call last)", 625 | "\u001b[1;32md:\\ITI_Embedded_Linux\\01-Python\\03-Lec3\\Lec3_1.ipynb Cell 25\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 23\u001b[0m Label(text\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mPosition 20\u001b[39m\u001b[39m\"\u001b[39m, bg\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m#FFFF00\u001b[39m\u001b[39m\"\u001b[39m, fg\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mblack\u001b[39m\u001b[39m\"\u001b[39m, width\u001b[39m=\u001b[39m\u001b[39m10\u001b[39m)\u001b[39m.\u001b[39mgrid(row\u001b[39m=\u001b[39m\u001b[39m9\u001b[39m, column\u001b[39m=\u001b[39m\u001b[39m1\u001b[39m)\n\u001b[0;32m 24\u001b[0m test \u001b[39m=\u001b[39m Label(root, text\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mPurple\u001b[39m\u001b[39m\"\u001b[39m, width\u001b[39m=\u001b[39m\u001b[39m5\u001b[39m,height\u001b[39m=\u001b[39m\u001b[39m2\u001b[39m ,bg\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mpurple\u001b[39m\u001b[39m\"\u001b[39m, fg\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mwhite\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m---> 25\u001b[0m test\u001b[39m.\u001b[39;49mpack(side\u001b[39m=\u001b[39;49mLEFT)\n\u001b[0;32m 26\u001b[0m root\u001b[39m.\u001b[39mmainloop()\n", 626 | "File \u001b[1;32mc:\\Users\\AlaaElnagar\\AppData\\Local\\Programs\\Python\\Python39\\lib\\tkinter\\__init__.py:2388\u001b[0m, in \u001b[0;36mPack.pack_configure\u001b[1;34m(self, cnf, **kw)\u001b[0m\n\u001b[0;32m 2372\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mpack_configure\u001b[39m(\u001b[39mself\u001b[39m, cnf\u001b[39m=\u001b[39m{}, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkw):\n\u001b[0;32m 2373\u001b[0m \u001b[39m\"\"\"Pack a widget in the parent widget. Use as options:\u001b[39;00m\n\u001b[0;32m 2374\u001b[0m \u001b[39m after=widget - pack it after you have packed widget\u001b[39;00m\n\u001b[0;32m 2375\u001b[0m \u001b[39m anchor=NSEW (or subset) - position widget according to\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 2386\u001b[0m \u001b[39m side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.\u001b[39;00m\n\u001b[0;32m 2387\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[1;32m-> 2388\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mtk\u001b[39m.\u001b[39;49mcall(\n\u001b[0;32m 2389\u001b[0m (\u001b[39m'\u001b[39;49m\u001b[39mpack\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mconfigure\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_w)\n\u001b[0;32m 2390\u001b[0m \u001b[39m+\u001b[39;49m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_options(cnf, kw))\n", 627 | "\u001b[1;31mTclError\u001b[0m: cannot use geometry manager pack inside . which already has slaves managed by grid" 628 | ] 629 | } 630 | ], 631 | "source": [ 632 | "from tkinter import *\n", 633 | "root = Tk()\n", 634 | "\n", 635 | "Label(text=\"Position 1\", bg=\"#FFF111\", fg=\"black\", width=10).grid(row=0, column=0)\n", 636 | "Label(text=\"Position 2\", bg=\"#AFFF00\", fg=\"black\", width=10).grid(row=0, column=1)\n", 637 | "Label(text=\"Position 3\", bg=\"#FFBF00\", fg=\"black\", width=10).grid(row=1, column=0)\n", 638 | "Label(text=\"Position 4\", bg=\"#CFFF00\", fg=\"black\", width=10).grid(row=1, column=1)\n", 639 | "Label(text=\"Position 5\", bg=\"#DFFF00\", fg=\"black\", width=10).grid(row=2, column=0)\n", 640 | "Label(text=\"Position 6\", bg=\"#EFFF00\", fg=\"black\", width=10).grid(row=2, column=1)\n", 641 | "Label(text=\"Position 7\", bg=\"#00F000\", fg=\"black\", width=10).grid(row=3, column=0)\n", 642 | "Label(text=\"Position 8\", bg=\"#FAEF00\", fg=\"black\", width=10).grid(row=3, column=1)\n", 643 | "Label(text=\"Position 9\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=4, column=0)\n", 644 | "Label(text=\"Position 10\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=4, column=1)\n", 645 | "Label(text=\"Position 11\", bg=\"#AAAF00\", fg=\"black\", width=10).grid(row=5, column=0)\n", 646 | "Label(text=\"Position 12\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=5, column=1)\n", 647 | "Label(text=\"Position 13\", bg=\"#CCCE00\", fg=\"black\", width=10).grid(row=6, column=0)\n", 648 | "Label(text=\"Position 14\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=6, column=1)\n", 649 | "Label(text=\"Position 15\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=7, column=0)\n", 650 | "Label(text=\"Position 16\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=7, column=1)\n", 651 | "Label(text=\"Position 17\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=8, column=0)\n", 652 | "Label(text=\"Position 18\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=8, column=1)\n", 653 | "Label(text=\"Position 19\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=9, column=0)\n", 654 | "Label(text=\"Position 20\", bg=\"#FFFF00\", fg=\"black\", width=10).grid(row=9, column=1)\n", 655 | "\n", 656 | "root.mainloop()" 657 | ] 658 | }, 659 | { 660 | "cell_type": "markdown", 661 | "metadata": {}, 662 | "source": [ 663 | "# The Entry widget is used to accept single-line text strings from a user.\n", 664 | "\n", 665 | "- If you want to display multiple lines of text that can be edited, then you should use the Text widget.\n", 666 | "\n", 667 | "- If you want to display one or more lines of text that cannot be modified by the user, then you should use the Label widget." 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "execution_count": 14, 673 | "metadata": {}, 674 | "outputs": [], 675 | "source": [ 676 | "from tkinter import *\n", 677 | "\n", 678 | "top = Tk()\n", 679 | "L1 = Label(top, text=\"User Name\")\n", 680 | "L1.pack( side = LEFT)\n", 681 | "E1 = Entry(top, bd =5)\n", 682 | "E1.pack(side = RIGHT)\n", 683 | "\n", 684 | "top.mainloop()" 685 | ] 686 | }, 687 | { 688 | "cell_type": "markdown", 689 | "metadata": {}, 690 | "source": [ 691 | "Methods: The various methods provided by the entry widget are: \n", 692 | "\n", 693 | "- get() : Returns the entry’s current text as a string. \n", 694 | "- insert ( index, ‘name’) : Inserts string ‘name’ before the character at the given index. " 695 | ] 696 | }, 697 | { 698 | "cell_type": "code", 699 | "execution_count": 1, 700 | "metadata": {}, 701 | "outputs": [], 702 | "source": [ 703 | "# Program to make a simple\n", 704 | "# login screen\n", 705 | "\n", 706 | "import tkinter as tk\n", 707 | "\n", 708 | "root=tk.Tk()\n", 709 | "\n", 710 | "# setting the windows size\n", 711 | "root.geometry(\"300x100\")\n", 712 | "\n", 713 | "# declaring string variable\n", 714 | "# for storing name and password\n", 715 | "name_var=tk.StringVar()\n", 716 | "passw_var=tk.StringVar()\n", 717 | "\n", 718 | "\n", 719 | "# defining a function that will\n", 720 | "# get the name and password and\n", 721 | "# print them on the screen\n", 722 | "def submit():\n", 723 | "\n", 724 | "\tname=name_var.get()\n", 725 | "\tpassword=passw_var.get()\n", 726 | "\t\n", 727 | "\tprint(\"The name is : \" + name)\n", 728 | "\tprint(\"The password is : \" + password)\n", 729 | "\t\n", 730 | "\tname_var.set(\"\")\n", 731 | "\tpassw_var.set(\"\")\n", 732 | "\t\n", 733 | "\t\n", 734 | "# creating a label for\n", 735 | "# name using widget Label\n", 736 | "name_label = tk.Label(root, text = 'Username', font=('calibre',10, 'bold'))\n", 737 | "\n", 738 | "# creating a entry for input\n", 739 | "# name using widget Entry\n", 740 | "name_entry = tk.Entry(root,textvariable = name_var, font=('calibre',10,'normal'))\n", 741 | "\n", 742 | "# creating a label for password\n", 743 | "passw_label = tk.Label(root, text = 'Password', font = ('calibre',10,'bold'))\n", 744 | "\n", 745 | "# creating a entry for password\n", 746 | "passw_entry=tk.Entry(root, textvariable = passw_var, font = ('calibre',10,'normal'), show = '@')\n", 747 | "\n", 748 | "# creating a button using the widget\n", 749 | "# Button that will call the submit function\n", 750 | "sub_btn=tk.Button(root,text = 'Submit', command = submit)\n", 751 | "\n", 752 | "# placing the label and entry in\n", 753 | "# the required position using grid\n", 754 | "# method\n", 755 | "name_label.grid(row=0,column=0)\n", 756 | "name_entry.grid(row=0,column=1)\n", 757 | "passw_label.grid(row=1,column=0)\n", 758 | "passw_entry.grid(row=1,column=1)\n", 759 | "sub_btn.grid(row=2,column=1)\n", 760 | "\n", 761 | "# performing an infinite loop\n", 762 | "# for the window to display\n", 763 | "root.mainloop()\n" 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "execution_count": 3, 769 | "metadata": {}, 770 | "outputs": [ 771 | { 772 | "name": "stdout", 773 | "output_type": "stream", 774 | "text": [ 775 | "led off\n", 776 | "motor on\n", 777 | "motor off\n", 778 | "led on\n", 779 | "led on\n", 780 | "led on\n", 781 | "led on\n", 782 | "led on\n", 783 | "led off\n" 784 | ] 785 | } 786 | ], 787 | "source": [ 788 | "\n", 789 | "import tkinter\n", 790 | "window_name=tkinter.Tk() #creat object from the window \n", 791 | "\n", 792 | "window_name.geometry(\"120x120\")\n", 793 | "#tkinter.Radiobutton()\n", 794 | "var=tkinter.IntVar() #WE CANT ACCESS THE VAR WHICH HAVE THE VARIABLE IN TK\n", 795 | "var1=tkinter.IntVar()\n", 796 | "def fun():\n", 797 | "\tglobal var\n", 798 | "\tif var.get()==0:\n", 799 | "\t\tprint(\"led on\")\n", 800 | "\telif var.get()==1:\n", 801 | "\t\tprint(\"led off\")\n", 802 | "def fun1():\n", 803 | "\tglobal var1\n", 804 | "\tif var1.get()==0:\n", 805 | "\t\tprint(\"motor on\")\n", 806 | "\telif var1.get()==1:\n", 807 | "\t\tprint(\"motor off\")\n", 808 | "RB1=tkinter.Radiobutton(window_name,value=0,variable=var,text=\"input1\",command=fun)\n", 809 | "RB1.grid(row=0,column=1)\n", 810 | "RB2=tkinter.Radiobutton(window_name,value=1,variable=var,text=\"input2\",command=fun)\n", 811 | "RB2.grid(row=0,column=2)\n", 812 | "#------------------------------------\n", 813 | "RB3=tkinter.Radiobutton(window_name,value=0,variable=var1,text=\"input3\",command =fun1)\n", 814 | "RB3.grid(row=1,column=1)\n", 815 | "RB4=tkinter.Radiobutton(window_name,value=1,variable=var1,text=\"input4\",command =fun1)\n", 816 | "RB4.grid(row=1,column=2)\n", 817 | "#------------------------------------\n", 818 | "window_name.mainloop()\n", 819 | "\n", 820 | "\n", 821 | "\n", 822 | "\n", 823 | "\n" 824 | ] 825 | } 826 | ], 827 | "metadata": { 828 | "kernelspec": { 829 | "display_name": "Python 3.9.1rc1 64-bit", 830 | "language": "python", 831 | "name": "python3" 832 | }, 833 | "language_info": { 834 | "codemirror_mode": { 835 | "name": "ipython", 836 | "version": 3 837 | }, 838 | "file_extension": ".py", 839 | "mimetype": "text/x-python", 840 | "name": "python", 841 | "nbconvert_exporter": "python", 842 | "pygments_lexer": "ipython3", 843 | "version": "3.9.1rc1" 844 | }, 845 | "orig_nbformat": 4, 846 | "vscode": { 847 | "interpreter": { 848 | "hash": "43623726d6407856152d67d5465a38392b5160cd189b98f2f24e14daccd05be9" 849 | } 850 | } 851 | }, 852 | "nbformat": 4, 853 | "nbformat_minor": 2 854 | } 855 | -------------------------------------------------------------------------------- /01-Python/03-Lec3/img1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/03-Lec3/img1.png -------------------------------------------------------------------------------- /01-Python/03-Lec3/iti.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/03-Lec3/iti.png -------------------------------------------------------------------------------- /01-Python/Lec1/.ipynb_checkpoints/Lec1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "334e70fe", 6 | "metadata": {}, 7 | "source": [ 8 | "## Introduction" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "id": "26d96a00", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stderr", 19 | "output_type": "stream", 20 | "text": [ 21 | "'[title]' is not recognized as an internal or external command,\n", 22 | "operable program or batch file.\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "![title](\"img/intro.jpg\") \n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "id": "e5c3cbf8", 33 | "metadata": {}, 34 | "source": [] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 4, 39 | "id": "4fdfb8e5", 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "ename": "SyntaxError", 44 | "evalue": "Missing parentheses in call to 'print'. Did you mean print(\"hello\")? (2253489653.py, line 1)", 45 | "output_type": "error", 46 | "traceback": [ 47 | "\u001b[1;36m Input \u001b[1;32mIn [4]\u001b[1;36m\u001b[0m\n\u001b[1;33m print \"hello\"\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(\"hello\")?\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "print (\"hello\")" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "id": "9332f8fe", 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "id": "d63059cd", 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "f1ebbe20", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "id": "b385835a", 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "b4b2ef1a", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "id": "ee7cdcba", 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "\n", 103 | "\n" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "a396e95b", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "id": "7728d8d3", 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "id": "6cbc9137", 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "id": "3a74190c", 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "id": "86a4e8b5", 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "id": "37d0bc91", 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "id": "5bf7f232", 158 | "metadata": {}, 159 | "outputs": [], 160 | "source": [] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "id": "b122efb4", 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [] 169 | } 170 | ], 171 | "metadata": { 172 | "kernelspec": { 173 | "display_name": "Python 3.10.0a1 64-bit", 174 | "language": "python", 175 | "name": "python3" 176 | }, 177 | "language_info": { 178 | "codemirror_mode": { 179 | "name": "ipython", 180 | "version": 3 181 | }, 182 | "file_extension": ".py", 183 | "mimetype": "text/x-python", 184 | "name": "python", 185 | "nbconvert_exporter": "python", 186 | "pygments_lexer": "ipython3", 187 | "version": "3.10.0" 188 | }, 189 | "vscode": { 190 | "interpreter": { 191 | "hash": "a0e8842e24919357e9d799e5407db1611799105107ef38b0e383b327aae76fad" 192 | } 193 | } 194 | }, 195 | "nbformat": 4, 196 | "nbformat_minor": 5 197 | } 198 | -------------------------------------------------------------------------------- /01-Python/Lec1/Lec1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "334e70fe", 6 | "metadata": {}, 7 | "source": [ 8 | "## Introduction" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "26328eb1", 14 | "metadata": {}, 15 | "source": [ 16 | "## History of python \n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "26d96a00", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from IPython import display\n", 27 | "display.Image(\"img/n1.jpg\")\n" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "id": "81cbda09", 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [ 37 | "from IPython import display\n", 38 | "display.Image(\"img/van.jpg\")" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "f337046e", 44 | "metadata": {}, 45 | "source": [ 46 | "### Python laid its foundation in the late 1980s. \n", 47 | "### The implementation of Python was started in December 1989 by Guido Van Rossum in Netherland.\n", 48 | "### Python 2.0 added new features such as list comprehensions, garbage collection systems.\n", 49 | "### It can be treated in a procedural way, an object-oriented way or a functional way\n", 50 | "### Python is an interpreted programming language, this means that as a developer\n", 51 | "### you write Python (.py) files in a text editor and then put those files into the python\n", 52 | "### interpreter to be executed" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "id": "e5c3cbf8", 58 | "metadata": {}, 59 | "source": [ 60 | "## Main usages of python \n", 61 | " \n" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "id": "88ef4ce3", 67 | "metadata": {}, 68 | "source": [ 69 | "#### Desktop Applications\n", 70 | "#### Web Applications \n", 71 | "#### Data Science\n", 72 | "#### Artificial Intelligence \n", 73 | "#### Machine Learning \n", 74 | "#### Scientific Computing \n", 75 | "#### Robotics \n", 76 | "#### Internet of Things (IoT) \n", 77 | "#### Gaming \n", 78 | "#### Mobile Apps\n", 79 | "#### Data Analysis and Preprocessing\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "a1453b7f", 85 | "metadata": {}, 86 | "source": [ 87 | "# Roles of Python in Real-time Embedded Systems\n", 88 | "\n", 89 | "\n", 90 | "## Role 1 – Device control and debugging\n", 91 | "\n", 92 | "- analyzing bus traffic such as USB, SPI or I2C. \n", 93 | "- control the bus analyzer and send messages to the embedded system.\n", 94 | "- control the tool but they also normally provide a way to develop scripts that can be used to control the tool as well.\n", 95 | "- Python is one scripting languages that is normally supported, sometimes exclusively, to interface with and control the tool.\n", 96 | "\n", 97 | "![](./img/frames.jpg) ![](./img/frames2.jpg)\n", 98 | "\n", 99 | "\n", 100 | "\n", 101 | "\n", 102 | "## Role 2 – Automating testing\n", 103 | "\n", 104 | "- ability to control tools that can send and receive messages from an embedded system through Python. \n", 105 | "- using Python to create automated tests that include regression testing.\n", 106 | "- Python scripts can be developed that set the embedded system into different states.\n", 107 | "- One advantage to using Python for automated testing is that regression tests can be developed that constantly test and exercise the system. Any code changes that result in bugs or non-conformances will be immediately discovered.\n", 108 | "\n", 109 | "## Role 3 – Data analysis\n", 110 | "\n", 111 | "- used to receive critical embedded system data that can then be stored in database or locally for analysis.\n", 112 | "- Developers can use Python to develop real-time visualizations that show critical parameters for later analysis. \n", 113 | "\n", 114 | "## Role 4 – Real-time Software\n", 115 | "\n", 116 | "- used in Real-Time ES through Micro Python port is designed to run on microcontrollers such as the ARM Cortex-M3/4.\n", 117 | "- Micro Python is not alone though. Companies such as Synapse and OpenMV are using either Micro Python or their own Python port within embedded systems. \n", 118 | "\n", 119 | "\n" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "id": "859d768c", 125 | "metadata": {}, 126 | "source": [ 127 | "# What is interperter\n", 128 | "- https://www.youtube.com/watch?v=e4ax90XmUBc\n", 129 | "\n", 130 | "\n", 131 | "![](./img/interpeter.jpg) \n", 132 | "\n" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "id": "4fdfb8e5", 139 | "metadata": {}, 140 | "outputs": [], 141 | "source": [ 142 | "# printing hello world \n", 143 | "print(\"Hello world \") " 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "id": "9332f8fe", 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "'''In python string is represented between\n", 154 | "double quotation “ This is a string “ or\n", 155 | "single quotation ‘ This is also a string ’'''\n", 156 | "print('This is also a string') " 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "id": "048f192a", 162 | "metadata": {}, 163 | "source": [ 164 | "## Lab1 \n", 165 | "Write a python code that will print your short \\\n", 166 | "biography. \\\n", 167 | "Full Name, Birth Year, Faculty, and \\\n", 168 | "graduation year " 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "id": "d63059cd", 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "print (\"I'm AlaaElnaggar\") \n", 179 | "print (\"My birth date is 10/4/1995\") \n", 180 | "print (\"I'm Graduated from HTI, Mechatronics engineering 2018\") \n" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "id": "f1ebbe20", 186 | "metadata": {}, 187 | "source": [ 188 | "# Variables in python \n", 189 | "Variable is a part from the memory, used to hold a piece of data. \\\n", 190 | "• Unlike other programming languages, Python has no command for declaring a\n", 191 | "variable. A variable is created the moment you first assign a value to it" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": null, 197 | "id": "b385835a", 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "num_1 = 100 \n", 202 | "MyName=\"AlaaElnaggar\"\n", 203 | "print(num_1)\n", 204 | "print(MyName)" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "id": "572a06bb", 210 | "metadata": {}, 211 | "source": [ 212 | "## Note\n", 213 | "Variables do not need to be\n", 214 | "declared with any particular\n", 215 | "type and can even change type\n", 216 | "after they have been set" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": null, 222 | "id": "b4b2ef1a", 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "num_1 = 100 \n", 227 | "MyName=\"AlaaElnaggar\"\n", 228 | "num_1 = \"AlaaElnaggar\" \n", 229 | "MyName=100 \n", 230 | "print(num_1)\n", 231 | "print(MyName)" 232 | ] 233 | }, 234 | { 235 | "cell_type": "markdown", 236 | "id": "a8fd4330", 237 | "metadata": {}, 238 | "source": [ 239 | "## Variable naming rules:\n", 240 | "Variable naming rules in Python: \\\n", 241 | "◆ A variable name must start with a letter or the underscore character \\\n", 242 | "◆ A variable name cannot start with a number \\\n", 243 | "◆ A variable name can only contain alphanumeric characters and \\\n", 244 | "underscores (A-z, 0-9, and _ ) \\\n", 245 | "◆ Variable names are case-sensitive (age, Age and AGE are three different \\\n", 246 | "variables)" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "id": "ee7cdcba", 252 | "metadata": {}, 253 | "source": [ 254 | "\n", 255 | "\n" 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "id": "a396e95b", 261 | "metadata": {}, 262 | "source": [ 263 | "### Using comma operator to define multiple variables in same line:" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 21, 269 | "id": "7728d8d3", 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "First number is 3 and second number is 4\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "\n", 282 | "A,B,F,S = 1 , 2 , 3 , 4\n", 283 | "\n", 284 | "print('First number is %d and second number is %d' % (F, S)) \n" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "id": "4594ad07", 290 | "metadata": {}, 291 | "source": [ 292 | "## Methods of injecting variables in string " 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 22, 298 | "id": "fd60dd1d", 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "First number is 3 and second number is 4\n", 306 | "First number is 5 and number is 10\n", 307 | "First number is 5 second number is 10\n", 308 | "First number 5 and second number is 10\n", 309 | "First number is 5 second number is 5\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "F,S = 5 ,10\n", 315 | "A,B = 3,4\n", 316 | "# Method 1 \n", 317 | "print(\"First number is {} and second number is {}\".format(A, B))\n", 318 | "# Method 2\n", 319 | "print(\"First number is {first} and number is {second}\".format(first=F, second=S)) \n", 320 | "# Method 3\n", 321 | "print('First number is', F, 'second number is', S) \n", 322 | "# Method 4\n", 323 | "print('First number %d and second number is %d' % (F, S))\n", 324 | "# Method 5\n", 325 | "print('First number is ' + str(F) + ' second number is '+ str(F))" 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "id": "a0754d2d", 331 | "metadata": {}, 332 | "source": [ 333 | "# Input output " 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 30, 339 | "id": "6cbc9137", 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "ename": "TypeError", 344 | "evalue": "%d format: a number is required, not str", 345 | "output_type": "error", 346 | "traceback": [ 347 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 348 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 349 | "\u001b[1;32md:\\ITI_Embedded_Linux\\Python\\Lec1\\Lec1.ipynb Cell 25\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m MyName \u001b[39m=\u001b[39m \u001b[39minput\u001b[39m (\u001b[39m\"\u001b[39m\u001b[39mPlease Enter your name \u001b[39m\u001b[39m\"\u001b[39m) \n\u001b[1;32m----> 2\u001b[0m \u001b[39mprint\u001b[39m (\u001b[39m\"\u001b[39;49m\u001b[39mMy name is \u001b[39;49m\u001b[39m%d\u001b[39;49;00m\u001b[39m\"\u001b[39;49m \u001b[39m%\u001b[39;49m(MyName))\n", 350 | "\u001b[1;31mTypeError\u001b[0m: %d format: a number is required, not str" 351 | ] 352 | } 353 | ], 354 | "source": [ 355 | "MyName = input (\"Please Enter your name \") \n", 356 | "print (\"My name is %d\" %(MyName))" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "id": "be6528d0", 363 | "metadata": {}, 364 | "outputs": [], 365 | "source": [] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "id": "0046f0e8", 370 | "metadata": {}, 371 | "source": [ 372 | "# Lab2 \n", 373 | "Write a python code that ask about your name , age , facaulity " 374 | ] 375 | }, 376 | { 377 | "cell_type": "markdown", 378 | "id": "e5342a83", 379 | "metadata": {}, 380 | "source": [ 381 | "# Receiving more than one variable \n", 382 | " need to take the multiple inputs in a single line\n", 383 | "\n", 384 | " The split() method is useful for getting multiple inputs from users. The syntax is given below.\n", 385 | "\n", 386 | "Syntax -\n", 387 | "\n", 388 | "input().split(separator, maxsplit) \n", 389 | "The split() method is used to split the Python string, but we can use it to get the multiple values.\n", 390 | "\n", 391 | "# Why\n", 392 | "Very useful in embedded systems parsing of the records for the code from another file specifically in Bootloader " 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 38, 398 | "id": "3a74190c", 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "ename": "ValueError", 403 | "evalue": "too many values to unpack (expected 4)", 404 | "output_type": "error", 405 | "traceback": [ 406 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 407 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 408 | "\u001b[1;32md:\\ITI_Embedded_Linux\\Python\\Lec1\\Lec1.ipynb Cell 28\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39m# taking two inputs at a time \u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[39m# a, b, c = input(\"Enter three values: \").split(\"M\",3) \u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39m# print(\"val1: \", a) \u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 17\u001b[0m \n\u001b[0;32m 18\u001b[0m \u001b[39m# taking four inputs at a time with white space \u001b[39;00m\n\u001b[1;32m---> 19\u001b[0m a, b, c, d \u001b[39m=\u001b[39m \u001b[39minput\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mEnter four values: \u001b[39m\u001b[39m\"\u001b[39m) \n\u001b[0;32m 20\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mFirst number is \u001b[39m\u001b[39m{}\u001b[39;00m\u001b[39m, second number is \u001b[39m\u001b[39m{}\u001b[39;00m\u001b[39m third is \u001b[39m\u001b[39m{}\u001b[39;00m\u001b[39m and fourth is \u001b[39m\u001b[39m{}\u001b[39;00m\u001b[39m\"\u001b[39m\u001b[39m.\u001b[39mformat(a, b, c, d))\n", 409 | "\u001b[1;31mValueError\u001b[0m: too many values to unpack (expected 4)" 410 | ] 411 | } 412 | ], 413 | "source": [ 414 | "# taking two inputs at a time \n", 415 | "# a, b, c = input(\"Enter three values: \").split(\"M\",3) \n", 416 | "# print(\"val1: \", a) \n", 417 | "# print(\"val1: \", b) \n", 418 | "# print(\"val1: \", c) \n", 419 | "\n", 420 | "# print(\"Enter Your Last Name: \", b) \n", 421 | "# print(\"Enter Your Class: \", c) \n", 422 | "# print() \n", 423 | " \n", 424 | "# # taking three inputs at a time \n", 425 | "# x, y, z = input(\"Enter three values: \").split(\"p\",3) \n", 426 | "# print(\"Total number of students: \", x) \n", 427 | "# print(\"Number of passed student : \", y) \n", 428 | "# print(\"Number of failed student : \", z) \n", 429 | "\n", 430 | " \n", 431 | "# taking four inputs at a time with white space \n", 432 | "a, b, c, d = input(\"Enter four values: \").split() \n", 433 | "print(\"First number is {}, second number is {} third is {} and fourth is {}\".format(a, b, c, d)) \n", 434 | "# print() " 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "id": "86a4e8b5", 440 | "metadata": {}, 441 | "source": [ 442 | "# Type Cating in python \n", 443 | "#### • int() - constructs an integer number from an integer literal, a float literal (by\n", 444 | "#### rounding down to the previous whole number), or a string literal (providing the\n", 445 | "#### string represents a whole number)\n", 446 | "#### • float() - constructs a float number from an integer literal, a float literal or a string\n", 447 | "#### literal (providing the string represents a float or an integer)\n", 448 | "#### • str() - constructs a string from a wide variety of data types, including strings,\n", 449 | "#### integer literals and float literals" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 39, 455 | "id": "37d0bc91", 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "100\n", 463 | "100\n", 464 | "100.0\n", 465 | "65\n" 466 | ] 467 | } 468 | ], 469 | "source": [ 470 | "x = int(\"100\")\n", 471 | "print (x)\n", 472 | "x = str (100)\n", 473 | "print (x)\n", 474 | "x = float (100)\n", 475 | "print (x)\n", 476 | "# printing asci value \n", 477 | "x=ord('A') \n", 478 | "print (x)\n" 479 | ] 480 | }, 481 | { 482 | "cell_type": "markdown", 483 | "id": "5bf7f232", 484 | "metadata": {}, 485 | "source": [ 486 | "# Operators in python " 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 40, 492 | "id": "f3366190", 493 | "metadata": {}, 494 | "outputs": [ 495 | { 496 | "name": "stdout", 497 | "output_type": "stream", 498 | "text": [ 499 | "110\n", 500 | "70\n", 501 | "1800\n", 502 | "4.5\n", 503 | "1215766545905692880100000000000000000000\n", 504 | "4\n", 505 | "10\n" 506 | ] 507 | } 508 | ], 509 | "source": [ 510 | "# Arithmatic operators \n", 511 | "a = 90\n", 512 | "b = 20\n", 513 | "#additaddition \n", 514 | "print(a+b)\n", 515 | "#subtraction\n", 516 | "print(a-b)\n", 517 | "#multiplication\n", 518 | "print(a*b)\n", 519 | "#divison\n", 520 | "print(a/b)\n", 521 | "#Exponentiation\n", 522 | "print(a**b)\n", 523 | "#floor divison\n", 524 | "print(a//b)\n", 525 | "#Modulas\n", 526 | "print(a%b)" 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "execution_count": 41, 532 | "id": "b122efb4", 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "name": "stdout", 537 | "output_type": "stream", 538 | "text": [ 539 | "False\n", 540 | "True\n", 541 | "True\n", 542 | "True\n", 543 | "False\n", 544 | "False\n" 545 | ] 546 | } 547 | ], 548 | "source": [ 549 | "# relational operators \n", 550 | "a = 5\n", 551 | "b = 10\n", 552 | "\n", 553 | "print(a == b) #return False\n", 554 | "print(a != b) #return True\n", 555 | "print(a < b) #return True\n", 556 | "print(a <= b) #return True\n", 557 | "print(a > b) #return False\n", 558 | "print(a >= b) #return False" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": 42, 564 | "id": "ba8aae8d", 565 | "metadata": {}, 566 | "outputs": [ 567 | { 568 | "name": "stdout", 569 | "output_type": "stream", 570 | "text": [ 571 | "Line 1 - Value of c is 12\n", 572 | "Line 2 - Value of c is 61\n", 573 | "Line 3 - Value of c is 49\n", 574 | "Line 4 - Value of c is -61\n", 575 | "Line 5 - Value of c is 240\n", 576 | "Line 6 - Value of c is 15\n" 577 | ] 578 | } 579 | ], 580 | "source": [ 581 | "# Bitwise operators in python \n", 582 | "\n", 583 | "a = 60 # 60 = 0011 1100 \n", 584 | "b = 13 # 13 = 0000 1101 \n", 585 | "c = 0\n", 586 | "\n", 587 | "c = a & b; # 12 = 0000 1100\n", 588 | "print (\"Line 1 - Value of c is \", c)\n", 589 | "\n", 590 | "c = a | b; # 61 = 0011 1101 \n", 591 | "print (\"Line 2 - Value of c is \", c)\n", 592 | "\n", 593 | "c = a ^ b; # 49 = 0011 0001\n", 594 | "print (\"Line 3 - Value of c is \", c)\n", 595 | "\n", 596 | "c = ~a; # -61 = 1100 0011\n", 597 | "print (\"Line 4 - Value of c is \", c)\n", 598 | "\n", 599 | "c = a << 2; # 240 = 1111 0000\n", 600 | "print (\"Line 5 - Value of c is \", c)\n", 601 | "\n", 602 | "c = a >> 2; # 15 = 0000 1111\n", 603 | "print (\"Line 6 - Value of c is \", c)" 604 | ] 605 | }, 606 | { 607 | "cell_type": "code", 608 | "execution_count": 43, 609 | "id": "06f8229a", 610 | "metadata": {}, 611 | "outputs": [ 612 | { 613 | "name": "stdout", 614 | "output_type": "stream", 615 | "text": [ 616 | "10\n", 617 | "20\n", 618 | "10\n", 619 | "100\n", 620 | "102400\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "\n", 626 | "# Assignment Operators\n", 627 | "a = 10\n", 628 | " \n", 629 | "# Assign value\n", 630 | "b = a\n", 631 | "print(b)\n", 632 | " \n", 633 | "# Add and assign value\n", 634 | "b += a\n", 635 | "print(b)\n", 636 | " \n", 637 | "# Subtract and assign value\n", 638 | "b -= a\n", 639 | "print(b)\n", 640 | " \n", 641 | "# multiply and assign\n", 642 | "b *= a\n", 643 | "print(b)\n", 644 | " \n", 645 | "# bitwise lishift operator\n", 646 | "b <<= a\n", 647 | "print(b)" 648 | ] 649 | }, 650 | { 651 | "cell_type": "markdown", 652 | "id": "10bda04a", 653 | "metadata": {}, 654 | "source": [ 655 | "#### Membership operators, Logical operators and Identity operators will be mentiond later " 656 | ] 657 | }, 658 | { 659 | "cell_type": "markdown", 660 | "id": "04b7aa68", 661 | "metadata": {}, 662 | "source": [ 663 | "### Complex type" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 44, 669 | "id": "9d0f7406", 670 | "metadata": {}, 671 | "outputs": [ 672 | { 673 | "name": "stdout", 674 | "output_type": "stream", 675 | "text": [ 676 | "(1+0j)\n", 677 | "(1+2j)\n" 678 | ] 679 | } 680 | ], 681 | "source": [ 682 | "# Python complex() function example \n", 683 | "# Calling function \n", 684 | "a = complex(1) # Passing single parameter \n", 685 | "b = complex(1,2) # Passing both parameters \n", 686 | "# Displaying result \n", 687 | "print(a) \n", 688 | "print(b) " 689 | ] 690 | }, 691 | { 692 | "cell_type": "markdown", 693 | "id": "178d5a7b", 694 | "metadata": {}, 695 | "source": [ 696 | "# Sequence Types in python [list , tuple , range]\n" 697 | ] 698 | }, 699 | { 700 | "cell_type": "markdown", 701 | "id": "c163686a", 702 | "metadata": {}, 703 | "source": [ 704 | "## List " 705 | ] 706 | }, 707 | { 708 | "cell_type": "markdown", 709 | "id": "8c7ded94", 710 | "metadata": {}, 711 | "source": [ 712 | "Lists are used to store multiple items in a single variable.\n", 713 | "\n", 714 | "Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.\n", 715 | "\n", 716 | "Lists are created using square brackets:" 717 | ] 718 | }, 719 | { 720 | "cell_type": "code", 721 | "execution_count": 65, 722 | "id": "f6d6ceb6", 723 | "metadata": {}, 724 | "outputs": [ 725 | { 726 | "name": "stdout", 727 | "output_type": "stream", 728 | "text": [ 729 | "3\n", 730 | "2\n", 731 | "['Elnaggar', 0.7, 'Elnaggar', 5.4, 1, 'Alaa']\n" 732 | ] 733 | } 734 | ], 735 | "source": [ 736 | "#Creating lists\n", 737 | "\n", 738 | "# my_list = ['Alaa', 1, 5.4, 'Elnaggar', 0.7]\n", 739 | "\n", 740 | "# print(my_list)\n", 741 | "\n", 742 | "# #Accessing list values\n", 743 | "# print(my_list[2])# 5.4\n", 744 | "\n", 745 | "# print(my_list[-1])# 0.7\n", 746 | "# print(my_list[:2]) # till element2\n", 747 | "\n", 748 | "# #Modifying lists\n", 749 | "# x = \"ITI\"\n", 750 | "# Y = \"567575I65767TI54667572\" \n", 751 | "# my_list.append(x) # append x to end of list\n", 752 | "\n", 753 | "# print(my_list)\n", 754 | "\n", 755 | "# my_list.extend(Y) # append all elements of Y to list\n", 756 | "# print(my_list)\n", 757 | "\n", 758 | "# my_list.insert(0, x) # insert x at index 1\n", 759 | "# print(my_list)\n", 760 | "\n", 761 | "# my_list.remove(x) # remove first occurance of x from list\n", 762 | "# print(my_list)\n", 763 | "\n", 764 | "# my_list.pop(3) # pop element at index i (defaults to end of list)\n", 765 | "# print(my_list)\n", 766 | "\n", 767 | "# my_list.clear() # delete all elements from the list\n", 768 | "# print(my_list)\n", 769 | "\n", 770 | "my_list = ['Alaa', 1, 5.4, 'Elnaggar', 0.7,'Elnaggar']\n", 771 | "x='Elnaggar'\n", 772 | "\n", 773 | "print(my_list.index(x)) # return index of element x\n", 774 | "\n", 775 | "\n", 776 | "print(my_list.count(x)) # return number of occurances of x in list\n", 777 | "\n", 778 | "my_list.reverse() # reverse elements of list in-place (no return)\n", 779 | "print(my_list)\n", 780 | "\n", 781 | "#my_list.sort(key=None, reverse=False) # sort list in-place\n", 782 | "#my_list.copy() # return a shallow copy of the list\n", 783 | "#my_nested_list = ['foobar', ['baz', 'qux'], [0]]\n" 784 | ] 785 | }, 786 | { 787 | "cell_type": "markdown", 788 | "id": "b2981485", 789 | "metadata": {}, 790 | "source": [ 791 | "# Tuple\n", 792 | "Tuples are used to store multiple items in a single variable.\n", 793 | "\n", 794 | "Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.\n", 795 | "\n", 796 | "A tuple is a collection which is ordered and unchangeable.\n", 797 | "\n", 798 | "Tuples are written with round brackets.\n", 799 | "\n", 800 | "#### Ordered\n", 801 | "When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.\n", 802 | "\n", 803 | "#### Unchangeable\n", 804 | "Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created\n", 805 | "#### Allow Duplicates\n", 806 | "Since tuples are indexed, they can have items with the same value:" 807 | ] 808 | }, 809 | { 810 | "cell_type": "code", 811 | "execution_count": 70, 812 | "id": "66bb4de5", 813 | "metadata": {}, 814 | "outputs": [ 815 | { 816 | "name": "stdout", 817 | "output_type": "stream", 818 | "text": [ 819 | "('apple', 2, 'cherry', 2)\n", 820 | "3\n", 821 | "banana\n", 822 | "\n", 823 | "('apple',)\n", 824 | "\n" 825 | ] 826 | } 827 | ], 828 | "source": [ 829 | "thistuple = (\"apple\", 2, \"cherry\",2)\n", 830 | "print(thistuple)\n", 831 | "\n", 832 | "# to print length of tuple \n", 833 | "thistuple = (\"apple\", \"banana\", \"cherry\")\n", 834 | "\n", 835 | "print(len(thistuple))\n", 836 | "print(thistuple[1])\n", 837 | "#thistuple[1] = 1 # will produce error tuple is a constant \n", 838 | "\n", 839 | "# Tuble at least consist of two elements \n", 840 | "thistuple = (\"apple\",)\n", 841 | "print(type(thistuple))\n", 842 | "print(thistuple)\n", 843 | "\n", 844 | "\n", 845 | "\n", 846 | "thistuple = (\"apple\")\n", 847 | "print(type(thistuple) ) \n", 848 | "\n", 849 | "\n", 850 | "\n", 851 | "\n" 852 | ] 853 | }, 854 | { 855 | "cell_type": "markdown", 856 | "id": "e3f36f1e", 857 | "metadata": {}, 858 | "source": [ 859 | "# Range \n", 860 | "The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number \\\n", 861 | "range(start, stop, step)\n" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": 74, 867 | "id": "6a23209d", 868 | "metadata": {}, 869 | "outputs": [ 870 | { 871 | "name": "stdout", 872 | "output_type": "stream", 873 | "text": [ 874 | "1\n", 875 | "21\n", 876 | "41\n", 877 | "61\n", 878 | "81\n", 879 | "101\n", 880 | "0\n", 881 | "1\n", 882 | "2\n", 883 | "3\n", 884 | "4\n", 885 | "5\n" 886 | ] 887 | } 888 | ], 889 | "source": [ 890 | "x = range(1, 102 , 20)\n", 891 | "\n", 892 | "for n in x:\n", 893 | " print(n)\n", 894 | "\n", 895 | " \n", 896 | "x = range(6)\n", 897 | "for n in x:\n", 898 | " print(n)" 899 | ] 900 | }, 901 | { 902 | "cell_type": "markdown", 903 | "id": "a65555e5", 904 | "metadata": {}, 905 | "source": [ 906 | "# Mapping type" 907 | ] 908 | }, 909 | { 910 | "cell_type": "markdown", 911 | "id": "977a9598", 912 | "metadata": {}, 913 | "source": [ 914 | "# dict\n", 915 | "Dictionary\n", 916 | "Dictionaries are used to store data values in key:value pairs.\n", 917 | "\n", 918 | "A dictionary is a collection which is ordered*, changeable and do not allow duplicates." 919 | ] 920 | }, 921 | { 922 | "cell_type": "markdown", 923 | "id": "6d339a51", 924 | "metadata": {}, 925 | "source": [] 926 | }, 927 | { 928 | "cell_type": "code", 929 | "execution_count": 79, 930 | "id": "78df18a5", 931 | "metadata": {}, 932 | "outputs": [ 933 | { 934 | "name": "stdout", 935 | "output_type": "stream", 936 | "text": [ 937 | "4\n", 938 | "1964\n", 939 | "white\n" 940 | ] 941 | } 942 | ], 943 | "source": [ 944 | "# thisdict = {\n", 945 | "# \"brand\": \"New\",\n", 946 | "# \"model\": \"Bently\",\n", 947 | "# \"year\": 1964,\n", 948 | "# \"year\": 2020\n", 949 | "# }\n", 950 | "# print(thisdict)\n", 951 | "\n", 952 | "# # Print the number of items in the dictionary:\n", 953 | "# print(len(thisdict))\n", 954 | "\n", 955 | "#The values in dictionary items can be of any data type:\n", 956 | "thisdict = {\n", 957 | " \"brand\": \"Ford\",\n", 958 | " \"electric\": False,\n", 959 | " \"year\": 1964,\n", 960 | " \"colors\": [\"red\", \"white\", \"blue\"]\n", 961 | "}\n", 962 | "print(len(thisdict))\n", 963 | "\n", 964 | "# printing dict elements \n", 965 | "\n", 966 | "print(thisdict[\"year\"]) \n", 967 | "\n", 968 | "print(thisdict[\"colors\"][1]) " 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "id": "5c4e429a", 974 | "metadata": {}, 975 | "source": [ 976 | "# Set Types\n", 977 | " ### set , frozenset" 978 | ] 979 | }, 980 | { 981 | "cell_type": "markdown", 982 | "id": "160b29b3", 983 | "metadata": {}, 984 | "source": [ 985 | "## set \n", 986 | "A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).\n", 987 | "\n", 988 | " a set itself is mutable. We can add or remove items from it.\n" 989 | ] 990 | }, 991 | { 992 | "cell_type": "code", 993 | "execution_count": 82, 994 | "id": "1af1d72f", 995 | "metadata": {}, 996 | "outputs": [ 997 | { 998 | "name": "stdout", 999 | "output_type": "stream", 1000 | "text": [ 1001 | "{1, 2, 3}\n", 1002 | "{1.0, (1, 2, 3), 'Hello'}\n", 1003 | "{1.0, (1, 2, 3), 'Hello'}\n", 1004 | "{1, 2, 3}\n", 1005 | "{'o', 'W', 'H', 'd', 'l', 'e', 'r'}\n", 1006 | "{'o', 'W', 'H', 100, 'd', 'l', 'e', 'r'}\n", 1007 | "o\n", 1008 | "{'H', 100, 'd', 'l', 'e', 'r'}\n", 1009 | "set()\n" 1010 | ] 1011 | } 1012 | ], 1013 | "source": [ 1014 | "# Different types of sets in Python\n", 1015 | "# set of integers\n", 1016 | "my_set = {1, 2, 3}\n", 1017 | "print(my_set)\n", 1018 | "\n", 1019 | "# set of mixed datatypes\n", 1020 | "my_set = {1.0, \"Hello\", (1, 2, 3)}\n", 1021 | "print(my_set)\n", 1022 | "print(my_set)\n", 1023 | "\n", 1024 | "# # set cannot have duplicates\n", 1025 | "# # Output: {1, 2, 3, 4}\n", 1026 | "# my_set = {1, 2, 3, 4, 3, 2}\n", 1027 | "# print(my_set)\n", 1028 | "\n", 1029 | "# we can make set from a list\n", 1030 | "# Output: {1, 2, 3}\n", 1031 | "my_set = set([1, 2, 3, 2])\n", 1032 | "print(my_set)\n", 1033 | "\n", 1034 | "\n", 1035 | "# # set cannot have mutable items\n", 1036 | "# # here [3, 4] is a mutable list\n", 1037 | "# # this will cause an error.\n", 1038 | "\n", 1039 | "# my_set = {1, 2, [3, 4]}\n", 1040 | "\n", 1041 | "# initialize my_set\n", 1042 | "# Output: set of unique elements\n", 1043 | "my_set = set(\"HelloWorld\")\n", 1044 | "print(my_set)\n", 1045 | "\n", 1046 | "my_set.add(100)\n", 1047 | "print(my_set)\n", 1048 | "\n", 1049 | "# pop an element\n", 1050 | "# Output: random element\n", 1051 | "print(my_set.pop())\n", 1052 | "\n", 1053 | "# pop another element\n", 1054 | "my_set.pop()\n", 1055 | "print(my_set)\n", 1056 | "\n", 1057 | "# clear my_set\n", 1058 | "# Output: set()\n", 1059 | "my_set.clear()\n", 1060 | "print(my_set)" 1061 | ] 1062 | }, 1063 | { 1064 | "cell_type": "markdown", 1065 | "id": "6f550e3a", 1066 | "metadata": {}, 1067 | "source": [ 1068 | "# The frozenset\n", 1069 | " function returns an immutable frozenset object initialized with elements from the given iterable.\n", 1070 | "\n", 1071 | "Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation.\n", 1072 | "\n", 1073 | "Due to this, frozen sets can be used as keys in Dictionary or as elements of another set. But like sets, it is not ordered (the elements can be set at any index)." 1074 | ] 1075 | }, 1076 | { 1077 | "cell_type": "code", 1078 | "execution_count": 83, 1079 | "id": "706575c6", 1080 | "metadata": {}, 1081 | "outputs": [ 1082 | { 1083 | "name": "stdout", 1084 | "output_type": "stream", 1085 | "text": [ 1086 | "The frozen set is: frozenset({'o', 'i', 'a', 'e', 'u'})\n" 1087 | ] 1088 | } 1089 | ], 1090 | "source": [ 1091 | "# tuple of vowels\n", 1092 | "vowels = ('a', 'e', 'i', 'o', 'u')\n", 1093 | "\n", 1094 | "fSet = frozenset(vowels)\n", 1095 | "print('The frozen set is:', fSet)\n", 1096 | "\n", 1097 | "# print('The frozen set is:', fSet[0]) # error 'frozenset' object is not subscriptable\n", 1098 | "\n", 1099 | "# frozensets are immutable\n", 1100 | "# fSet.add('v') # error \n", 1101 | "\n", 1102 | "# fSet.remove(0) # allowed in normal set\n" 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "markdown", 1107 | "id": "af96ecd3", 1108 | "metadata": {}, 1109 | "source": [ 1110 | "# bytes and bytearray\n", 1111 | " \n", 1112 | " are used for manipulating binary data.\n", 1113 | "\n", 1114 | "# The memoryview uses the buffer protocol to access the memory of other binary objects without needing to make a copy.\n", 1115 | "\n", 1116 | "Bytes objects are immutable sequences of single bytes. We should use them only when working with ASCII compatible data.\n", 1117 | "\n", 1118 | "The syntax for bytes literals is same as string literals, except that a 'b' prefix is added.\n", 1119 | "\n", 1120 | "bytearray objects are always created by calling the constructor bytearray(). These are mutable objects." 1121 | ] 1122 | }, 1123 | { 1124 | "cell_type": "code", 1125 | "execution_count": 87, 1126 | "id": "37b7482e", 1127 | "metadata": {}, 1128 | "outputs": [ 1129 | { 1130 | "name": "stdout", 1131 | "output_type": "stream", 1132 | "text": [ 1133 | "bytearray(b'\\x00\\x00\\x00\\x00\\x00')\n", 1134 | "\n" 1135 | ] 1136 | } 1137 | ], 1138 | "source": [ 1139 | "x = b'char_data'\n", 1140 | "x = b\"char_data\"\n", 1141 | " \n", 1142 | "y = bytearray(5)\n", 1143 | "print (y)\n", 1144 | " \n", 1145 | "z = memoryview(y )\n", 1146 | " \n", 1147 | "print(z)\n", 1148 | "\n", 1149 | "x = 2 \n", 1150 | "y = 2*x/5\n", 1151 | "\n", 1152 | "x = y\n", 1153 | "\n", 1154 | "# print(x) # b'char_data'\n", 1155 | "# print(y) # bytearray(b'\\x00\\x00\\x00\\x00\\x00')\n", 1156 | "# print(z) # \n", 1157 | "\n", 1158 | "# bytestr = bytes(b'abc') \n", 1159 | " \n", 1160 | "# # initializing a string with b\n", 1161 | "# # makes it a binary string\n", 1162 | "# print('bytestr')\n", 1163 | "# print(bytestr[0])\n", 1164 | " \n", 1165 | "# print(b'\\xC2\\xA9\\x20\\xF0\\x9D\\x8C\\x86\\x20\\xE2\\x98\\x83'.decode('utf-8'))\n", 1166 | "# print(b'\\xC2\\xA9\\x20\\xF0\\x9D\\x8C\\x86\\x20\\xE2\\x98\\x83'.decode('latin-1'))\n", 1167 | "\n", 1168 | "\n", 1169 | "# bytesArr = bytearray(b'\\x00\\x0F')\n", 1170 | " \n", 1171 | "# # Bytearray allows modification\n", 1172 | "# bytesArr[0] = 255\n", 1173 | "# bytesArr.append(255)\n", 1174 | "# print(bytesArr)\n", 1175 | "\n" 1176 | ] 1177 | }, 1178 | { 1179 | "cell_type": "markdown", 1180 | "id": "088b5f25", 1181 | "metadata": {}, 1182 | "source": [ 1183 | "# bool types " 1184 | ] 1185 | }, 1186 | { 1187 | "cell_type": "code", 1188 | "execution_count": 88, 1189 | "id": "80a93a5e", 1190 | "metadata": {}, 1191 | "outputs": [ 1192 | { 1193 | "name": "stdout", 1194 | "output_type": "stream", 1195 | "text": [ 1196 | "False\n", 1197 | "True\n", 1198 | "False\n", 1199 | "False\n", 1200 | "False\n", 1201 | "False\n", 1202 | "False\n", 1203 | "True\n" 1204 | ] 1205 | } 1206 | ], 1207 | "source": [ 1208 | "# Python program to illustrate\n", 1209 | "# built-in method bool()\n", 1210 | " \n", 1211 | "# Returns False as x is False\n", 1212 | "x = False\n", 1213 | "print(bool(x))\n", 1214 | " \n", 1215 | "# Returns True as x is True\n", 1216 | "x = True\n", 1217 | "print(bool(x))\n", 1218 | " \n", 1219 | "# Returns False as x is not equal to y\n", 1220 | "x = 5\n", 1221 | "y = 10\n", 1222 | "print(bool(x == y))\n", 1223 | " \n", 1224 | "# Returns False as x is None\n", 1225 | "x = None\n", 1226 | "print(bool(x))\n", 1227 | " \n", 1228 | "# Returns False as x is an empty sequence\n", 1229 | "x = ()\n", 1230 | "print(bool(x))\n", 1231 | " \n", 1232 | "# Returns False as x is an empty mapping\n", 1233 | "x = {}\n", 1234 | "print(bool(x))\n", 1235 | " \n", 1236 | "# Returns False as x is 0\n", 1237 | "x = 0.0\n", 1238 | "print(bool(x))\n", 1239 | " \n", 1240 | "# Returns True as x is a non empty string\n", 1241 | "x = 'AlaaElnaggar'\n", 1242 | "print(bool(x))" 1243 | ] 1244 | }, 1245 | { 1246 | "cell_type": "markdown", 1247 | "id": "e1ff146b", 1248 | "metadata": {}, 1249 | "source": [] 1250 | }, 1251 | { 1252 | "cell_type": "code", 1253 | "execution_count": 91, 1254 | "id": "c03bfa83", 1255 | "metadata": {}, 1256 | "outputs": [ 1257 | { 1258 | "name": "stdout", 1259 | "output_type": "stream", 1260 | "text": [ 1261 | "True\n", 1262 | "True\n", 1263 | "False\n", 1264 | "False\n", 1265 | "False\n", 1266 | "False\n", 1267 | "\n" 1268 | ] 1269 | } 1270 | ], 1271 | "source": [ 1272 | "print (10 > 9)\n", 1273 | "print (bool(\"Hello\"))\n", 1274 | "#empty string is used to chek if contains value or not\n", 1275 | "print (bool(\"\"))\n", 1276 | "#empty tuple is used to chek if contains value or not\n", 1277 | "print (bool())\n", 1278 | "#empty list is used to chek if contains value or not\n", 1279 | "print (bool([]))\n", 1280 | "#empty set is used to chek if contains value or not\n", 1281 | "print (bool({}))\n", 1282 | "print (type(int))" 1283 | ] 1284 | }, 1285 | { 1286 | "cell_type": "markdown", 1287 | "id": "4675157a", 1288 | "metadata": {}, 1289 | "source": [] 1290 | }, 1291 | { 1292 | "cell_type": "code", 1293 | "execution_count": 90, 1294 | "id": "bebb88a5", 1295 | "metadata": {}, 1296 | "outputs": [ 1297 | { 1298 | "name": "stdout", 1299 | "output_type": "stream", 1300 | "text": [ 1301 | "b'\\x00\\x00\\x00\\x00'\n", 1302 | "\n" 1303 | ] 1304 | } 1305 | ], 1306 | "source": [ 1307 | "# bytes is a class which creates bytes\n", 1308 | "# bytes are constructor of class bytes\n", 1309 | "# when used create instance of bytes\n", 1310 | "# bytes are immutable const \n", 1311 | "empty_bytes = bytes(4)\n", 1312 | "print(empty_bytes) # b'\\x00\\x00\\x00\\x00'\n", 1313 | "print (type(empty_bytes)) # class \n", 1314 | "#empty_bytes[0]= 5 ; #'bytes' object does not support item assignment\n" 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "code", 1319 | "execution_count": 96, 1320 | "id": "a601bfa7", 1321 | "metadata": {}, 1322 | "outputs": [ 1323 | { 1324 | "name": "stdout", 1325 | "output_type": "stream", 1326 | "text": [ 1327 | "Byte Conversion :b'Hello world'\n", 1328 | "Byte Conversion :b'Hello ????world'\n" 1329 | ] 1330 | }, 1331 | { 1332 | "ename": "UnicodeEncodeError", 1333 | "evalue": "'ascii' codec can't encode characters in position 6-9: ordinal not in range(128)", 1334 | "output_type": "error", 1335 | "traceback": [ 1336 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1337 | "\u001b[1;31mUnicodeEncodeError\u001b[0m Traceback (most recent call last)", 1338 | "\u001b[1;32md:\\ITI_Embedded_Linux\\Python\\Lec1\\Lec1.ipynb Cell 64\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[39mprint\u001b[39m (\u001b[39m\"\u001b[39m\u001b[39mByte Conversion :\u001b[39m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m\u001b[39mstr\u001b[39m(\u001b[39mbytes\u001b[39m(str1,\u001b[39m'\u001b[39m\u001b[39mascii\u001b[39m\u001b[39m'\u001b[39m,errors \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mreplace\u001b[39m\u001b[39m'\u001b[39m)) )\n\u001b[0;32m 15\u001b[0m \u001b[39m# stop excution \u001b[39;00m\n\u001b[1;32m---> 16\u001b[0m \u001b[39mprint\u001b[39m (\u001b[39m\"\u001b[39m\u001b[39mByte Conversion :\u001b[39m\u001b[39m\"\u001b[39m \u001b[39m+\u001b[39m\u001b[39mstr\u001b[39m(\u001b[39mbytes\u001b[39;49m(str1,\u001b[39m'\u001b[39;49m\u001b[39mascii\u001b[39;49m\u001b[39m'\u001b[39;49m,errors \u001b[39m=\u001b[39;49m \u001b[39m'\u001b[39;49m\u001b[39mstrict\u001b[39;49m\u001b[39m'\u001b[39;49m)) )\n", 1339 | "\u001b[1;31mUnicodeEncodeError\u001b[0m: 'ascii' codec can't encode characters in position 6-9: ordinal not in range(128)" 1340 | ] 1341 | } 1342 | ], 1343 | "source": [ 1344 | "\n", 1345 | "# list conversion into bytes \n", 1346 | "\n", 1347 | "# My_List =[1,2,3,2,5]\n", 1348 | "\n", 1349 | "# My_List_bytes = bytes(My_List)\n", 1350 | "\n", 1351 | "# print (My_List_bytes)\n", 1352 | "'''\n", 1353 | "# '''\n", 1354 | "str1 = 'Hello علاءworld'\n", 1355 | "\n", 1356 | "print (\"Byte Conversion :\" +str(bytes(str1,'ascii',errors = 'ignore')) )\n", 1357 | "#relace error with question martk\n", 1358 | "print (\"Byte Conversion :\" +str(bytes(str1,'ascii',errors = 'replace')) )\n", 1359 | "# stop excution \n", 1360 | "print (\"Byte Conversion :\" +str(bytes(str1,'ascii',errors = 'strict')) )" 1361 | ] 1362 | }, 1363 | { 1364 | "cell_type": "code", 1365 | "execution_count": 97, 1366 | "id": "b4a04c77", 1367 | "metadata": {}, 1368 | "outputs": [ 1369 | { 1370 | "name": "stdout", 1371 | "output_type": "stream", 1372 | "text": [ 1373 | "hello\n", 1374 | "Byte Conversion :b'hello'\n" 1375 | ] 1376 | } 1377 | ], 1378 | "source": [ 1379 | "# parsing data from the user \n", 1380 | "\n", 1381 | "data = \"\\x68\\x65\\x6c\\x6c\\x6f\"\n", 1382 | "\n", 1383 | "print (data)\n", 1384 | "\n", 1385 | "print (\"Byte Conversion :\" +str(bytes(data,'ascii',errors = 'ignore')) )" 1386 | ] 1387 | }, 1388 | { 1389 | "cell_type": "code", 1390 | "execution_count": 98, 1391 | "id": "962f57b5", 1392 | "metadata": {}, 1393 | "outputs": [ 1394 | { 1395 | "name": "stdout", 1396 | "output_type": "stream", 1397 | "text": [ 1398 | "\n", 1399 | "bytearray(b'\\x00\\x00\\x00\\x00\\x00')\n", 1400 | "bytearray(b'\\x05\\x00\\x00\\x00\\x00')\n", 1401 | "bytearray(b'\\x05\\x00\\x00\\x00\\x00\\xff')\n" 1402 | ] 1403 | } 1404 | ], 1405 | "source": [ 1406 | "# byte array is a changeable array of bytes \n", 1407 | "\n", 1408 | "byte_array_1 = bytearray(5) \n", 1409 | "print (type(byte_array_1))\n", 1410 | "print (byte_array_1)\n", 1411 | "byte_array_1[0] = 5\n", 1412 | "print (byte_array_1)\n", 1413 | "byte_array_1.append(0xff)\n", 1414 | "print (byte_array_1)" 1415 | ] 1416 | }, 1417 | { 1418 | "cell_type": "code", 1419 | "execution_count": null, 1420 | "id": "690b93fc", 1421 | "metadata": {}, 1422 | "outputs": [], 1423 | "source": [ 1424 | "# Memory view is function which receiev\n", 1425 | "# array receive object of bytes or byte array \n", 1426 | "# Use python buffer protocol \n", 1427 | "# return object address \n", 1428 | "\n", 1429 | "byte_array = bytearray('AlaaElnaggar','utf-8')\n", 1430 | "\n", 1431 | "memoryview_1 = memoryview(byte_array)\n", 1432 | "memoryview_1[0] = 0xff\n", 1433 | "print (byte_array)\n", 1434 | "# print the adress of memory location for byte_array\n", 1435 | "\n", 1436 | "print (\"The address of byte_array = \" + str(memoryview_1) )" 1437 | ] 1438 | }, 1439 | { 1440 | "cell_type": "code", 1441 | "execution_count": 99, 1442 | "id": "6e4a45c8", 1443 | "metadata": {}, 1444 | "outputs": [ 1445 | { 1446 | "name": "stdout", 1447 | "output_type": "stream", 1448 | "text": [ 1449 | "bytes 10000 0.009523630142211914\n", 1450 | "bytes 20000 0.008976221084594727\n", 1451 | "bytes 30000 0.018949270248413086\n", 1452 | "bytes 40000 0.025966405868530273\n", 1453 | "bytes 50000 0.039891719818115234\n", 1454 | "memory view 10000 0.0009636878967285156\n", 1455 | "memory view 20000 0.0020368099212646484\n", 1456 | "memory view 30000 0.002949953079223633\n", 1457 | "memory view 40000 0.003989219665527344\n", 1458 | "memory view 50000 0.0060176849365234375\n" 1459 | ] 1460 | } 1461 | ], 1462 | "source": [ 1463 | "# slicing create local copies \n", 1464 | "import time \n", 1465 | "# printing bytes \n", 1466 | "for n in (10000 , 20000 , 30000 , 40000 , 50000):\n", 1467 | "\tdata = 'A'*n\n", 1468 | "\tstart =time.time()\n", 1469 | "\twhile data:\n", 1470 | "\t\tdata = data[1:]\n", 1471 | "\tprint ('bytes ' , n ,time.time() - start)\n", 1472 | "\n", 1473 | "\n", 1474 | "for n in (10000 , 20000 , 30000 , 40000 , 50000):\n", 1475 | "\tdata = 'A'*n\n", 1476 | "\tstart =time.time()\n", 1477 | "\tdata = bytearray(data,'utf-8')\n", 1478 | "\t# no copies edit will occur in the same place \n", 1479 | "\t\n", 1480 | "\tdata = memoryview(data)\n", 1481 | "\t\n", 1482 | "\twhile data:\n", 1483 | "\t\tdata = data[1:]\n", 1484 | "\tprint ('memory view ' , n ,time.time() - start)\n" 1485 | ] 1486 | }, 1487 | { 1488 | "cell_type": "code", 1489 | "execution_count": 100, 1490 | "id": "627774da", 1491 | "metadata": {}, 1492 | "outputs": [ 1493 | { 1494 | "name": "stdout", 1495 | "output_type": "stream", 1496 | "text": [ 1497 | "0b101\n" 1498 | ] 1499 | } 1500 | ], 1501 | "source": [ 1502 | "# binary view \n", 1503 | "\n", 1504 | "print(bin(5))" 1505 | ] 1506 | }, 1507 | { 1508 | "cell_type": "code", 1509 | "execution_count": 105, 1510 | "id": "4278ff78", 1511 | "metadata": {}, 1512 | "outputs": [ 1513 | { 1514 | "name": "stdout", 1515 | "output_type": "stream", 1516 | "text": [ 1517 | "it's not exist \n", 1518 | "Thanks\n", 1519 | "a= b\n" 1520 | ] 1521 | } 1522 | ], 1523 | "source": [ 1524 | "# membership operators \n", 1525 | "# in - not in - \n", 1526 | "a = 20 \n", 1527 | "b = 20 \n", 1528 | "\n", 1529 | "list = [1 , 2 , 3 , 4]\n", 1530 | "\n", 1531 | "if a in list:\n", 1532 | "\tprint (\"it exist\")\n", 1533 | "else:\n", 1534 | "\tprint (\"it's not exist \")\n", 1535 | "\n", 1536 | "if a not in list:\n", 1537 | "\tprint (\"Thanks\")\n", 1538 | "else:\n", 1539 | "\tprint (\"it exist\")\n", 1540 | "\n", 1541 | "if a is b :\n", 1542 | "\tprint (\"a= b\")\n", 1543 | "else:\n", 1544 | "\tprint (\"a!=b\")" 1545 | ] 1546 | }, 1547 | { 1548 | "cell_type": "code", 1549 | "execution_count": 116, 1550 | "id": "b23c16ed", 1551 | "metadata": {}, 1552 | "outputs": [ 1553 | { 1554 | "name": "stdout", 1555 | "output_type": "stream", 1556 | "text": [ 1557 | "[10, 20, 30, 40, 50, 60, 70, 80, 90]\n", 1558 | "[10, 20, 30, 40, 50, 60, 70, 80, 90]\n", 1559 | "[20, 30, 40, 50, 60, 70, 80, 90]\n", 1560 | "[30, 40, 50]\n", 1561 | "[90]\n", 1562 | "[90, 80, 70, 60, 50, 40, 30, 20, 10]\n", 1563 | "[90]\n", 1564 | "[]\n", 1565 | "[]\n", 1566 | "[90, 80, 70]\n", 1567 | "[50, 60, 70, 80]\n" 1568 | ] 1569 | } 1570 | ], 1571 | "source": [ 1572 | "# slicing\n", 1573 | "\n", 1574 | "nums = [10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 ]\n", 1575 | "\n", 1576 | "print(nums[:])\n", 1577 | "print(nums[0:])\n", 1578 | "print(nums[1:])\n", 1579 | "print(nums[2:5:])\n", 1580 | "print(nums[-1::])\n", 1581 | "print(nums[-1::-1])\n", 1582 | "print(nums[-1::2])\n", 1583 | "print(nums[-1:4:]) \n", 1584 | "print(nums[-1:-4:]) \n", 1585 | "print(nums[-1:-4:-1]) \n", 1586 | "print(nums[-5:-1:]) \n" 1587 | ] 1588 | }, 1589 | { 1590 | "cell_type": "code", 1591 | "execution_count": 102, 1592 | "id": "439f0e6d", 1593 | "metadata": {}, 1594 | "outputs": [ 1595 | { 1596 | "name": "stdout", 1597 | "output_type": "stream", 1598 | "text": [ 1599 | "a==20\n" 1600 | ] 1601 | } 1602 | ], 1603 | "source": [ 1604 | "# Statements \n", 1605 | "\n", 1606 | "a = 20\n", 1607 | "b = 20 \n", 1608 | "\n", 1609 | "if a == 10:\n", 1610 | "\tprint(\"a= 10\")\n", 1611 | "elif a == 20:\n", 1612 | "\tprint(\"a==20\")\n", 1613 | "else:\n", 1614 | "\tprint(\"Wrong Input \")" 1615 | ] 1616 | }, 1617 | { 1618 | "cell_type": "code", 1619 | "execution_count": 103, 1620 | "id": "5edb19b6", 1621 | "metadata": {}, 1622 | "outputs": [ 1623 | { 1624 | "name": "stdout", 1625 | "output_type": "stream", 1626 | "text": [ 1627 | "1\n", 1628 | "2\n", 1629 | "3\n" 1630 | ] 1631 | } 1632 | ], 1633 | "source": [ 1634 | "i = 1\n", 1635 | "while True:\n", 1636 | " print(i)\n", 1637 | " i = i + 1\n", 1638 | " if(i > 3):\n", 1639 | " break" 1640 | ] 1641 | }, 1642 | { 1643 | "cell_type": "code", 1644 | "execution_count": 104, 1645 | "id": "5ff9da85", 1646 | "metadata": {}, 1647 | "outputs": [ 1648 | { 1649 | "name": "stdout", 1650 | "output_type": "stream", 1651 | "text": [ 1652 | "4\n", 1653 | "3\n", 1654 | "2\n", 1655 | "1\n" 1656 | ] 1657 | } 1658 | ], 1659 | "source": [ 1660 | "\n", 1661 | "# checks if list still\n", 1662 | "# contains any element\n", 1663 | "a = [1, 2, 3, 4]\n", 1664 | " \n", 1665 | "while a:\n", 1666 | " print(a.pop())" 1667 | ] 1668 | }, 1669 | { 1670 | "cell_type": "code", 1671 | "execution_count": null, 1672 | "id": "b065eae3", 1673 | "metadata": {}, 1674 | "outputs": [], 1675 | "source": [ 1676 | "\n" 1677 | ] 1678 | } 1679 | ], 1680 | "metadata": { 1681 | "kernelspec": { 1682 | "display_name": "Python 3.9.1rc1 64-bit", 1683 | "language": "python", 1684 | "name": "python3" 1685 | }, 1686 | "language_info": { 1687 | "codemirror_mode": { 1688 | "name": "ipython", 1689 | "version": 3 1690 | }, 1691 | "file_extension": ".py", 1692 | "mimetype": "text/x-python", 1693 | "name": "python", 1694 | "nbconvert_exporter": "python", 1695 | "pygments_lexer": "ipython3", 1696 | "version": "3.9.1rc1" 1697 | }, 1698 | "vscode": { 1699 | "interpreter": { 1700 | "hash": "43623726d6407856152d67d5465a38392b5160cd189b98f2f24e14daccd05be9" 1701 | } 1702 | } 1703 | }, 1704 | "nbformat": 4, 1705 | "nbformat_minor": 5 1706 | } 1707 | -------------------------------------------------------------------------------- /01-Python/Lec1/Untitled-2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "hi\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "print (\"hi\")" 18 | ] 19 | } 20 | ], 21 | "metadata": { 22 | "kernelspec": { 23 | "display_name": "Python 3.9.1rc1 64-bit", 24 | "language": "python", 25 | "name": "python3" 26 | }, 27 | "language_info": { 28 | "codemirror_mode": { 29 | "name": "ipython", 30 | "version": 3 31 | }, 32 | "file_extension": ".py", 33 | "mimetype": "text/x-python", 34 | "name": "python", 35 | "nbconvert_exporter": "python", 36 | "pygments_lexer": "ipython3", 37 | "version": "3.9.1rc1" 38 | }, 39 | "orig_nbformat": 4, 40 | "vscode": { 41 | "interpreter": { 42 | "hash": "43623726d6407856152d67d5465a38392b5160cd189b98f2f24e14daccd05be9" 43 | } 44 | } 45 | }, 46 | "nbformat": 4, 47 | "nbformat_minor": 2 48 | } 49 | -------------------------------------------------------------------------------- /01-Python/Lec1/img/frames.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/Lec1/img/frames.jpg -------------------------------------------------------------------------------- /01-Python/Lec1/img/frames2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/Lec1/img/frames2.jpg -------------------------------------------------------------------------------- /01-Python/Lec1/img/interpeter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/Lec1/img/interpeter.jpg -------------------------------------------------------------------------------- /01-Python/Lec1/img/intro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/Lec1/img/intro.jpg -------------------------------------------------------------------------------- /01-Python/Lec1/img/mc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/Lec1/img/mc.png -------------------------------------------------------------------------------- /01-Python/Lec1/img/n1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/Lec1/img/n1.jpg -------------------------------------------------------------------------------- /01-Python/Lec1/img/van.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlaaElnagar/ITI_Embedded_Linux_Lec-29-8-20222-/5545829df5b763d48e189d0b623bd4d1ead8d1b1/01-Python/Lec1/img/van.jpg -------------------------------------------------------------------------------- /01-Python/Lec2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lec2\n" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Strings " 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "AlaaElnaggar\n", 27 | "Alaa'Elnaggar\n", 28 | "Alaa'Elnaggar\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "# printing of string \n", 34 | "print(\"AlaaElnaggar\")\n", 35 | "print('Alaa\\'Elnaggar')\n", 36 | "print('''Alaa\\'Elnaggar''')" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "ITI \n", 49 | " \n", 50 | " Embedded \n", 51 | " \n", 52 | " \n", 53 | " Linux \n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "# Printing of multiple Lines \n", 59 | "s = '''ITI \n", 60 | " \n", 61 | " Embedded \n", 62 | " \n", 63 | " \n", 64 | " Linux '''\n", 65 | "print(s)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 3, 71 | "metadata": {}, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "A\n", 78 | "Alaa\n", 79 | "12\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "Data = \"AlaaElnaggar\"\n", 85 | "# string is un changeable\n", 86 | "#Data[0] = 5 # error\n", 87 | "print(Data [0] )\n", 88 | "# slicing is applicable in string \n", 89 | "print(Data [0:4] )\n", 90 | "\n", 91 | "print(len(Data))\n" 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "## String Functis \n", 99 | "- strip : responsible for removing first and last spaces BY default \n", 100 | "- : responsible for removing first and last argument \n", 101 | "- lower : Converting string into lowercase \n", 102 | "- upper : Converting string into uppercase \n", 103 | "- replace : replace(\"A\" , \" B \") --> replace any occurencc of A with b" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "AlaaElnaggar\n", 116 | "AlaaElnaggar\n", 117 | "Alaa Elnaggar\n", 118 | " && && Alaa Elnaggar && \n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "Data = \" AlaaElnaggar\"\n", 124 | "print(Data.strip() )\n", 125 | "\n", 126 | "Data = \"AlaaElnaggar \"\n", 127 | "print(Data.strip() )\n", 128 | "\n", 129 | "Data = \"Alaa Elnaggar \"\n", 130 | "print(Data.strip() )\n", 131 | "\n", 132 | "Data = \"&& && && Alaa Elnaggar && &&\"\n", 133 | "print(Data.strip(\"&&\") )" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 5, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | " alaaelnaggar\n", 146 | " ALAAELNAGGAR\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "Data = \" AlaaElnaggar\"\n", 152 | "print(Data.lower() )\n", 153 | "\n", 154 | "Data = \" AlaaElnaggar\"\n", 155 | "print(Data.upper() )" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 6, 161 | "metadata": {}, 162 | "outputs": [ 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | " Al$$Eln$gg$r\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "Data = \" AlaaElnaggar\"\n", 173 | "print(Data.replace(\"a\" , \"$\") )" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 7, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "name": "stdout", 183 | "output_type": "stream", 184 | "text": [ 185 | "['Alaa-(Elnaggar)']\n", 186 | "[5]\n", 187 | "['Alaa', '(Elnaggar)']\n" 188 | ] 189 | } 190 | ], 191 | "source": [ 192 | "Data = \"Alaa-(Elnaggar)\"\n", 193 | "Data = Data.split() # return list \n", 194 | "print(Data) # split the code and convert it into pieces of strings\n", 195 | "Data[0] = 5 \n", 196 | "print(Data) \n", 197 | "\n", 198 | "Data = \"Alaa-(Elnaggar)\"\n", 199 | "print(Data.split(\"-\")) # split the code and convert it into pieces \n", 200 | "# Data[0] = 5 # error \n", 201 | "# print(Data) " 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 8, 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "name": "stdout", 211 | "output_type": "stream", 212 | "text": [ 213 | "True\n" 214 | ] 215 | } 216 | ], 217 | "source": [ 218 | "\n", 219 | "code = '''.code\n", 220 | "demomain:\n", 221 | " REPEAT 20\n", 222 | "\tswitch rv(nrandom, 9)\t; generate a number between 0 and 8\n", 223 | "\tmov ecx, 7\n", 224 | "\tcase 0\n", 225 | "\t\tprint \"case 0\"\n", 226 | "\tcase ecx\t\t\t\t; in contrast to most other programming languages,\n", 227 | "\t\tprint \"case 7\"\t\t; the Masm32 switch allows \"variable cases\"\n", 228 | "\tcase 1 .. 3\n", 229 | "\t\t.if eax==1\n", 230 | "\t\t\tprint \"case 1\"\n", 231 | "\t\t.elseif eax==2\n", 232 | "\t\t\tprint \"case 2\"\n", 233 | "\t\t.else\n", 234 | "\t\t\tprint \"cases 1 to 3: other\"\n", 235 | "\t\t.endif\n", 236 | "\tcase 4, 6, 8\n", 237 | "\t\tprint \"cases 4, 6 or 8\"\n", 238 | "\tdefault\n", 239 | "\t\tmov ebx, 19\t\t ; print 20 stars\n", 240 | "\t\t.Repeat\n", 241 | "\t\t\tprint \"*\"\n", 242 | "\t\t\tdec ebx\n", 243 | "\t\t.Until Sign?\t\t ; loop until the sign flag is set\n", 244 | "\tendsw\n", 245 | "\tprint chr$(13, 10)\n", 246 | " ENDM\n", 247 | " exit\n", 248 | "end demomain'''\n", 249 | "Check_if_exist = \"mov\" in code\n", 250 | "\n", 251 | "print (Check_if_exist)" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 9, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "Start End \n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "# String Concatination \n", 269 | "a = \"Start\" \n", 270 | "b = \" End \"\n", 271 | "c = a + b # Operator overloading \n", 272 | "print (c) \n", 273 | "\n", 274 | "# a = \"Start\" \n", 275 | "# b = 5\n", 276 | "# c = a + b # Will produce error operator overloading needs two string \n", 277 | "# print (c) " 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 10, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "name": "stdout", 287 | "output_type": "stream", 288 | "text": [ 289 | "C = 10 , b = 20 , c = 30\n", 290 | "C = 30 , b = 20 , c = 10\n" 291 | ] 292 | } 293 | ], 294 | "source": [ 295 | "# Injecting elements in an text \n", 296 | "A = 10 \n", 297 | "B = 20\n", 298 | "C = 30\n", 299 | "\n", 300 | "data = (\"C = {} , b = {} , c = {}\" )\n", 301 | "print (data.format(A,B,C))\n", 302 | "\n", 303 | "\n", 304 | "data = (\"C = {2} , b = {1} , c = {0}\" )\n", 305 | "print (data.format(A,B,C))\n", 306 | " \n", 307 | " " 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 11, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "dlroW olleH\n", 320 | "drWolH\n", 321 | "dlr\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "# reversing of string \n", 327 | "txt = \"Hello World\"[::-1]\n", 328 | "print(txt)\n", 329 | "\n", 330 | "txt = \"Hello World\"[::-2]\n", 331 | "print(txt)\n", 332 | "\n", 333 | "txt = \"Hello World\"[-1:-4:-1]\n", 334 | "print(txt)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 12, 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "name": "stdout", 344 | "output_type": "stream", 345 | "text": [ 346 | "All****is****good\n" 347 | ] 348 | } 349 | ], 350 | "source": [ 351 | "# Adding string to list \n", 352 | "data = [\"All\" , \"is\" , \"good\" ] \n", 353 | "data2 = \"****\".join(data)\n", 354 | "print(data2)\n" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 13, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "name": "stdout", 364 | "output_type": "stream", 365 | "text": [ 366 | "Hello_Hello_Hello_Hello_Hello_\n" 367 | ] 368 | } 369 | ], 370 | "source": [ 371 | "# String douplication \n", 372 | "\n", 373 | "a = \"Hello_\" \n", 374 | "print(a*5)" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "# Python string\n", 382 | "https://www.w3schools.com/python/python_ref_string.asp\n", 383 | "\n", 384 | "- capitalize()\tConverts the first character to upper case\n", 385 | "- casefold()\tConverts string into lower case\n", 386 | "- center()\tReturns a centered string\n", 387 | "- count()\tReturns the number of times a specified value occurs in a string\n", 388 | "- encode()\tReturns an encoded version of the string\n", 389 | "- endswith()\tReturns true if the string ends with the specified value\n", 390 | "- expandtabs()\tSets the tab size of the string\n", 391 | "- find()\tSearches the string for a specified value and returns the position of where it was found\n", 392 | "- format()\tFormats specified values in a string\n", 393 | "- format_map()\tFormats specified values in a string\n", 394 | "- index()\tSearches the string for a specified value and returns the position of where it was found\n", 395 | "- isalnum()\tReturns True if all characters in the string are alphanumeric\n", 396 | "- isalpha()\tReturns True if all characters in the string are in the alphabet\n", 397 | "- isascii()\tReturns True if all characters in the string are ascii characters\n", 398 | "- isdecimal()\tReturns True if all characters in the string are decimals\n", 399 | "- isdigit()\tReturns True if all characters in the string are digits\n", 400 | "- isidentifier()\tReturns True if the string is an identifier\n", 401 | "- islower()\tReturns True if all characters in the string are lower case\n", 402 | "- isnumeric()\tReturns True if all characters in the string are numeric\n", 403 | "- isprintable()\tReturns True if all characters in the string are printable\n", 404 | "- isspace()\tReturns True if all characters in the string are whitespaces\n", 405 | "- istitle()\tReturns True if the string follows the rules of a title\n", 406 | "- isupper()\tReturns True if all characters in the string are upper case\n", 407 | "- join()\tConverts the elements of an iterable into a string\n", 408 | "- ljust()\tReturns a left justified version of the string\n", 409 | "- lower()\tConverts a string into lower case\n", 410 | "- lstrip()\tReturns a left trim version of the string\n", 411 | "- maketrans()\tReturns a translation table to be used in translations\n", 412 | "- partition()\tReturns a tuple where the string is parted into three parts\n", 413 | "- replace()\tReturns a string where a specified value is replaced with a specified value\n", 414 | "- rfind()\tSearches the string for a specified value and returns the last position of where it was found\n", 415 | "- rindex()\tSearches the string for a specified value and returns the last position of where it was found\n", 416 | "- rjust()\tReturns a right justified version of the string\n", 417 | "- rpartition()\tReturns a tuple where the string is parted into three parts\n", 418 | "- rsplit()\tSplits the string at the specified separator, and returns a list\n", 419 | "- rstrip()\tReturns a right trim version of the string\n", 420 | "- split()\tSplits the string at the specified separator, and returns a list\n", 421 | "- splitlines()\tSplits the string at line breaks and returns a list\n", 422 | "- startswith()\tReturns true if the string starts with the specified value\n", 423 | "- strip()\tReturns a trimmed version of the string\n", 424 | "- swapcase()\tSwaps cases, lower case becomes upper case and vice versa\n", 425 | "- title()\tConverts the first character of each word to upper case\n", 426 | "- translate()\tReturns a translated string\n", 427 | "- upper()\tConverts a string into upper case\n", 428 | "- zfill()\tFills the string with a specified number of 0 values at the beginning" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": null, 434 | "metadata": {}, 435 | "outputs": [], 436 | "source": [] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "# Functions " 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 14, 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "name": "stdout", 452 | "output_type": "stream", 453 | "text": [ 454 | "My_Name is :AlaaElnaggar\n" 455 | ] 456 | } 457 | ], 458 | "source": [ 459 | "def print_name (name ):\n", 460 | " print (\"My_Name is :\" + name)\n", 461 | " \n", 462 | "print_name(\"AlaaElnaggar\")" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 15, 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "name": "stdout", 472 | "output_type": "stream", 473 | "text": [ 474 | "The sum is is : 15\n" 475 | ] 476 | } 477 | ], 478 | "source": [ 479 | "def sum (a , b ):\n", 480 | " print (\"The sum is is :\" , a+b)\n", 481 | "\n", 482 | "sum(5,10)" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 16, 488 | "metadata": {}, 489 | "outputs": [ 490 | { 491 | "name": "stdout", 492 | "output_type": "stream", 493 | "text": [ 494 | "1\n", 495 | "AlaaElnaggar\n", 496 | "None\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "# function with return \n", 502 | "def func_1():\n", 503 | " return 1 \n", 504 | "print(func_1())\n", 505 | "\n", 506 | "# by default the function return none \n", 507 | "\n", 508 | "def func_2():\n", 509 | " print(\"AlaaElnaggar\")\n", 510 | "print(func_2())\n", 511 | "\n" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "# Arbitrary Argument \n", 519 | "- This function receive tuble and give the ability to access it's arguments accordinglly \n", 520 | "Variable-length arguments, varargs for short, are arguments that can take an unspecified amount of input. When these are used, the programmer does not need to wrap the data in a list or an alternative sequence.\n", 521 | "\n", 522 | "- we can use" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 17, 528 | "metadata": {}, 529 | "outputs": [ 530 | { 531 | "name": "stdout", 532 | "output_type": "stream", 533 | "text": [ 534 | "('Alaa', 'Yousef', 'Hamed', 'Elnaggar')\n", 535 | "\n", 536 | "\n", 537 | "Here is the input ('Alaa', 'Yousef', 'Hamed', 'Elnaggar')\n" 538 | ] 539 | } 540 | ], 541 | "source": [ 542 | "# Variadic function \n", 543 | "# always return tuble \n", 544 | "\n", 545 | "# always return tuble \n", 546 | "# useful incase of unknown inputs \n", 547 | "\n", 548 | "def Variadic_Input(*Input):\n", 549 | " print(Input)\n", 550 | " x = Input\n", 551 | " print(type(Input))\n", 552 | " print(type(x))\n", 553 | " print(\"Here is the input \"+str (x))\n", 554 | "\n", 555 | "Variadic_Input(\"Alaa\" ,\"Yousef\" , \"Hamed\" , \"Elnaggar\")" 556 | ] 557 | }, 558 | { 559 | "cell_type": "code", 560 | "execution_count": 18, 561 | "metadata": {}, 562 | "outputs": [ 563 | { 564 | "name": "stdout", 565 | "output_type": "stream", 566 | "text": [ 567 | "('foo', 'bar', 'baz')\n", 568 | "foo\n", 569 | "bar\n", 570 | "baz\n", 571 | "----------------\n", 572 | "()\n", 573 | "----------------\n", 574 | "('Hello', 'World')\n", 575 | "Hello\n", 576 | "World\n" 577 | ] 578 | } 579 | ], 580 | "source": [ 581 | "# always return tuble \n", 582 | "# very useful in parsing a list of string \n", 583 | "def f(*args):\n", 584 | " print(args)\n", 585 | " for arg in args:\n", 586 | " print(arg)\n", 587 | "\n", 588 | "Data_record = []\n", 589 | "f(\"foo\", \"bar\", \"baz\")\n", 590 | "print(\"----------------\")\n", 591 | "f()\n", 592 | "print(\"----------------\")\n", 593 | "f(\"Hello\", \"World\")" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "execution_count": 19, 599 | "metadata": {}, 600 | "outputs": [], 601 | "source": [ 602 | "# Python binding by object reference \n", 603 | "# How it works in python memory " 604 | ] 605 | }, 606 | { 607 | "cell_type": "markdown", 608 | "metadata": {}, 609 | "source": [ 610 | "# passing unlimited dict to function \n", 611 | "## What is Python **kwargs\n", 612 | "- variable-length argument list. We use the name kwargs with the double star. \n", 613 | "- The reason is that the double star allows us to pass through keyword arguments (and any number of them).\n", 614 | "- A keyword argument is where you provide a name to the variable as you pass it into the function.\n", 615 | "- One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": 20, 621 | "metadata": {}, 622 | "outputs": [ 623 | { 624 | "name": "stdout", 625 | "output_type": "stream", 626 | "text": [ 627 | "serial1 == SPI\n", 628 | "serial2 == I2C\n", 629 | "serial3 == UART\n", 630 | "\n", 631 | "{'serial1': 'SPI', 'serial2': 'I2C', 'serial3': 'UART'}\n" 632 | ] 633 | } 634 | ], 635 | "source": [ 636 | "\n", 637 | "def myFun(**kwargs):\n", 638 | " for key, value in kwargs.items():\n", 639 | " print(\"%s == %s\" % (key, value))\n", 640 | " print(type(kwargs))\n", 641 | " \n", 642 | "# Driver code\n", 643 | "myFun(serial1='SPI', serial2='I2C', serial3='UART')\n", 644 | "Available_Serials = {\"serial1\":\"SPI\", \"serial2\":\"I2C\", \"serial3\":\"UART\"}\n", 645 | "print(Available_Serials)\n", 646 | "# myFun(Available_Serials) # will produce error The function will recive dict as element by element " 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": 21, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "name": "stdout", 656 | "output_type": "stream", 657 | "text": [ 658 | "The key: serial1 the data: SPI\n", 659 | "The key: serial2 the data: I2C\n", 660 | "The key: serial3 the data: UART\n" 661 | ] 662 | } 663 | ], 664 | "source": [ 665 | "#Function recive dict : printing keys and value of dict \n", 666 | "\n", 667 | "def printdic(Data = \"Alaa\"):\n", 668 | " for key in Data:\n", 669 | " print(\"The key:\" , key , \" the data: \" , Data[key] ) \n", 670 | "\n", 671 | "Available_Serials = { \"serial1\":\"SPI\",\n", 672 | " \"serial2\":\"I2C\",\n", 673 | " \"serial3\":\"UART\"}\n", 674 | "printdic(Available_Serials)\n" 675 | ] 676 | }, 677 | { 678 | "cell_type": "markdown", 679 | "metadata": {}, 680 | "source": [ 681 | "# function default argument " 682 | ] 683 | }, 684 | { 685 | "cell_type": "code", 686 | "execution_count": 22, 687 | "metadata": {}, 688 | "outputs": [ 689 | { 690 | "name": "stdout", 691 | "output_type": "stream", 692 | "text": [ 693 | "MyName is Elnaggar\n", 694 | "MyName is AlaaElnaggar\n" 695 | ] 696 | } 697 | ], 698 | "source": [ 699 | "def PrintMyName (name = \"AlaaElnaggar\"):\n", 700 | " print(\"MyName is \" + name )\n", 701 | "\n", 702 | "PrintMyName(\"Elnaggar\")\n", 703 | "PrintMyName() # default argument \n", 704 | "\n" 705 | ] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": 23, 710 | "metadata": {}, 711 | "outputs": [ 712 | { 713 | "name": "stdout", 714 | "output_type": "stream", 715 | "text": [ 716 | "------------DataList--------------\n", 717 | "1\n", 718 | "2\n", 719 | "3\n", 720 | "4\n", 721 | "5\n", 722 | "------------Data_Tuble------------\n", 723 | "a\n", 724 | "b\n", 725 | "c\n", 726 | "d\n", 727 | "e\n", 728 | "------------Data_str-------------\n", 729 | "A\n", 730 | "l\n", 731 | "a\n", 732 | "a\n", 733 | "E\n", 734 | "l\n", 735 | "n\n", 736 | "a\n", 737 | "g\n", 738 | "g\n", 739 | "a\n", 740 | "r\n", 741 | "------------End------------------\n" 742 | ] 743 | } 744 | ], 745 | "source": [ 746 | "# Generic function to print any variable \n", 747 | "\n", 748 | "def Print_AnyPassedVariable(Data) : \n", 749 | " for d1 in Data:\n", 750 | " print(d1)\n", 751 | "\n", 752 | "Data_List = [1 , 2, 3 , 4 , 5]\n", 753 | "Data_Tuble= (\"a\" , \"b\" , \"c\" , \"d\" , \"e\") \n", 754 | "Data_str = \"AlaaElnaggar\"\n", 755 | "print(\"------------DataList--------------\")\n", 756 | "Print_AnyPassedVariable(Data_List)\n", 757 | "print(\"------------Data_Tuble------------\")\n", 758 | "Print_AnyPassedVariable(Data_Tuble)\n", 759 | "print(\"------------Data_str-------------\")\n", 760 | "Print_AnyPassedVariable(Data_str)\n", 761 | "print(\"------------End------------------\")\n" 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": 24, 767 | "metadata": {}, 768 | "outputs": [ 769 | { 770 | "data": { 771 | "text/plain": [ 772 | "5" 773 | ] 774 | }, 775 | "execution_count": 24, 776 | "metadata": {}, 777 | "output_type": "execute_result" 778 | } 779 | ], 780 | "source": [ 781 | "def CAN_Module_Stub():\n", 782 | " pass\n", 783 | "\n", 784 | "def BootloaderParser_stub():\n", 785 | " return 5 \n", 786 | "\n", 787 | "CAN_Module_Stub()\n", 788 | "BootloaderParser_stub()" 789 | ] 790 | }, 791 | { 792 | "cell_type": "markdown", 793 | "metadata": {}, 794 | "source": [ 795 | "# Lab to Make bitmath " 796 | ] 797 | }, 798 | { 799 | "cell_type": "code", 800 | "execution_count": 25, 801 | "metadata": {}, 802 | "outputs": [], 803 | "source": [ 804 | "def SET_BIT(VAR,BIT):\n", 805 | "\tVAR = int(VAR)\n", 806 | "\tBIT = int(BIT)\n", 807 | "\tresult = VAR | (1 << BIT)\n", 808 | "\treturn result\n", 809 | "\t\n", 810 | "def CLR_BIT(VAR,BIT):\n", 811 | "\tVAR = int(VAR)\n", 812 | "\tBIT = int(BIT)\n", 813 | "\tresult = VAR & (~(1 << BIT))\n", 814 | "\treturn result\n", 815 | "\t\n", 816 | "def GET_BIT(VAR,BIT):\n", 817 | "\tVAR = int(VAR)\n", 818 | "\tBIT = int(BIT)\n", 819 | "\tresult = (VAR >> BIT) & 1\n", 820 | "\treturn result\n", 821 | "\t\n", 822 | "def TOG_BIT(VAR,BIT):\n", 823 | "\tVAR = int(VAR)\n", 824 | "\tBIT = int(BIT)\n", 825 | "\tresult =VAR ^ (1 << BIT) \n", 826 | "\treturn result\n" 827 | ] 828 | }, 829 | { 830 | "cell_type": "markdown", 831 | "metadata": {}, 832 | "source": [ 833 | "# Global Keyword" 834 | ] 835 | }, 836 | { 837 | "cell_type": "code", 838 | "execution_count": 26, 839 | "metadata": {}, 840 | "outputs": [ 841 | { 842 | "name": "stdout", 843 | "output_type": "stream", 844 | "text": [ 845 | "100\n", 846 | "50\n", 847 | "50\n" 848 | ] 849 | } 850 | ], 851 | "source": [ 852 | "Data = 100\n", 853 | "print(Data)\n", 854 | "\n", 855 | "def changeGlobal(Data_1):\n", 856 | " global Data\n", 857 | " Data = Data_1\n", 858 | " print(Data)\n", 859 | "\n", 860 | "changeGlobal(50)\n", 861 | "print(Data)\n", 862 | "\n", 863 | " \n" 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "# Modules in python \n", 871 | "- User defined : import math...import time ...\n", 872 | "- Builtin : import " 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": 27, 878 | "metadata": {}, 879 | "outputs": [ 880 | { 881 | "ename": "ModuleNotFoundError", 882 | "evalue": "No module named 'BIT_MATH'", 883 | "output_type": "error", 884 | "traceback": [ 885 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 886 | "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", 887 | "\u001b[1;32md:\\ITI_Embedded_Linux\\01-Python\\Lec2.ipynb Cell 39\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mBIT_MATH\u001b[39;00m\n\u001b[0;32m 2\u001b[0m REG \u001b[39m=\u001b[39m \u001b[39m5\u001b[39m \n\u001b[0;32m 3\u001b[0m BIT_MATH\u001b[39m.\u001b[39mSET_BIT(REG , \u001b[39m1\u001b[39m)\n", 888 | "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'BIT_MATH'" 889 | ] 890 | } 891 | ], 892 | "source": [ 893 | "import BIT_MATH\n", 894 | "REG = 5 \n", 895 | "BIT_MATH.SET_BIT(REG , 1)\n", 896 | "print (REG)" 897 | ] 898 | }, 899 | { 900 | "cell_type": "code", 901 | "execution_count": null, 902 | "metadata": {}, 903 | "outputs": [], 904 | "source": [ 905 | "import My_info\n", 906 | "import BIT_MATH\n", 907 | "\n", 908 | "REG = 5 \n", 909 | "\n", 910 | "\n", 911 | "print (BIT_MATH.SET_BIT(REG , 1))\n", 912 | "\n", 913 | "print (My_info.CLR_BIT(REG , 1))\n", 914 | "# to print supported functions in the module\n", 915 | "dir(BIT_MATH)\n", 916 | "#My_info.PrintMyInfo(Data)\n" 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": null, 922 | "metadata": {}, 923 | "outputs": [], 924 | "source": [ 925 | "# Long name module import \n", 926 | "import JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ as J_Module\n", 927 | "J_Module.printModuleName()" 928 | ] 929 | }, 930 | { 931 | "cell_type": "code", 932 | "execution_count": null, 933 | "metadata": {}, 934 | "outputs": [], 935 | "source": [ 936 | "# builtin modules \n", 937 | "import platform\n", 938 | "# print system name \n", 939 | "platform.system()\n", 940 | "\n" 941 | ] 942 | }, 943 | { 944 | "cell_type": "code", 945 | "execution_count": null, 946 | "metadata": {}, 947 | "outputs": [], 948 | "source": [ 949 | "# numpy \n", 950 | "import numpy\n", 951 | "\n", 952 | "arr1 = numpy.array([1,2,3])\n", 953 | "arr2 = numpy.array([[1,2,3] , [4,5,6]])\n", 954 | "arr3 = numpy.array([[1,2,3] , [4,5,6]])\n", 955 | "print(arr1)\n", 956 | "type(arr1)\n", 957 | "\n" 958 | ] 959 | }, 960 | { 961 | "cell_type": "code", 962 | "execution_count": null, 963 | "metadata": {}, 964 | "outputs": [], 965 | "source": [] 966 | }, 967 | { 968 | "cell_type": "code", 969 | "execution_count": null, 970 | "metadata": {}, 971 | "outputs": [], 972 | "source": [] 973 | }, 974 | { 975 | "cell_type": "code", 976 | "execution_count": null, 977 | "metadata": {}, 978 | "outputs": [], 979 | "source": [] 980 | }, 981 | { 982 | "cell_type": "code", 983 | "execution_count": null, 984 | "metadata": {}, 985 | "outputs": [], 986 | "source": [] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": null, 991 | "metadata": {}, 992 | "outputs": [], 993 | "source": [ 994 | "def UsedCommunicationProtocol(**UsedCommunicationMethods): \n", 995 | " print (\"The available :\" + UsedCommunicationMethods) \n", 996 | "\n", 997 | "UsedCommunicationProtocol(prot" 998 | ] 999 | }, 1000 | { 1001 | "cell_type": "code", 1002 | "execution_count": null, 1003 | "metadata": {}, 1004 | "outputs": [], 1005 | "source": [] 1006 | }, 1007 | { 1008 | "cell_type": "code", 1009 | "execution_count": null, 1010 | "metadata": {}, 1011 | "outputs": [], 1012 | "source": [] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "execution_count": null, 1017 | "metadata": {}, 1018 | "outputs": [], 1019 | "source": [] 1020 | }, 1021 | { 1022 | "cell_type": "code", 1023 | "execution_count": null, 1024 | "metadata": {}, 1025 | "outputs": [], 1026 | "source": [] 1027 | }, 1028 | { 1029 | "cell_type": "markdown", 1030 | "metadata": {}, 1031 | "source": [ 1032 | "# Python architecture \n", 1033 | "- bindig : linking between variable value in memory and the variable \n", 1034 | "- python based on object reference \n", 1035 | "- the binding to the variable is changed once the variable bindded to another variable \n", 1036 | "- in , float , bool , tuble are mutable objects \n", 1037 | "- binding can not be braked by .append function\n", 1038 | "- garbage collector responsible for clearing of the broken un bindded memory sections which save memory location \n", 1039 | "- id() :Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime.\n", 1040 | "\n", 1041 | "```\n", 1042 | " c_int \n", 1043 | " ___________\n", 1044 | "| | 0x22940d0 This is what id() returns\n", 1045 | "| metadata | \n", 1046 | "| | \n", 1047 | "| | \n", 1048 | "| | \n", 1049 | "| | \n", 1050 | "|___________| \n", 1051 | "| value | 0x22940f8 This is what addressof() returns \n", 1052 | "|___________| ```\n", 1053 | "\n", 1054 | "* The metadata in a Python object takes up space. Because of this, that 4-byte value probably won't live at the very beginning of the Python object.\n", 1055 | "- MetaData is a container object that keeps together many different features of a database\n", 1056 | "\n" 1057 | ] 1058 | }, 1059 | { 1060 | "cell_type": "code", 1061 | "execution_count": null, 1062 | "metadata": {}, 1063 | "outputs": [], 1064 | "source": [ 1065 | "# Notes about python \n", 1066 | "# Every thing in python is object \n", 1067 | "# The variables are refered to the value \n", 1068 | "\n", 1069 | "a = [1 , 2 , 3]\n", 1070 | "b = a\n", 1071 | "# The variables are refered to the value\n", 1072 | "print(b)\n", 1073 | "a.append(1000)\n", 1074 | "print(\"b1 = \" , b)\n", 1075 | "print(\"a1 = \",a)\n", 1076 | "print(id(a))\n", 1077 | "print(id(b))\n", 1078 | "\n", 1079 | "# what to do if we need a just copy \n", 1080 | "a = [1 , 2 , 3]\n", 1081 | "b = a.copy()\n", 1082 | "a.append(1000)\n", 1083 | "print(\"b2 = \",b)\n", 1084 | "print(\"a2 = \",a)\n", 1085 | "\n", 1086 | "# This identity has to be unique and constant for this object during the lifetime\n", 1087 | "# memory address of an object in Python\n", 1088 | "\n", 1089 | "# import addressof and c_int modules \n", 1090 | "# from ctypes module\n", 1091 | "from ctypes import c_int, addressof\n", 1092 | "a = 10 \n", 1093 | "# get memory address of variable\n", 1094 | "print(addressof(c_int(a)))\n", 1095 | "print(id(a))\n", 1096 | "\n", 1097 | "# del a\n", 1098 | "# print(id(a)) # will return error " 1099 | ] 1100 | }, 1101 | { 1102 | "cell_type": "markdown", 1103 | "metadata": {}, 1104 | "source": [ 1105 | "# File Handling \n", 1106 | "## .CSV comma seperated file \n", 1107 | "- Runs over OS \n", 1108 | "\n", 1109 | "usages :\n", 1110 | "important in embedded systems legacy code to append main configuration parameters\n", 1111 | "\n", 1112 | "- The key function for working with files in Python is the open() function.\n", 1113 | "\n", 1114 | "- The open() function takes two parameters; filename, and mode:\n", 1115 | "\n", 1116 | "- \"r\" - Read - Default value. Opens a file for reading, error if the file does not exist\n", 1117 | "\n", 1118 | "- \"a\" - Append - Opens a file for appending, creates the file if it does not exist\n", 1119 | "\n", 1120 | "- \"w\" - Write - Opens a file for writing, creates the file if it does not exist\n", 1121 | "\n", 1122 | "- \"x\" - Create - Creates the specified file, returns an error if the file exists\n", 1123 | "\n", 1124 | "- \"t\" - Text - Default value. Text mode\n", 1125 | "\n", 1126 | "- \"b\" - Binary - Binary mode (e.g. images)\n" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "code", 1131 | "execution_count": 3, 1132 | "metadata": {}, 1133 | "outputs": [ 1134 | { 1135 | "ename": "FileNotFoundError", 1136 | "evalue": "[Errno 2] No such file or directory: 'file1.txt'", 1137 | "output_type": "error", 1138 | "traceback": [ 1139 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1140 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 1141 | "\u001b[1;32md:\\ITI_Embedded_Linux\\01-Python\\Lec2.ipynb Cell 56\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39m#f1 = open(\"file1.txt\" , \"x\") # Create a new file \u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m f1 \u001b[39m=\u001b[39m \u001b[39mopen\u001b[39;49m(\u001b[39m\"\u001b[39;49m\u001b[39mfile1.txt\u001b[39;49m\u001b[39m\"\u001b[39;49m , \u001b[39m\"\u001b[39;49m\u001b[39mr\u001b[39;49m\u001b[39m\"\u001b[39;49m) \u001b[39m# open a new file to read it \u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(f1\u001b[39m.\u001b[39mread())\n\u001b[0;32m 4\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mtype\u001b[39m (f1\u001b[39m.\u001b[39mread()))\n", 1142 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'file1.txt'" 1143 | ] 1144 | } 1145 | ], 1146 | "source": [ 1147 | "#f1 = open(\"file1.txt\" , \"x\") # Create a new file \n", 1148 | "f1 = open(\"file1.txt\" , \"r\") # open a new file to read it \n", 1149 | "print(f1.read())\n", 1150 | "print(type (f1.read()))" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": 30, 1156 | "metadata": {}, 1157 | "outputs": [ 1158 | { 1159 | "name": "stdout", 1160 | "output_type": "stream", 1161 | "text": [ 1162 | "Alaa\n" 1163 | ] 1164 | } 1165 | ], 1166 | "source": [ 1167 | "# opening file with abslute path \n", 1168 | "f1 = open(\"D:\\\\ITI_Embedded_Linux\\\\01-Python\\\\02-Lec2\\\\file1.txt\", \"r\") # open a new file to read it \n", 1169 | "#print(f1.read())\n", 1170 | "print(f1.read(4)) # read the first four char\n" 1171 | ] 1172 | }, 1173 | { 1174 | "cell_type": "code", 1175 | "execution_count": 2, 1176 | "metadata": {}, 1177 | "outputs": [ 1178 | { 1179 | "ename": "FileNotFoundError", 1180 | "evalue": "[Errno 2] No such file or directory: 'file1.txt'", 1181 | "output_type": "error", 1182 | "traceback": [ 1183 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1184 | "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", 1185 | "\u001b[1;32md:\\ITI_Embedded_Linux\\01-Python\\Lec2.ipynb Cell 58\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39m# reading file line by line \u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m f1 \u001b[39m=\u001b[39m \u001b[39mopen\u001b[39;49m(\u001b[39m\"\u001b[39;49m\u001b[39mfile1.txt\u001b[39;49m\u001b[39m\"\u001b[39;49m , \u001b[39m\"\u001b[39;49m\u001b[39mr\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n", 1186 | "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'file1.txt'" 1187 | ] 1188 | } 1189 | ], 1190 | "source": [ 1191 | "# reading file line by line \n", 1192 | "f1 = open(\"file1.txt\" , \"r\") # open a new file to read it \n", 1193 | "#print(f1.readline())\n" 1194 | ] 1195 | } 1196 | ], 1197 | "metadata": { 1198 | "kernelspec": { 1199 | "display_name": "Python 3.9.1rc1 64-bit", 1200 | "language": "python", 1201 | "name": "python3" 1202 | }, 1203 | "language_info": { 1204 | "codemirror_mode": { 1205 | "name": "ipython", 1206 | "version": 3 1207 | }, 1208 | "file_extension": ".py", 1209 | "mimetype": "text/x-python", 1210 | "name": "python", 1211 | "nbconvert_exporter": "python", 1212 | "pygments_lexer": "ipython3", 1213 | "version": "3.9.1rc1" 1214 | }, 1215 | "orig_nbformat": 4, 1216 | "vscode": { 1217 | "interpreter": { 1218 | "hash": "43623726d6407856152d67d5465a38392b5160cd189b98f2f24e14daccd05be9" 1219 | } 1220 | } 1221 | }, 1222 | "nbformat": 4, 1223 | "nbformat_minor": 2 1224 | } 1225 | --------------------------------------------------------------------------------