├── 01_SyntaxLearn_Introduction.ipynb ├── 02_SyntaxLearn_DataTypes_1.ipynb ├── 03_SyntaxLearn_Print&Output.ipynb ├── 04_SyntaxLearn_Control_ifelse.ipynb ├── 05_SyntaxLearn_Control_Iteration.ipynb ├── 06_SyntaxLearn_Function_1_Syntax.ipynb ├── 07_SyntaxLearn_NameSpace&Scope.ipynb ├── 08_SyntaxLearn_DataTypeMethods_2.ipynb ├── 09_SyntaxLearn_Function_2_Addition.ipynb ├── 10_SyntaxLearn_Comprehension&Walrus.ipynb ├── 11_EncodDecod_UNICODE.ipynb ├── 12_Modules&Packages.ipynb ├── 13_Files.ipynb ├── 14_Class.ipynb ├── 15_Class.ipynb ├── 16_Class.ipynb ├── 17_Class.ipynb ├── 18_Class.ipynb ├── 19_Class.ipynb ├── 20_Class.ipynb ├── 21_Exception.ipynb ├── 22_Extra.ipynb ├── 23_Extra.ipynb ├── README.md ├── Screenshot from 2023-07-24 17-23-24.png ├── Screenshot from 2023-07-24 17-47-30.png ├── Screenshot from 2023-07-25 11-19-48.png ├── Screenshot from 2023-08-02 11-44-48.png ├── Screenshot from 2023-08-02 18-28-09.png ├── Screenshot from 2023-08-02 18-28-26.png ├── Screenshot from 2023-08-02 18-28-47.png ├── Screenshot from 2023-08-02 18-29-10.png ├── Screenshot from 2023-08-16 10-05-20.png ├── Screenshot from 2023-08-16 10-11-15.png ├── Screenshot from 2023-08-16 10-50-42.png ├── Screenshot from 2023-08-16 10-50-58.png ├── Screenshot from 2023-08-16 10-51-13.png ├── new.txt ├── sample2.txt ├── test.txt ├── test ├── .ipynb_checkpoints │ └── test-checkpoint.py ├── new.py └── test2.py └── text.txt /01_SyntaxLearn_Introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "0ecdcf7d-fb66-4f55-8d83-b93ae36cdd29", 6 | "metadata": {}, 7 | "source": [ 8 | "# آموزش سینتکس کدنویسی " 9 | ] 10 | }, 11 | { 12 | "cell_type": "raw", 13 | "id": "f28c5b9b-40ad-4c8c-8f2f-d7a154df1f97", 14 | "metadata": {}, 15 | "source": [ 16 | "Syntax = قواعد نگارش کد ها\n", 17 | "اگر قواعد سینتکس رعایت نشود با خطای\n", 18 | "SyntaxError\n", 19 | "مواجه می شویم." 20 | ] 21 | }, 22 | { 23 | "cell_type": "raw", 24 | "id": "d2f0db18-faa0-49f6-b02d-50b038afc0d5", 25 | "metadata": {}, 26 | "source": [ 27 | "دو نوع سطر داریم:\n", 28 | " Physical Line = در واقع با هر اینتر یک سطر ایجاد می شود و در کدنویسی شماره گذاری می شود.\n", 29 | " Logical Line = سطری که مفسر در نظر می گیرد.\n", 30 | "\n", 31 | "\" ; \" = در پایتون الزامی نیست ولی با گذاشتن سمی کالون سطر منطقی تمام می شود و بعد آن در همان سطر، یک سطر منطقی جدید است.\n", 32 | "\n", 33 | "در مثال اول یک سطر فیزیکی و دو سطر منطقی داریم و در مثال دوم دو سطر فیزیکی و یک سطر منطقی داریم." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 1, 39 | "id": "76b789fc-8bea-4d0d-8aed-c4a22eb3848a", 40 | "metadata": { 41 | "tags": [] 42 | }, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Hello\n", 49 | "World!\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "print(\"Hello\"); print(\"World!\")" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 2, 60 | "id": "ae399094-945e-4184-b502-7ed02eacac5b", 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "59\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "print( 7 + 8 + 7 + 6 + 9 + 4 + 3 \\\n", 73 | "+ 8 + 2 + 1 + 4)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "id": "573a28ac-4f7e-4799-93b2-ccfb01e0d059", 79 | "metadata": {}, 80 | "source": [ 81 | "PEP8 = استانداردی از توصیه های نگارشی پایتون\n", 82 | "رعایت آن الزامی نیست و فقط توصیه می شود.\n", 83 | "بر اساس آن توصیه می شود سطر فیزیکی بیش از 79 کاراکتر نباشد." 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "id": "50f241e4-277a-453d-807b-0d4065b00753", 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "# برای کامنت گذاری\n", 94 | "# توجه شود بعد \\ نمی توان کامنت گذاشت" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 5, 100 | "id": "51da1c64-9d92-4ed8-8111-8a136b12c9d6", 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "#در تعریف متغیر ها با پرانتز و آکولاد نیازی به \\ نیست\n", 105 | "\n", 106 | "X = (1, 2, #test\n", 107 | " 3,\n", 108 | " 4)" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "id": "381fdcfb-048a-4e9c-a40b-4c52095360a1", 114 | "metadata": {}, 115 | "source": [ 116 | "NOTE: سطر های خالی هیچ تاثیر در اجرای برنامه نداشته و توسط مفسر نادیده گرفته می شوند." 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "id": "e3763aaa-1424-4f7a-a916-ef19f6c77a8d", 122 | "metadata": {}, 123 | "source": [ 124 | "داک استرینگ برای توضیحات بخش های مختلف استفاده می شود و برخالف کامنت نادیده گرفته نمی شود و میتوان آن را بازخوانی کرد ولی ارزش محاسباتی و دستوری ندارد." 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "id": "ebf2224c-d6d6-4510-8520-7a265f0974a2", 130 | "metadata": {}, 131 | "source": [ 132 | "کامنت برای برنامه نویس است ولی داک استرنیگ برای کاربر ماژول است." 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 6, 138 | "id": "7795b27b-2933-4c7d-b583-f0c6fd6beef0", 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | " DocString \n", 146 | "Hi World\n" 147 | ] 148 | } 149 | ], 150 | "source": [ 151 | "#doCString برای افزودن توضیح به بخش های مشخص مثل تابع\n", 152 | "\n", 153 | "def F():\n", 154 | " \"\"\" DocString \"\"\"\n", 155 | " print(\"Hi World\")\n", 156 | " \n", 157 | "print(F.__doc__)\n", 158 | "F()" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 7, 164 | "id": "9e326162-2097-4dd7-95c8-852929c85b59", 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "'\\nسلام\\n'" 171 | ] 172 | }, 173 | "execution_count": 7, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "#شکل دیگر داک استرینگ که میشه به عنوان کامنت در نظر گرفت\n", 180 | "\"\"\"\n", 181 | "سلام\n", 182 | "\"\"\"" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "id": "34591bc7-d6b6-4fc3-a7c5-1f1680fa7f44", 188 | "metadata": {}, 189 | "source": [ 190 | "تو رفتگی ها برای نمایش زیر مجموعه بودن دستور است:Indentation" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 8, 196 | "id": "9369c6fb-dc61-4a71-ba83-619aec249208", 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "def F():\n", 201 | " def A():\n", 202 | " def B():\n", 203 | " print(\"Hello World!\")" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "id": "fc2f4938-1273-4048-9f59-42bdcac9083b", 209 | "metadata": {}, 210 | "source": [ 211 | "# دستورات ورودی و خروجی: Input & Output" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 9, 217 | "id": "eb0889a9-9d70-48eb-a50e-7f79e6f8817b", 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "1 2 3 4 5\n", 225 | "1, 2, 3, 4, 5\n", 226 | "2 + 2 = 4\n", 227 | "\n", 228 | "Next Line\n", 229 | "Two print, In one Line\n", 230 | "\n", 231 | "one print\n", 232 | "In two line\n" 233 | ] 234 | } 235 | ], 236 | "source": [ 237 | "# دستور خروجی\n", 238 | "print(1,2,3,4,5) # کاما برای جداسازی\n", 239 | "print(1,2,3,4,5,sep=\", \") # پارامتر sep برای تعیین روش جداسازی\n", 240 | "print(\"2 + 2 =\", 2+2)\n", 241 | "\n", 242 | "#بطور پیش فرض هر دستور پرینت در انتها به خط بعدی می رود\n", 243 | "print()\n", 244 | "print(\"Next Line\")\n", 245 | "print(\"Two print\", end=\", \") # با end می توان نوع پایان خروجی را تغییر داد\n", 246 | "print(\"In one Line\")\n", 247 | "\n", 248 | "print(end='\\n')\n", 249 | "# \\n برای رفتن به خط بعدی\n", 250 | "print(\"one print\",'\\n', \"In two line\", sep=\"\")" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 12, 256 | "id": "8ef4fa3e-eddd-4598-8b9d-a6e3525d3181", 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdin", 261 | "output_type": "stream", 262 | "text": [ 263 | "Enter Your Name: Mohammad Hoseyn Behroozi\n" 264 | ] 265 | }, 266 | { 267 | "name": "stdout", 268 | "output_type": "stream", 269 | "text": [ 270 | "Your Name Is: Mohammad Hoseyn Behroozi\n", 271 | "a: 5 .b: 4 .c: 6 .d: 7\n" 272 | ] 273 | } 274 | ], 275 | "source": [ 276 | "# دستور Input برای گرفتن ورودی از کاربر\n", 277 | "x = input(\"Enter Your Name: \")\n", 278 | "print(\"Your Name Is:\", x )\n", 279 | "\n", 280 | "#تعریف متغیر ها\n", 281 | "#پایتون خودکار نوع متغیر را مشخص می کند\n", 282 | "a = 4\n", 283 | "b = 5\n", 284 | "c , d = 6 , 7 # لازم هست تعداد و ترتیب رعایت شود\n", 285 | "\n", 286 | "a , b = b , a # برای جا به جا کردن مقادیر متغیر ها\n", 287 | "\n", 288 | "print(\"a:\",a,\".b:\",b,\".c:\",c,\".d:\",d)\n", 289 | "\n", 290 | "# بطور پیش فرض در پایتون امکان تعریف ثابت وجود ندارد \n", 291 | "\n", 292 | "# برای تعریف متغیر در ابتدای شروع فقط میتونیم از حروف انگلیسی و ـ استفاده کنیم و در ادامه موارد قبلی بعلاوه اعداد" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "id": "9c3826f0-f4bd-415a-9224-cdbb18c720f5", 298 | "metadata": {}, 299 | "source": [ 300 | "# عملگر های حسابی" 301 | ] 302 | }, 303 | { 304 | "cell_type": "raw", 305 | "id": "4926ba9a-7f7b-4276-92a0-616ab89cc702", 306 | "metadata": {}, 307 | "source": [ 308 | "+ : جمع\n", 309 | "- : تفریق\n", 310 | "* : ضریب\n", 311 | "/ : تقسیم\n", 312 | "% : باقیمانده\n", 313 | "** : توان\n", 314 | "// : تقسیم بدون اعشار" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 13, 320 | "id": "b7d96578-8cd6-4836-8de8-feb1771439ba", 321 | "metadata": {}, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "+: 9\n", 328 | "-: 1\n", 329 | "*: 20\n", 330 | "/: 1.25\n", 331 | "%: 1\n", 332 | "**: 625\n", 333 | "//: 1\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "x, y = 5, 4\n", 339 | "print(\"+: \",x+y)\n", 340 | "print(\"-: \",x-y)\n", 341 | "print(\"*: \",x*y)\n", 342 | "print(\"/: \",x/y)\n", 343 | "print(\"%: \",x%y)\n", 344 | "print(\"**: \",x**y)\n", 345 | "print(\"//: \",x//y)" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "id": "f7d16212-c70e-4cd9-a27b-05c344fb24f2", 351 | "metadata": {}, 352 | "source": [ 353 | "# عملگر های مقایسه ای" 354 | ] 355 | }, 356 | { 357 | "cell_type": "raw", 358 | "id": "640d1ad4-5159-4523-ae73-3d36c9d5c096", 359 | "metadata": {}, 360 | "source": [ 361 | "== : برابری\n", 362 | "!= : نابرابری\n", 363 | "> : بزرگتر از\n", 364 | "< : کوچکتر از\n", 365 | ">= : بزرگتر مساوی\n", 366 | "<= : کوچکتر مساوی" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 14, 372 | "id": "bd549a6c-29a3-49cf-ab5a-e1ada3819f4d", 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "name": "stdout", 377 | "output_type": "stream", 378 | "text": [ 379 | "False\n", 380 | "True\n" 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "#جواب بصورت True و False\n", 386 | "#غیر از این دو جواب نیست\n", 387 | "x, y = 5, 4\n", 388 | "print(x==y)\n", 389 | "x, y = 4, 4\n", 390 | "z = x==y #متغیر z دیگر از نوع بولین است\n", 391 | "print(z)" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "id": "4ad3dc4a-e130-449e-a3a0-d0e33ec632bb", 397 | "metadata": {}, 398 | "source": [ 399 | "# عملگرهای انتصاب عملوند راستی به عملوند چپ" 400 | ] 401 | }, 402 | { 403 | "cell_type": "raw", 404 | "id": "3f559d18-7e77-4e11-80c1-8213e1d58de0", 405 | "metadata": {}, 406 | "source": [ 407 | "مثلا x += 5\n", 408 | " = : x = 5\n", 409 | "+= : x = x + 5\n", 410 | "-= : x = x - 5\n", 411 | "*= : x = x * 5\n", 412 | "/= : x = x / 5\n", 413 | "%= : x = x % 5\n", 414 | "//=: x = x // 5\n", 415 | "\n", 416 | "عملگرهای بیتی انتصابی به شرح زیر است\n", 417 | "&= |= ^= >>= <<=\n", 418 | "که محاسبه شان مانند مثال های فوق است." 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 15, 424 | "id": "1b1f059d-bbd8-43ad-9da5-e80360c13849", 425 | "metadata": {}, 426 | "outputs": [ 427 | { 428 | "name": "stdout", 429 | "output_type": "stream", 430 | "text": [ 431 | "False\n", 432 | "True\n", 433 | "False\n" 434 | ] 435 | } 436 | ], 437 | "source": [ 438 | "# عمگلر های منطقی and , or , not\n", 439 | "x , y = 5 , 4\n", 440 | "print( x < 5 and y < 5) # و\n", 441 | "print( x < 5 or y < 5) # یا\n", 442 | "print(not y < 5) # عدم صحت" 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "id": "a3333835-dfa0-4d34-88ce-0f1c4aa36ef1", 448 | "metadata": {}, 449 | "source": [ 450 | "# عملگرهای بررسی عضویت in و not in\n" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 16, 456 | "id": "1caf3580-1148-4cf1-bb70-c10838e505c3", 457 | "metadata": {}, 458 | "outputs": [ 459 | { 460 | "name": "stdout", 461 | "output_type": "stream", 462 | "text": [ 463 | "h in S? False ,and H is S? True\n", 464 | "2 not in L? False ,and 3 in L? True\n" 465 | ] 466 | } 467 | ], 468 | "source": [ 469 | "S = \"Hello World!\" # رشته\n", 470 | "L = [1,2,3,4,5,6,'r'] # لیست\n", 471 | "\n", 472 | "print(\"h in S?\",'h' in S,\",and H is S?\",'H' in S)\n", 473 | "print(\"2 not in L?\",2 not in L,\",and 3 in L?\",3 in L)" 474 | ] 475 | }, 476 | { 477 | "cell_type": "markdown", 478 | "id": "539e585e-0af5-4eb4-ba6f-725ab9c5b455", 479 | "metadata": {}, 480 | "source": [ 481 | "# عملگر بررسی هویت is و is not" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 17, 487 | "id": "c6293472-7830-4a8a-9683-efec075efe23", 488 | "metadata": {}, 489 | "outputs": [ 490 | { 491 | "name": "stdout", 492 | "output_type": "stream", 493 | "text": [ 494 | "True\n", 495 | "True\n", 496 | "False\n", 497 | "True\n", 498 | "False\n" 499 | ] 500 | } 501 | ], 502 | "source": [ 503 | "x1 = y1 =4\n", 504 | "print(x1 is y1)\n", 505 | "\n", 506 | "x2 = y2 = [1 ,2 ,4]\n", 507 | "print(x2 is y2)\n", 508 | "\n", 509 | "x3, y3 = [1 ,2 ,4], [1 ,2 ,4]\n", 510 | "print(x3 is y3)\n", 511 | "\n", 512 | "z = x3\n", 513 | "print(x3 is z)\n", 514 | "print(x3 is not z)" 515 | ] 516 | }, 517 | { 518 | "cell_type": "markdown", 519 | "id": "5e36e84e-5b6b-4fcf-8193-9954a0208024", 520 | "metadata": {}, 521 | "source": [ 522 | "# عملگر شیرماهی:Walrus" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 18, 528 | "id": "05b3729f-559d-482c-ae24-88c45a62e24b", 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "name": "stdout", 533 | "output_type": "stream", 534 | "text": [ 535 | "2\n" 536 | ] 537 | } 538 | ], 539 | "source": [ 540 | "#:=\n", 541 | "print(x:=2)\n", 542 | "# عمده کاربرد در شرط ها و حلقه ها است\n", 543 | "# مقدار دهی را درون عبارات انجام می دهد" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 19, 549 | "id": "4228738d-e619-4042-8dd7-d9374a3f6f70", 550 | "metadata": {}, 551 | "outputs": [ 552 | { 553 | "name": "stdout", 554 | "output_type": "stream", 555 | "text": [ 556 | "\n" 557 | ] 558 | } 559 | ], 560 | "source": [ 561 | "x = 'r'\n", 562 | "print(type(x))\n" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "id": "3081abb7-6a64-4d52-8c34-5f28bf99fab5", 569 | "metadata": {}, 570 | "outputs": [], 571 | "source": [] 572 | } 573 | ], 574 | "metadata": { 575 | "kernelspec": { 576 | "display_name": "Python 3 (ipykernel)", 577 | "language": "python", 578 | "name": "python3" 579 | }, 580 | "language_info": { 581 | "codemirror_mode": { 582 | "name": "ipython", 583 | "version": 3 584 | }, 585 | "file_extension": ".py", 586 | "mimetype": "text/x-python", 587 | "name": "python", 588 | "nbconvert_exporter": "python", 589 | "pygments_lexer": "ipython3", 590 | "version": "3.10.9" 591 | } 592 | }, 593 | "nbformat": 4, 594 | "nbformat_minor": 5 595 | } 596 | -------------------------------------------------------------------------------- /03_SyntaxLearn_Print&Output.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f77f50af", 6 | "metadata": {}, 7 | "source": [ 8 | "# روش سنتی" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "6ce419c0", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "x is : 2\n", 22 | "y is : 3\n", 23 | "z is : 7\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "#فرمت دهی رشته ها با روش سنتی\n", 29 | "# استفاده از %i برای اینتیجر\n", 30 | "x = 2\n", 31 | "y = 3.6\n", 32 | "print(\"x is : %i\\ny is : %i\\nz is : %i\" % (x, y, 5+2)) # برای اینتینجر می توان از %d نیز استفاده کرد\n", 33 | "\n", 34 | "# بطور کلی قابل به شکل زیر است\n", 35 | "# [(key)] [flag] [w] [.p] type" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "id": "37119ad6", 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "A\n", 49 | "K\n", 50 | "Hello\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "# % [(key)] [flag] [w] [.p] type\n", 56 | "print(\"%c\" % ('A')) # %c برای برگردانی کاراکتر است\n", 57 | "print(\"%c\" % (75)) # استفاده از یونیکد به جای کاراکتر\n", 58 | "print(\"%s\" % (\"Hello\")) # برای نمایش رشته\n", 59 | "\n", 60 | "# %i نمایش اینتیجر\n", 61 | "# %d نمایش اینتیجر\n", 62 | "# %c نمایش کاراکتر\n", 63 | "# %s نمایش رشته\n", 64 | "# %e نمایش اعداد بصورت علمی\n", 65 | "# %f نمایش اعداد اعشاری\n", 66 | "# %c نمایش در مبانی 8\n", 67 | "# %x نمایش در مبانی 16\n", 68 | "# %r نمایش رشته ولی به شکلی خاص" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 3, 74 | "id": "b6836e7e", 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "4321.12\n", 82 | " 4321.12\n", 83 | " +4321.12\n", 84 | "+4321.12 \n", 85 | "0004321.12\n", 86 | "4321.123457 ----- 25\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "#مثال ها\n", 92 | "f = 4321.123456789\n", 93 | "print(\"%.2f\" % (f)) # تعداد اعشار\n", 94 | "print(\"%10.2f\" % (f)) # طول میدان\n", 95 | "print(\"%+10.2f\" % (f)) # نمایش علامت\n", 96 | "print(\"%-+10.2f\" % (f)) # تغییر مکان فضای خالی میدان\n", 97 | "print(\"%010.2f\" % (f)) # پر کردن فضای خالی میدان با صفر\n", 98 | "print(\"%(b)f ----- %(a)i\" % ({'a' : 25, 'b' : f})) # استفاده از دیکشنری که می توان ترتیب را رعایت نکرد" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 4, 104 | "id": "7bca5e11", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "p : 3\n", 112 | "z : 20\n", 113 | "0000000000004321.123\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "# میتوان پارامتر های قالب بندی را از کاربر دریافت کرد\n", 119 | "f = 4321.123456789\n", 120 | "p = int(input(\"p : \"))\n", 121 | "z = int(input(\"z : \"))\n", 122 | "print(\"%0*.*f\" % (z, p , f))" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 1, 128 | "id": "ac87ae76-b9cf-4e97-ace9-10aeacbab023", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "x : 3 \n", 136 | "y : 5 \n", 137 | "z : 8\n", 138 | "00005.44*\n", 139 | "7.000000 \n", 140 | " 3\n" 141 | ] 142 | }, 143 | { 144 | "name": "stdin", 145 | "output_type": "stream", 146 | "text": [ 147 | "p : 562\n" 148 | ] 149 | }, 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "5.4356342243563631200231611728668212890625000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "# format string syntax\n", 160 | "# استفاده از درصد برای نگهداری داده ها\n", 161 | "x = 3\n", 162 | "y = 5.43563422435636353\n", 163 | "\n", 164 | "print(\"x : %i \\ny : %i \\nz : %i\"%(x,y,5+3))\n", 165 | "\n", 166 | "# [(key)] [flag] [w] [.p] type\n", 167 | "# type is mandatory\n", 168 | "\n", 169 | "print(\"%08.2f\" % (y) , \"*\" , sep=\"\")\n", 170 | "\n", 171 | "d = { \"a\": 3 , \"b\": 7 }\n", 172 | "print(\"%(b)f \\n %(a)i\" %(d))\n", 173 | "\n", 174 | "p = int(input(\"p :\"));\n", 175 | "\n", 176 | "print(\"%*.*f\" % (5,p,y))" 177 | ] 178 | }, 179 | { 180 | "cell_type": "markdown", 181 | "id": "690f8477", 182 | "metadata": {}, 183 | "source": [ 184 | "# .format" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 5, 190 | "id": "41654aac", 191 | "metadata": {}, 192 | "outputs": [ 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "x is 4321.123456789\n", 198 | "y is 459876\n", 199 | "z is 12\n", 200 | "\n", 201 | "\n", 202 | "y is 459876\n", 203 | "x is 4321.123456789\n", 204 | "z is 12\n", 205 | "\n", 206 | "\n", 207 | "z is 12\n", 208 | "y is 459876\n", 209 | "x is 4321.123456789\n", 210 | "\n", 211 | "\n", 212 | "z is 12\n", 213 | "y is 459876\n", 214 | "x is 4321.123456789\n", 215 | "\n", 216 | "\n", 217 | "y is o\n", 218 | "x is r\n", 219 | "z is w\n", 220 | "\n", 221 | "\n", 222 | "y is 459876\n", 223 | "x is 4321.123456789\n", 224 | "z is 20\n", 225 | "\n", 226 | "\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "# فرمت دهی به خروجی با .format\n", 232 | "x = 4321.123456789\n", 233 | "y = 459876\n", 234 | "print(\"x is {}\\ny is {}\\nz is {}\\n\\n\" .format(x, y, 5 + 7))\n", 235 | "\n", 236 | "print(\"y is {1}\\nx is {2}\\nz is {0}\\n\\n\" .format(5 + 7, y, x)) # در این روش اگر ترتیب متغیر ها متفاوت است می توان ایندکس را داخل آکولات گذاشت\n", 237 | "# توجه شود که ایندکس گذاری در پایتون از صفر شروع می شود\n", 238 | "\n", 239 | "d = {'x':x, 'y':y, 'z':5+7}\n", 240 | "print(\"z is {z}\\ny is {y}\\nx is {x}\\n\\n\" .format(**d)) # می توان برای مشخص کردن ترتیب از دیکشنری استفاده کرد\n", 241 | "print(\"z is {z}\\ny is {y}\\nx is {x}\\n\\n\" .format(x = d['x'], y = d['y'], z = d['z'])) # شکل دیگری از استفاده از دیکشنری\n", 242 | "\n", 243 | "\n", 244 | "#استفاده از توالی ها\n", 245 | "print(\"y is {1}\\nx is {2}\\nz is {0}\\n\\n\" .format(*\"world\")) # با توجه به ایندکس استفاده از رشته\n", 246 | "print(\"y is {1}\\nx is {2}\\nz is {0}\\n\\n\" .format(*[z, y, x])) #استفاده از لیست" 247 | ] 248 | }, 249 | { 250 | "cell_type": "raw", 251 | "id": "753a7447", 252 | "metadata": {}, 253 | "source": [ 254 | " شکل قالب خروجی با .format\n", 255 | " \n", 256 | " \"{\" [field_name] [\"!\" conversion] [\":\" format_spec] \"}\"\n", 257 | " filed_name= آدرس دهی Conersion= تبدیل به فرمت جدید format_spec= شکل فرمت\n", 258 | "\n", 259 | "بعد دو نقطه [[fill]بalign] [sign] [#] [0] [width] [grouping_option] [.precision] [type]\n", 260 | "[type] = c | s | % | e | d | i نداریم\n", 261 | "[.precision] = بعد . تعداد اعشار\n", 262 | "[grouping_option] = تمایز بین اعداد مثلا پول = , | _\n", 263 | "[width] = طول میدان\n", 264 | "[0] = پر کردن فاصله خالی میدان با صفر\n", 265 | "[#] = مبنا = #b | b | o | x = فقط برای اعداد\n", 266 | "[sign] + نمایش علامت \n", 267 | "[align] = < ^ > جهت فاصله های میدان\n", 268 | "[fill] = فاصله طول میدان را به چی پر کند\n" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 6, 274 | "id": "c3d439c0", 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "name": "stdout", 279 | "output_type": "stream", 280 | "text": [ 281 | "Hello 'world'\n", 282 | "10,164,932,056,947.14\n", 283 | "+000_987\n", 284 | "x is ..............12..............\n", 285 | "x is @@@@@@@@@@@@@@@@@@@@@@@@@@@@75\n" 286 | ] 287 | } 288 | ], 289 | "source": [ 290 | "print(\"Hello {0!r:}\" .format(\"world\"))\n", 291 | "print(\"{0:,.2f}\" .format(10164932056947.1364896487))\n", 292 | "print(\"{0:+08_.0f}\" .format(987.321))\n", 293 | "print(\"x is {:.^30}\" .format(12))\n", 294 | "print(\"x is {:@{align}30{type}}\" .format(75, align='>', type='d')) # تعیین مقادیر فرمت در خارج از رشته پرینت" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 2, 300 | "id": "471e338d-15f3-436e-a0fe-d477b8103dfa", 301 | "metadata": {}, 302 | "outputs": [ 303 | { 304 | "name": "stdout", 305 | "output_type": "stream", 306 | "text": [ 307 | "x : 3 \ty : 5.435634224356363 \tz : 8\n", 308 | "x : 3 \ty : 8 \tz : 5.435634224356363\n", 309 | "x : 5.435634224356363 \ty : 3 \tz : 5.435634224356363\n", 310 | "a :3 \t b :7\n", 311 | "a :3 \t b :3\n", 312 | "x : e \ty : r \tz : a\n", 313 | "x : 2 \ty : 1 \tz : 5\n", 314 | "x is '+'\n", 315 | "x is n\n", 316 | "x is 0,000,001,231.32\n", 317 | "x is 0b1100\n", 318 | "x is +000,001,240.00\n", 319 | "x is *****1240******\n", 320 | "x is ***********1240\n" 321 | ] 322 | } 323 | ], 324 | "source": [ 325 | "x = 3\n", 326 | "y = 5.43563422435636353\n", 327 | "\n", 328 | "print(\"x : {} \\ty : {} \\tz : {}\".format(x,y,5+3))\n", 329 | "print(\"x : {0} \\ty : {2} \\tz : {1}\".format(x,y,5+3))\n", 330 | "print(\"x : {1} \\ty : {0} \\tz : {1}\".format(x,y,5+3))\n", 331 | "\n", 332 | "d = { \"a\": 3 , \"b\": 7 }\n", 333 | "print(\"a :{a} \\t b :{b}\".format(**d))\n", 334 | "print(\"a :{a} \\t b :{a}\".format(a = d['a'] , b = d['b']))\n", 335 | "\n", 336 | "print(\"x : {1} \\ty : {0} \\tz : {3}\".format(*'reza'))\n", 337 | "print(\"x : {1} \\ty : {0} \\tz : {2}\".format(*[1,2,5]))\n", 338 | "\n", 339 | "# \"{\" [field_name] [\"!\" conversion] [\":\" format_spec] \"}\"\n", 340 | "print(\"x is {0!r:3}\".format(\"+\"))\n", 341 | "\n", 342 | "# after \":\"\n", 343 | "# [[fill]align][sign][#][0][with][grouping_option][.precision][type]\n", 344 | "\n", 345 | "print(\"x is {0:c}\".format(110))\n", 346 | "print(\"x is {0:015,.2f}\".format(1231.3240))\n", 347 | "print(\"x is {0:#b}\".format(12))\n", 348 | "print(\"x is {0:+015,.2f}\".format(1240))\n", 349 | "print(\"x is {0:*^15}\".format(1240))\n", 350 | "\n", 351 | "\n", 352 | "print(\"x is {0:*{align}15}\".format(1240 , align = '>'))" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "id": "c7c19a25", 358 | "metadata": {}, 359 | "source": [ 360 | "# روش f - string" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 7, 366 | "id": "b42fe3f1", 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "name": "stdout", 371 | "output_type": "stream", 372 | "text": [ 373 | "x is 5\n", 374 | "y is 4.6353\n", 375 | "z is 25\n", 376 | "Hello 'world!'\n", 377 | "{x} is {5}\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "x = 5\n", 383 | "y = 4.6353\n", 384 | "print(f\"x is {x}\\ny is {y}\\nz is {5**2}\") # در این روش متغیر یا مقدارش را در آکولات گذاشته و فقط قبل رشته اِف می گذاریم\n", 385 | "# در این روش فرمت دهی در همان آکولات به شکل .format\n", 386 | "s = \"world!\"\n", 387 | "print(f\"Hello {s!r:}\")\n", 388 | "print(f\"{{x}} is {{{x}}}\") # اگر می خواهیم آکولات هم چاپ شود. این مورد نیز به همین شکل در .format" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 8, 394 | "id": "9c3b5c8d", 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "name": "stdout", 399 | "output_type": "stream", 400 | "text": [ 401 | "result: HELLO\n", 402 | "replace: hell!!!\n" 403 | ] 404 | } 405 | ], 406 | "source": [ 407 | "# چاپ خروجی تابع\n", 408 | "def f(s):\n", 409 | " return s.upper()\n", 410 | " \n", 411 | "x = \"hello\"\n", 412 | "print(f\"result: {f(x)}\")\n", 413 | "print(f\"replace: {x.replace('o','!!!')}\")" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 9, 419 | "id": "6c2e03ae", 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "name": "stdout", 424 | "output_type": "stream", 425 | "text": [ 426 | "Enter the Name: Salam\n", 427 | "Enter the Age: 25\n", 428 | "\n", 429 | "The name is: Salam\n", 430 | "The age is: 25\n" 431 | ] 432 | } 433 | ], 434 | "source": [ 435 | "#مثال\n", 436 | "name = input(\"Enter the Name: \")\n", 437 | "age = int(input(\"Enter the Age: \"))\n", 438 | "\n", 439 | "msg = (\n", 440 | " f\"\\nThe name is: {name}\\n\"\n", 441 | " f\"The age is: {age}\"\n", 442 | ")\n", 443 | "\n", 444 | "print(msg)" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 10, 450 | "id": "2956254b", 451 | "metadata": {}, 452 | "outputs": [ 453 | { 454 | "name": "stdout", 455 | "output_type": "stream", 456 | "text": [ 457 | "today is 2022/11\n" 458 | ] 459 | } 460 | ], 461 | "source": [ 462 | "import datetime\n", 463 | "s = datetime.datetime.now()\n", 464 | "print(f\"today is {s:%Y/%m}\")" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": null, 470 | "id": "c806d931-4fb9-4776-8624-6273ac2b8c68", 471 | "metadata": {}, 472 | "outputs": [], 473 | "source": [ 474 | "#F string\n", 475 | "x = 3\n", 476 | "y = 5.43563422435636353\n", 477 | "\n", 478 | "print(F\"x : {x!r} \\ty : {y:010.2f} \\tz : {5+8}\")\n", 479 | "print(F\"x : {{x}} ,, {{{x}}}\")\n", 480 | " \n", 481 | "x = \"amir\"\n", 482 | "def f(s):\n", 483 | " return s.upper()\n", 484 | "print(f\"x is {f(x)}\")\n", 485 | "\n", 486 | "# you can use methode in curly bracket \n", 487 | "\n", 488 | "name = \"amir\"\n", 489 | "age = 23\n", 490 | "\n", 491 | "msg = (\n", 492 | " f\"name: {name}\\n\"\n", 493 | " f\"age: {age}\\n\"\n", 494 | ")\n", 495 | "print(msg)\n", 496 | "\n", 497 | "\n", 498 | "import datetime\n", 499 | "today = datetime.datetime.today()\n", 500 | "\n", 501 | "print(f\"{today:%Y/%m****%B}\")\n", 502 | "\n", 503 | "\n", 504 | "d = { \"a\": 3 , \"b\": 7 }\n", 505 | "print(\"a :{a} \\t b :{b}\".format(**d))\n", 506 | "print(\"a :{a} \\t b :{a}\".format(a = d['a'] , b = d['b']))\n", 507 | "\n", 508 | "print(\"x : {1} \\ty : {0} \\tz : {3}\".format(*'reza'))\n", 509 | "print(\"x : {1} \\ty : {0} \\tz : {2}\".format(*[1,2,5]))\n", 510 | "\n", 511 | "# \"{\" [field_name] [\"!\" conversion] [\":\" format_spec] \"}\"\n", 512 | "\n", 513 | "# after \":\"\n", 514 | "# [[fill]align][sign][#][0][with][grouping_option][.precision][type]\n", 515 | "\n", 516 | "print(f\"{x:+<12}*\")" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": null, 522 | "id": "d440b52c-753f-450d-b7ac-2f84d68187da", 523 | "metadata": {}, 524 | "outputs": [], 525 | "source": [] 526 | } 527 | ], 528 | "metadata": { 529 | "kernelspec": { 530 | "display_name": "Python 3 (ipykernel)", 531 | "language": "python", 532 | "name": "python3" 533 | }, 534 | "language_info": { 535 | "codemirror_mode": { 536 | "name": "ipython", 537 | "version": 3 538 | }, 539 | "file_extension": ".py", 540 | "mimetype": "text/x-python", 541 | "name": "python", 542 | "nbconvert_exporter": "python", 543 | "pygments_lexer": "ipython3", 544 | "version": "3.10.9" 545 | } 546 | }, 547 | "nbformat": 4, 548 | "nbformat_minor": 5 549 | } 550 | -------------------------------------------------------------------------------- /04_SyntaxLearn_Control_ifelse.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 12, 6 | "id": "5327edfd-81d6-4fec-b10f-822d0320efd6", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdin", 11 | "output_type": "stream", 12 | "text": [ 13 | "enter x,y,z : 10,30,50\n" 14 | ] 15 | }, 16 | { 17 | "name": "stdout", 18 | "output_type": "stream", 19 | "text": [ 20 | "x :10 | type of x: | y :30 | z :50\n" 21 | ] 22 | }, 23 | { 24 | "name": "stdin", 25 | "output_type": "stream", 26 | "text": [ 27 | "enter numbers as you want(sep with ,) : 1,2,3,4,5,6\n", 28 | "enter numbers as you want(sep with space) : 7 8 9 10 11 12 13\n" 29 | ] 30 | }, 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "['1', '2', '3', '4', '5', '6'] \n", 36 | " [7, 8, 9, 10, 11, 12, 13]\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "# گرفتن چندین ورودی در یک خط\n", 42 | "x, y, z = input(\"enter x,y,z :\").split(\",\") # دستور اینپوت یک رشه را بر میگرداند.\n", 43 | "print(f\"x :{x} | type of x:{type(x)} | y :{y} | z :{z}\")\n", 44 | "\n", 45 | "L1 = input(\"enter numbers as you want(sep with ,) :\").split(',')\n", 46 | "# بزرگترین محدودیت این روش این است که دریافتی ها بصورت رشته است\n", 47 | "# می توان از دستوری مستقیم یا حلقه برای تبدیل به اینتیجر یا فلوت استفاده کرد\n", 48 | "# روش خلاصه سازی و درکی cpmprehension\n", 49 | "L2 = [int(x) for x in input(\"enter numbers as you want(sep with space) :\").split(' ')]\n", 50 | "print(L1,'\\n',L2)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "id": "1cc65ed2-0bf8-4cfa-a472-18d44547a992", 56 | "metadata": {}, 57 | "source": [ 58 | "# دستور شرطی" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 1, 64 | "id": "f7edfc47-425e-4749-8f28-058426da032a", 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdin", 69 | "output_type": "stream", 70 | "text": [ 71 | "Enter the mark : 12\n" 72 | ] 73 | }, 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "Accepted\n", 79 | "End\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "mark = int(input('Enter the mark :'))\n", 85 | "\n", 86 | "if mark < 10 :\n", 87 | " print('Rejected!')\n", 88 | "else: # می تواند نباشد ولی این در صورتی اجرا میشود که شروط قبلی صحیح نباشند\n", 89 | " print('Accepted') \n", 90 | " \n", 91 | "print('End')" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 15, 97 | "id": "4e6f887b-47f7-4a86-b3b2-22858bef2c9c", 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdin", 102 | "output_type": "stream", 103 | "text": [ 104 | "Enter the mark : 9.89\n" 105 | ] 106 | }, 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "C\n", 112 | "\n", 113 | "\\*****End\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "# elif\n", 119 | "mark = float(input('Enter the mark :'))\n", 120 | "\n", 121 | "if mark <= 20 and mark >= 15:\n", 122 | " print('A')\n", 123 | "elif mark < 15 and mark >= 10:\n", 124 | " print('B')\n", 125 | "elif mark < 10 and mark >= 5:\n", 126 | " print('C')\n", 127 | "elif 5 > mark >= 0: # نوع دیگری از نوشتن شرط های بالا\n", 128 | " print('D')\n", 129 | "else:\n", 130 | " print('Wrong!')\n", 131 | "\n", 132 | "\n", 133 | "print('\\n\\*****End')\n", 134 | "\n", 135 | "# فقط یک if\n", 136 | "# elif بعد if\n", 137 | "# else فقط یکی و حتما در آخر" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 2, 143 | "id": "3869c2ec-56b9-4250-84c2-65bc6c33aee4", 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "5\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "# شرط در هنگام مقدار دهی\n", 156 | "x = 25\n", 157 | "y = 10+2 if x < 20 else 5\n", 158 | "print(y)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 6, 164 | "id": "0108cbc5-c1d2-40f6-b3b2-ae4fa1342b69", 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "name": "stdin", 169 | "output_type": "stream", 170 | "text": [ 171 | "x is : 2523175434\n" 172 | ] 173 | }, 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "Even\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "# مثال : عدد زوج است یا فرد\n", 184 | "x = int(input(\"x is :\"))\n", 185 | "\n", 186 | "if x % 2 == 0:\n", 187 | " print(\"Even\")\n", 188 | "else:\n", 189 | " print(\"Odd\")" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 1, 195 | "id": "b542ce57-16fc-44d2-b48c-1f80181e3fed", 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdin", 200 | "output_type": "stream", 201 | "text": [ 202 | "enter x: 5\n", 203 | "enter y: 6\n", 204 | "enter z: 8\n" 205 | ] 206 | }, 207 | { 208 | "name": "stdout", 209 | "output_type": "stream", 210 | "text": [ 211 | "5\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "x = int(input('enter x:'))\n", 217 | "y = int(input('enter y:'))\n", 218 | "z = int(input('enter z:'))\n", 219 | "\n", 220 | "min = x \n", 221 | "if x > y:\n", 222 | " min = y\n", 223 | "if y > z:\n", 224 | " min = z\n", 225 | "\n", 226 | "print(min)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 4, 232 | "id": "2781b47c-15f7-4611-8f7d-70e5a6de73c9", 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "name": "stdout", 237 | "output_type": "stream", 238 | "text": [ 239 | "101\n" 240 | ] 241 | } 242 | ], 243 | "source": [ 244 | "print(ord(\"e\"))\n", 245 | "# to get ascii code of character" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": null, 251 | "id": "8eaf7256-b14f-41e4-9bd4-420c622df885", 252 | "metadata": {}, 253 | "outputs": [], 254 | "source": [] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "id": "5f3b8ae7-9bfe-4002-8511-a9366c62ebe3", 259 | "metadata": {}, 260 | "source": [ 261 | "# Sum, Max , min" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 22, 267 | "id": "7e4b9630-5722-4bdd-9baf-32ef3b7d2217", 268 | "metadata": {}, 269 | "outputs": [ 270 | { 271 | "ename": "TypeError", 272 | "evalue": "'int' object is not callable", 273 | "output_type": "error", 274 | "traceback": [ 275 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 276 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 277 | "Cell \u001b[0;32mIn[22], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m L \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m12\u001b[39m, \u001b[38;5;241m15\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mmin\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mL\u001b[49m\u001b[43m \u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdefault\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mError!\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m) \u001b[38;5;66;03m# اگر بصورت لیست باشد می توان از دیفالت استفاده کرد\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mmin\u001b[39m(\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m))\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# if list or tuple are empety ,default value shown\u001b[39;00m\n", 278 | "\u001b[0;31mTypeError\u001b[0m: 'int' object is not callable" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "L = [0, 12, 15, 1, 2]\n", 284 | "print(min(L , default = 'Error!')) # اگر بصورت لیست باشد می توان از دیفالت استفاده کرد\n", 285 | "print(min(1,2,3,4,5))\n", 286 | "# if list or tuple are empety ,default value shown\n", 287 | "\n", 288 | "print(max(L))\n", 289 | "\n", 290 | "print(min('ali', 'reza', 'zeydan', 'farhad')) # اولین حرف الفبا\n", 291 | "print(max('ali', 'reza', 'zeydan', 'farhad')) # آخرین حرف الفبا\n", 292 | "\n", 293 | "print(sum(L))\n", 294 | "print(sum([1,2,3])) # حتمال داخل لیست یا تاپل یا مجموعه\n", 295 | "print(sum(L, start = 3)) # استارت، عددی است که به نتیجه جمع اضافه می شود" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": null, 301 | "id": "09686a61-6e57-43cb-afae-b7f78e07d626", 302 | "metadata": {}, 303 | "outputs": [], 304 | "source": [] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": null, 309 | "id": "2174c02f-fca9-45f9-965f-f2652a11b32f", 310 | "metadata": {}, 311 | "outputs": [], 312 | "source": [] 313 | } 314 | ], 315 | "metadata": { 316 | "kernelspec": { 317 | "display_name": "Python 3 (ipykernel)", 318 | "language": "python", 319 | "name": "python3" 320 | }, 321 | "language_info": { 322 | "codemirror_mode": { 323 | "name": "ipython", 324 | "version": 3 325 | }, 326 | "file_extension": ".py", 327 | "mimetype": "text/x-python", 328 | "name": "python", 329 | "nbconvert_exporter": "python", 330 | "pygments_lexer": "ipython3", 331 | "version": "3.10.9" 332 | } 333 | }, 334 | "nbformat": 4, 335 | "nbformat_minor": 5 336 | } 337 | -------------------------------------------------------------------------------- /05_SyntaxLearn_Control_Iteration.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "aa858869-cab0-4e71-9458-2a39c72bdcd8", 6 | "metadata": {}, 7 | "source": [ 8 | "# حلقه While" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "e1112af6-f1bf-415a-b748-2725bdec7a5c", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdin", 19 | "output_type": "stream", 20 | "text": [ 21 | "x : 5\n" 22 | ] 23 | }, 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "6, 8, " 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "# While\n", 34 | "n = int(input(\"x : \")) # نقطه شروع\n", 35 | "\n", 36 | "while n<10:\n", 37 | " if n%2 == 0:\n", 38 | " print(n, end=\", \")\n", 39 | " n += 1\n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "id": "42116e4b-39d5-4efb-832b-5e4b0b810702", 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdin", 50 | "output_type": "stream", 51 | "text": [ 52 | "x is : 24\n" 53 | ] 54 | }, 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "x : {1, 2, 3, 4, 6, 8, 12, 24, }\n" 60 | ] 61 | }, 62 | { 63 | "name": "stdin", 64 | "output_type": "stream", 65 | "text": [ 66 | "x is : 12\n" 67 | ] 68 | }, 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "x : {1, 2, 3, 4, 6, 12, }\n" 74 | ] 75 | }, 76 | { 77 | "name": "stdin", 78 | "output_type": "stream", 79 | "text": [ 80 | "x is : 18\n" 81 | ] 82 | }, 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "x : {1, 2, 3, 6, 9, 18, }\n" 88 | ] 89 | }, 90 | { 91 | "name": "stdin", 92 | "output_type": "stream", 93 | "text": [ 94 | "x is : 36\n" 95 | ] 96 | }, 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "x : {1, 2, 3, 4, 6, 9, 12, 18, 36, }\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "# چاپ مقسوم علیه های یک عدد دلخواه\n", 107 | "temp = 0\n", 108 | "while temp < 4:\n", 109 | " x = int(input(\"x is : \"))\n", 110 | " count = 1\n", 111 | " print(\"x : {\", end = (''))\n", 112 | " while count <= x:\n", 113 | " if x%count == 0:\n", 114 | " print(count, end = (', '))\n", 115 | " count += 1\n", 116 | " print(\"}\")\n", 117 | " temp += 1\n" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 3, 123 | "id": "40ff99cf-6057-4164-a06c-b5511fd1e83c", 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "0\n", 131 | "1\n", 132 | "1\n", 133 | "2\n", 134 | "3\n", 135 | "5\n", 136 | "8\n", 137 | "13\n", 138 | "21\n", 139 | "34\n", 140 | "55\n", 141 | "89\n", 142 | "144\n", 143 | "233\n", 144 | "377\n", 145 | "610\n", 146 | "987\n", 147 | "1597\n", 148 | "2584\n", 149 | "4181\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "# 0,1,1,2,3,5,8,13,21,34,... سری فیبوناچی\n", 155 | "a, b = 0, 1\n", 156 | "i = 1\n", 157 | "while i <= 20:\n", 158 | " print(a)\n", 159 | " a, b = b, a+b # اگر در دو خط جدا بنویسیم اشتباه میشود مقدار دهی بصورت همزمان انجام میشود\n", 160 | " i += 1" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "id": "c790273b-eea6-421f-9558-0e41f7f097b9", 166 | "metadata": {}, 167 | "source": [ 168 | "## دستور Break\n" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 4, 174 | "id": "e4122bdf-35b6-41a9-bca5-af708b5dc1d4", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, end of code\n" 182 | ] 183 | } 184 | ], 185 | "source": [ 186 | "i = 1\n", 187 | "while i<= 20:\n", 188 | " print(i,end=\", \")\n", 189 | " if i == 10: \n", 190 | " break # از حلقه بیرون می پرد، نزدیک ترین حلقه\n", 191 | " i += 1\n", 192 | "print(\"end of code\")" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 6, 198 | "id": "f828ed06-a11a-4934-856b-8e68a1efab1b", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdin", 203 | "output_type": "stream", 204 | "text": [ 205 | "n: 12.4\n", 206 | "Do you want to conitnue? yes\n", 207 | "n: 36.124\n", 208 | "Do you want to conitnue? yes\n", 209 | "n: 2.647\n", 210 | "Do you want to conitnue? no\n" 211 | ] 212 | }, 213 | { 214 | "name": "stdout", 215 | "output_type": "stream", 216 | "text": [ 217 | "2.647 is smallest number!\n" 218 | ] 219 | } 220 | ], 221 | "source": [ 222 | "# کنترل حلقه بی نهایت با break\n", 223 | "m = float(\"inf\")\n", 224 | "while True:\n", 225 | " n = float(input(\"n: \"))\n", 226 | " if n1:\n", 302 | " while i\n", 642 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 643 | "******************************\n", 644 | "0-1-2-3-4-\n", 645 | "******************************\n", 646 | "2-3-4-5-6-7-8-9-\n", 647 | "******************************\n", 648 | "0-3-6-9-12-15-18-21-24-27-30-\n", 649 | "******************************\n", 650 | "-20\t-33\t-46\t-59\t-72\t-85\t-98\t-111\t-124\t" 651 | ] 652 | } 653 | ], 654 | "source": [ 655 | "print(type(range(10)))\n", 656 | "print(list(range(10))) # از صفر شروع می شود و خود 10 شامل نمی شود\n", 657 | "print('*'*30)\n", 658 | "\n", 659 | "for i in range(5):\n", 660 | " print(i, end = \"-\")\n", 661 | "print()\n", 662 | "print('*'*30)\n", 663 | "\n", 664 | "for i in range(2,10): # از 2 شروع می شود تا 10\n", 665 | " print(i, end = \"-\") \n", 666 | "print()\n", 667 | "print('*'*30)\n", 668 | "\n", 669 | "for i in range(0,31,3): # عدد 3 گام رنج هست\n", 670 | " print(i, end = \"-\") \n", 671 | "print()\n", 672 | "print('*'*30)\n", 673 | "for i in range(-20, -131, -13): \n", 674 | " print(i, end = \"\\t\") " 675 | ] 676 | }, 677 | { 678 | "cell_type": "code", 679 | "execution_count": 15, 680 | "id": "a4d1abbc-609f-4e89-a2a3-4081ebf532b7", 681 | "metadata": {}, 682 | "outputs": [ 683 | { 684 | "name": "stdin", 685 | "output_type": "stream", 686 | "text": [ 687 | "n : 5\n" 688 | ] 689 | }, 690 | { 691 | "name": "stdout", 692 | "output_type": "stream", 693 | "text": [ 694 | "f = 120\n" 695 | ] 696 | } 697 | ], 698 | "source": [ 699 | "# مثال فاکتوریل\n", 700 | "n = int(input(\"n : \"))\n", 701 | "f = 1 # Initial value\n", 702 | "\n", 703 | "for i in range(1, n+1):\n", 704 | " f *= i\n", 705 | "print(\"f =\", f)\n" 706 | ] 707 | }, 708 | { 709 | "cell_type": "code", 710 | "execution_count": 17, 711 | "id": "c48acc85-421d-42c9-8dcb-34edbc5afcb2", 712 | "metadata": { 713 | "tags": [] 714 | }, 715 | "outputs": [ 716 | { 717 | "name": "stdin", 718 | "output_type": "stream", 719 | "text": [ 720 | "n is : 123456789\n" 721 | ] 722 | }, 723 | { 724 | "name": "stdout", 725 | "output_type": "stream", 726 | "text": [ 727 | "987654321" 728 | ] 729 | } 730 | ], 731 | "source": [ 732 | "#مثال برعکس نوشتن یک عدد\n", 733 | "n = input('n is : ') # دریافت بصورت رشته\n", 734 | "for i in range(len(n)-1, -1, -1):\n", 735 | " print(n[i], end=\"\")" 736 | ] 737 | }, 738 | { 739 | "cell_type": "code", 740 | "execution_count": 18, 741 | "id": "03ef1733-30cc-4575-b579-9c6eb5d3dfa8", 742 | "metadata": {}, 743 | "outputs": [ 744 | { 745 | "name": "stdout", 746 | "output_type": "stream", 747 | "text": [ 748 | "\n", 749 | "[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]\n", 750 | "0 : a\n", 751 | "1 : b\n", 752 | "2 : c\n", 753 | "3 : d\n" 754 | ] 755 | } 756 | ], 757 | "source": [ 758 | "#enumerate\n", 759 | "l = ['a','b','c','d']\n", 760 | "print(enumerate(l))\n", 761 | "print(list(enumerate(l)))\n", 762 | "for i,j in enumerate(l):\n", 763 | " print(i, \":\", j)" 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "execution_count": 19, 769 | "id": "ec54ee84-c941-4d35-9ac7-8ca4cf4597f3", 770 | "metadata": {}, 771 | "outputs": [ 772 | { 773 | "name": "stdout", 774 | "output_type": "stream", 775 | "text": [ 776 | "name: reaz . age: 13\n", 777 | "name: ali . age: 35\n", 778 | "name: neda . age: 26\n", 779 | "name: mina . age: 41\n" 780 | ] 781 | } 782 | ], 783 | "source": [ 784 | "#zip در حلقه for\n", 785 | "x = [\"reaz\",\"ali\",\"neda\",\"mina\"]\n", 786 | "y = [13, 35, 26, 41]\n", 787 | "\n", 788 | "for i, j in zip(x,y):\n", 789 | " print(\"name:\", i, \". age:\", j)" 790 | ] 791 | }, 792 | { 793 | "cell_type": "code", 794 | "execution_count": 20, 795 | "id": "7f332d77-db81-44d5-84b4-211e2370a356", 796 | "metadata": {}, 797 | "outputs": [ 798 | { 799 | "name": "stdout", 800 | "output_type": "stream", 801 | "text": [ 802 | "4\n", 803 | "3\n", 804 | "2\n", 805 | "1\n", 806 | "******************************\n", 807 | "72\n", 808 | "56\n", 809 | "32\n", 810 | "23\n", 811 | "14\n", 812 | "9\n" 813 | ] 814 | } 815 | ], 816 | "source": [ 817 | "# معکوس کردن حرکت با reversed\n", 818 | "l = [1,2,3,4]\n", 819 | "for i in reversed(l):\n", 820 | " print(i)\n", 821 | "print('*'*30)\n", 822 | "\n", 823 | "# مرتب کردن لیست در حلقه با sorted\n", 824 | "\n", 825 | "l2 = [23,32,14,56,72,9]\n", 826 | "for i in sorted(l2, reverse=True):\n", 827 | " print(i)" 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "execution_count": null, 833 | "id": "1228f343-9633-45d4-a9e9-fbf487a24b52", 834 | "metadata": {}, 835 | "outputs": [], 836 | "source": [] 837 | }, 838 | { 839 | "cell_type": "markdown", 840 | "id": "ce4ff746-dc5e-464a-82b8-28db90923f4e", 841 | "metadata": {}, 842 | "source": [ 843 | "# اعداد تصادفی Random (شبه تصادفی)" 844 | ] 845 | }, 846 | { 847 | "cell_type": "code", 848 | "execution_count": 21, 849 | "id": "c896580f-d707-4cd7-9bc9-3eba1477ce6e", 850 | "metadata": {}, 851 | "outputs": [ 852 | { 853 | "name": "stdout", 854 | "output_type": "stream", 855 | "text": [ 856 | "0.8495821014177612\n" 857 | ] 858 | } 859 | ], 860 | "source": [ 861 | "import random # نیازمند وارد کردن ماژول مرتبط\n", 862 | "print(random.random()) #تولید عدد تصادفی بین باز صفر تا یک\n" 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 18, 868 | "id": "a12f8012-3f46-4e17-94a2-c2bb5971b847", 869 | "metadata": {}, 870 | "outputs": [ 871 | { 872 | "name": "stdout", 873 | "output_type": "stream", 874 | "text": [ 875 | "0.9248652516259452\n", 876 | "0.9486057779931771\n", 877 | "0.8924333440485793\n", 878 | "0.08355067683068362\n" 879 | ] 880 | } 881 | ], 882 | "source": [ 883 | "# seed در اعداد تصادفی\n", 884 | "# با تعیین seed همیشه عدد تصادفی مرتبط با آن تولید می شود\n", 885 | "from random import random, seed\n", 886 | "seed(23)\n", 887 | "print(random())\n", 888 | "print(random())\n", 889 | "print(random())\n", 890 | "print(random())\n", 891 | "# وقتی ما سید نمی دهیم، زمان که هر لحظه متغیر است به عنوان سید در نظر گرفته می شود" 892 | ] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "execution_count": 20, 897 | "id": "66f9812c-3729-44a5-a8f2-c7d6be64973d", 898 | "metadata": {}, 899 | "outputs": [ 900 | { 901 | "name": "stdout", 902 | "output_type": "stream", 903 | "text": [ 904 | "8.104957275690335\n", 905 | "9.853676137894785\n", 906 | "6.813962592390542\n", 907 | "8.798933061217655\n", 908 | "6.843419073841034\n", 909 | "7.860233905958904\n", 910 | "******************************\n", 911 | "8\n", 912 | "6\n", 913 | "5\n", 914 | "7\n", 915 | "******************************\n", 916 | "10\n", 917 | "8\n", 918 | "9\n", 919 | "8\n", 920 | "******************************\n", 921 | "5\n", 922 | "5\n", 923 | "5\n", 924 | "8\n", 925 | "******************************\n", 926 | "e\n", 927 | "b\n", 928 | "e\n", 929 | "******************************\n", 930 | "['a', 'e']\n", 931 | "******************************\n", 932 | "['c', 'b', 'e', 'd', 'a']\n" 933 | ] 934 | } 935 | ], 936 | "source": [ 937 | "# عدد تصادفی در بازه\n", 938 | "# فرمول low + rand*(up - low)\n", 939 | "from random import random\n", 940 | "for _ in range(6): # از _ معمولا وقتی که استفاده ای از شمارنده نمی شود استفاده می شود\n", 941 | " print(5 + random()*(10-5)) # بازه 5 تا 10\n", 942 | "print(\"*\"*30) # *****************************************************************************\n", 943 | "\n", 944 | "# متد uniform\n", 945 | "import random\n", 946 | "for _ in range(4):\n", 947 | " print(int(random.uniform(5,10))) # استفاده از یونیفورم برای تعیین بازه برای رندوم و اینت برای حذف اعشار\n", 948 | " # خود 10 وانتهای بازه نخواهد بود\n", 949 | "print(\"*\"*30) # *****************************************************************************\n", 950 | "\n", 951 | "for _ in range(4):\n", 952 | " print(random.randint(5,10)) # متدی برای تولید اعداد تصادفی صحیح و خود انتهای بازه هم می تواند باشد\n", 953 | "print(\"*\"*30) # *****************************************************************************\n", 954 | "\n", 955 | "for _ in range(4):\n", 956 | " print(random.randrange(5,10,3)) # گام هم میتونیم تعیین بکنیم ـ متدی برای تولید اعداد تصادفی صحیح و خود انتهای بازه هم می تواند باشد\n", 957 | "print(\"*\"*30) # *****************************************************************************\n", 958 | "\n", 959 | "l = ['a','b','c','d','e']\n", 960 | "for _ in range(3):\n", 961 | " print(random.choice(l)) # انتخاب تصادفی از بین لیست\n", 962 | "print(\"*\"*30) # *****************************************************************************\n", 963 | "\n", 964 | "print(random.sample(l,2)) # از لیست یک نمونه تصادفی تولید می کند\n", 965 | "print(\"*\"*30) # *****************************************************************************\n", 966 | "\n", 967 | "random.shuffle(l) # ترتیب خود لیست را تصادفی تغییر می دهد و بُر میزند\n", 968 | "print(l)\n", 969 | " " 970 | ] 971 | }, 972 | { 973 | "cell_type": "code", 974 | "execution_count": 24, 975 | "id": "79bdfdf7-3a07-4ce5-b1b7-ade841fd5c40", 976 | "metadata": {}, 977 | "outputs": [ 978 | { 979 | "name": "stdout", 980 | "output_type": "stream", 981 | "text": [ 982 | "{1: 1684, 2: 1690, 3: 1637, 4: 1627, 5: 1680, 6: 1682}\n" 983 | ] 984 | } 985 | ], 986 | "source": [ 987 | "# مثال تاس\n", 988 | "from random import randint\n", 989 | "tas = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0} # مقدار اولیه برای هر رویداد تاس\n", 990 | "\n", 991 | "for _ in range(10000):\n", 992 | " tas[randint(1,6)] += 1\n", 993 | "\n", 994 | "print(tas)\n" 995 | ] 996 | }, 997 | { 998 | "cell_type": "markdown", 999 | "id": "ab081b89-e6d4-47fd-8555-a2d87234d070", 1000 | "metadata": {}, 1001 | "source": [ 1002 | "از numpy هم می توان اعداد تصادفی تولید کرد که نیازمند نصب هست تا بتوان ایمپورت کرد" 1003 | ] 1004 | }, 1005 | { 1006 | "cell_type": "code", 1007 | "execution_count": 29, 1008 | "id": "1478e5b7-f0a1-4206-94bd-95f11287dbf7", 1009 | "metadata": {}, 1010 | "outputs": [ 1011 | { 1012 | "name": "stdin", 1013 | "output_type": "stream", 1014 | "text": [ 1015 | "x : 23\n", 1016 | "y : 23\n" 1017 | ] 1018 | }, 1019 | { 1020 | "name": "stdout", 1021 | "output_type": "stream", 1022 | "text": [ 1023 | "23\n", 1024 | "******************************\n" 1025 | ] 1026 | }, 1027 | { 1028 | "name": "stdin", 1029 | "output_type": "stream", 1030 | "text": [ 1031 | "x : 23\n", 1032 | "y : 23\n" 1033 | ] 1034 | }, 1035 | { 1036 | "name": "stdout", 1037 | "output_type": "stream", 1038 | "text": [ 1039 | "23\n" 1040 | ] 1041 | } 1042 | ], 1043 | "source": [ 1044 | "# بزرگترین مقسوم علیه مشترک\n", 1045 | "x = int(input('x :'))\n", 1046 | "y = int(input('y :'))\n", 1047 | "\n", 1048 | "min_ = min(x , y)\n", 1049 | "tem = 0\n", 1050 | "for i in range(1 , (min_+1)):\n", 1051 | " if (x%i == 0) and (y%i == 0):\n", 1052 | " if i > tem :\n", 1053 | " tem = i\n", 1054 | " \n", 1055 | "print(tem)\n", 1056 | "\n", 1057 | "print('*'*30)\n", 1058 | "\n", 1059 | "\n", 1060 | "# بزرگترین مقسوم علیه مشترک\n", 1061 | "x = int(input('x :'))\n", 1062 | "y = int(input('y :'))\n", 1063 | "\n", 1064 | "min_ = min(x , y)\n", 1065 | "for i in range(min_ , 0 , -1):\n", 1066 | " if (x%i == 0) and (y%i == 0):\n", 1067 | " print(i)\n", 1068 | " break" 1069 | ] 1070 | }, 1071 | { 1072 | "cell_type": "code", 1073 | "execution_count": 38, 1074 | "id": "2f30b645-b6a5-4a5c-8b9d-e671661b21d2", 1075 | "metadata": {}, 1076 | "outputs": [ 1077 | { 1078 | "name": "stdin", 1079 | "output_type": "stream", 1080 | "text": [ 1081 | "e is your choice?(y/n) \n", 1082 | "a is your choice?(y/n) y\n" 1083 | ] 1084 | }, 1085 | { 1086 | "name": "stdout", 1087 | "output_type": "stream", 1088 | "text": [ 1089 | "you done!\n" 1090 | ] 1091 | } 1092 | ], 1093 | "source": [ 1094 | "from random import choice\n", 1095 | "name = ['a' , 'e' , 'r' , 't' , 'y']\n", 1096 | "name_cop = name.copy()\n", 1097 | "\n", 1098 | "while True : \n", 1099 | " if len(name_cop) == 0: \n", 1100 | " break\n", 1101 | " cmp_choice = choice(name_cop)\n", 1102 | " ans = input(f\"{cmp_choice} is your choice?(y/n)\")\n", 1103 | " if ans == \"y\":\n", 1104 | " print(\"you done!\")\n", 1105 | " break\n", 1106 | " name_cop.remove(cmp_choice)" 1107 | ] 1108 | }, 1109 | { 1110 | "cell_type": "code", 1111 | "execution_count": null, 1112 | "id": "a2121e84-dcde-4bc2-a5d1-8dda613f6a58", 1113 | "metadata": {}, 1114 | "outputs": [], 1115 | "source": [] 1116 | } 1117 | ], 1118 | "metadata": { 1119 | "kernelspec": { 1120 | "display_name": "Python 3 (ipykernel)", 1121 | "language": "python", 1122 | "name": "python3" 1123 | }, 1124 | "language_info": { 1125 | "codemirror_mode": { 1126 | "name": "ipython", 1127 | "version": 3 1128 | }, 1129 | "file_extension": ".py", 1130 | "mimetype": "text/x-python", 1131 | "name": "python", 1132 | "nbconvert_exporter": "python", 1133 | "pygments_lexer": "ipython3", 1134 | "version": "3.10.9" 1135 | } 1136 | }, 1137 | "nbformat": 4, 1138 | "nbformat_minor": 5 1139 | } 1140 | -------------------------------------------------------------------------------- /06_SyntaxLearn_Function_1_Syntax.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "9bda01c9-8f7d-43b6-bd6d-2c4b5e2090a8", 6 | "metadata": { 7 | "tags": [] 8 | }, 9 | "source": [ 10 | "# تابع در پایتون" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "ddc69f33-6190-4140-bf27-d344a64e2f2d", 16 | "metadata": {}, 17 | "source": [ 18 | "اگر نیاز به اعمال یکسری پروسه های مکرر روی ورودی های مختلف داریم می توانیم از تابع کمک بیگریم" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "id": "f43378e5-7559-4635-aab7-d2fde79356c5", 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "7\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "def f(x): # تعریف تابع\n", 37 | " return 2*x+1 # خروجی تابع\n", 38 | "\n", 39 | "print(f(3)) # فراخوانی تابع" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 2, 45 | "id": "5bfd61c8-4973-4e82-9a64-2708b5761c9a", 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "# تعریف تابع\n", 50 | "# در پایتون نیازی به مشخص کردن نوع خروجی و نوع ورودی ها نیست\n", 51 | "# تعریف تابع با def\n", 52 | "# توصیه میشه توابع به یک شکل اسنیک کیس یا کمل کیس نوشته شود\n", 53 | "# اسم تابع نباید کلمه کلیدی باشد\n", 54 | "# اسم تابع مرتبط با عملکرد باشد\n", 55 | "# بعد از دِف و اسم تابع لازم هست داخل پرانتز ورودی های تابع را مشخص کرد\n", 56 | "# قوانین دندانه گذاری و تو رفتگی لازم هست رعایت شود\n", 57 | "\n", 58 | "\n", 59 | "def cube_pow (x):\n", 60 | " return x ** 3\n" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 3, 66 | "id": "c292d0fe-a7d5-46bc-a93d-4a18c5c7910f", 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "1728\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "# تابع بصورت پیش فرض اجرا نمی شود\n", 79 | "# باید فراخوانی شود تا اجرا شود\n", 80 | "# در صورتی فراخوانی تابع صحیح هست که پرانتز های آن گذاشته شود\n", 81 | "# در داخل پرانتز باید به تعداد ورودی های تعریف شده در هنگام تعریف، ورودی داده شود\n", 82 | "\n", 83 | "def cube_pow (x):\n", 84 | " return x ** 3\n", 85 | "\n", 86 | "print(cube_pow(12))\n" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 1, 92 | "id": "7947788f-8fd2-48a5-9faa-e81d90f656ac", 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "125\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "# حالتی خاص\n", 105 | "\n", 106 | "def cube (x):\n", 107 | " return x ** 3\n", 108 | "\n", 109 | "n = cube # بدون پرانتز ها در واقع تابع فراخوانی نمی شود و انگار اِن همان تابع است و فقط اسمش عوض شده\n", 110 | "\n", 111 | "print(n(5))" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 5, 117 | "id": "0e9f07c4-8fd9-4974-a626-9e6d41ffc6d9", 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdin", 122 | "output_type": "stream", 123 | "text": [ 124 | "Enter x : 4\n" 125 | ] 126 | }, 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "64\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "# تابع می تواند ورودی نداشته باشد و یا حتی ورودی خود را از کاربر بگیرد\n", 137 | "\n", 138 | "def cube ():\n", 139 | " x = int(input(\"Enter x : \"))\n", 140 | " return x ** 3\n", 141 | "\n", 142 | "n = cube()\n", 143 | "print(n)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 7, 149 | "id": "644cb4af-5029-4507-b626-6dc2d20f36e3", 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "name": "stdin", 154 | "output_type": "stream", 155 | "text": [ 156 | "Enter x : 4\n" 157 | ] 158 | }, 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "64\n" 164 | ] 165 | }, 166 | { 167 | "name": "stdin", 168 | "output_type": "stream", 169 | "text": [ 170 | "Enter x : 5\n" 171 | ] 172 | }, 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "125\n", 178 | "None\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "# لزومی ندارد تابع خروجی ای را برگرداند\n", 184 | "\n", 185 | "def cube ():\n", 186 | " x = int(input(\"Enter x : \"))\n", 187 | " print(x ** 3)\n", 188 | " \n", 189 | "cube()\n", 190 | "\n", 191 | "# اگر برای تابع خروجی ای برنگردانیم بطور پیش فرض برمیگرداند None\n", 192 | "\n", 193 | "n = cube()\n", 194 | "print(n)\n", 195 | "\n", 196 | "# return پایان تابع است و دستورات بعد آن اجرا نمی گردند\n" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "id": "40cef28d-0566-49fc-b05c-ed45d2859d95", 202 | "metadata": {}, 203 | "source": [ 204 | "پارامتر و آروگومان" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "id": "95df3983-ca82-4b72-819d-0ef38acdfdec", 210 | "metadata": {}, 211 | "source": [ 212 | "ورودی های تابع در هنگام تعریف پارامتر گفته میشه و در هنگام فراخوان آرگومان گفته می شود" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 8, 218 | "id": "71f9f969-3327-4950-b530-5a8b0954b9e2", 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [ 222 | "# توصیه های نگارش توابع\n", 223 | "# هر تابع حداکثر 20 خط باشد\n", 224 | "# هر تابع فقط یک کار خاص را انجام دهد\n", 225 | "# اسم تابع کارکرد تابع را شرح دهد\n", 226 | "# پارامتر های تابع تا حد امکان کمترین باشد\n", 227 | "# تابع حتما کاری باشد که انجام دهد" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 9, 233 | "id": "10ea6ae3-fa09-46fe-a7da-bdf099ee1333", 234 | "metadata": {}, 235 | "outputs": [], 236 | "source": [ 237 | "# اگر می خواهید اول تابع را تعریف، و در داخل کد بیاورید ولی بعدا دستورات آن را بنویسید pass استفاده کنید از\n", 238 | "# تا دچار خطا نشوید\n", 239 | "\n", 240 | "def funct ():\n", 241 | " pass\n", 242 | "\n", 243 | "funct()\n" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 10, 249 | "id": "4fbfa499-52ba-4224-bada-970781dcd300", 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdin", 254 | "output_type": "stream", 255 | "text": [ 256 | "number ? : 123456789\n", 257 | "digit ? : 5\n" 258 | ] 259 | }, 260 | { 261 | "name": "stdout", 262 | "output_type": "stream", 263 | "text": [ 264 | "5 repeated 1 times\n" 265 | ] 266 | } 267 | ], 268 | "source": [ 269 | "# مثال از تابع\n", 270 | "def repeat(number ,digit):\n", 271 | " count = 0\n", 272 | " while number > 0:\n", 273 | " if number % 10 == digit:\n", 274 | " count += 1\n", 275 | " number //= 10\n", 276 | " return count\n", 277 | " \n", 278 | "number = int(input(\"number ? : \"))\n", 279 | "digit = int(input(\"digit ? : \"))\n", 280 | "\n", 281 | "print(digit, \" repeated\", repeat(number, digit),\"times\")" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": null, 287 | "id": "9f6b42d5-cd55-46ed-bc51-c8d1ac28dd84", 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "name": "stdin", 292 | "output_type": "stream", 293 | "text": [ 294 | "Enter n : 5\n" 295 | ] 296 | }, 297 | { 298 | "name": "stdout", 299 | "output_type": "stream", 300 | "text": [ 301 | "Result = 153\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "# مثال دوم\n", 307 | "# 1! + 2! + 3! + ... + n!\n", 308 | "\n", 309 | "def fact(n):\n", 310 | " f = 1\n", 311 | " for i in range(1, n+1):\n", 312 | " f *= i\n", 313 | " return f\n", 314 | "\n", 315 | "def sum_fact(n): # این اِن هیچ ربطی به اِن قبلی ندارد\n", 316 | " s = 0\n", 317 | " for i in range(1, n+1):\n", 318 | " s += fact(i)\n", 319 | " return s\n", 320 | "\n", 321 | "number = int(input(\"Enter n : \"))\n", 322 | "print(\"Result = \", sum_fact(number))\n", 323 | "\n", 324 | "# توصیه می شود هر تابع فقط یک کار را انجام دهد" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": null, 330 | "id": "f9e73504-021b-43e9-a138-b697c43860d7", 331 | "metadata": {}, 332 | "outputs": [], 333 | "source": [] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "id": "4dbc60a3-6209-45e6-b537-ca6d8cb9730a", 338 | "metadata": {}, 339 | "source": [ 340 | "## سینتکس آرگومان ها در تابع" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 2, 346 | "id": "1f046d60-8b28-4cc1-b444-8e88640eb587", 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "name": "stdout", 351 | "output_type": "stream", 352 | "text": [ 353 | "6\n", 354 | "4\n", 355 | "9\n", 356 | "6\n", 357 | "9\n" 358 | ] 359 | } 360 | ], 361 | "source": [ 362 | "def max3(a,b,c):\n", 363 | " print(max(a,b,c))\n", 364 | "\n", 365 | "# normal\n", 366 | "max3(3, 6, 2) # ترتیب مهم است\n", 367 | "# در این شیوه آرگومان نویسی پارامتر ها با توجه به ترتیب آرگومان ها مقدار دهی می شوند\n", 368 | "\n", 369 | "# name = vlaue\n", 370 | "max3(b=4, a=2, c=3) # ترتیب مهم نیست\n", 371 | "\n", 372 | "# normal + name/value\n", 373 | "max3(4, c=9, b=5) # a با توجه به ترتیب مقدار گرفت ولی بقیه با اسمشان\n", 374 | "# در این روش باید اول آرگومان هایی نوشته شود که قرار نیست اسمشان را بنویسیم\n", 375 | "# max3(5 , a=7 , c=9) --> * error beacause a assigne at first argument\n", 376 | "\n", 377 | "\n", 378 | "# *iterable (str, tuple, list, set)\n", 379 | "l = [5,6,4]\n", 380 | "max3(*l) # عملگر * نقش آنپک را دارد\n", 381 | "\n", 382 | "# **dictionary\n", 383 | "d = {\"a\" : 5, \"c\" : 9, \"b\" : 4} # کلید ها فقط باید شامل پارامترها باشد\n", 384 | "max3(**d) # با توجه به کلید ها پارامتر ها را مقدار دهی میکند\n" 385 | ] 386 | }, 387 | { 388 | "cell_type": "markdown", 389 | "id": "9d9c6c2a-41d9-4dc0-87e3-c712ccb9ee3d", 390 | "metadata": {}, 391 | "source": [ 392 | "## سینتکس پارامتر های تابع" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 20, 398 | "id": "6fc490b6-39ae-49c0-a93e-4a48c77f434f", 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "name": "stdout", 403 | "output_type": "stream", 404 | "text": [ 405 | "f6 {'a': 2, 'b': 3, 'c': 4}\n", 406 | "c is: 4\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "# normal\n", 412 | "def func1(a,b,c):\n", 413 | " pass\n", 414 | " return None\n", 415 | "\n", 416 | "# default value\n", 417 | "def func2(a=2,b=4,c=3):\n", 418 | " pass\n", 419 | " return None\n", 420 | "func2(c=8) # مقدار سی غیر از دیفالت خواهد بود\n", 421 | "\n", 422 | "# normal + default value\n", 423 | "def func3(a,b,c=3): # درصورت مقدار دهی نرمال ، پارامتر باید قبل تر از دارای مقدار دیفالت باشد\n", 424 | " pass\n", 425 | " return None\n", 426 | "func3(a=2, b=3)\n", 427 | "\n", 428 | "# *name (در شرایطی که ممکن است آرگومان ها بیشتر از پارامتر ها باشند مناسب است\n", 429 | "a , b ,*c = [1,2,3,4,5,9]\n", 430 | "def func4(a, b, *c):\n", 431 | " pass\n", 432 | " return None\n", 433 | "func4(1,2,3,4,5,6,7) # آ و بی مقدار می گیرند و اضافی ها به سی داده می شود\n", 434 | "\n", 435 | "# *name (بدون خطا)\n", 436 | "def func5(a, b, *c, d): # سی به شکل تاپل مقدار می گیرد\n", 437 | " pass\n", 438 | " return None\n", 439 | "func5(1,2,3,4,5,6,7, d = 8) # لازم است مقدار های آخر که اضافی نیستند از روش نام مقدار، مقداردهی شوند\n", 440 | "\n", 441 | "# **name\n", 442 | "def func6(**d):\n", 443 | " print(\"f6\", d)\n", 444 | " print(\"c is:\", d['c'])\n", 445 | "func6(a=2, b=3, c=4)" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": null, 451 | "id": "df1f5fef-591f-409a-8e14-f945bc57c595", 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "name": "stdout", 456 | "output_type": "stream", 457 | "text": [ 458 | "a : 1\n", 459 | "b : 2\n", 460 | "c : (3, 4, 5, 6, 7, 8, 9)\n", 461 | "d : {'c': 10, 'd': 11}\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "# سینتکس ترکیبی پارامترها\n", 467 | "\n", 468 | "def func(a, b=0, *c, **d): # ترتیب فقط به همین شکل\n", 469 | " print(\"a :\", a)\n", 470 | " print(\"b :\", b)\n", 471 | " print(\"c :\", c)\n", 472 | " print(\"d :\", d)\n", 473 | "\n", 474 | "func(1, 2, 3, 4, 5, 6, 7, 8, 9, c=10, d=11) # نباید آ و بی بصورت نام مقدار نوشته شوند" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 15, 480 | "id": "33077d90-50b6-433e-a283-dc06ce39f9e4", 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "name": "stdout", 485 | "output_type": "stream", 486 | "text": [ 487 | "1 2 3 4 5 6\n" 488 | ] 489 | } 490 | ], 491 | "source": [ 492 | "# پارامتر های نشانگر\n", 493 | "\n", 494 | "# *\n", 495 | "def func1(a,b, *,c): # پارامتر های بعد ستاره حتما باید آرگومان هایشان بصورت مقدار نام باشد\n", 496 | " pass\n", 497 | " return None\n", 498 | "\n", 499 | "\n", 500 | "# / در شرایطی که ممکن است بعد نام پارامتر ها تغییر کند مناسب است\n", 501 | "def func2(a,b, /,c): # پارامتر های قبل اسلش حتما باید آرگومان هایشان بصورت موقعیتی باشد\n", 502 | " pass\n", 503 | " return None\n", 504 | "\n", 505 | "# مثال\n", 506 | "def func(a, b, /, c, d, *, e, f):\n", 507 | " print(a, b, c, d, e, f)\n", 508 | "func(1, 2, 3, d=4, e=5, f=6)" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": null, 514 | "id": "196a5e4d-e39d-4917-b2cd-a70c37a1fd4c", 515 | "metadata": {}, 516 | "outputs": [], 517 | "source": [] 518 | }, 519 | { 520 | "cell_type": "markdown", 521 | "id": "6bfbd596-847d-4d06-9f17-7f337f4761f8", 522 | "metadata": {}, 523 | "source": [ 524 | "## داک استرینک: ابزاری برای معرفی تابع" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 16, 530 | "id": "96a4c16d-3088-4478-95d3-f88c297d87d7", 531 | "metadata": {}, 532 | "outputs": [ 533 | { 534 | "name": "stdout", 535 | "output_type": "stream", 536 | "text": [ 537 | " Receives 3 numb as input and returns the largest of them \n", 538 | "******************************\n", 539 | "Help on function max3 in module __main__:\n", 540 | "\n", 541 | "max3(x, y, z)\n", 542 | " Receives 3 numb as input and returns the largest of them\n", 543 | "\n", 544 | "None\n" 545 | ] 546 | } 547 | ], 548 | "source": [ 549 | "# باید در اولین خط بعد معرفی اسم و پارامتر های تابع در داخل 3تا کوتیشن آورده شود\n", 550 | "# بهتره دابل کوتیشن باشد \n", 551 | "def max3(x, y, z):\n", 552 | " \"\"\" Receives 3 numb as input and returns the largest of them \"\"\"\n", 553 | " return max(x, y ,z)\n", 554 | "\n", 555 | "print(max3.__doc__) # دانتل مخفف دابل آندر اسکور\n", 556 | "\n", 557 | "print(\"*\"*30)\n", 558 | "\n", 559 | "print(help(max3))\n" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 17, 565 | "id": "efe4b198-137b-4839-917d-73197373f0a8", 566 | "metadata": {}, 567 | "outputs": [], 568 | "source": [ 569 | "# برای نوشتن داک استرینک بهتر است از دابل کوتیشن استفاده شود تا کوتیشن\n", 570 | "# متن با حرف بزرگ شروع شده و نقطه پایان یابد\n", 571 | "# خط اول خلاصه ای از کارکرد\n", 572 | "# بعد یک خط فاصله پارامتر های تابع\n", 573 | "\n", 574 | "\n", 575 | "def func(x, y, z):\n", 576 | " \"\"\"Receives 3 numbers as input and returns the largest of them.\n", 577 | "\n", 578 | " Parameters:\n", 579 | " x (int): a decimal integer\n", 580 | " y (int): another decimal integer\n", 581 | " z (int): another decimal integer\n", 582 | " \n", 583 | " Returns:\n", 584 | " max_int (int): largest of three numbers\n", 585 | " \"\"\"\n", 586 | " return max(x, y, z)\n", 587 | "\n", 588 | "# توابع خود پایتون هم داک استرینک دارند" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": null, 594 | "id": "730bc420-1379-4e49-8484-de9aceb56259", 595 | "metadata": {}, 596 | "outputs": [], 597 | "source": [] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "id": "d1c03090-059f-4ca2-9246-950e726d23fd", 602 | "metadata": {}, 603 | "source": [ 604 | "## Annotation یادداشت گذاری" 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "execution_count": 18, 610 | "id": "93077dee-7664-4b68-a887-7b836328352f", 611 | "metadata": {}, 612 | "outputs": [ 613 | { 614 | "name": "stdout", 615 | "output_type": "stream", 616 | "text": [ 617 | "a 1\n", 618 | "b 2\n", 619 | "c 3.1\n" 620 | ] 621 | } 622 | ], 623 | "source": [ 624 | "# تعیین نوع پارامترها\n", 625 | "def func(a : int, b : str, c : float):\n", 626 | " print(\"a\" , a)\n", 627 | " print(\"b\" , b)\n", 628 | " print(\"c\" , c)\n", 629 | " \n", 630 | "func(1, '2', 3.1)\n", 631 | "\n", 632 | "# لازم هست ماژول mypy نصب شود تا آرگومان خلاف نوع مشخص شده خطا داده شود" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": 30, 638 | "id": "eb1af27f-3b3a-4b66-8fd1-cb17c8954709", 639 | "metadata": {}, 640 | "outputs": [ 641 | { 642 | "name": "stdout", 643 | "output_type": "stream", 644 | "text": [ 645 | "{'a': , 'b': , 'c': , 'return': }\n", 646 | "\n", 647 | " 7\n" 648 | ] 649 | } 650 | ], 651 | "source": [ 652 | "# مشخص کردن نوع خروجی تابع\n", 653 | "def func(a : int , b : int = 9, **c : dict) -> float: # دادن مقدار پیشفرض\n", 654 | " return a+b\n", 655 | "\n", 656 | "print(func.__annotations__)\n", 657 | "print()\n", 658 | "print(type(func(3, 5, c=5 )), func(3,4, c=5 ))\n", 659 | "\n", 660 | "# ????!!!!!" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": null, 666 | "id": "a3ee0c56-f568-4dd0-89c1-9d9d61f1d507", 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [] 670 | }, 671 | { 672 | "cell_type": "markdown", 673 | "id": "e69c1c38-51f7-4e74-ac5f-126346f1778b", 674 | "metadata": { 675 | "tags": [] 676 | }, 677 | "source": [ 678 | "# first class" 679 | ] 680 | }, 681 | { 682 | "cell_type": "markdown", 683 | "id": "56afd1ea-e433-4e4d-9077-391c2be7fe70", 684 | "metadata": {}, 685 | "source": [ 686 | "چیزی فرست کلاس هست که مانند اشیا دیگر در پایتون باشد و محدودیتی نداشته باشد" 687 | ] 688 | }, 689 | { 690 | "cell_type": "markdown", 691 | "id": "f6c77c08-7a8d-4446-bfa4-d27fb3778be1", 692 | "metadata": { 693 | "tags": [] 694 | }, 695 | "source": [ 696 | "در سی پلاس پلاس توابعی که تعریف می شوند فرست کلاس نیستند\n", 697 | "# ولی در پایتون همه توابع و کلاس ها فرست کلاس هستند" 698 | ] 699 | }, 700 | { 701 | "cell_type": "code", 702 | "execution_count": 20, 703 | "id": "cfa47ae2-fc41-4d70-847c-4f258919023f", 704 | "metadata": {}, 705 | "outputs": [ 706 | { 707 | "name": "stdout", 708 | "output_type": "stream", 709 | "text": [ 710 | "2\n", 711 | "25\n" 712 | ] 713 | } 714 | ], 715 | "source": [ 716 | "def func(x):\n", 717 | " print(x)\n", 718 | "\n", 719 | "y = func\n", 720 | "\n", 721 | "y(2)\n", 722 | "# 6->9\n", 723 | "# ویژگی فرست کلاس ها\n", 724 | "# 1. Can be created at runtime. ممکنه در شرط یا در تابع دیگر تعریف شوند\n", 725 | "# 2. Can be assigned to a variable. f = func\n", 726 | "# 3. Can be passed as a argument to a function.\n", 727 | "# 4. Can be return as a result from a function.\n", 728 | "# 5. Can have properties and methods.\n", 729 | "\n", 730 | "a = True # بدلیل فرست کلاس بدون توابع در پایتون می توان توابع را اضافه و حذف کرد\n", 731 | "if a:\n", 732 | " def func11(x):\n", 733 | " print(x**2)\n", 734 | "func11(5)" 735 | ] 736 | }, 737 | { 738 | "cell_type": "code", 739 | "execution_count": 21, 740 | "id": "d6dcb8e0-7b60-40b0-870f-b65ef630125f", 741 | "metadata": {}, 742 | "outputs": [ 743 | { 744 | "name": "stdout", 745 | "output_type": "stream", 746 | "text": [ 747 | "[0, 1, 2, 5, 6, 12, 13, 47, 68, 894]\n", 748 | "****************************************\n", 749 | "[894, 68, 47, 13, 12, 6, 5, 2, 1, 0]\n" 750 | ] 751 | } 752 | ], 753 | "source": [ 754 | "# اختصاص تابع به عنوان آرگومان\n", 755 | "def descending(mylist):\n", 756 | " print(sorted(mylist, reverse = True))\n", 757 | "\n", 758 | "def ascending(mylist):\n", 759 | " print(sorted(mylist))\n", 760 | "\n", 761 | "def mysort(f, mylist):\n", 762 | " f(mylist)\n", 763 | " \n", 764 | "l = [5, 6, 12, 2, 1, 68, 47, 894, 13, 0]\n", 765 | "\n", 766 | "mysort(ascending, l)\n", 767 | "print('*' *40)\n", 768 | "mysort(descending, l)" 769 | ] 770 | }, 771 | { 772 | "cell_type": "code", 773 | "execution_count": 33, 774 | "id": "ad5dbd19-3ebb-4eed-b2c9-b859a1ee693c", 775 | "metadata": {}, 776 | "outputs": [ 777 | { 778 | "name": "stdin", 779 | "output_type": "stream", 780 | "text": [ 781 | "action: a\n" 782 | ] 783 | }, 784 | { 785 | "name": "stdout", 786 | "output_type": "stream", 787 | "text": [ 788 | "[894, 68, 47, 13, 12, 6, 5, 2, 1, 0]\n" 789 | ] 790 | } 791 | ], 792 | "source": [ 793 | "# برگشت دادن تابع \n", 794 | "\n", 795 | "def mysort(s):\n", 796 | "\n", 797 | " def descending(mylist):\n", 798 | " print(sorted(mylist, reverse = True))\n", 799 | "\n", 800 | " def ascending(mylist):\n", 801 | " print(sorted(mylist))\n", 802 | " \n", 803 | " if s == 'd':\n", 804 | " return ascending\n", 805 | " elif s == 'a':\n", 806 | " return descending\n", 807 | " \n", 808 | "l = [5, 6, 12, 2, 1, 68, 47, 894, 13, 0]\n", 809 | "\n", 810 | "action = input('action: ')\n", 811 | "func = mysort(action)\n", 812 | "func(l)" 813 | ] 814 | }, 815 | { 816 | "cell_type": "markdown", 817 | "id": "91a40289-0f70-4f5a-af8c-ce92e8e8f351", 818 | "metadata": { 819 | "tags": [] 820 | }, 821 | "source": [ 822 | "# ارسال با ارجاع یا با مقدار" 823 | ] 824 | }, 825 | { 826 | "cell_type": "markdown", 827 | "id": "ed3d299e-b3f7-4ea0-af6d-09808b1868b8", 828 | "metadata": {}, 829 | "source": [ 830 | "pass by value default-->pass by refrence\n", 831 | "ارسال با ارجاع یعنی آدرس آرگومان ها به تابع ارسال می شود و ارسال با مقدار یعنی فقط مقدار متغیر آرگومان به تابع ارسال میشود" 832 | ] 833 | }, 834 | { 835 | "cell_type": "markdown", 836 | "id": "48ecd6de-c5d8-4ef2-b163-19a6bec1f475", 837 | "metadata": {}, 838 | "source": [ 839 | "در زبان های مثل سی و سی پلاسپلاس ارسال به هردو شکل ممکن است" 840 | ] 841 | }, 842 | { 843 | "cell_type": "markdown", 844 | "id": "3ace1eb7-ddab-4de5-8ced-0a6dde0fb00e", 845 | "metadata": {}, 846 | "source": [ 847 | "در پایتون همه چی شیء است و ارسال ها pass by reference هستند" 848 | ] 849 | }, 850 | { 851 | "cell_type": "markdown", 852 | "id": "0e20b45f-abbe-4688-8914-11b7b05a391e", 853 | "metadata": {}, 854 | "source": [ 855 | "اما با توجه تغییر پذیر بودن یا تغییرپذیر نبودن متغیر ها، نتیجه ارسال آرگومان ها چیز دیگری مشاهده میشود" 856 | ] 857 | }, 858 | { 859 | "cell_type": "markdown", 860 | "id": "bf4b9d4c-92b2-4ce7-8e01-d55b5b48f400", 861 | "metadata": {}, 862 | "source": [ 863 | "در پایتون اشیائی که تغییر ناپذیر هستند مانند اعداد، رشته و تاپل رفتار در مورد آنها ارسال با مقدار است. در واقع کپی از آنها به تابع ارسال می شود" 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "id": "7d630944-a7dd-4fa7-8207-89c1f23c106a", 869 | "metadata": {}, 870 | "source": [ 871 | "در ارسال با ارجاع مقدار متغییر تغییر میکند" 872 | ] 873 | }, 874 | { 875 | "cell_type": "code", 876 | "execution_count": 23, 877 | "id": "83ed2123-0105-4c16-9fb1-ecfbb4c9a0e7", 878 | "metadata": {}, 879 | "outputs": [], 880 | "source": [ 881 | "# تغییر نا پذیر شامل : عدد، رشته، تاپل و بولین" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 24, 887 | "id": "40ce9b6f-e6f9-4ce1-8f96-6ce6f1636602", 888 | "metadata": {}, 889 | "outputs": [], 890 | "source": [ 891 | "# تغییر پذیر شامل : لیست، دیکشنری و مجموعه" 892 | ] 893 | }, 894 | { 895 | "cell_type": "code", 896 | "execution_count": 3, 897 | "id": "f31ca264-1f36-4e4f-aed2-03a8c2a97803", 898 | "metadata": {}, 899 | "outputs": [ 900 | { 901 | "name": "stdout", 902 | "output_type": "stream", 903 | "text": [ 904 | "[1, 2, 3, 9, 0]\n", 905 | "[1, 3, 4, 6]\n" 906 | ] 907 | } 908 | ], 909 | "source": [ 910 | "def func(a):\n", 911 | " a = [1,2] \n", 912 | " # or\n", 913 | " a = a + [3,9,0]\n", 914 | " print(a)\n", 915 | " \n", 916 | "a = [1,3,4,6]\n", 917 | "func(a)\n", 918 | "print(a)\n", 919 | "\n", 920 | "# در اینجا در داخل فانکشن اصلا یک متغییر دیگه ساختیم" 921 | ] 922 | }, 923 | { 924 | "cell_type": "code", 925 | "execution_count": 6, 926 | "id": "3469ca9c-0a40-4207-8ad4-2f6bb1e03eed", 927 | "metadata": {}, 928 | "outputs": [ 929 | { 930 | "name": "stdout", 931 | "output_type": "stream", 932 | "text": [ 933 | "[1, 3, 4, 6, 1, 12]\n", 934 | "[1, 3, 4, 6, 1, 12]\n" 935 | ] 936 | } 937 | ], 938 | "source": [ 939 | "def func(a):\n", 940 | " a += [1,2]\n", 941 | " a[5] = 12\n", 942 | " print(a)\n", 943 | " \n", 944 | "a = [1,3,4,6]\n", 945 | "func(a)\n", 946 | "print(a)\n" 947 | ] 948 | }, 949 | { 950 | "cell_type": "markdown", 951 | "id": "ecad7d64-5aa3-4e02-bc78-8391d38eabb9", 952 | "metadata": {}, 953 | "source": [ 954 | "می توان برای تغییر پذیر ها کپی را فرستاد تا ارسال با مقدار باشد" 955 | ] 956 | }, 957 | { 958 | "cell_type": "code", 959 | "execution_count": 8, 960 | "id": "325bc31a-a88a-4504-938e-fef0bda44694", 961 | "metadata": {}, 962 | "outputs": [], 963 | "source": [ 964 | "a = [1,3,4, [5,6]]\n", 965 | "\n", 966 | "b = a.copy() # کپی سطحی\n", 967 | "e = a[:] # شکل دیگری از کپی سطحی\n", 968 | "\n", 969 | "import copy\n", 970 | "c = copy.deepcopy(a) # کپی عمیق\n", 971 | "# برای لیست ها و دیکشنری های تو در تو لازم است کپی عمیق صورت گیرد\n" 972 | ] 973 | }, 974 | { 975 | "cell_type": "code", 976 | "execution_count": 10, 977 | "id": "c7934ce0-c054-4d42-a757-39259a13e27e", 978 | "metadata": {}, 979 | "outputs": [ 980 | { 981 | "name": "stdout", 982 | "output_type": "stream", 983 | "text": [ 984 | "6\n", 985 | "6\n" 986 | ] 987 | } 988 | ], 989 | "source": [ 990 | "# اگر میخواهیم تغییر نا پذیر ها نیز در نتیجه ارسال با ارجاع باشند:\n", 991 | "def func(a):\n", 992 | " a += 1\n", 993 | " print(a)\n", 994 | " return a\n", 995 | "\n", 996 | "a = 5\n", 997 | "a = func(a) # لازم است به این شکل عمل شود\n", 998 | "print(a)" 999 | ] 1000 | }, 1001 | { 1002 | "cell_type": "code", 1003 | "execution_count": null, 1004 | "id": "a3b25c94-f9e6-4231-b891-001d1510ba4d", 1005 | "metadata": {}, 1006 | "outputs": [], 1007 | "source": [] 1008 | } 1009 | ], 1010 | "metadata": { 1011 | "kernelspec": { 1012 | "display_name": "Python 3 (ipykernel)", 1013 | "language": "python", 1014 | "name": "python3" 1015 | }, 1016 | "language_info": { 1017 | "codemirror_mode": { 1018 | "name": "ipython", 1019 | "version": 3 1020 | }, 1021 | "file_extension": ".py", 1022 | "mimetype": "text/x-python", 1023 | "name": "python", 1024 | "nbconvert_exporter": "python", 1025 | "pygments_lexer": "ipython3", 1026 | "version": "3.10.9" 1027 | } 1028 | }, 1029 | "nbformat": 4, 1030 | "nbformat_minor": 5 1031 | } 1032 | -------------------------------------------------------------------------------- /07_SyntaxLearn_NameSpace&Scope.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "5551b541-908e-4c87-98b7-349a51cd2f78", 6 | "metadata": { 7 | "tags": [] 8 | }, 9 | "source": [ 10 | "## Namespace & Scope" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "0a9aac10-e4cb-4169-9ae3-9f87e1089ad1", 16 | "metadata": {}, 17 | "source": [ 18 | "6-->10" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "id": "ab8dbe89-1a7f-47ce-a70f-9c24f41bd9de", 24 | "metadata": {}, 25 | "source": [ 26 | "هنگامی که برنامه ما در کنار سایر برنامه ها قرار میگیرد ممکن است اشیائی با نام یکسان ولی در برنامه های متفاوت وجود داشته باشند" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "90e8962e-9351-4a65-b5fb-177d11c64138", 32 | "metadata": {}, 33 | "source": [ 34 | "فضانام مانند نام خانوادگی است که افراد با اسم یکسان با یکدیگر تمایز داده شوند" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "id": "67d00c6e-4768-4067-8159-7eb85747efe0", 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "# یک دیکنشری برای هر برنامه تعریف می شود که اشیاء برنامه را مشخص می کند" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "id": "3b06d851-2625-4d88-9bf4-83eb61add313", 50 | "metadata": {}, 51 | "source": [ 52 | "حوزه محیطی است که دسترسی به فضانام در آن مشخص می شود" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "id": "b1c60d6e-a276-4bf8-ab35-b5c4b8903eb9", 58 | "metadata": { 59 | "tags": [] 60 | }, 61 | "source": [ 62 | "مثل این که صدا کنیم محمد، کسی جواب می دهد که به شما نزدیک تر است" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "id": "fef9c4b1-6cbd-46b7-9e5c-6809ee39fa30", 68 | "metadata": {}, 69 | "source": [ 70 | "در صورتی که فضانام را مشخص نکنیم، با یک اسم شیئی فراخوانده می شود که در یک حوزه هست" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "id": "494e82f7-0c54-4744-a538-0fdf35d3f661", 76 | "metadata": {}, 77 | "source": [ 78 | "مثل این که صدا کنیم محمد، کسی جواب می دهد که به شما نزدیک تر است" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "id": "e1fe02f9-ead6-4104-8dc4-62440dfaf347", 84 | "metadata": {}, 85 | "source": [ 86 | "Scope priority : Local > Enclosed > Global > Built-in" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 4, 92 | "id": "8574404d-a7d7-40fd-a113-4ff10d6e5ae3", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 5, 102 | "id": "2bea40a9-7772-4692-8c2b-642bb3408026", 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "# به هر اسکریپت .py\n", 107 | "# یک ماژول گفته می شود" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "id": "7b234649-48d6-4dd0-98e6-61039a48c149", 113 | "metadata": {}, 114 | "source": [ 115 | "Built-in" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 6, 121 | "id": "5a948aec-9b28-4ad4-84e8-5fe5f2bb2170", 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__IPYTHON__', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec', 'execfile', 'filter', 'float', 'format', 'frozenset', 'get_ipython', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', 'round', 'runfile', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']\n" 129 | ] 130 | } 131 | ], 132 | "source": [ 133 | "# در همه ماژول ها وجود دارند\n", 134 | "# در واقع کلمات کلیدی خود پایتون هست\n", 135 | "print(dir(__builtins__))" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "id": "1d5caa90-760b-45d7-be13-b3735a208144", 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "id": "b1c167a0-4e6d-4293-9ad0-c14d15a4d6be", 149 | "metadata": {}, 150 | "source": [ 151 | "Global" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 7, 157 | "id": "443ad203-0c19-4f64-872d-dfb767c3194a", 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "{'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': , '__builtins__': , '_ih': ['', \"# فضانام داخلی توابع و کلاس ها\\n\\ndef A():\\n x = 4\\n y = 5\\n print(locals())\\n \\ndef B():\\n x = 's'\\n y = 'r'\\n print(locals())\\n\\nA()\\nB()\", '# برای استفاده از فضانام یک ماژول دیگر:\\nimport math\\npi = 20\\nprint(\"local pi: \", pi)\\nprint(\"math pi: \", math.pi)\\n\\n# چاپ فضای نمای یک ماژول دیگر:\\nprint(dir(math))', '# یک دیکنشری برای هر برنامه تعریف می شود که اشیاء برنامه را مشخص می کند', '# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا', '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\\nprint(globals())\\n# این فضای نام قابل تغییر است'], '_oh': {}, '_dh': [WindowsPath('C:/Users/Hoseyn/Jupiter Python Learning NoteBooks')], 'In': ['', \"# فضانام داخلی توابع و کلاس ها\\n\\ndef A():\\n x = 4\\n y = 5\\n print(locals())\\n \\ndef B():\\n x = 's'\\n y = 'r'\\n print(locals())\\n\\nA()\\nB()\", '# برای استفاده از فضانام یک ماژول دیگر:\\nimport math\\npi = 20\\nprint(\"local pi: \", pi)\\nprint(\"math pi: \", math.pi)\\n\\n# چاپ فضای نمای یک ماژول دیگر:\\nprint(dir(math))', '# یک دیکنشری برای هر برنامه تعریف می شود که اشیاء برنامه را مشخص می کند', '# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا', '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\\nprint(globals())\\n# این فضای نام قابل تغییر است'], 'Out': {}, 'get_ipython': >, 'exit': , 'quit': , '_': '', '__': '', '___': '', '_i': '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '_ii': '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '_iii': '# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا', '_i1': \"# فضانام داخلی توابع و کلاس ها\\n\\ndef A():\\n x = 4\\n y = 5\\n print(locals())\\n \\ndef B():\\n x = 's'\\n y = 'r'\\n print(locals())\\n\\nA()\\nB()\", 'A': , 'B': , '_i2': '# برای استفاده از فضانام یک ماژول دیگر:\\nimport math\\npi = 20\\nprint(\"local pi: \", pi)\\nprint(\"math pi: \", math.pi)\\n\\n# چاپ فضای نمای یک ماژول دیگر:\\nprint(dir(math))', 'math': , 'pi': 20, '_i3': '# یک دیکنشری برای هر برنامه تعریف می شود که اشیاء برنامه را مشخص می کند', '_i4': '# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا', '_i5': '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '_i6': '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '_i7': '# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\\nprint(globals())\\n# این فضای نام قابل تغییر است'}\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\n", 170 | "print(globals())\n", 171 | "# این فضای نام قابل تغییر است" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 8, 177 | "id": "b3a50af0-e780-4213-b4c9-fc7b0fb1804e", 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "b : می توان اسامی در حوزه گلوبال را تغییر داد\n", 185 | "********************\n", 186 | "{'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': , '__builtins__': , '_ih': ['', \"# فضانام داخلی توابع و کلاس ها\\n\\ndef A():\\n x = 4\\n y = 5\\n print(locals())\\n \\ndef B():\\n x = 's'\\n y = 'r'\\n print(locals())\\n\\nA()\\nB()\", '# برای استفاده از فضانام یک ماژول دیگر:\\nimport math\\npi = 20\\nprint(\"local pi: \", pi)\\nprint(\"math pi: \", math.pi)\\n\\n# چاپ فضای نمای یک ماژول دیگر:\\nprint(dir(math))', '# یک دیکنشری برای هر برنامه تعریف می شود که اشیاء برنامه را مشخص می کند', '# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا', '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\\nprint(globals())\\n# این فضای نام قابل تغییر است', 'x = 5\\ny = 10\\nz = 20\\na = 2\\ndef func():\\n pass\\nglobals()[\"b\"] = \"می توان اسامی در حوزه گلوبال را تغییر داد\"\\nprint(\"b :\", b)\\nprint(\"*\"*20)\\nprint(globals())'], '_oh': {}, '_dh': [WindowsPath('C:/Users/Hoseyn/Jupiter Python Learning NoteBooks')], 'In': ['', \"# فضانام داخلی توابع و کلاس ها\\n\\ndef A():\\n x = 4\\n y = 5\\n print(locals())\\n \\ndef B():\\n x = 's'\\n y = 'r'\\n print(locals())\\n\\nA()\\nB()\", '# برای استفاده از فضانام یک ماژول دیگر:\\nimport math\\npi = 20\\nprint(\"local pi: \", pi)\\nprint(\"math pi: \", math.pi)\\n\\n# چاپ فضای نمای یک ماژول دیگر:\\nprint(dir(math))', '# یک دیکنشری برای هر برنامه تعریف می شود که اشیاء برنامه را مشخص می کند', '# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا', '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\\nprint(globals())\\n# این فضای نام قابل تغییر است', 'x = 5\\ny = 10\\nz = 20\\na = 2\\ndef func():\\n pass\\nglobals()[\"b\"] = \"می توان اسامی در حوزه گلوبال را تغییر داد\"\\nprint(\"b :\", b)\\nprint(\"*\"*20)\\nprint(globals())'], 'Out': {}, 'get_ipython': >, 'exit': , 'quit': , '_': '', '__': '', '___': '', '_i': '# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\\nprint(globals())\\n# این فضای نام قابل تغییر است', '_ii': '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '_iii': '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '_i1': \"# فضانام داخلی توابع و کلاس ها\\n\\ndef A():\\n x = 4\\n y = 5\\n print(locals())\\n \\ndef B():\\n x = 's'\\n y = 'r'\\n print(locals())\\n\\nA()\\nB()\", 'A': , 'B': , '_i2': '# برای استفاده از فضانام یک ماژول دیگر:\\nimport math\\npi = 20\\nprint(\"local pi: \", pi)\\nprint(\"math pi: \", math.pi)\\n\\n# چاپ فضای نمای یک ماژول دیگر:\\nprint(dir(math))', 'math': , 'pi': 20, '_i3': '# یک دیکنشری برای هر برنامه تعریف می شود که اشیاء برنامه را مشخص می کند', '_i4': '# localخانواده | Enclosedفامیل | Globalکشور | BuiltInدنیا', '_i5': '# به هر اسکریپت .py\\n# یک ماژول گفته می شود', '_i6': '# در واقع کلمات کلیدی خود پایتون هست\\nprint(dir(__builtins__))', '_i7': '# اسم های اشیائی است که داخل هر ماژول تعریف می گردد\\nprint(globals())\\n# این فضای نام قابل تغییر است', '_i8': 'x = 5\\ny = 10\\nz = 20\\na = 2\\ndef func():\\n pass\\nglobals()[\"b\"] = \"می توان اسامی در حوزه گلوبال را تغییر داد\"\\nprint(\"b :\", b)\\nprint(\"*\"*20)\\nprint(globals())', 'x': 5, 'y': 10, 'z': 20, 'a': 2, 'func': , 'b': 'می توان اسامی در حوزه گلوبال را تغییر داد'}\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "x = 5\n", 192 | "y = 10\n", 193 | "z = 20\n", 194 | "a = 2\n", 195 | "def func():\n", 196 | " pass\n", 197 | "globals()[\"b\"] = \"می توان اسامی در حوزه گلوبال را تغییر داد\"\n", 198 | "print(\"b :\", b)\n", 199 | "print(\"*\"*20)\n", 200 | "print(globals())\n" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "id": "38fc7b3a-a0fa-453a-8973-8db2d98e934d", 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "id": "8dbed9c9-932b-40ac-a679-6b3a74639c1e", 214 | "metadata": {}, 215 | "source": [ 216 | "Local" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 9, 222 | "id": "43f4391a-9101-45fd-8902-43889a32e24a", 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "{'x': 4, 'y': 5}\n", 230 | "{'x': 's', 'y': 'r'}\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "# فضانام داخلی توابع و کلاس ها\n", 236 | "\n", 237 | "def A():\n", 238 | " x = 4\n", 239 | " y = 5\n", 240 | " print(locals())\n", 241 | " \n", 242 | "def B():\n", 243 | " x = 's'\n", 244 | " y = 'r'\n", 245 | " print(locals())\n", 246 | "\n", 247 | "A()\n", 248 | "B()" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "id": "c2bb35ff-9ee0-4d57-a1e0-ae773b8132f8", 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "# گلوبال و لوکال در فضای خارجی ترین لایه ماژول یکسان هستند" 259 | ] 260 | }, 261 | { 262 | "cell_type": "markdown", 263 | "id": "464f5497-53ce-44eb-a2a6-8413001bf6e8", 264 | "metadata": {}, 265 | "source": [ 266 | "Enclosed" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 10, 272 | "id": "394062dc-549e-4668-b687-5d83f56a3cbe", 273 | "metadata": {}, 274 | "outputs": [], 275 | "source": [ 276 | "# در توابع و کلاس های تو در تو مورد استفاده می گیرد" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": null, 282 | "id": "7b04517b-d3f6-4e5a-9c6e-cbfe31feba8c", 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": 11, 290 | "id": "3fefffbe-669d-4d57-830b-897678deb491", 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "local pi: 20\n", 298 | "math pi: 3.141592653589793\n", 299 | "['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']\n" 300 | ] 301 | } 302 | ], 303 | "source": [ 304 | "# برای استفاده از فضانام یک ماژول دیگر:\n", 305 | "import math\n", 306 | "pi = 20\n", 307 | "print(\"local pi: \", pi)\n", 308 | "print(\"math pi: \", math.pi)\n", 309 | "\n", 310 | "# چاپ فضای نمای یک ماژول دیگر:\n", 311 | "print(dir(math))" 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "id": "6e160dde-8d65-4367-b336-98b886b2381e", 317 | "metadata": {}, 318 | "source": [ 319 | "# قوانین حوزه ها" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 13, 325 | "id": "fb449487-ecf5-4832-9eb5-9752d70bbf77", 326 | "metadata": {}, 327 | "outputs": [], 328 | "source": [ 329 | "# شرطی ها و حلقه ها حوزه لوکال خود را ندارند و متغییری که تعریف بشه در آنها در فضای گاوبال قابل دسترسی است\n", 330 | "# فضای نام لوکال به همه فضانام های بالاتر از خودش دسترس دارد\n", 331 | "# با فراخوانی یک اسم ابتدا در پایین ترین حوزه فراخوانی می شود و اگر اسمی نبود با حوزه بالاتری می رود\n", 332 | "# ولی برعکس آن صادق نیست! اگر فراخوانی در حوزه خود مثلا گلوبال اسمی نیافت به حوزه پایین تر نمی رود!\n", 333 | "# توابع حوزه لوکال خود را دارند\n" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 1, 339 | "id": "ca7ed47e-5456-4980-a8e8-3814272de18e", 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "name": "stdout", 344 | "output_type": "stream", 345 | "text": [ 346 | "local x in A: 2\n", 347 | "global x is: 10\n" 348 | ] 349 | } 350 | ], 351 | "source": [ 352 | "# تغییر x گلوبال\n", 353 | "\n", 354 | "x = 0\n", 355 | "\n", 356 | "def A():\n", 357 | " x = 2 # لوکال\n", 358 | " def B():\n", 359 | " global x # دسترسی به گلوبال\n", 360 | " x += 10\n", 361 | " B()\n", 362 | " print(\"local x in A: \", x)\n", 363 | "A()\n", 364 | "print(\"global x is: \", x)" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 22, 370 | "id": "b54f4c0e-362e-4424-82a1-deccddf52297", 371 | "metadata": {}, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "local x in A: 10\n", 378 | "global x is: 0\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "# تغییر x بالاتر\n", 384 | "\n", 385 | "x = 0\n", 386 | "\n", 387 | "def A():\n", 388 | " x = 2 # لوکال\n", 389 | " def B():\n", 390 | " nonlocal x # دسترسی به انکلوزد\n", 391 | " x = 10\n", 392 | " B()\n", 393 | " print(\"local x in A: \", x)\n", 394 | "A()\n", 395 | "print(\"global x is: \", x)" 396 | ] 397 | } 398 | ], 399 | "metadata": { 400 | "kernelspec": { 401 | "display_name": "Python 3 (ipykernel)", 402 | "language": "python", 403 | "name": "python3" 404 | }, 405 | "language_info": { 406 | "codemirror_mode": { 407 | "name": "ipython", 408 | "version": 3 409 | }, 410 | "file_extension": ".py", 411 | "mimetype": "text/x-python", 412 | "name": "python", 413 | "nbconvert_exporter": "python", 414 | "pygments_lexer": "ipython3", 415 | "version": "3.10.9" 416 | } 417 | }, 418 | "nbformat": 4, 419 | "nbformat_minor": 5 420 | } 421 | -------------------------------------------------------------------------------- /16_Class.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "42ea1554-b252-4e4b-a00a-e015b40a6ac0", 6 | "metadata": {}, 7 | "source": [ 8 | "# moduls and classes" 9 | ] 10 | }, 11 | { 12 | "cell_type": "raw", 13 | "id": "324c4e1a-7af1-4eb0-815a-7b7d52830179", 14 | "metadata": {}, 15 | "source": [ 16 | "from math import *\n", 17 | "\n", 18 | "# با ستاره میتونیم تمام متد های ماژول را ایمپورت بکنیم که یکسری معایبی دارد\n", 19 | "# یک وارد شدن یکسری اسامی ناخواسته\n", 20 | "# دو ریشه یابی و دیباگ را سخت تر میکند\n", 21 | "# سه تداخل با سایر اسامی ماژول هایی که ایمپورت میکنیم و ممکن است اوررایت شود\n" 22 | ] 23 | }, 24 | { 25 | "cell_type": "raw", 26 | "id": "a945063c-341c-4d3e-93f8-3d549a96cef3", 27 | "metadata": {}, 28 | "source": [ 29 | "from parent.pk2.animal import Animal, MOUTH\n", 30 | "a = Animal()\n", 31 | "print(a)\n", 32 | "\n", 33 | "همانطور که مشاهده میکنید مسیردهی طولانی میباشد" 34 | ] 35 | }, 36 | { 37 | "cell_type": "raw", 38 | "id": "2307575b-f2c2-4b76-8614-0df9e6947413", 39 | "metadata": {}, 40 | "source": [ 41 | "# برای جلوگیری از اینطور مسیردهی طولانی راهکاری وجود دارد\n", 42 | "# با ایمپورت کردن هر پکیج فایل اینیت آن نیز اجرا میشود\n", 43 | "# بنابراین میتوانیم در فایل اینیت پکیج های فرزند را ایمپورت کنیم\n", 44 | "\n", 45 | "__init__.py\n", 46 | "from .pk2.animal import Animal, MOUTH\n", 47 | "\n", 48 | "# با توجه به ایمپورت که در فایل اینیت صورت گرفت میتونیم تنها پکیج والد را ایمپورت کنید\n", 49 | "\n", 50 | "from parent import Animal, MOUTH\n", 51 | "a = Animal()\n", 52 | "print(a)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "raw", 57 | "id": "1777b5aa-98f8-4bd8-b9df-718f611d3620", 58 | "metadata": {}, 59 | "source": [ 60 | "در اکثر مواقع کلاس را ایمپورت میکنیم و بعد از آن آبجکت میسازیم \n", 61 | "\n", 62 | "اما گاهی ماژول هایی وجود دارد که یک آبجکت را برمیگردانند که باید آن آبجکت را از آن ماژول برگردانیم \n", 63 | "\n", 64 | "اما گاهی ساختن آن آبجکت طولانی است و این باعث میشه در شروع برنامه کلی وقت ماتلف شود و بهتر است همان ابتدا آبجکت ساخته نشود\n", 65 | "\n", 66 | "برای حل این موضوع یک تابع میسازیم که هر وقت میخاستیم آن آبجکت ساخته شود بسازد ن در ابتدای اجرای برنامه و بعد همان تابع را ایمپورت میکنیم\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 5, 72 | "id": "6d6197f1-eaee-415b-b516-17c50ca622f3", 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "class Point:\n", 77 | " pass\n", 78 | "\n", 79 | "\n", 80 | "point = None\n", 81 | "\n", 82 | "\n", 83 | "def get_point():\n", 84 | " global point\n", 85 | " if not point:\n", 86 | " point = Point()\n", 87 | " return point" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "id": "4557c1b3-1643-4fe4-8997-a556d3ef900b", 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [ 97 | "# در موقع استفاده فقط همین تابع را ایمپورت میکنیم\n", 98 | "from point import get_point" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 4, 104 | "id": "738e124c-393c-4e53-ace3-8858da8b70f5", 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "# بهتره تایپ داده ها را مشخص بکنیم\n", 109 | "# اما در این حالت اگر بخاهیم تایپ آبجکت را کلاس پوینت قرار دهیم به خطا برخورد میکنیم زیرا نان مقدار دهی کرده ایم\n", 110 | "# point: Point = None\n", 111 | "\n", 112 | "# optional\n", 113 | "# در این مواقع از آپشنال استفاده میکنیم\n", 114 | "from typing import Optional\n", 115 | "\n", 116 | "class Point:\n", 117 | " pass\n", 118 | "\n", 119 | "# در اینصورت هم میتواند نان باشد هم پوینت\n", 120 | "point: Optional[Point] = None\n", 121 | "\n", 122 | "\n", 123 | "def get_point():\n", 124 | " global point\n", 125 | " if not point:\n", 126 | " point = Point()\n", 127 | " return point" 128 | ] 129 | }, 130 | { 131 | "cell_type": "raw", 132 | "id": "83996928-c405-4907-8ab4-1851145eb18a", 133 | "metadata": {}, 134 | "source": [ 135 | "ما شاید در درون یک ماژول کد هایی یا کلاسی داریم که میخاهیم در فایل های دیگر استفاده بکنیم و درون آن ماژول شاید کدهایی دیگری نیز دارند که اجرا میشوند مثل پرینت ها\n", 136 | "\n", 137 | "و این با هربار ایمپورت کردن آن ماژول فراخوانی میشود و اجرا میشود\n", 138 | "\n", 139 | "دقت کنید هر ماژول یک نام دارد\n", 140 | "\n", 141 | "که این نام در خود ماژول میین را برمیگرداند و در سایر فایل ها اسم ماژول را\n", 142 | "__main__\n", 143 | "\n", 144 | "با توجه به این تمام دستوراتی که میخاهیم اجرا شود را در تابع معمولا بنام میین قرار میدهیم و از یک شرط استفاده میکنیم \n" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 7, 150 | "id": "01d92bb9-be5c-4bbd-b09d-052c8f439267", 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "math\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "# نام هر ماژول\n", 163 | "import math\n", 164 | "\n", 165 | "print(math.__name__)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 8, 171 | "id": "025bed56-e860-4b9e-8730-191d16b0bbdc", 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "__main__\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "print(__name__)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 11, 189 | "id": "6f2b955b-2fe0-4556-abf8-29fc65b4484c", 190 | "metadata": {}, 191 | "outputs": [ 192 | { 193 | "name": "stdout", 194 | "output_type": "stream", 195 | "text": [ 196 | "\n" 197 | ] 198 | } 199 | ], 200 | "source": [ 201 | "from typing import Optional\n", 202 | "\n", 203 | "class Point:\n", 204 | " pass\n", 205 | " \n", 206 | "point: Optional[Point] = None\n", 207 | "\n", 208 | "\n", 209 | "def get_point():\n", 210 | " global point\n", 211 | " if not point:\n", 212 | " point = Point()\n", 213 | " return point\n", 214 | "\n", 215 | "# تمام دستوراتی که قرار است اجرا شود در این تابع قرار میدهیم\n", 216 | "def main():\n", 217 | " print(get_point)\n", 218 | " \n", 219 | "if __name__ == \"__main__\" :\n", 220 | " main()" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 13, 226 | "id": "643e8589-4e52-4a08-a839-ecef4204e594", 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | ".p'>\n" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "# ما هم میتونیم درون کلاس تابع تعریف کنیم و هم میتونیم درون تابع کلاس تعریف کینم\n", 239 | "# اگر درون تابع کلاس تعریف کنیم فقط درون آن قابل دسترسی است\n", 240 | "\n", 241 | "def fun():\n", 242 | " class p:\n", 243 | " pass\n", 244 | " print(p)\n", 245 | " \n", 246 | "fun()" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "id": "dd9ca426-25ff-49b9-ac1c-9fc2ec71c2e3", 252 | "metadata": {}, 253 | "source": [ 254 | "# access\n", 255 | "# سطح دسترسی ها در پایتون " 256 | ] 257 | }, 258 | { 259 | "cell_type": "raw", 260 | "id": "20e5caa3-0dc1-4dd3-9d88-eadfb15bec6d", 261 | "metadata": {}, 262 | "source": [ 263 | "در زبان های برنامه نویسی\n", 264 | "\n", 265 | "public > در همه جا در دسترس است\n", 266 | "\n", 267 | "protectet > در خود کلاس و کلاس هایی که از آن کلاس ارث بری کرده اند در دسترس است\n", 268 | "\n", 269 | "private > فقط در خود کلاس دردسترس است" 270 | ] 271 | }, 272 | { 273 | "cell_type": "raw", 274 | "id": "2e44c337-3cd4-4ad9-b027-43642cc5b73f", 275 | "metadata": {}, 276 | "source": [ 277 | "در پایتون چیزی تحت عنوان پرایوت واقعی نداریم\n", 278 | "\n", 279 | "در واقع در پایتون گفته میشود پرایوت یا پابلیک یا پروتکت است اما جلوی استفاده از آن را نمیگیرد\n", 280 | "\n", 281 | "محدودیت صد در صدی ندارد\n", 282 | "\n", 283 | "در پایتون این سطح دسترسی ها را با آندر اسکور ـ مشخص میکنیم" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "id": "87c8a097-affd-4cb4-b108-367e44e48c70", 289 | "metadata": {}, 290 | "source": [ 291 | "# 5 state of usage underscore" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 14, 297 | "id": "df9041cf-64cf-431e-9fd3-6e58f5d1edbb", 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "name": "stdout", 302 | "output_type": "stream", 303 | "text": [ 304 | "hi\n", 305 | "hi\n", 306 | "hi\n", 307 | "hi\n", 308 | "hi\n", 309 | "hi\n", 310 | "hi\n", 311 | "hi\n", 312 | "hi\n", 313 | "hi\n", 314 | "amir\n" 315 | ] 316 | } 317 | ], 318 | "source": [ 319 | "# _ به تنهایی\n", 320 | "\n", 321 | "# در حلقه وقتی که از کد درون حلقه از متغیر استفاده ایی نمیکند\n", 322 | "\n", 323 | "for _ in range(10):\n", 324 | " print(\"hi\")\n", 325 | "\n", 326 | "# وقتی یک متغییر داریم که بعدا نمیخاهیم از آن استفاده بکنیم و نامگذاری آن برای ما مهم نیست\n", 327 | "# مثلا در کد یک تاپل داریم که نمیخاهیم از آنها استفاده کنیم \n", 328 | "name ,_ ,_ = (\"amir\", 23, 2)\n", 329 | "print(name)\n", 330 | "\n", 331 | "# در حالت تعاملی وقتی از اندر اسکور استفاده میکنیم آخرین مقدار را برمیگرداند\n", 332 | "# در واقع آخرین مقدار را در آندراسکور ذخیره میکند\n", 333 | "# >>> \"ali\"\n", 334 | "# 'ali'\n", 335 | "# >>> _\n", 336 | "# 'ali'" 337 | ] 338 | }, 339 | { 340 | "cell_type": "raw", 341 | "id": "9078dafc-ab9e-46e2-9464-2fd9619dad03", 342 | "metadata": {}, 343 | "source": [ 344 | "استفاده از آندر اسکور قبل از متد یا متغییر\n", 345 | "\n", 346 | "public > name\n", 347 | "بصورت عادی تعریف میکنیم\n", 348 | "\n", 349 | "private >\n", 350 | "\n", 351 | "protective > _name\n", 352 | "\n", 353 | "همانطور که میدانید این متغیرر ها یا متد ها و غیره قابل دسترسی هستند اما این نوع نوشتن فقط قرارداد است\n", 354 | "\n", 355 | "----------------------------------------------------------------------\n", 356 | "\n", 357 | "استفاده دوم در بدنه ماژول ما میتوانیم متغییر هایی داشته باشیم که قبل آنها آندر اسکور وجود دارد \n", 358 | "این متغییر ها وقتی از ستاره برای ایمپورت کردن استفاده میکنیم در فایلی که ایمپورت میکنیم در دسترس نیست\n", 359 | "\n", 360 | "in point.py:\n", 361 | "\n", 362 | "_x = 4\n", 363 | "y = 3\n", 364 | "\n", 365 | "-------\n", 366 | "\n", 367 | "in test.py:\n", 368 | "\n", 369 | "from point import *\n", 370 | "\n", 371 | "print(y) > 3\n", 372 | "print(_x) > Error\n", 373 | "\n", 374 | "\n", 375 | "راه حل :\n", 376 | "\n", 377 | "در ماژول یک متغییر آال داریم که میتوانیم در آن این متغییر را نیز تعریف کنیم\n", 378 | "این باعث میشود که وقتی از ستاره برای ایمپورت استفاده میکنیم مشکلی وجود نداشته باشد\n", 379 | "\n", 380 | "in point.py:\n", 381 | "\n", 382 | "__all__ = ['_x', 'y']\n", 383 | "\n", 384 | "_x = 4\n", 385 | "y = 3\n", 386 | "\n", 387 | "-------\n", 388 | "\n", 389 | "in test.py:\n", 390 | "\n", 391 | "from point import *\n", 392 | "\n", 393 | "print(y) > 3\n", 394 | "print(_x) > 4\n" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 18, 400 | "id": "50442841-cfd5-41b7-ac73-2f1b9aaa18d7", 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "140359528837184\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "class User:\n", 413 | " def __init__(self, name):\n", 414 | " self._id = id(self)\n", 415 | " self.name = name\n", 416 | " \n", 417 | " \n", 418 | "user1 = User(\"ali\")\n", 419 | "\n", 420 | "# همانطور که میدانید آیدی پروتکتد است\n", 421 | "print(user1._id)" 422 | ] 423 | }, 424 | { 425 | "cell_type": "raw", 426 | "id": "d504b8d3-f590-4465-b7b8-e906624eb569", 427 | "metadata": {}, 428 | "source": [ 429 | "استفاده آندراسکور بعد از متغییر و متد\n", 430 | "\n", 431 | "همانطور که میدانید در پایتون ما یکسری کلمات کلیدی داریم\n", 432 | "و وقتی میخاهیم از این کلمات استفاده بکنیم از آندر اسکور بعد آن استفاده میکنیم\n", 433 | "\n", 434 | "min_ = 8\n", 435 | "class_ = \"A5\"\n" 436 | ] 437 | }, 438 | { 439 | "cell_type": "raw", 440 | "id": "3a9aa52c-8b0b-4681-9156-ac87bd235f6f", 441 | "metadata": {}, 442 | "source": [ 443 | "استفاده از دوتا آندراسکور قبل از متغییر یا متد\n", 444 | "\n", 445 | "public > name\n", 446 | "بصورت عادی تعریف میکنیم\n", 447 | "\n", 448 | "private > __name\n", 449 | "\n", 450 | "protective > _name\n", 451 | "\n", 452 | "*به دوتا آندر اسکور داندر میگوییند \n", 453 | "\n" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 24, 459 | "id": "d77fb8d1-1695-4ead-88dd-78f860aa3bde", 460 | "metadata": {}, 461 | "outputs": [ 462 | { 463 | "ename": "AttributeError", 464 | "evalue": "'User' object has no attribute '__id'", 465 | "output_type": "error", 466 | "traceback": [ 467 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 468 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 469 | "Cell \u001b[0;32mIn[24], line 10\u001b[0m\n\u001b[1;32m 7\u001b[0m user1 \u001b[38;5;241m=\u001b[39m User(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mali\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m# همانطور که میدانید آیدی پرایوت است\u001b[39;00m\n\u001b[0;32m---> 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43muser1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__id\u001b[49m)\n", 470 | "\u001b[0;31mAttributeError\u001b[0m: 'User' object has no attribute '__id'" 471 | ] 472 | } 473 | ], 474 | "source": [ 475 | "class User:\n", 476 | " def __init__(self, name):\n", 477 | " self.__id = id(self)\n", 478 | " self.name = name\n", 479 | " \n", 480 | " \n", 481 | "user1 = User(\"ali\")\n", 482 | "\n", 483 | "# همانطور که میدانید آیدی پرایوت است\n", 484 | "print(user1.__id)" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 29, 490 | "id": "e6d50fe2-7c04-4dda-b8b3-2f847814b853", 491 | "metadata": {}, 492 | "outputs": [ 493 | { 494 | "name": "stdout", 495 | "output_type": "stream", 496 | "text": [ 497 | "['_User__id', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'str']\n", 498 | "140359533848576\n" 499 | ] 500 | } 501 | ], 502 | "source": [ 503 | "class User:\n", 504 | " def __init__(self, name):\n", 505 | " self.__id = id(self)\n", 506 | " self.name = name\n", 507 | "# درون کلاس به همین شکل قابل استفاده است \n", 508 | " self.str = str(self.__id) + \"name\" + self.name\n", 509 | " \n", 510 | " \n", 511 | "user1 = User(\"ali\")\n", 512 | "\n", 513 | "print(dir(user1))\n", 514 | "# همانطور که میبینید اندراسکور و اسم کلاس به آیدی شده است که به این نیم مانگلینگ میگوییند\n", 515 | "# name mangling\n", 516 | "# __id > _User__id\n", 517 | "\n", 518 | "print(user1._User__id)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 36, 524 | "id": "68463aed-244b-4cd2-941a-1baa6cade364", 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "name": "stdout", 529 | "output_type": "stream", 530 | "text": [ 531 | "['_Account__id', '_User__id', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'str']\n" 532 | ] 533 | } 534 | ], 535 | "source": [ 536 | "# اگر بخاهیم در کلاس های فرزند استفاده بکنیم از نیم انگلینگ متغییر استفاده میکنیم\n", 537 | "\n", 538 | "class User:\n", 539 | " def __init__(self, name=\"\"):\n", 540 | " self.__id = id(self)\n", 541 | " self.name = name \n", 542 | " self.str = str(self.__id) + \"name\" + self.name\n", 543 | " \n", 544 | " \n", 545 | "class Account(User):\n", 546 | " def __init__(self):\n", 547 | " super().__init__()\n", 548 | " self.__id = \"1234\"\n", 549 | " \n", 550 | "\n", 551 | "ali = User(\"ali\")\n", 552 | "ali_account = Account()\n", 553 | "\n", 554 | "# همانطور که میبینید هم متغییر ایدی والد را دارد هم کلاس سازنده خودش\n", 555 | "print(dir(ali_account))\n", 556 | "\n", 557 | "# این نیم مانگلینگ باعث میشود تداخل اسم بین متغیر ها و غیره رخ ندهد در صورتیکه اسامی متغییر ها یکسان است" 558 | ] 559 | }, 560 | { 561 | "cell_type": "raw", 562 | "id": "bba275a3-91bc-4e0d-879a-b7b49d718ad1", 563 | "metadata": {}, 564 | "source": [ 565 | "دوتا اندراسکور قبل و بعد از اسم متغییر یا متد و غیره\n", 566 | "\n", 567 | "special method - magic method\n", 568 | "\n", 569 | "برای متد ها و متغییر های خاص خود پایتون کاربرد دارد\n", 570 | "\n" 571 | ] 572 | }, 573 | { 574 | "cell_type": "raw", 575 | "id": "b10645c8-413c-427d-b099-ffaa16e3de81", 576 | "metadata": {}, 577 | "source": [ 578 | "حالا میخاهیم برای تغییر متغییر ها یکسری کنترل هایی داشته باشیم روش\n", 579 | "\n", 580 | "setter > تغییر دادن\n", 581 | "getter > برگرداندن\n", 582 | "\n", 583 | "معمولا از ست و گت ابتدای اسامی آنها استفاده میکنند\n", 584 | "معمولا برای متغییر های پرایوت براینکه دسترسی مستقیم نداشته باشیم\n", 585 | "با استفاده از این دو دسترسی غیر مستقیم داریم به متغییر های پرایوت" 586 | ] 587 | }, 588 | { 589 | "cell_type": "code", 590 | "execution_count": 40, 591 | "id": "d5821f18-d1bc-4790-8bb3-b3b76cfd4f3a", 592 | "metadata": {}, 593 | "outputs": [ 594 | { 595 | "name": "stdout", 596 | "output_type": "stream", 597 | "text": [ 598 | "01234567889\n" 599 | ] 600 | } 601 | ], 602 | "source": [ 603 | "class User:\n", 604 | " def __init__(self, name=\"\", phone=\"---\"):\n", 605 | " self.__id = id(self)\n", 606 | " self.name = name \n", 607 | " self.__phone = phone \n", 608 | " self.str = str(self.__id) + \"name\" + self.name\n", 609 | " \n", 610 | " def set_phone(self, phone):\n", 611 | " if len(phone) == 11 and phone.isdigit():\n", 612 | " self.__phone = phone\n", 613 | " \n", 614 | " def get_phone(self):\n", 615 | " return self.__phone\n", 616 | " \n", 617 | "\n", 618 | "ali = User(\"ali\")\n", 619 | "ali.set_phone(\"01234567889\")\n", 620 | "number = ali.get_phone()\n", 621 | "print(number)" 622 | ] 623 | }, 624 | { 625 | "cell_type": "code", 626 | "execution_count": null, 627 | "id": "128cfaef-43f6-4667-ab25-56ccc38492d1", 628 | "metadata": {}, 629 | "outputs": [], 630 | "source": [] 631 | } 632 | ], 633 | "metadata": { 634 | "kernelspec": { 635 | "display_name": "Python 3 (ipykernel)", 636 | "language": "python", 637 | "name": "python3" 638 | }, 639 | "language_info": { 640 | "codemirror_mode": { 641 | "name": "ipython", 642 | "version": 3 643 | }, 644 | "file_extension": ".py", 645 | "mimetype": "text/x-python", 646 | "name": "python", 647 | "nbconvert_exporter": "python", 648 | "pygments_lexer": "ipython3", 649 | "version": "3.10.9" 650 | } 651 | }, 652 | "nbformat": 4, 653 | "nbformat_minor": 5 654 | } 655 | -------------------------------------------------------------------------------- /17_Class.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "4fb85db8-5778-4e03-8740-a246dff6f3cc", 6 | "metadata": {}, 7 | "source": [ 8 | "# str - repr" 9 | ] 10 | }, 11 | { 12 | "cell_type": "raw", 13 | "id": "62b31c85-8674-423a-8dd7-316af3564791", 14 | "metadata": {}, 15 | "source": [ 16 | "str -> string -> ــstrــ \n", 17 | "repr -> representation -> __repr__\n", 18 | "\n", 19 | "فانکشن هایی داخلی خود پایتون هستند\n", 20 | "\n", 21 | "با صدا زدن این ها متدهایی خاصی صدا زده میشوند در پشت صحنه که اندر اسکور اطراف خود دارند\n", 22 | "ــstrــ\n", 23 | "__repr__\n", 24 | "\n", 25 | "با رپر کوتیشت های استرینگ نیز نشان داده میشود اما با تابع اس تی ار ن\n", 26 | "\n", 27 | "در پرینت دیفالت اس تی ار نمایش داده میشود ام در حالت تعاملی ریپر است\n", 28 | "\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "id": "c903bf90-31c0-4b9e-9d0d-39fdf69439c3", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "reza\n", 42 | "'reza'\n", 43 | "['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "print(str(\"reza\"))\n", 49 | "print(repr(\"reza\"))\n", 50 | "print(dir(\"reza\"))" 51 | ] 52 | }, 53 | { 54 | "cell_type": "raw", 55 | "id": "38dac2d4-87c5-444f-84ee-dd6e58c6721d", 56 | "metadata": {}, 57 | "source": [ 58 | "repr\n", 59 | "برای دیباگ کردن است و زبان ماشین و توسعه دادن\n", 60 | "جزییات بیشتر و علاییم نگارشی خاص پاییتون را دارد\n", 61 | "به شکلی است که معمولا میتونیم دوباره ابجکت را بازسازی کنیم\n", 62 | "\n", 63 | "\n", 64 | "str\n", 65 | "برای اینکه به کاربر نشان دهیم است\n", 66 | "ساده است" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "id": "36699968-8761-4867-9bee-3352ef606141", 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "2023-07-30 21:26:48.315398\n", 80 | "datetime.datetime(2023, 7, 30, 21, 26, 48, 315398)\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "import datetime\n", 86 | "\n", 87 | "today = datetime.datetime.now()\n", 88 | "print(str(today))\n", 89 | "print(repr(today))" 90 | ] 91 | }, 92 | { 93 | "cell_type": "raw", 94 | "id": "1c9b8225-fd94-4724-b161-40f533590e3d", 95 | "metadata": {}, 96 | "source": [ 97 | "repr and str in class" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "id": "4c4ebb7c-f7bb-4b2d-b83c-c6742beb5d38", 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "<__main__.Person object at 0x7fe9db4e6680>\n", 111 | "<__main__.Person object at 0x7fe9db4e6680>\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "class Person:\n", 117 | " def __init__(self , fname, lname):\n", 118 | " self.fname = fname\n", 119 | " self.lname = lname\n", 120 | " \n", 121 | " \n", 122 | "person = Person(\"ali\", \"mohammadi\")\n", 123 | "print(person)\n", 124 | "print(repr(person))\n", 125 | "\n", 126 | "# وقتی در یک کلاس اس تی ار مشخص نکرده باشیم به صورت دیفالت رپر را نشان میدهد\n", 127 | "\n" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "id": "d45d6643-608a-497d-9bba-3de900042355", 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "ali mohammadi\n", 141 | "Person('ali', 'mohammadi')\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "class Person:\n", 147 | " def __init__(self , fname, lname):\n", 148 | " self.fname = fname\n", 149 | " self.lname = lname\n", 150 | " def __str__(self):\n", 151 | " return f\"{self.fname} {self.lname}\"\n", 152 | " \n", 153 | " def __repr__(self):\n", 154 | " return f\"{self.__class__.__name__}('{self.fname}', '{self.lname}')\"\n", 155 | " \n", 156 | " \n", 157 | "person = Person(\"ali\", \"mohammadi\")\n", 158 | "print(person) # --> print(person.__str__) - print(str(person))\n", 159 | "print(repr(person)) # --> print(person.__repr__)\n", 160 | "\n", 161 | "# اگر از اس تی ار استفاده نکنیم از رپر بجای اس تی ار استفاده میشه\n", 162 | "# اما اگر اس تی ار داشته باشیم رپر نداشته باشیم از رپر بجای اس تی ار استفاده نمیکند\n" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "id": "448c9514-713a-4538-8124-4dd09683d3b7", 168 | "metadata": {}, 169 | "source": [ 170 | "# classs attribute and instance attribute" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "id": "eb3d5c6a-bde9-4258-8029-11e6ab7f5477", 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "# instance attribute\n", 181 | "\n", 182 | "class Instance:\n", 183 | " pass\n", 184 | "\n", 185 | "instance = Instance()\n", 186 | "\n", 187 | "instance.x = 2\n", 188 | "\n", 189 | "# x is a instance attribute\n", 190 | "# ممکن است در ابجکت های دیگر نباشد و اینکه مقدار های متفاوتی در ابجکت های دگر دارد\n", 191 | "# ویژگی ایی که مربوط فقط به یک نمونه است\n", 192 | " " 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "id": "592170a4-b897-434d-a98d-368e7025bddc", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "User('ali', 3)\n", 206 | "\n", 207 | "[User('ali', 3), User('amir', 12), User('alis', 23)]\n", 208 | "\n", 209 | "ali\n", 210 | "[User('ali', 3), User('amir', 12), User('alis', 23)]\n" 211 | ] 212 | } 213 | ], 214 | "source": [ 215 | "# class attribute\n", 216 | "\n", 217 | "# بین همه نمونه ها و آبجکت ها مشترک است\n", 218 | "# بیرون بدنه متد ها استفاده میشه و از سلف و غیره برای تعریف آن استفاده نمکنیم\n", 219 | "from typing import List\n", 220 | " \n", 221 | "class User:\n", 222 | " all_users: List[\"User\"] = []\n", 223 | " def __init__(self, name:str ,age:int) -> None:\n", 224 | " self.name = name\n", 225 | " self.age = age\n", 226 | " # هرکدام از این اتریبوت ها که از سلف استفاده میکنیم یعنی مختص به آن ابجکت تعریف میشه بنابراین اینستنس اتریبیوت هستند\n", 227 | " # insstance attribute\n", 228 | " User.all_users.append(self)\n", 229 | " \n", 230 | " def __repr__(self):\n", 231 | " return f\"{self.__class__.__name__}({self.name!r}, {self.age})\"\n", 232 | "\n", 233 | "user1 = User(\"ali\", 3)\n", 234 | "user2 = User(\"amir\", 12)\n", 235 | "user3 = User(\"alis\", 23)\n", 236 | "\n", 237 | "print(user)\n", 238 | "print()\n", 239 | "print(User.all_users)\n", 240 | "# همانطور که میبینید رپر آنها را در این اتریبیوت قرار میدهد\n", 241 | "\n", 242 | "# با توجه به این که در هر آبجکتی وجود دارد با اسم ابجکت ها هم میتوانیم به آنها دسترسی داشته باشیم \n", 243 | "# اما اگر آن ابجگت همچین اتریبیوتی داشته باشد ابتدا آن اینستنس اتریبیوت را نمایش میدهد\n", 244 | "user1.all_users = \"ali\"\n", 245 | "\n", 246 | "print()\n", 247 | "print(user1.all_users)\n", 248 | "print(User.all_users)\n", 249 | " \n", 250 | "# در نسخه های جدید تر نیازی به ایمپورت لیست نیست\n", 251 | "# List \n" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "id": "b21eb302-fed7-4132-abaf-df40b61b3397", 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [ 261 | "# اگر فیوچری در پایتون وجود دارد که میدانیم در اینده قرار است بیایید اما در نسخه ایی که با آن کار میکنید وجود ندارد میتوانید بدین شکل استفاده بکنید\n", 262 | "from __future__ import annotations\n", 263 | "# حتما باید در خط اول ایمپورت شود\n" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": null, 269 | "id": "d1012a84-8aaf-4acd-9fad-cfccef82078c", 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [ 273 | "# همان پرینت اما به شکل خوشکل تری نمایش میدهد\n", 274 | "from pprint import pprint\n" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": null, 280 | "id": "df54edb5-d9c6-4fd6-a87f-251d512e2ff6", 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "[User('amir', 12), User('alis', 23)]\n", 288 | "\n", 289 | "['asd']\n", 290 | "[User('ali', 3), User('amir', 12), User('alis', 23)]\n", 291 | "\n", 292 | "[User('ali', 3), 'asd', User('amir', 12)]\n", 293 | "[User('ali', 3), 'asd', User('amir', 12)]\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "from typing import List\n", 299 | "from pprint import pprint\n", 300 | " \n", 301 | "class User:\n", 302 | " all_users: List[\"User\"] = []\n", 303 | " def __init__(self, name:str ,age:int) -> None:\n", 304 | " self.name = name\n", 305 | " self.age = age\n", 306 | " User.all_users.append(self)\n", 307 | " \n", 308 | " def __repr__(self):\n", 309 | " return f\"{self.__class__.__name__}({self.name!r}, {self.age})\"\n", 310 | "\n", 311 | "user1 = User(\"ali\", 3)\n", 312 | "User.all_users = []\n", 313 | "user2 = User(\"amir\", 12)\n", 314 | "user3 = User(\"alis\", 23)\n", 315 | "pprint(User.all_users)\n", 316 | "\n", 317 | "User.all_users = []\n", 318 | "\n", 319 | "# وقتی میخاهیم تغییراتی در این کلاس اتریبیوت دهیم باید با اسم کلاس این کار را انجام دهیم ن با اسم آبجکت ها\n", 320 | "# زیرا یک اتریبیوت جدید ساخته میشود در آبجکت و تغییری در آن کلاس اتریبیوت نمیافتد\n", 321 | "\n", 322 | "print()\n", 323 | "user1 = User(\"ali\", 3)\n", 324 | "user1.all_users = [\"asd\"]\n", 325 | "user2 = User(\"amir\", 12)\n", 326 | "user3 = User(\"alis\", 23)\n", 327 | "pprint(user1.all_users)\n", 328 | "pprint(User.all_users)\n", 329 | "\n", 330 | "User.all_users = []\n", 331 | "\n", 332 | "# اگر از اپند استفاده بکنیم مشکلی بوجود نمیاد\n", 333 | "print()\n", 334 | "user1 = User(\"ali\", 3)\n", 335 | "user1.all_users.append(\"asd\")\n", 336 | "user2 = User(\"amir\", 12)\n", 337 | "pprint(user1.all_users)\n", 338 | "pprint(User.all_users)" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": null, 344 | "id": "84a16fba-b9e0-4774-99a7-034d99271521", 345 | "metadata": {}, 346 | "outputs": [], 347 | "source": [] 348 | } 349 | ], 350 | "metadata": { 351 | "kernelspec": { 352 | "display_name": "Python 3 (ipykernel)", 353 | "language": "python", 354 | "name": "python3" 355 | }, 356 | "language_info": { 357 | "codemirror_mode": { 358 | "name": "ipython", 359 | "version": 3 360 | }, 361 | "file_extension": ".py", 362 | "mimetype": "text/x-python", 363 | "name": "python", 364 | "nbconvert_exporter": "python", 365 | "pygments_lexer": "ipython3", 366 | "version": "3.10.9" 367 | } 368 | }, 369 | "nbformat": 4, 370 | "nbformat_minor": 5 371 | } 372 | -------------------------------------------------------------------------------- /21_Exception.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "2d2e4dd9-0f89-4d7e-be3d-7582c47fd13b", 6 | "metadata": {}, 7 | "source": [ 8 | "انواع خطاها\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 4, 14 | "id": "1544b354-637d-4707-9845-cea26d6f289d", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# logical error\n", 19 | "# خطاهای منطقی\n", 20 | "# ربنامه به درستی اجرا میشود اما جواب نادرست است\n", 21 | "\n", 22 | "# syntax error\n", 23 | "# خطا در ساختار که قبل از اجرای برنامه ارورر میدهد\n", 24 | "# با فلش نشان میدهد در کجا رخ داده است\n", 25 | "\n", 26 | "# runtime error\n", 27 | "# استثناها\n", 28 | "# یکسری خطاها که موقع اجرا رخ میدهند\n", 29 | "# میشه استثنا ها را مدیریت کرد\n", 30 | "# دارای traceback - ردیابی خطا\n" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "id": "68cf9bd6-0af2-4b12-8e98-ad4a87730560", 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "ename": "SyntaxError", 41 | "evalue": "incomplete input (3716500908.py, line 5)", 42 | "output_type": "error", 43 | "traceback": [ 44 | "\u001b[0;36m Cell \u001b[0;32mIn[2], line 5\u001b[0;36m\u001b[0m\n\u001b[0;31m \u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m incomplete input\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "# syntax error\n", 50 | "\n", 51 | "if True:\n", 52 | " print(\n", 53 | " " 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "id": "4f29f2ef-b901-4927-9f10-97fc39fd93ae", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdin", 64 | "output_type": "stream", 65 | "text": [ 66 | " 5\n", 67 | " 0\n" 68 | ] 69 | }, 70 | { 71 | "ename": "ZeroDivisionError", 72 | "evalue": "division by zero", 73 | "output_type": "error", 74 | "traceback": [ 75 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 76 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 77 | "Cell \u001b[0;32mIn[3], line 5\u001b[0m\n\u001b[1;32m 2\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m())\n\u001b[1;32m 3\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m())\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mX / Y =\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m)\n", 78 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "# exception\n", 84 | "\n", 85 | "x = int(input())\n", 86 | "y = int(input())\n", 87 | "\n", 88 | "print('X / Y =', x / y)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 2, 94 | "id": "1467096f-1915-4938-b68b-43cc959e4484", 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdin", 99 | "output_type": "stream", 100 | "text": [ 101 | " 2\n", 102 | " 0\n" 103 | ] 104 | }, 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "erorr zero\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "# داخل بدنه ترای کدهایی که احتمال خطا دارد نشان میدهد و با این روش ادامه دستورات اجرا میشه بدون خوردن به خطا\n", 115 | "# داخل بدنه اکسپت کاری رو که اگر کد با خطا مواجهه شده را باید بنویسیم\n", 116 | "# اگر در داخل بدنه ترای به ارورر بخوریم دستورات بعد از آن اجرا نمیشود\n", 117 | "\n", 118 | "try:\n", 119 | " x = int(input())\n", 120 | " y = int(input())\n", 121 | "\n", 122 | " print('X / Y =', x / y)\n", 123 | "except:\n", 124 | " print('erorr zero')" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 5, 130 | "id": "e9cbc13d-6467-476d-a45a-d244a856823c", 131 | "metadata": {}, 132 | "outputs": [ 133 | { 134 | "name": "stdin", 135 | "output_type": "stream", 136 | "text": [ 137 | "x: s\n" 138 | ] 139 | }, 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "try again...\n" 145 | ] 146 | }, 147 | { 148 | "name": "stdin", 149 | "output_type": "stream", 150 | "text": [ 151 | "x: 2\n" 152 | ] 153 | }, 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "2\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "\n", 164 | "while True:\n", 165 | " try:\n", 166 | " x = int(input(\"x: \"))\n", 167 | " break\n", 168 | " except:\n", 169 | " print(\"try again...\")\n", 170 | " \n", 171 | "print(x)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 3, 177 | "id": "b92def72-09ef-4c94-bb7c-716ff6c5a099", 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdin", 182 | "output_type": "stream", 183 | "text": [ 184 | " 5\n", 185 | " 0\n" 186 | ] 187 | }, 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "ZeroDivisionError :\n" 193 | ] 194 | }, 195 | { 196 | "name": "stdin", 197 | "output_type": "stream", 198 | "text": [ 199 | " 2\n" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "# گاهی اوقات نوع استثنا را مشخص میکنیم\n", 205 | "# برای ارور های دیگر میتونیم یک اکسپت دیگر بنویسیم\n", 206 | "# میتونیم چنتا مدل اررور داشته باشیم\n", 207 | "\n", 208 | "\n", 209 | "try:\n", 210 | " x = int(input())\n", 211 | " y = int(input())\n", 212 | " print('X / Y =', x / y)\n", 213 | " \n", 214 | "except ZeroDivisionError:\n", 215 | " print('ZeroDivisionError :')\n", 216 | " y = int(input())\n", 217 | " print('X / Y =', x / y)\n", 218 | " \n", 219 | "except ValueError:\n", 220 | " print('ValueError')\n", 221 | " \n", 222 | "except:\n", 223 | " print('unknow Error')\n", 224 | " " 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 5, 230 | "id": "2a50cb91-4280-4686-b698-5ccfe555d6f9", 231 | "metadata": {}, 232 | "outputs": [ 233 | { 234 | "name": "stdin", 235 | "output_type": "stream", 236 | "text": [ 237 | " k\n" 238 | ] 239 | }, 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "Error :\n" 245 | ] 246 | }, 247 | { 248 | "name": "stdin", 249 | "output_type": "stream", 250 | "text": [ 251 | " 5\n", 252 | " 2\n" 253 | ] 254 | }, 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "X / Y = 2.5\n" 260 | ] 261 | } 262 | ], 263 | "source": [ 264 | "# گاهی برای چند اررور یک کار خاص میخاهیم انجام دهیم\n", 265 | "# استثنا ها به ترتیب از بالا به پایین بررسی میشوند\n", 266 | "\n", 267 | "try:\n", 268 | " x = int(input())\n", 269 | " y = int(input())\n", 270 | " print('X / Y =', x / y)\n", 271 | " \n", 272 | "except (ZeroDivisionError,ValueError):\n", 273 | " print('Error :')\n", 274 | " x = int(input())\n", 275 | " y = int(input())\n", 276 | " print('X / Y =', x / y)\n", 277 | " \n", 278 | "except NameError:\n", 279 | " print('NameError') \n", 280 | "\n", 281 | "# این استثناها شی هستند\n", 282 | "# اکسپشن کلاس پایه اکثر خطاها و ارورر ها\n", 283 | "except: # or except Exception:\n", 284 | " print('unknow Error')\n", 285 | " " 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 9, 291 | "id": "5ada18a9-b1d8-4e8f-83a3-2473deca7ce4", 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdin", 296 | "output_type": "stream", 297 | "text": [ 298 | " s\n" 299 | ] 300 | }, 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "ValueError\n" 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "# این استثناها شی هستند\n", 311 | "# اکسپشن کلاس پایه اکثر خطاها و ارورر ها\n", 312 | "\n", 313 | "try:\n", 314 | " x = int(input())\n", 315 | " y = int(input())\n", 316 | " print('X / Y =', x / y)\n", 317 | "\n", 318 | "# میشه از اسم مستعار آبجکت استفاده کرد\n", 319 | "except (ZeroDivisionError,ValueError) as E:\n", 320 | " print(E.__class__.__name__)\n", 321 | " \n", 322 | "except NameError:\n", 323 | " print('NameError') \n", 324 | "\n", 325 | "except Exception:\n", 326 | " print('unknow Error')" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": 13, 332 | "id": "1f739ca4-20a9-4f9d-9456-2ff9ecde3908", 333 | "metadata": {}, 334 | "outputs": [ 335 | { 336 | "name": "stdin", 337 | "output_type": "stream", 338 | "text": [ 339 | " بث\n" 340 | ] 341 | }, 342 | { 343 | "name": "stdout", 344 | "output_type": "stream", 345 | "text": [ 346 | "ValueError\n" 347 | ] 348 | } 349 | ], 350 | "source": [ 351 | "# نوع خطا را مشخص میکند و برنامه هم متوقف نمیشود\n", 352 | "\n", 353 | "try:\n", 354 | " x = int(input())\n", 355 | " y = int(input())\n", 356 | " print('X / Y =', x / y)\n", 357 | "\n", 358 | "except Exception as Ex:\n", 359 | " print(Ex.__class__.__name__)" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 4, 365 | "id": "c593d4e4-2c81-4259-8b37-4799e2072aa2", 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "name": "stdin", 370 | "output_type": "stream", 371 | "text": [ 372 | " 5\n", 373 | " 6\n" 374 | ] 375 | }, 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "X / Y = 0.8333333333333334\n", 381 | "end\n" 382 | ] 383 | } 384 | ], 385 | "source": [ 386 | "# یک قسمت الس هم داریم که در صورتیکه هیچکدام از اکسپت ها اجرا نشد آن قسمت اجرا میشود\n", 387 | "# زمانیکه بدنه ترای به طور کامل اجرا شود\n", 388 | "# ریترن کردن مقداری در ترای باعث میشه الس دیگر اجرا نشود\n", 389 | "# اگر استثنایی در الس باشد به خطا خاهیم خورد\n", 390 | "\n", 391 | "try:\n", 392 | " x = int(input())\n", 393 | " y = int(input())\n", 394 | " print('X / Y =', x / y)\n", 395 | "\n", 396 | "# میتونیم تعیین بکنیم برای چند نوع استثنا یک اتفاق بیافتد\n", 397 | "except (ZeroDivisionError,ValueError):\n", 398 | " print('Error :')\n", 399 | " x = int(input())\n", 400 | " y = int(input())\n", 401 | " print('X / Y =', x / y)\n", 402 | " \n", 403 | "except NameError:\n", 404 | " print('NameError') \n", 405 | " \n", 406 | "except:\n", 407 | " print('unknow Error')\n", 408 | " \n", 409 | "else:\n", 410 | " print(\"end\")" 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": 14, 416 | "id": "de571ee8-0e99-45bc-8958-3926cc82aa79", 417 | "metadata": {}, 418 | "outputs": [ 419 | { 420 | "name": "stdin", 421 | "output_type": "stream", 422 | "text": [ 423 | " 5\n", 424 | " 0\n" 425 | ] 426 | }, 427 | { 428 | "name": "stdout", 429 | "output_type": "stream", 430 | "text": [ 431 | "Error :\n" 432 | ] 433 | }, 434 | { 435 | "name": "stdin", 436 | "output_type": "stream", 437 | "text": [ 438 | " 6\n", 439 | " 3\n" 440 | ] 441 | }, 442 | { 443 | "name": "stdout", 444 | "output_type": "stream", 445 | "text": [ 446 | "X / Y = 2.0\n", 447 | "finally end\n" 448 | ] 449 | } 450 | ], 451 | "source": [ 452 | "# کد درون فاینلی چه استثنا وجود داشته باشد چه نداشته باشد اجرا میشود\n", 453 | "# فاینلی میتونه به تنهایی استفاده بشه\n", 454 | "\n", 455 | "try:\n", 456 | " x = int(input())\n", 457 | " y = int(input())\n", 458 | " if x == 10:\n", 459 | " raise NameError(\"hahahahaha error\")\n", 460 | " print('X / Y =', x / y) \n", 461 | "except (ZeroDivisionError,ValueError):\n", 462 | " print('Error :')\n", 463 | " x = int(input())\n", 464 | " y = int(input())\n", 465 | " print('X / Y =', x / y) \n", 466 | "else:\n", 467 | " print(\"end\")\n", 468 | "finally:\n", 469 | " print(\"finally end\")\n" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 15, 475 | "id": "f95212f3-af44-4d4d-98cf-c367fc9369ab", 476 | "metadata": {}, 477 | "outputs": [ 478 | { 479 | "name": "stdin", 480 | "output_type": "stream", 481 | "text": [ 482 | " 6\n", 483 | " 0\n" 484 | ] 485 | }, 486 | { 487 | "name": "stdout", 488 | "output_type": "stream", 489 | "text": [ 490 | "Error :\n" 491 | ] 492 | }, 493 | { 494 | "name": "stdin", 495 | "output_type": "stream", 496 | "text": [ 497 | " 6\n", 498 | " 9\n" 499 | ] 500 | }, 501 | { 502 | "name": "stdout", 503 | "output_type": "stream", 504 | "text": [ 505 | "finally end\n" 506 | ] 507 | }, 508 | { 509 | "ename": "ZeroDivisionError", 510 | "evalue": "division by zero", 511 | "output_type": "error", 512 | "traceback": [ 513 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 514 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 515 | "Cell \u001b[0;32mIn[15], line 8\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNameError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhahahahaha error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mX / Y =\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m) \n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mZeroDivisionError\u001b[39;00m):\n", 516 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero", 517 | "\nDuring handling of the above exception, another exception occurred:\n", 518 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 519 | "Cell \u001b[0;32mIn[15], line 13\u001b[0m\n\u001b[1;32m 11\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m())\n\u001b[1;32m 12\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m())\n\u001b[0;32m---> 13\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mX / Y =\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m) \n\u001b[1;32m 14\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mend\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", 520 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 521 | ] 522 | } 523 | ], 524 | "source": [ 525 | "# حتی درون بدنه استثنا هم استثنایی باشد فاینلی اجرا میشود\n", 526 | "\n", 527 | "try:\n", 528 | " x = int(input())\n", 529 | " y = int(input())\n", 530 | " if x == 10:\n", 531 | " raise NameError(\"hahahahaha error\")\n", 532 | " print('X / Y =', x / y) \n", 533 | "except (ZeroDivisionError):\n", 534 | " print('Error :')\n", 535 | " x = int(input())\n", 536 | " print('X / 0 =', x / 0) \n", 537 | "else:\n", 538 | " print(\"end\")\n", 539 | "finally:\n", 540 | " print(\"finally end\")\n", 541 | "\n", 542 | " " 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 19, 548 | "id": "d7efd7f0-75bf-4e1f-8612-f13a1a14f33c", 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "name": "stdout", 553 | "output_type": "stream", 554 | "text": [ 555 | "finally end\n", 556 | "2\n" 557 | ] 558 | } 559 | ], 560 | "source": [ 561 | "# برای پاکسازی یا بستن فایل و یا شی ساختیم که بخاهیم حذفش کنیم استفاده میشود\n", 562 | "# ریترن کردن در بدنه ترای باعث میشه الس اجرا نشود اما در عملکرد فاینلی تاثیری نندارد\n", 563 | "\n", 564 | "# در صورت ریترن کردن در ترای و الس و فاینلی ریترن در فاینلی اجرا میشود\n", 565 | "\n", 566 | "def func(x,y):\n", 567 | " try:\n", 568 | " return x/y \n", 569 | " except (ZeroDivisionError):\n", 570 | " print('Error :')\n", 571 | " x = int(input())\n", 572 | " print('X / 0 =', x / 0) \n", 573 | " else:\n", 574 | " print(\"end\")\n", 575 | " return 9\n", 576 | " finally:\n", 577 | " print(\"finally end\")\n", 578 | " return 2\n", 579 | " \n", 580 | "print(func(5,6))" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 20, 586 | "id": "49b11bb9-6007-447b-9e33-462e23c9bad1", 587 | "metadata": {}, 588 | "outputs": [ 589 | { 590 | "name": "stdout", 591 | "output_type": "stream", 592 | "text": [ 593 | "finally end\n", 594 | "2\n" 595 | ] 596 | } 597 | ], 598 | "source": [ 599 | "# فاینلی بخشی که خطا برمیگرداند را نگه میدارد و ابتدا اجرا میشود\n", 600 | "# بنابراین اگر چیزی ریترن بکنیم خطایی اجرا نمیشود\n", 601 | "\n", 602 | "def func(x,y):\n", 603 | " try:\n", 604 | " print(x/y) \n", 605 | " finally:\n", 606 | " print(\"finally end\")\n", 607 | " return 2\n", 608 | " \n", 609 | "print(func(5,0))" 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "execution_count": 22, 615 | "id": "dbe5606c-89d1-470b-a8e2-85645e996f44", 616 | "metadata": {}, 617 | "outputs": [ 618 | { 619 | "name": "stdin", 620 | "output_type": "stream", 621 | "text": [ 622 | "a: 6\n", 623 | "b: 3\n", 624 | "c: 0\n" 625 | ] 626 | }, 627 | { 628 | "name": "stdout", 629 | "output_type": "stream", 630 | "text": [ 631 | "ZeroDivisionError\n" 632 | ] 633 | } 634 | ], 635 | "source": [ 636 | "# nested exception\n", 637 | "# با اینکه ارورر در یک فانکشن دیگر رخ داده مدیریت آن در فانکشن بالاتر است البته الویت با فانکشنی است که خطا در آن رخ داده است\n", 638 | "# در توابع تو در تور استثنا اگر در توابع داخلی مدیریت شده بود آنرا برمیگرداند اما به لایه های خارجی می آید و در نهایت آن را برمیگرداند\n", 639 | "# در نهایت اگر مدیریت نشده بود خطا را نشان میدهد\n", 640 | "\n", 641 | "def div(x, y, n):\n", 642 | " try:\n", 643 | " s = x + y\n", 644 | " result = s / n\n", 645 | " print(result)\n", 646 | " except TypeError as te:\n", 647 | " # nested exception\n", 648 | " print(te.__class__.__name__)\n", 649 | " try:\n", 650 | " print(z)\n", 651 | " except Exception as Ex:\n", 652 | " print(Ex.__class__.__name__)\n", 653 | " \n", 654 | " except ZeroDivisionError as te:\n", 655 | " print(te.__class__.__name__)\n", 656 | " \n", 657 | " \n", 658 | "def func():\n", 659 | " try:\n", 660 | " a = int(input(\"a: \"))\n", 661 | " b = int(input(\"b: \")) \n", 662 | " c = int(input(\"c: \")) \n", 663 | " div(a,b,c)\n", 664 | " except TypeError as te:\n", 665 | " print(te.__class__.__name__)\n", 666 | " except ZeroDivisionError as te:\n", 667 | " print(te.__class__.__name__)\n", 668 | " \n", 669 | "func()" 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "execution_count": 8, 675 | "id": "f631b536-90f7-458f-8ab8-2e92a328f983", 676 | "metadata": {}, 677 | "outputs": [ 678 | { 679 | "name": "stdin", 680 | "output_type": "stream", 681 | "text": [ 682 | " 10\n", 683 | " 2\n" 684 | ] 685 | }, 686 | { 687 | "ename": "NameError", 688 | "evalue": "hahahahaha error", 689 | "output_type": "error", 690 | "traceback": [ 691 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 692 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 693 | "Cell \u001b[0;32mIn[8], line 7\u001b[0m\n\u001b[1;32m 5\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m())\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m x \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m10\u001b[39m:\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNameError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhahahahaha error\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mX / Y =\u001b[39m\u001b[38;5;124m'\u001b[39m, x \u001b[38;5;241m/\u001b[39m y)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mZeroDivisionError\u001b[39;00m,\u001b[38;5;167;01mValueError\u001b[39;00m):\n", 694 | "\u001b[0;31mNameError\u001b[0m: hahahahaha error" 695 | ] 696 | } 697 | ], 698 | "source": [ 699 | "# میتونیم ما خودمان هم با استفاده از ریس یک اررور تولید کنیم\n", 700 | "# خطایی که ایجاد میکنیم باید هندل نشده باشد\n", 701 | "\n", 702 | "try:\n", 703 | " x = int(input())\n", 704 | " y = int(input())\n", 705 | " if x == 10:\n", 706 | " raise NameError(\"hahahahaha error\")\n", 707 | " print('X / Y =', x / y) \n", 708 | "except (ZeroDivisionError,ValueError):\n", 709 | " print('Error :')\n", 710 | " x = int(input())\n", 711 | " y = int(input())\n", 712 | " print('X / Y =', x / y) \n", 713 | "else:\n", 714 | " print(\"end\")\n" 715 | ] 716 | }, 717 | { 718 | "cell_type": "code", 719 | "execution_count": 24, 720 | "id": "b69f9122-3f80-449f-8073-cfbe1a382c32", 721 | "metadata": {}, 722 | "outputs": [ 723 | { 724 | "ename": "RuntimeError", 725 | "evalue": "Oops!", 726 | "output_type": "error", 727 | "traceback": [ 728 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 729 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 730 | "Cell \u001b[0;32mIn[24], line 6\u001b[0m, in \u001b[0;36mdiv\u001b[0;34m(x, y, n)\u001b[0m\n\u001b[1;32m 5\u001b[0m s \u001b[38;5;241m=\u001b[39m x \u001b[38;5;241m+\u001b[39m y\n\u001b[0;32m----> 6\u001b[0m d \u001b[38;5;241m=\u001b[39m \u001b[43ms\u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(d)\n", 731 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero", 732 | "\nThe above exception was the direct cause of the following exception:\n", 733 | "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", 734 | "Cell \u001b[0;32mIn[24], line 11\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mZeroDivisionError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m zde:\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOops!\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mzde\u001b[39;00m\n\u001b[0;32m---> 11\u001b[0m \u001b[43mdiv\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", 735 | "Cell \u001b[0;32mIn[24], line 9\u001b[0m, in \u001b[0;36mdiv\u001b[0;34m(x, y, n)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(d)\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mZeroDivisionError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m zde:\n\u001b[0;32m----> 9\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOops!\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mzde\u001b[39;00m\n", 736 | "\u001b[0;31mRuntimeError\u001b[0m: Oops!" 737 | ] 738 | } 739 | ], 740 | "source": [ 741 | "# میتونیم مرجع اررور را هم مشخص بکنیم\n", 742 | "\n", 743 | "def div(x, y, n):\n", 744 | " try:\n", 745 | " s = x + y\n", 746 | " d = s/ n\n", 747 | " print(d)\n", 748 | " except ZeroDivisionError as zde:\n", 749 | " raise RuntimeError(\"Oops!\") from zde\n", 750 | " \n", 751 | "div(4, 0, 0)" 752 | ] 753 | }, 754 | { 755 | "cell_type": "raw", 756 | "id": "581ee8f4-725f-4b19-bf41-d3cdca5fc04a", 757 | "metadata": {}, 758 | "source": [ 759 | "create an exception\n", 760 | "\n", 761 | "هر یک از استنا ها زیر مجموعه بیس اکسپشن هستند که اکثر استثناها زیر مجموعه اکسپشن هستند\n", 762 | "\n", 763 | "برای ساخت استثنا باید از کلاس های والد ارثبری بکنیم\n", 764 | "\n", 765 | "بهتره اخر کلاس از کلمه اررور استفاده بکنیم\n", 766 | "\n" 767 | ] 768 | }, 769 | { 770 | "cell_type": "code", 771 | "execution_count": 25, 772 | "id": "027b0a97-a9d6-49f5-957b-74af5d503f80", 773 | "metadata": {}, 774 | "outputs": [ 775 | { 776 | "ename": "Exception", 777 | "evalue": "the string does not contain only numbers!", 778 | "output_type": "error", 779 | "traceback": [ 780 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 781 | "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", 782 | "Cell \u001b[0;32mIn[25], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m numbers\u001b[38;5;241m.\u001b[39mappend(\u001b[38;5;28mint\u001b[39m(i))\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(numbers)\n\u001b[0;32m----> 9\u001b[0m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mh661\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", 783 | "Cell \u001b[0;32mIn[25], line 3\u001b[0m, in \u001b[0;36mfunc\u001b[0;34m(s)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfunc\u001b[39m(s):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m s\u001b[38;5;241m.\u001b[39misdigit():\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthe string does not contain only numbers!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 4\u001b[0m numbers \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m s:\n", 784 | "\u001b[0;31mException\u001b[0m: the string does not contain only numbers!" 785 | ] 786 | } 787 | ], 788 | "source": [ 789 | "def func(s):\n", 790 | " if not s.isdigit():\n", 791 | " raise Exception(\"the string does not contain only numbers!\")\n", 792 | " numbers = []\n", 793 | " for i in s:\n", 794 | " numbers.append(int(i))\n", 795 | " print(numbers)\n", 796 | " \n", 797 | "func(\"h661\")" 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": 29, 803 | "id": "519ff751-f5d3-4b61-af21-cf86afc3dbc5", 804 | "metadata": {}, 805 | "outputs": [ 806 | { 807 | "name": "stdout", 808 | "output_type": "stream", 809 | "text": [ 810 | "h661\n", 811 | "**DigitError**\n" 812 | ] 813 | } 814 | ], 815 | "source": [ 816 | "class DigitError(Exception):\n", 817 | " # میتونیم هرچی را در این دریافت بکنیم مثل اس که در واقع رشته موردنظر است\n", 818 | " def __init__(self, s, message=\"Error\"):\n", 819 | " self.message = message\n", 820 | " self.s = s\n", 821 | " print(s)\n", 822 | " super().__init__(self.message)\n", 823 | " \n", 824 | " def __str__(self):\n", 825 | " return \"**DigitError**\"\n", 826 | "\n", 827 | "def func(s):\n", 828 | " if not s.isdigit():\n", 829 | " raise DigitError(s, \"the string does not contain only numbers!\")\n", 830 | " numbers = []\n", 831 | " for i in s:\n", 832 | " numbers.append(int(i))\n", 833 | " print(numbers)\n", 834 | "\n", 835 | "try:\n", 836 | " func(\"h661\")\n", 837 | "except DigitError as de:\n", 838 | " print(de)\n", 839 | " " 840 | ] 841 | }, 842 | { 843 | "cell_type": "raw", 844 | "id": "3da6e16c-03b2-4db5-85d6-467ff3d7e773", 845 | "metadata": {}, 846 | "source": [ 847 | "warnings\n", 848 | "\n", 849 | "هشدار ها\n", 850 | "\n", 851 | "اگر با ریز وارنینگ را استفاده بکنیم مثل استثنا برخورد میکند\n", 852 | "\n", 853 | "بنابراین از تابع وارنینگز باید استفاده بکنیم\n", 854 | "\n", 855 | "بعد از برنامه اگر دستوری باشد اجرا میشود\n", 856 | "\n", 857 | "مثل استثنا ها میتونیم با ارثبری از کلاس وارنینگ وارنینگ اختصاصی خودمان را درست بکنیم\n" 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "execution_count": 32, 863 | "id": "9d6f8f82-0376-436f-af05-6c72c1cc299f", 864 | "metadata": {}, 865 | "outputs": [ 866 | { 867 | "name": "stdout", 868 | "output_type": "stream", 869 | "text": [ 870 | "33\n", 871 | "ok!\n" 872 | ] 873 | }, 874 | { 875 | "name": "stderr", 876 | "output_type": "stream", 877 | "text": [ 878 | "/tmp/ipykernel_2675/2360017770.py:7: UserWarning: x and y cannot be strings!\n", 879 | " warnings.warn(\"x and y cannot be strings!\", UserWarning)\n" 880 | ] 881 | } 882 | ], 883 | "source": [ 884 | "# مثلا در مثال زیر میخاهیم بگیم بهتر است از عدد بجای رشته استفاده شود\n", 885 | "\n", 886 | "import warnings\n", 887 | "\n", 888 | "def fun(x:int, y:int):\n", 889 | " if not isinstance(x, int) and not isinstance(x, int):\n", 890 | " # فرستادن نوع اخطار اجباری نیست\n", 891 | " warnings.warn(\"x and y cannot be strings!\", UserWarning)\n", 892 | " print(x + y)\n", 893 | " \n", 894 | "fun(\"3\", \"3\")\n", 895 | "\n", 896 | "print(\"ok!\")" 897 | ] 898 | }, 899 | { 900 | "cell_type": "raw", 901 | "id": "d064d994-0dac-452a-a5de-42bf3c90673a", 902 | "metadata": {}, 903 | "source": [ 904 | "assert\n", 905 | "\n", 906 | "زمانی که انتظار داریم یک شرط درست باشد اگر درست باشد ادامه میدهد وگرن دچار مشکل میشود\n", 907 | "\n", 908 | "برای دیباگ استفده میشود\n", 909 | "\n", 910 | "برای استفاده کلمه اسرت را مینویسیم و بعد جلوی آن یک شرط قرار میدهیم\n", 911 | "\n", 912 | "اگر شرط درست باشد برنامه به درستی اجرا میشود وگرن به اررور خواهیم خورد\n", 913 | "\n", 914 | "معمولا وقتی که میدونیم یه شرطی درست است و برای مطمین شدن اینکه درست است یا ن از اسرررت استفاده میکنیم\n", 915 | "\n" 916 | ] 917 | }, 918 | { 919 | "cell_type": "code", 920 | "execution_count": 36, 921 | "id": "a44b4292-4b1f-4913-9541-55ddfd12c040", 922 | "metadata": {}, 923 | "outputs": [ 924 | { 925 | "ename": "AssertionError", 926 | "evalue": "yes is not true!!", 927 | "output_type": "error", 928 | "traceback": [ 929 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 930 | "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", 931 | "Cell \u001b[0;32mIn[36], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mint\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mint\u001b[39m), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myes is not true!!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(x \u001b[38;5;241m+\u001b[39m y)\n\u001b[0;32m----> 6\u001b[0m \u001b[43mfun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m3\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m3\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mok!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", 932 | "Cell \u001b[0;32mIn[36], line 3\u001b[0m, in \u001b[0;36mfun\u001b[0;34m(x, y)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfun\u001b[39m(x:\u001b[38;5;28mint\u001b[39m, y:\u001b[38;5;28mint\u001b[39m):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# میتونیم با یک کاما پیام دلخواه خودمان را به آن بدهیم\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mint\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, \u001b[38;5;28mint\u001b[39m), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myes is not true!!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(x \u001b[38;5;241m+\u001b[39m y)\n", 933 | "\u001b[0;31mAssertionError\u001b[0m: yes is not true!!" 934 | ] 935 | } 936 | ], 937 | "source": [ 938 | "\n", 939 | "\n", 940 | "def fun(x:int, y:int):\n", 941 | " # میتونیم با یک کاما پیام دلخواه خودمان را به آن بدهیم\n", 942 | " assert isinstance(x, int) and isinstance(x, int), \"yes is not true!!\"\n", 943 | " print(x + y)\n", 944 | " \n", 945 | "fun(\"3\", \"3\")\n", 946 | "\n", 947 | "print(\"ok!\")" 948 | ] 949 | }, 950 | { 951 | "cell_type": "code", 952 | "execution_count": null, 953 | "id": "50c77073-4e00-4171-9afb-2abf29ab1b7d", 954 | "metadata": {}, 955 | "outputs": [], 956 | "source": [] 957 | } 958 | ], 959 | "metadata": { 960 | "kernelspec": { 961 | "display_name": "Python 3 (ipykernel)", 962 | "language": "python", 963 | "name": "python3" 964 | }, 965 | "language_info": { 966 | "codemirror_mode": { 967 | "name": "ipython", 968 | "version": 3 969 | }, 970 | "file_extension": ".py", 971 | "mimetype": "text/x-python", 972 | "name": "python", 973 | "nbconvert_exporter": "python", 974 | "pygments_lexer": "ipython3", 975 | "version": "3.10.9" 976 | } 977 | }, 978 | "nbformat": 4, 979 | "nbformat_minor": 5 980 | } 981 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pythonlearning 2 | 3 | I Make This Handbook based on Sabzlearn.ir free python course by Reza Dolati 4 | 5 | Markdown sections are written in Farsi. 6 | 7 | Note: Using these notebooks is not enough to learn Python and it is necessary to watch the videos presented on the Sabzlearn website. 8 | -------------------------------------------------------------------------------- /Screenshot from 2023-07-24 17-23-24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-07-24 17-23-24.png -------------------------------------------------------------------------------- /Screenshot from 2023-07-24 17-47-30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-07-24 17-47-30.png -------------------------------------------------------------------------------- /Screenshot from 2023-07-25 11-19-48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-07-25 11-19-48.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-02 11-44-48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-02 11-44-48.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-02 18-28-09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-02 18-28-09.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-02 18-28-26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-02 18-28-26.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-02 18-28-47.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-02 18-28-47.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-02 18-29-10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-02 18-29-10.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-16 10-05-20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-16 10-05-20.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-16 10-11-15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-16 10-11-15.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-16 10-50-42.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-16 10-50-42.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-16 10-50-58.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-16 10-50-58.png -------------------------------------------------------------------------------- /Screenshot from 2023-08-16 10-51-13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/Screenshot from 2023-08-16 10-51-13.png -------------------------------------------------------------------------------- /new.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | -------------------------------------------------------------------------------- /sample2.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 2 | -------------------------------------------------------------------------------- /test.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | -------------------------------------------------------------------------------- /test/.ipynb_checkpoints/test-checkpoint.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/test/.ipynb_checkpoints/test-checkpoint.py -------------------------------------------------------------------------------- /test/new.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/test/new.py -------------------------------------------------------------------------------- /test/test2.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHBehroozi/pythonlearning/852ee4255f6e122db5c4c0f062b07b742e83f4ab/test/test2.py -------------------------------------------------------------------------------- /text.txt: -------------------------------------------------------------------------------- 1 | hello!!! --------------------------------------------------------------------------------