├── README.md ├── Section2(if-for-while).ipynb ├── Section3(function).ipynb └── section5(Object Oriented).ipynb /README.md: -------------------------------------------------------------------------------- 1 | # Python_Basic 2 | #Python Section 2 : if,for, while 3 | #Python Section 3 : function 4 | #Python Section 5 :Object Orinted Programming(OOP) 5 | -------------------------------------------------------------------------------- /Section2(if-for-while).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "ee39dcd3", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Positive number\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "num = 1\n", 19 | "if num >0:\n", 20 | " print(\"Positive number\")" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "id": "b52a5493", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "Enter a number: -4\n", 34 | "Negative number\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "#یک عدد از ورودی خوانده و تشخیص دهد مثبت یا منفی است\n", 40 | "\n", 41 | "num = float(input(\"Enter a number: \"))\n", 42 | "\n", 43 | "if num >= 0:\n", 44 | " print(\"Positive number\")\n", 45 | "else:\n", 46 | " print(\"Negative number\")" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "id": "8ac31ab9", 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "Enter a number: 0\n", 60 | "Zero\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "#یک عدد از ورودی خوانده و تشخیص دهد مثبت، صفر یا منفی است\n", 66 | "\n", 67 | "num = float(input(\"Enter a number: \"))\n", 68 | "\n", 69 | "if num > 0:\n", 70 | " print(\"Positive number\")\n", 71 | "elif num == 0:\n", 72 | " print(\"Zero\")\n", 73 | "else:\n", 74 | " print(\"Negative number\")" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 8, 80 | "id": "31eea440", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "please Enter integer number: 3\n", 88 | "Your number is odd\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "#یک عدد از ورودی خوانده و تشخیص دهد زوج یا فرد است\n", 94 | "\n", 95 | "number = int(input(\"please Enter integer number: \"))\n", 96 | " \n", 97 | "if number % 2 == 0:\n", 98 | " print('Your number is even')\n", 99 | "else:\n", 100 | " print('Your number is odd')" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 9, 106 | "id": "1aea9b06", 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "name": "stdout", 111 | "output_type": "stream", 112 | "text": [ 113 | "please Enter integer number1: 1\n", 114 | "please Enter integer number2: 2\n", 115 | "please Enter integer number3: 3\n", 116 | "3\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "#سه عدد از ورودی خوانده و بزرگترین آنها را چاپ کند\n", 122 | "\n", 123 | "n1 = int(input(\"please Enter integer number1: \"))\n", 124 | "n2 = int(input(\"please Enter integer number2: \"))\n", 125 | "n3 = int(input(\"please Enter integer number3: \"))\n", 126 | "\n", 127 | "if n1 >= n2 and n1 >= n3:\n", 128 | " print(n1)\n", 129 | "elif n2 >= n1 and n2 >= n3:\n", 130 | " print(n2)\n", 131 | "else:\n", 132 | " print(n3)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 10, 138 | "id": "16ca4643", 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "please Enter temperature: 23\n", 146 | "nice weather\n", 147 | "go and play outside\n", 148 | "end\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "#دما را از ورودی گرفته و وضعیت آنر را چاپ کند\n", 154 | "\n", 155 | "temperature = int(input(\"please Enter temperature: \"))\n", 156 | "\n", 157 | "if temperature > 30: \n", 158 | " print(\"hot\")\n", 159 | " print(\"drink water\")\n", 160 | "elif temperature > 20: \n", 161 | " print(\"nice weather\")\n", 162 | " print(\"go and play outside\")\n", 163 | "else: # در غیر این صورت\n", 164 | " print(\"cold\")\n", 165 | "print(\"end\")" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 11, 171 | "id": "499f930d", 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "Please Enter a: 1\n", 179 | "Please Enter b: 4\n", 180 | "Please Enter c: 2\n", 181 | "Root is:\n", 182 | " X1= -0.5857864376269049 \n", 183 | "X2= -3.414213562373095\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "#حل معادله درجه ۲\n", 189 | "\n", 190 | "import math\n", 191 | "\n", 192 | "a=int(input(\"Please Enter a: \"))\n", 193 | "b=int(input(\"Please Enter b: \"))\n", 194 | "c=int(input(\"Please Enter c: \"))\n", 195 | "\n", 196 | "delta=b*b-4*a*c\n", 197 | "\n", 198 | "if delta==0 :\n", 199 | " x=(-b + math.sqrt(delta))\n", 200 | " print(\"Root is: X=\",x)\n", 201 | "elif delta<0:\n", 202 | " print(\"No Root!\")\n", 203 | "else : \n", 204 | " x1=(-b + math.sqrt(delta))/(2*a)\n", 205 | " x2=(-b - math.sqrt(delta))/(2*a)\n", 206 | " print(\"Root is:\\n X1= \",x1, \"\\nX2= \" , x2)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 12, 212 | "id": "82124265", 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "please input your number: 10\n", 220 | "The sum is: 55\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "#یک عدد از ورودی خوانده و مجموع اعداد صفر تا آن عدد را محاسبه و چاپ کند\n", 226 | "\n", 227 | "number = int(input(\"please input your number: \"))\n", 228 | "\n", 229 | "sum = 0\n", 230 | "for i in range(number+1):\n", 231 | " sum=sum+i\n", 232 | " \n", 233 | "print(\"The sum is:\",sum)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 13, 239 | "id": "f389cb7b", 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "please input your number: 10\n", 247 | "The sum is: 30\n" 248 | ] 249 | } 250 | ], 251 | "source": [ 252 | "#یک عدد از ورودی خوانده و مجموع اعداد زوج صفر تا آن عدد را محاسبه و چاپ کند\n", 253 | "\n", 254 | "number = int(input(\"please input your number: \"))\n", 255 | "\n", 256 | "sum = 0\n", 257 | "for i in range(0,number+1,2):\n", 258 | " sum=sum+i\n", 259 | " \n", 260 | "print(\"The sum is:\",sum)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": 14, 266 | "id": "c1fe75f3", 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "Enter a Number: 9\n", 274 | "your number is not prime\n" 275 | ] 276 | } 277 | ], 278 | "source": [ 279 | "#یک عدد از ورودی خوانده و مشخص کند اول است یا خیر\n", 280 | "\n", 281 | "n = int(input(\"Enter a Number: \"))\n", 282 | "\n", 283 | "p = 0\n", 284 | "for i in range(2, n):\n", 285 | " if n%i==0:\n", 286 | " p = 1\n", 287 | " break\n", 288 | "\n", 289 | "if p==0:\n", 290 | " print(\"your number is prime\")\n", 291 | "else:\n", 292 | " print(\"your number is not prime\")" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 15, 298 | "id": "bfe44f23", 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "name": "stdout", 303 | "output_type": "stream", 304 | "text": [ 305 | "Enter the number: 8\n", 306 | "1\n", 307 | "1\n", 308 | "2\n", 309 | "3\n", 310 | "5\n", 311 | "8\n", 312 | "13\n", 313 | "21\n" 314 | ] 315 | } 316 | ], 317 | "source": [ 318 | "#یک عدد از ورودی خوانده و دنباله فیبوناچی تا آن عدد را محاسبه و چاپ کند\n", 319 | "\n", 320 | "# fibonacci series: z = x + y\n", 321 | "# 1 1 2 3 5 8 13 21\n", 322 | "# x y z →\n", 323 | "# x y z →\n", 324 | "# x y z →\n", 325 | "# x y z →\n", 326 | "\n", 327 | "num = int(input(\"Enter the number: \"))\n", 328 | "\n", 329 | "x = 1\n", 330 | "y = 1\n", 331 | "for i in range(num):\n", 332 | " print(x)\n", 333 | " z = x + y\n", 334 | " x = y\n", 335 | " y = z" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 16, 341 | "id": "8e442824", 342 | "metadata": {}, 343 | "outputs": [ 344 | { 345 | "name": "stdout", 346 | "output_type": "stream", 347 | "text": [ 348 | "1 \n", 349 | "2 2 \n", 350 | "3 3 3 \n", 351 | "4 4 4 4 \n" 352 | ] 353 | } 354 | ], 355 | "source": [ 356 | "#ساختار عددی زیر را چاپ کند\n", 357 | "#1 \n", 358 | "#2 2 \n", 359 | "#3 3 3 \n", 360 | "#4 4 4 4\n", 361 | "\n", 362 | "for i in range(1, 5):\n", 363 | " for j in range(i):\n", 364 | " print(i, end=' ')\n", 365 | " print()" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 17, 371 | "id": "bafc471c", 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "name": "stdout", 376 | "output_type": "stream", 377 | "text": [ 378 | "Enter the number of rows: 5\n", 379 | "*****\n", 380 | "****\n", 381 | "***\n", 382 | "**\n", 383 | "*\n" 384 | ] 385 | } 386 | ], 387 | "source": [ 388 | "#ساختار شکلی زیر را چاپ کند\n", 389 | "#*****\n", 390 | "#****\n", 391 | "#***\n", 392 | "#**\n", 393 | "#*\n", 394 | "\n", 395 | "num_rows = int(input(\"Enter the number of rows: \"))\n", 396 | "\n", 397 | "for i in range(num_rows):\n", 398 | " for num_cols in range(num_rows-i):\n", 399 | " print(\"*\", end=\"\")\n", 400 | " print()" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 18, 406 | "id": "a60fc94f", 407 | "metadata": {}, 408 | "outputs": [ 409 | { 410 | "name": "stdout", 411 | "output_type": "stream", 412 | "text": [ 413 | "Enter the number: 10\n", 414 | "The sum is 25\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "#یک عدد از ورودی خوانده و مجموع اعداد فرد یک تا آن عدد را محاسبه و چاپ کند\n", 420 | "\n", 421 | "num = int(input(\"Enter the number: \"))\n", 422 | "\n", 423 | "sum = 0\n", 424 | "i = 1\n", 425 | "while i <= num:\n", 426 | " sum = sum + i\n", 427 | " i = i+2 \n", 428 | "print(\"The sum is\",sum)" 429 | ] 430 | }, 431 | { 432 | "cell_type": "code", 433 | "execution_count": 19, 434 | "id": "501d60c6", 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "name": "stdout", 439 | "output_type": "stream", 440 | "text": [ 441 | "Enter the number: 10\n", 442 | "The sum is: 30\n" 443 | ] 444 | } 445 | ], 446 | "source": [ 447 | "#یک عدد از ورودی خوانده و مجموع اعداد زوج صفر تا آن عدد را محاسبه و چاپ کند(با حلقه while , if)\n", 448 | "\n", 449 | "num = int(input(\"Enter the number: \"))\n", 450 | "\n", 451 | "sum = 0\n", 452 | "i = 1\n", 453 | "while i <= num:\n", 454 | " if i%2==0:\n", 455 | " sum = sum + i\n", 456 | " i = i+1\n", 457 | "print(\"The sum is:\",sum)" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 20, 463 | "id": "fbf2eb1c", 464 | "metadata": {}, 465 | "outputs": [ 466 | { 467 | "name": "stdout", 468 | "output_type": "stream", 469 | "text": [ 470 | "Enter the number: 8\n", 471 | "1\n", 472 | "1\n", 473 | "2\n", 474 | "3\n", 475 | "5\n", 476 | "8\n" 477 | ] 478 | } 479 | ], 480 | "source": [ 481 | "#یک عدد از ورودی خوانده و دنباله فیبوناچی تا آن عدد را محاسبه و چاپ کند\n", 482 | "\n", 483 | "num = int(input(\"Enter the number: \"))\n", 484 | "\n", 485 | "a, b = 1, 1\n", 486 | "while a <= num:\n", 487 | " print(a)\n", 488 | " a, b = b, a + b" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": null, 494 | "id": "fce1e1e3", 495 | "metadata": {}, 496 | "outputs": [], 497 | "source": [] 498 | } 499 | ], 500 | "metadata": { 501 | "kernelspec": { 502 | "display_name": "Python 3 (ipykernel)", 503 | "language": "python", 504 | "name": "python3" 505 | }, 506 | "language_info": { 507 | "codemirror_mode": { 508 | "name": "ipython", 509 | "version": 3 510 | }, 511 | "file_extension": ".py", 512 | "mimetype": "text/x-python", 513 | "name": "python", 514 | "nbconvert_exporter": "python", 515 | "pygments_lexer": "ipython3", 516 | "version": "3.11.4" 517 | } 518 | }, 519 | "nbformat": 4, 520 | "nbformat_minor": 5 521 | } 522 | -------------------------------------------------------------------------------- /Section3(function).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "231baf5c", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "14\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# تعریف و پیاده سازی تابع بدون خروجی \n", 19 | "\n", 20 | "def function():\n", 21 | " a = 14\n", 22 | " print(a)\n", 23 | " \n", 24 | "# فراخوانی تابع \n", 25 | "function()" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "id": "7fa97235", 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "name": "stdout", 36 | "output_type": "stream", 37 | "text": [ 38 | "power is: 81\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "# تعریف و پیاده سازی تابع با خروجی\n", 44 | "\n", 45 | "def power():\n", 46 | " c=x**y\n", 47 | " return c\n", 48 | "\n", 49 | "x=3\n", 50 | "y=4\n", 51 | "z=power()\n", 52 | "\n", 53 | "print(\"power is:\",z)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "id": "c5f285c5", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "power is: 81\n" 67 | ] 68 | } 69 | ], 70 | "source": [ 71 | "# فراخوانی تابع در دستور چاپ\n", 72 | "\n", 73 | "print(\"power is:\",power())" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 4, 79 | "id": "5a9ff95a", 80 | "metadata": {}, 81 | "outputs": [ 82 | { 83 | "ename": "NameError", 84 | "evalue": "name 'c' is not defined", 85 | "output_type": "error", 86 | "traceback": [ 87 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 88 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 89 | "Cell \u001b[1;32mIn[4], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# دسترسی نداشتن در خارج تابع به متغیرهای داخل تابع(متغیرهای محلی)\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpower is:\u001b[39m\u001b[38;5;124m\"\u001b[39m,c)\n", 90 | "\u001b[1;31mNameError\u001b[0m: name 'c' is not defined" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "# دسترسی نداشتن در خارج تابع به متغیرهای داخل تابع(متغیرهای محلی)\n", 96 | "\n", 97 | "print(\"power is:\",c)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 5, 103 | "id": "5e7faded", 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "30\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "# ارسال مقادیر صریح به تابع\n", 116 | "\n", 117 | "def myFunction(a, b):\n", 118 | " c = a + b\n", 119 | " return c\n", 120 | "\n", 121 | "x = myFunction(10, 20)\n", 122 | "print(x) " 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 6, 128 | "id": "0b60e8dd", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "16\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "# برگرداندن صریح مقدار از تابع\n", 141 | "\n", 142 | "def square(x):\n", 143 | " return x*x\n", 144 | "\n", 145 | "print(square(4))" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 7, 151 | "id": "436ee6ad", 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "1 2 3\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "# ارسال آرگوانها هنگام فراخوانی بدون رعایت ترتیب پارامترهای تابع \n", 164 | "\n", 165 | "def function(a, b, c):\n", 166 | " print(a, b, c)\n", 167 | " \n", 168 | "function(a=1, c=3, b=2)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 8, 174 | "id": "b5987cdd", 175 | "metadata": {}, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "(1,)\n" 182 | ] 183 | } 184 | ], 185 | "source": [ 186 | "# مشخص نبودن تعداد آرگومانها\n", 187 | "\n", 188 | "def function(*name):\n", 189 | " print(name)\n", 190 | "\n", 191 | "# ارسال یک آرکومان\n", 192 | "function(1)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 9, 198 | "id": "681220f3", 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "(1, 2, 3)\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "#ارسال ۳ آرگومان \n", 211 | "\n", 212 | "function(1, 2, 3)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "id": "80b06e53", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "30\n", 226 | "35\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "# استفاده مجدد از تابع \n", 232 | "\n", 233 | "def sum(a, b): \n", 234 | " return a + b\n", 235 | "\n", 236 | "total=sum(10, 20) \n", 237 | "print(total)\n", 238 | "\n", 239 | "total=sum(5, sum(10, 20))\n", 240 | "print(total)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 11, 246 | "id": "e002c8cd", 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "The value of pi is 3.141592653589793\n" 254 | ] 255 | } 256 | ], 257 | "source": [ 258 | "# استفاده از تابع کتابخانه ای ریاضی\n", 259 | "#روش اول\n", 260 | "\n", 261 | "import math\n", 262 | "\n", 263 | "print(\"The value of pi is\", math.pi)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 12, 269 | "id": "a739429b", 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "The value of pi is 3.141592653589793\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "# استفاده از تابع کتابخانه ای ریاضی\n", 282 | "#روش دوم\n", 283 | "\n", 284 | "import math as m\n", 285 | "print(\"The value of pi is\", m.pi)" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 13, 291 | "id": "af3e138e", 292 | "metadata": {}, 293 | "outputs": [ 294 | { 295 | "name": "stdout", 296 | "output_type": "stream", 297 | "text": [ 298 | "The value of pi is 3.141592653589793\n" 299 | ] 300 | } 301 | ], 302 | "source": [ 303 | "# استفاده از تابع کتابخانه ای ریاضی\n", 304 | "#روش سوم\n", 305 | "\n", 306 | "from math import pi\n", 307 | "print(\"The value of pi is\", pi)" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 14, 313 | "id": "5a756ca9", 314 | "metadata": {}, 315 | "outputs": [ 316 | { 317 | "name": "stdout", 318 | "output_type": "stream", 319 | "text": [ 320 | "enter your number:5\n", 321 | "Your number is odd\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "# تابع تشخیص زوج یا فرد عدد\n", 327 | "\n", 328 | "def odd_even(num):\n", 329 | " if num % 2 == 0:\n", 330 | " print('Your number is even')\n", 331 | " else:\n", 332 | " print('Your number is odd')\n", 333 | " \n", 334 | "\n", 335 | "n= int(input('enter your number:')) \n", 336 | "odd_even(n)" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 15, 342 | "id": "c43d87d3", 343 | "metadata": {}, 344 | "outputs": [ 345 | { 346 | "name": "stdout", 347 | "output_type": "stream", 348 | "text": [ 349 | "please Enter first number: 5\n", 350 | "please Enter second number: 3\n", 351 | "The Max Number: 5\n" 352 | ] 353 | } 354 | ], 355 | "source": [ 356 | "# چاپ عدد بزرگ بین دو عدد\n", 357 | "\n", 358 | "def max(a,b):\n", 359 | " if (a >= b):\n", 360 | " return a\n", 361 | " else:\n", 362 | " return b\n", 363 | " \n", 364 | "number1 = int(input(\"please Enter first number: \"))\n", 365 | "number2 = int(input(\"please Enter second number: \"))\n", 366 | "\n", 367 | "print(\"The Max Number:\", max(number1,number2))" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 16, 373 | "id": "efefe06e", 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "please Enter integer number1: 2\n", 381 | "please Enter integer number2: 6\n", 382 | "please Enter integer number3: 4\n", 383 | "6\n" 384 | ] 385 | } 386 | ], 387 | "source": [ 388 | "# چاپ عدد بزرگ بین سه عدد\n", 389 | "\n", 390 | "def largest(a, b, c):\n", 391 | " \"\"\"Return the largest of a, b, and c.\"\"\"\n", 392 | " if a >= b and a >= c:\n", 393 | " return a\n", 394 | " elif b >= a and b >= c:\n", 395 | " return b\n", 396 | " return c\n", 397 | "\n", 398 | "number1 = int(input(\"please Enter integer number1: \"))\n", 399 | "number2 = int(input(\"please Enter integer number2: \")) \n", 400 | "number3 = int(input(\"please Enter integer number3: \")) \n", 401 | "\n", 402 | "print (largest(number1, number2,number3))" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 17, 408 | "id": "783fea45", 409 | "metadata": {}, 410 | "outputs": [ 411 | { 412 | "name": "stdout", 413 | "output_type": "stream", 414 | "text": [ 415 | "12\n" 416 | ] 417 | } 418 | ], 419 | "source": [ 420 | "#ماشین حساب چهار عمل اصلی\n", 421 | "\n", 422 | "def calculator(num1,op,num2):\n", 423 | " if op=='+':\n", 424 | " return num1+num2\n", 425 | " elif op=='-':\n", 426 | " return num1-num2\n", 427 | " elif op=='*':\n", 428 | " return num1*num2\n", 429 | " elif op=='/':\n", 430 | " return num1/num2\n", 431 | " else:\n", 432 | " return 'your operator is wrong! '\n", 433 | "\n", 434 | "result=calculator(10,'+',2)\n", 435 | "print(result)" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 18, 441 | "id": "a84ebda3", 442 | "metadata": {}, 443 | "outputs": [ 444 | { 445 | "name": "stdout", 446 | "output_type": "stream", 447 | "text": [ 448 | "Enter the number: 10\n", 449 | "The sum is: 55\n" 450 | ] 451 | } 452 | ], 453 | "source": [ 454 | "#محاسبه مجموع اعداد یک تا عدد ورودی\n", 455 | "\n", 456 | "def sum_1_n(n):\n", 457 | " sum = 0\n", 458 | " i = 1\n", 459 | " while i <= n:\n", 460 | " sum = sum + i\n", 461 | " i = i+1\n", 462 | " return sum\n", 463 | "\n", 464 | "num = int(input(\"Enter the number: \"))\n", 465 | "s=sum_1_n(num)\n", 466 | "print(\"The sum is:\",s)" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 23, 472 | "id": "68f287c0", 473 | "metadata": {}, 474 | "outputs": [ 475 | { 476 | "name": "stdout", 477 | "output_type": "stream", 478 | "text": [ 479 | "Enter the number: ۱۰\n", 480 | "The sum is: 30\n" 481 | ] 482 | } 483 | ], 484 | "source": [ 485 | "#محاسبه مجموع اعداد زوج صفر تا عدد ورودی\n", 486 | "\n", 487 | "def sum_even_1_n(n):\n", 488 | " sum = 0\n", 489 | " i = 1\n", 490 | " while i <= n:\n", 491 | " if i%2==0:\n", 492 | " sum = sum + i\n", 493 | " i = i+1\n", 494 | " return sum\n", 495 | "\n", 496 | "num = int(input(\"Enter the number: \"))\n", 497 | "s=sum_even_1_n(num)\n", 498 | "print(\"The sum is:\",s)" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 24, 504 | "id": "6731da17", 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "enter your number:10\n" 512 | ] 513 | }, 514 | { 515 | "data": { 516 | "text/plain": [ 517 | "25" 518 | ] 519 | }, 520 | "execution_count": 24, 521 | "metadata": {}, 522 | "output_type": "execute_result" 523 | } 524 | ], 525 | "source": [ 526 | "#مجموع اعداد فرد یک تا عدد ورودی \n", 527 | "\n", 528 | "def sum_odd_1_n(n):\n", 529 | " sum = 0\n", 530 | " for i in range(1,n+1):\n", 531 | " if i%2!=0 :\n", 532 | " sum += i \n", 533 | " return sum \n", 534 | "\n", 535 | "n= int(input('enter your number:'))\n", 536 | "sum_odd_1_n(n)" 537 | ] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": 25, 542 | "id": "419060cc", 543 | "metadata": {}, 544 | "outputs": [ 545 | { 546 | "name": "stdout", 547 | "output_type": "stream", 548 | "text": [ 549 | "enter your number:10\n" 550 | ] 551 | }, 552 | { 553 | "data": { 554 | "text/plain": [ 555 | "10.0" 556 | ] 557 | }, 558 | "execution_count": 25, 559 | "metadata": {}, 560 | "output_type": "execute_result" 561 | } 562 | ], 563 | "source": [ 564 | "# محاسبه میانگین تعداد نامشخصی از اعداد ارسالی به تابع \n", 565 | "\n", 566 | "def average( *vals ):\n", 567 | " total = 0\n", 568 | " for val in vals:\n", 569 | " total += val\n", 570 | " return total/len(vals)\n", 571 | "\n", 572 | "n= int(input('enter your number:'))\n", 573 | "\n", 574 | "# ارسال یک عدد به تابع\n", 575 | "average(n)" 576 | ] 577 | }, 578 | { 579 | "cell_type": "code", 580 | "execution_count": 27, 581 | "id": "7a28ff54", 582 | "metadata": {}, 583 | "outputs": [ 584 | { 585 | "data": { 586 | "text/plain": [ 587 | "3.0" 588 | ] 589 | }, 590 | "execution_count": 27, 591 | "metadata": {}, 592 | "output_type": "execute_result" 593 | } 594 | ], 595 | "source": [ 596 | "# ارسال سه عدد به تابع\n", 597 | "\n", 598 | "average(1, 2, 3, 4, 5)" 599 | ] 600 | }, 601 | { 602 | "cell_type": "code", 603 | "execution_count": 28, 604 | "id": "bd2461e5", 605 | "metadata": {}, 606 | "outputs": [ 607 | { 608 | "name": "stdout", 609 | "output_type": "stream", 610 | "text": [ 611 | "please Enter integer number: 5\n", 612 | "The factorial of 5 is: 120\n" 613 | ] 614 | } 615 | ], 616 | "source": [ 617 | "# محاسبه فاکتوریل\n", 618 | "# روش اول\n", 619 | "\n", 620 | "def fact(n):\n", 621 | " factorial = 1\n", 622 | " for i in range(1, n+1):\n", 623 | " factorial = factorial * i\n", 624 | " print(\"The factorial of {} is: {}\".format(number, factorial))\n", 625 | "\n", 626 | "number = int(input(\"please Enter integer number: \")) \n", 627 | "fact(number)" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 29, 633 | "id": "9d4b289e", 634 | "metadata": {}, 635 | "outputs": [ 636 | { 637 | "name": "stdout", 638 | "output_type": "stream", 639 | "text": [ 640 | "please Enter integer number: 5\n", 641 | "The factorial of 5 is: 120\n" 642 | ] 643 | } 644 | ], 645 | "source": [ 646 | "# محاسبه فاکتوریل\n", 647 | "# روش دوم\n", 648 | "\n", 649 | "def factorial(n):\n", 650 | " if n == 0:\n", 651 | " return 1\n", 652 | " else:\n", 653 | " return n * factorial(n-1)\n", 654 | " \n", 655 | "number = int(input(\"please Enter integer number: \"))\n", 656 | "result = factorial(number)\n", 657 | "\n", 658 | "print(\"The factorial of {} is: {}\".format(number, result))" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 30, 664 | "id": "1df7b25e", 665 | "metadata": {}, 666 | "outputs": [ 667 | { 668 | "name": "stdout", 669 | "output_type": "stream", 670 | "text": [ 671 | "please Enter integer number: 5\n", 672 | "The factorial of 5 is: 120\n" 673 | ] 674 | } 675 | ], 676 | "source": [ 677 | "# محاسبه فاکتوریل\n", 678 | "# روش سوم\n", 679 | "\n", 680 | "import math\n", 681 | "\n", 682 | "number = int(input(\"please Enter integer number: \"))\n", 683 | "result = math.factorial(number)\n", 684 | "print(\"The factorial of {} is: {}\".format(number, result))" 685 | ] 686 | }, 687 | { 688 | "cell_type": "code", 689 | "execution_count": 31, 690 | "id": "061d4b9d", 691 | "metadata": {}, 692 | "outputs": [ 693 | { 694 | "name": "stdout", 695 | "output_type": "stream", 696 | "text": [ 697 | "please Enter integer number: 5\n", 698 | "The factorial of 5 is: 120\n" 699 | ] 700 | } 701 | ], 702 | "source": [ 703 | "# محاسبه فاکتوریل\n", 704 | "# روش چهارم\n", 705 | "\n", 706 | "import math\n", 707 | "\n", 708 | "number = int(input(\"please Enter integer number: \"))\n", 709 | "if number < 0:\n", 710 | " print(\"Sorry, factorial does not exist for negative numbers\")\n", 711 | "else:\n", 712 | " result = math.factorial(number)\n", 713 | " print(\"The factorial of {} is: {}\".format(number, result))" 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": 32, 719 | "id": "ea462c4b", 720 | "metadata": {}, 721 | "outputs": [ 722 | { 723 | "name": "stdout", 724 | "output_type": "stream", 725 | "text": [ 726 | "please input your number: 9\n", 727 | "your number is not prime\n" 728 | ] 729 | } 730 | ], 731 | "source": [ 732 | "# تشخیص عدد اول\n", 733 | "# روش اول\n", 734 | "\n", 735 | "def prime(n):\n", 736 | " accumulator = 0\n", 737 | " for i in range(1, number+1):\n", 738 | " if number%i == 0:\n", 739 | " accumulator+=1\n", 740 | " if accumulator==2:\n", 741 | " print('your number is prime')\n", 742 | " else:\n", 743 | " print('your number is not prime')\n", 744 | " \n", 745 | "number = int(input('please input your number: '))\n", 746 | "prime(number)" 747 | ] 748 | }, 749 | { 750 | "cell_type": "code", 751 | "execution_count": 33, 752 | "id": "9e550d6b", 753 | "metadata": {}, 754 | "outputs": [ 755 | { 756 | "name": "stdout", 757 | "output_type": "stream", 758 | "text": [ 759 | "please input your number: 9\n" 760 | ] 761 | }, 762 | { 763 | "data": { 764 | "text/plain": [ 765 | "False" 766 | ] 767 | }, 768 | "execution_count": 33, 769 | "metadata": {}, 770 | "output_type": "execute_result" 771 | } 772 | ], 773 | "source": [ 774 | "# تشخیص عدد اول\n", 775 | "# روش دوم\n", 776 | "\n", 777 | "def prime(n):\n", 778 | " divisor = 2\n", 779 | " while divisor*divisor <= n:\n", 780 | " if n % divisor == 0:\n", 781 | " return False\n", 782 | " divisor += 1\n", 783 | " return True\n", 784 | "\n", 785 | "number = int(input('please input your number: '))\n", 786 | "prime(number)" 787 | ] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "execution_count": 34, 792 | "id": "7ba15a49", 793 | "metadata": {}, 794 | "outputs": [ 795 | { 796 | "name": "stdout", 797 | "output_type": "stream", 798 | "text": [ 799 | "Enter a Number: 9\n", 800 | "\n", 801 | "9 is not a Prime Number\n" 802 | ] 803 | } 804 | ], 805 | "source": [ 806 | "# تشخیص عدد اول\n", 807 | "# روش سوم\n", 808 | "\n", 809 | "def checkPrime(x):\n", 810 | " for i in range(2, x):\n", 811 | " if n%i==0:\n", 812 | " return 1\n", 813 | "\n", 814 | "print(\"Enter a Number: \", end=\"\")\n", 815 | "n = int(input())\n", 816 | "\n", 817 | "p = checkPrime(n)\n", 818 | "\n", 819 | "if p==1:\n", 820 | " print(\"\\n\" +str(n)+ \" is not a Prime Number\")\n", 821 | "else:\n", 822 | " print(\"\\n\" +str(n)+ \" is a Prime Number\")" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": 35, 828 | "id": "c58d03c8", 829 | "metadata": {}, 830 | "outputs": [ 831 | { 832 | "name": "stdout", 833 | "output_type": "stream", 834 | "text": [ 835 | "please Enter integer number: 10\n", 836 | "55\n" 837 | ] 838 | } 839 | ], 840 | "source": [ 841 | "# مجموع اعداد یک تا عدد ورودی بصورت بازگشتی \n", 842 | "\n", 843 | "def my_sum(n):\n", 844 | " if(n==1):\n", 845 | " return 1\n", 846 | " else:\n", 847 | " return n + my_sum(n-1)\n", 848 | " \n", 849 | "number = int(input(\"please Enter integer number: \"))\n", 850 | "print((my_sum(number)))" 851 | ] 852 | }, 853 | { 854 | "cell_type": "code", 855 | "execution_count": 36, 856 | "id": "1e07846c", 857 | "metadata": {}, 858 | "outputs": [ 859 | { 860 | "name": "stdout", 861 | "output_type": "stream", 862 | "text": [ 863 | "please Enter integer number1: 2\n", 864 | "please Enter integer number2: 3\n", 865 | "8\n" 866 | ] 867 | } 868 | ], 869 | "source": [ 870 | "#تابع توان بصورت بازگشتی \n", 871 | "\n", 872 | "def my_pow(n, m):\n", 873 | " if(m==1):\n", 874 | " return n\n", 875 | " elif(m==0):\n", 876 | " return 1\n", 877 | " else:\n", 878 | " return n * my_pow(n, m-1)\n", 879 | " \n", 880 | "number1 = int(input(\"please Enter integer number1: \"))\n", 881 | "number2 = int(input(\"please Enter integer number2: \")) \n", 882 | "\n", 883 | "print (my_pow(number1, number2))" 884 | ] 885 | }, 886 | { 887 | "cell_type": "code", 888 | "execution_count": 37, 889 | "id": "a3f979c4", 890 | "metadata": {}, 891 | "outputs": [ 892 | { 893 | "name": "stdout", 894 | "output_type": "stream", 895 | "text": [ 896 | "please Enter integer number: 5\n", 897 | "8\n" 898 | ] 899 | } 900 | ], 901 | "source": [ 902 | "#تابع فیبوناچی بصورت بازگشتی \n", 903 | "\n", 904 | "def fib(n):\n", 905 | " if n==0 or n==1:\n", 906 | " return 1\n", 907 | " else:\n", 908 | " return fib(n-1)+fib(n-2)\n", 909 | "\n", 910 | "number = int(input(\"please Enter integer number: \"))\n", 911 | "print((fib(number)))" 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 38, 917 | "id": "b322a3f9", 918 | "metadata": {}, 919 | "outputs": [ 920 | { 921 | "name": "stdout", 922 | "output_type": "stream", 923 | "text": [ 924 | "Enter the number of Row: 6\n", 925 | "******\n", 926 | "*****\n", 927 | "****\n", 928 | "***\n", 929 | "**\n", 930 | "*\n" 931 | ] 932 | } 933 | ], 934 | "source": [ 935 | "# چاپ الگوی زیر \n", 936 | "#*****\n", 937 | "#****\n", 938 | "#***\n", 939 | "#**\n", 940 | "#*\n", 941 | "\n", 942 | "def print_pattern(num):\n", 943 | " for i in range(num):\n", 944 | " for j in range(num-i):\n", 945 | " print(\"*\", end=\"\")\n", 946 | " print()\n", 947 | " \n", 948 | "num_rows = int(input(\"Enter the number of Row: \")) \n", 949 | "print_pattern(num_rows)" 950 | ] 951 | }, 952 | { 953 | "cell_type": "code", 954 | "execution_count": 39, 955 | "id": "d4bde048", 956 | "metadata": {}, 957 | "outputs": [ 958 | { 959 | "name": "stdout", 960 | "output_type": "stream", 961 | "text": [ 962 | "Enter the number of Row: 6\n", 963 | "Enter Pattern Character: #\n", 964 | "######\n", 965 | "#####\n", 966 | "####\n", 967 | "###\n", 968 | "##\n", 969 | "#\n" 970 | ] 971 | } 972 | ], 973 | "source": [ 974 | "# چاپ الگوی بر اساس کاراکتر و تعداد سطرهای ورودی\n", 975 | "\n", 976 | "def print_pattern(num,char):\n", 977 | " for i in range(num):\n", 978 | " for j in range(num-i):\n", 979 | " print(char, end=\"\")\n", 980 | " print()\n", 981 | " \n", 982 | "num_rows = int(input(\"Enter the number of Row: \")) \n", 983 | "char_pattern=input(\"Enter Pattern Character: \")\n", 984 | "\n", 985 | "print_pattern(num_rows,char_pattern) " 986 | ] 987 | }, 988 | { 989 | "cell_type": "code", 990 | "execution_count": 40, 991 | "id": "c25a93da", 992 | "metadata": {}, 993 | "outputs": [ 994 | { 995 | "name": "stdout", 996 | "output_type": "stream", 997 | "text": [ 998 | "Sum of numbers 0\n", 999 | "Sum of numbers 5\n" 1000 | ] 1001 | } 1002 | ], 1003 | "source": [ 1004 | "# مجموع اعداد فرد لیست ارسالی \n", 1005 | "\n", 1006 | "def print_sum_even_nums(even_nums):\n", 1007 | " total = 0\n", 1008 | "\n", 1009 | " for x in even_nums:\n", 1010 | " if x % 2 != 0:\n", 1011 | " total = total +x\n", 1012 | " print(f'Sum of numbers {total}')\n", 1013 | "\n", 1014 | "\n", 1015 | "print_sum_even_nums([2, 4, 6, 8])\n", 1016 | "\n", 1017 | "print_sum_even_nums([2, 4, 5, 8])" 1018 | ] 1019 | } 1020 | ], 1021 | "metadata": { 1022 | "kernelspec": { 1023 | "display_name": "Python 3 (ipykernel)", 1024 | "language": "python", 1025 | "name": "python3" 1026 | }, 1027 | "language_info": { 1028 | "codemirror_mode": { 1029 | "name": "ipython", 1030 | "version": 3 1031 | }, 1032 | "file_extension": ".py", 1033 | "mimetype": "text/x-python", 1034 | "name": "python", 1035 | "nbconvert_exporter": "python", 1036 | "pygments_lexer": "ipython3", 1037 | "version": "3.11.4" 1038 | } 1039 | }, 1040 | "nbformat": 4, 1041 | "nbformat_minor": 5 1042 | } 1043 | -------------------------------------------------------------------------------- /section5(Object Oriented).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 28, 6 | "id": "83ed5fba", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | "Area: 18.84\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "#۳محاسبه محیط دایره به شعاع \n", 19 | "\n", 20 | "class cycle:\n", 21 | " r=3\n", 22 | " def area(self):\n", 23 | " a=2*3.14*self.r\n", 24 | " print(\"Area:\",a)\n", 25 | "\n", 26 | "c=cycle()\n", 27 | "c.area()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 29, 33 | "id": "8e0756f6", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "enter redius:3\n", 41 | "Area: 18.84\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "#محاسبه محیط دایره\n", 47 | "\n", 48 | "class cycle:\n", 49 | " def readr(self):\n", 50 | " self.r=float(input(\"enter redius:\"))\n", 51 | " \n", 52 | " def area(self):\n", 53 | " self.a=2*3.14*self.r\n", 54 | " print(\"Area:\",self.a)\n", 55 | "\n", 56 | "c=cycle()\n", 57 | "c.readr()\n", 58 | "c.area()" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 30, 64 | "id": "761df082", 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "enter r:3\n", 72 | "Area: 18.84\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "# محاسبه محیط دایره با سازنده\n", 78 | "\n", 79 | "class cycle:\n", 80 | " def __init__(self,y):\n", 81 | " self.r=y\n", 82 | " \n", 83 | " def area(self):\n", 84 | " self.a=2*3.14*self.r\n", 85 | " print(\"Area:\",self.a)\n", 86 | "\n", 87 | "x=float(input(\"enter r:\"))\n", 88 | "c=cycle(x)\n", 89 | "c.area()\n" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 27, 95 | "id": "72c0bcf1", 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "<__main__.Circle object at 0x00000292E6AB0A90>\n", 103 | "13.854423602330987\n", 104 | "2.1\n", 105 | "<__main__.Circle object at 0x00000292E6B9E8D0>\n", 106 | "3.141592653589793\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "# محاسبه محیط دایره با سازنده- روش دوم\n", 112 | "\n", 113 | "from math import pi\n", 114 | " \n", 115 | "class Circle: \n", 116 | " \n", 117 | " def __init__(self, r=1):\n", 118 | " self.radius = r # Create an instance variable radius\n", 119 | " \n", 120 | " def get_area(self):\n", 121 | " \"\"\"Return the area of this Circle instance\"\"\"\n", 122 | " return self.radius * self.radius * pi\n", 123 | " \n", 124 | "c1 = Circle(2.1) # Construct an instance\n", 125 | "print(c1) \n", 126 | "print(c1.get_area()) # 13.854423602330987\n", 127 | "print(c1.radius) # 2.1\n", 128 | "\n", 129 | "c2 = Circle() # Default radius\n", 130 | "print(c2)\n", 131 | "print(c2.get_area()) # Invoke member method\n", 132 | " \n", 133 | " " 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 20, 139 | "id": "c8be017e", 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "name": "stdout", 144 | "output_type": "stream", 145 | "text": [ 146 | "I can eat!\n", 147 | "I can sleep!\n", 148 | "I can bark! Woof woof!!\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "#ارث بری ساده \n", 154 | "\n", 155 | "# base class\n", 156 | "class Animal:\n", 157 | " \n", 158 | " def eat(self):\n", 159 | " print( \"I can eat!\")\n", 160 | " \n", 161 | " def sleep(self):\n", 162 | " print(\"I can sleep!\")\n", 163 | "\n", 164 | "# derived class\n", 165 | "class Dog(Animal):\n", 166 | " \n", 167 | " def bark(self):\n", 168 | " print(\"I can bark! Woof woof!!\")\n", 169 | "\n", 170 | "# Create object of the Dog class\n", 171 | "dog1 = Dog()\n", 172 | "\n", 173 | "# Calling members of the base class\n", 174 | "dog1.eat()\n", 175 | "dog1.sleep()\n", 176 | "\n", 177 | "# Calling member of the derived class\n", 178 | "dog1.bark(); \n", 179 | " \n", 180 | " \n", 181 | " " 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 22, 187 | "id": "b4bf7f69", 188 | "metadata": {}, 189 | "outputs": [ 190 | { 191 | "name": "stdout", 192 | "output_type": "stream", 193 | "text": [ 194 | "I can eat!\n", 195 | "I can sleep!\n", 196 | "I can bark! Woof woof!!\n", 197 | "I can eat!\n", 198 | "Cat is meowing...\n" 199 | ] 200 | } 201 | ], 202 | "source": [ 203 | "#ارث بری دو فرزندی \n", 204 | "\n", 205 | "# base class\n", 206 | "class Animal:\n", 207 | " \n", 208 | " def eat(self):\n", 209 | " print( \"I can eat!\")\n", 210 | " \n", 211 | " def sleep(self):\n", 212 | " print(\"I can sleep!\")\n", 213 | "\n", 214 | "# derived class\n", 215 | "class Dog(Animal):\n", 216 | " def bark(self):\n", 217 | " print(\"I can bark! Woof woof!!\")\n", 218 | " \n", 219 | "class Cat(Animal):\n", 220 | " def speak(self):\n", 221 | " print(\"Cat is meowing...\")\n", 222 | " \n", 223 | " \n", 224 | "# Create object of the Dog class\n", 225 | "dog1 = Dog()\n", 226 | "\n", 227 | "# Calling members of the base class\n", 228 | "dog1.eat()\n", 229 | "dog1.sleep()\n", 230 | "\n", 231 | "# Calling member of the derived class\n", 232 | "dog1.bark(); \n", 233 | "\n", 234 | "# Create object of the Cat class\n", 235 | "cat1=Cat()\n", 236 | "\n", 237 | "cat1.eat()\n", 238 | "cat1.speak()\n", 239 | "\n", 240 | " " 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 23, 246 | "id": "ac5dfae5", 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "name": "stdout", 251 | "output_type": "stream", 252 | "text": [ 253 | "Animal is speaking...\n", 254 | "I can eat!\n", 255 | "Dog is barking...\n", 256 | "Cat is meowing...\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "#چند ریختی\n", 262 | "#Polymorphism\n", 263 | "\n", 264 | "class Animal:\n", 265 | " def eat(self):\n", 266 | " print( \"I can eat!\")\n", 267 | " \n", 268 | " def speak(self):\n", 269 | " print(\"Animal is speaking...\")\n", 270 | "\n", 271 | "class Dog(Animal):\n", 272 | " def speak(self):\n", 273 | " print(\"Dog is barking...\")\n", 274 | "\n", 275 | "class Cat(Animal):\n", 276 | " def speak(self):\n", 277 | " print(\"Cat is meowing...\")\n", 278 | "\n", 279 | "# Create objects of the different classes\n", 280 | "animal = Animal()\n", 281 | "dog = Dog()\n", 282 | "cat = Cat()\n", 283 | "\n", 284 | "# Call the speak() method on each object\n", 285 | "animal.speak() # Output: Animal is speaking...\n", 286 | "animal.eat() # Output:I can eat!\n", 287 | "\n", 288 | "dog.speak() # Output: Dog is barking...\n", 289 | "cat.speak() # Output: Cat is meowing...\n", 290 | "cat.eat() # Output:I can eat!" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 24, 296 | "id": "5090c006", 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "name": "stdout", 301 | "output_type": "stream", 302 | "text": [ 303 | "6 8\n" 304 | ] 305 | } 306 | ], 307 | "source": [ 308 | "#سربارگذاری عملگر + برای جمع دو نقطه دو بعدی\n", 309 | "#Operator Overloading\n", 310 | "\n", 311 | "class Point:\n", 312 | " def __init__(self, x, y):\n", 313 | " self.x = x\n", 314 | " self.y = y\n", 315 | "\n", 316 | " def __add__(self, point):\n", 317 | " x = self.x + point.x\n", 318 | " y = self.y + point.y\n", 319 | " return Point(x, y)\n", 320 | "\n", 321 | "p1 = Point(4, 5)\n", 322 | "p2 = Point(2, 3)\n", 323 | "\n", 324 | "p3 = p1 + p2\n", 325 | "# p3 is (6, 8)\n", 326 | "print(p3.x,p3.y)" 327 | ] 328 | } 329 | ], 330 | "metadata": { 331 | "kernelspec": { 332 | "display_name": "Python 3 (ipykernel)", 333 | "language": "python", 334 | "name": "python3" 335 | }, 336 | "language_info": { 337 | "codemirror_mode": { 338 | "name": "ipython", 339 | "version": 3 340 | }, 341 | "file_extension": ".py", 342 | "mimetype": "text/x-python", 343 | "name": "python", 344 | "nbconvert_exporter": "python", 345 | "pygments_lexer": "ipython3", 346 | "version": "3.11.4" 347 | } 348 | }, 349 | "nbformat": 4, 350 | "nbformat_minor": 5 351 | } 352 | --------------------------------------------------------------------------------