در این مطلب، ویدئو ًںگچ Python 101: یادگیری اصول پایتون برای مبتدیان مطلق [دوره کامل] با زیرنویس فارسی را برای دانلود قرار داده ام. شما میتوانید با پرداخت 15 هزار تومان ، این ویدیو به علاوه تمامی فیلم های سایت را دانلود کنید.اکثر فیلم های سایت به زبان انگلیسی می باشند. این ویدئو دارای زیرنویس فارسی ترجمه شده توسط هوش مصنوعی می باشد که میتوانید نمونه ای از آن را در قسمت پایانی این مطلب مشاهده کنید.
مدت زمان فیلم: 3:06:59
تصاویر این ویدئو:
قسمتی از زیرنویس این فیلم:
00:00:00,000 –> 00:00:05,840
سلام به همه ، و به Digital Academy ، آموزش کامل توسعه پایتون برای مبتدیان
2
00:00:05,840 –> 00:00:12,080
، خوش آمدید . به Python A to Z ، دوره کامل توسعه پایتون برای مبتدیان خوش آمدید.
3
00:00:13,120 –> 00:00:16,880
در کل این دوره ، شما آموزش های گام به گام را می گذرانید:
4
00:00:17,840 –> 00:00:22,480
پوشش تنظیمات محیط خود ، مفاهیم و ویژگی های اساسی پایتون ، بلکه
5
00:00:22,480 –> 00:00:27,920
برخی کارهای پیچیده تر و پیشرفته تر ، برای تبدیل شدن به یک توسعه دهنده پایتون. شما خواهید یافت
6
00:00:27,920 –> 00:00:34,800
و یاد خواهید گرفت: متغیرها و انواع داده ها ، دستورات شرطی و حلقه ها ، توابع ، لیست ها و فرهنگ لغت ها ،
7
00:00:34,800 –> 00:00:41,360
ورودی ها و خروجی ها ، پرونده ها ، کلاس ها و اشیا. مدیریت ماژول ها و بسته ها ، خطاها و استثناها
8
00:00:41,360 –> 00:00:48,640
. محیط مجازی ، رشته ، پایگاه داده ، عبارات منظم ، برنامه های وب و شبکه ،
9
00:00:48,640 –> 00:00:54,960
تجزیه و تحلیل داده های علمی ، هوش مصنوعی و یادگیری ماشین ، بلکه برنامه های رابط کاربر گرافیکی
10
00:00:54,960 –> 00:01:00,240
و موارد دیگر. همه چیز برای شخصی که می خواهد کدنویسی در پایتون را یاد بگیرد مفید است.
11
00:01:00,880 –> 00:01:04,879
چه در برنامه نویسی تازه کار باشید و چه یک توسعه دهنده باتجربه که می خواهد
12
00:01:04,879 –> 00:01:11,360
زبان جدیدی یاد بگیرد و مهارت های خود را افزایش دهد ، یادگیری و استفاده از پایتون آسان است. بنابراین ، این دوره برای
13
00:01:11,360 –> 00:01:18,480
دانشجویان ، کارمندان و هر کسی که می خواهد برنامه نویسی را شروع کند یا به احتمال زیاد می خواهد زبان
14
00:01:18,480 –> 00:01:24,960
پایتون را یاد بگیرد ، اختصاص دارد ، اما کاملاً به دانش برنامه نویسی قبلی نیازی نیست. در پایان این دوره ،
15
00:01:24,960 –> 00:01:29,520
شاید بتوانید برخی از کارهای خود را در زندگی روزمره خودکار کنید. حتی موارد دشوارتر:
16
00:01:30,320 –> 00:01:34,800
از برخی اسکریپت های بسیار ابتدایی ، بنابراین می توانید اوقات فراغت بیشتری برای خود و خانواده خود داشته باشید ،
17
00:01:35,440 –> 00:01:41,760
یا تماشای یک وب سایت برای هرگونه تغییر ، سازماندهی فیلم های خود ، حتی امور مالی شخصی خود را مدیریت کنید.
18
00:01:41,760 –> 00:01:47,360
علاوه بر تخیل شما محدودیتی وجود ندارد. دوست دارید در کمترین زمان به این هدف برسید؟
19
00:01:47,360 –> 00:01:53,840
به خاطر داشته باشید که شما بیش از هر چیز باید با ریتم خود یاد بگیرید – با نظم و تمرین.
20
00:01:59,040 –> 00:02:04,080
آیا آماده یادگیری پایتون هستید؟ بیایید شروع کنیم: اکنون ثبت نام کنید و بخشی از این سفر باشید.
21
00:02:05,920 –> 00:02:08,400
آکادمی دیجیتال ، اکنون ثبت نام کنید ، رایگان یاد بگیرید.
22
00:02:10,479 –> 00:02:16,480
لطفا مشترک شوید. زنگ اعلان را بزنید ، لایک کنید ، نظر دهید و به اشتراک بگذارید.
23
00:02:20,320 –> 00:02:26,240
در این فیلم اول از یک سریال طولانی ، خواهید فهمید که پایتون چیست و برخی از ویژگی ها یا مزایای
24
00:02:26,240 –> 00:02:31,600
آن چیست . اما همچنین چرا این محبوبیت پیدا کرده است ، مخصوصاً در این چند سال اخیر.
25
00:02:32,320 –> 00:02:38,400
پایتون برای چه استفاده می شود؟ فرصت های شغلی شما چیست؟ و سرانجام: چه چیزی یاد بگیریم؟
26
00:02:38,400 –> 00:02:44,240
از کجا شروع کنیم؟ آیا می خواهید در برخی از این س answersالات پاسخ پیدا کنید؟ بیایید این ویدئو را پخش کنیم و شروع کنیم
27
00:02:44,240 –> 00:02:50,480
با: پایتون چیست؟ پایتون یک زبان برنامه نویسی عمومی است که به راحتی قابل یادگیری است و به
28
00:02:50,480 –> 00:02:57,280
صورت پویا شروع می شود. این یک نحو آسان و زیبا دارد ، با معنایی سطح بالا و پویا – که
29
00:02:57,280 –> 00:03:04,000
آن را به یک زبان ایده آل برای مبتدیان ، اسکریپت نویسی و توسعه سریع برنامه تبدیل می کند. پایتون برای اولین بار
30
00:03:04,000 –> 00:03:10,880
در سال 1991 منتشر شد و توسط Guido Van Rossum ، برنامه نویس هلندی معروف به نویسنده
31
00:03:10,880 –> 00:03:16,400
زبان برنامه نویسی پایتون که می خواست زبانی بسیار ساده برای نوشتن و خواندن آسان طراحی کند ، ایجاد شد.
32
00:03:17,200 –> 00:03:22,160
بنابراین یادگیری پایتون بسیار آسان تر از سایر زبان های برنامه نویسی است و به شما کمک می کند
33
00:03:22,160 –> 00:03:28,720
با تلاش کمتری برنامه های زیبایی ایجاد کنید . اما شما تعجب می کنید که پایتون چه ویژگی ها و مزایایی را
34
00:03:28,720 –> 00:03:35,040
ارائه می دهد؟ بسیاری از ویژگی های ارائه شده وجود دارد از جمله مزایای یادگیری پایتون ، به ویژه به عنوان
35
00:03:35,040 –> 00:03:42,240
زبان اول شما . بیایید با مزایای یادگیری پایتون شروع کنیم. آسان: پایتون بهترین زبان
36
00:03:42,240 –> 00:03:48,400
برای شروع و زبان ترجیحی مبتدیان و متخصصان است. نحو آسان و ویژگی های سطح بالا
37
00:03:48,400 –> 00:03:53,280
آن را به یکی از بهترین زبان ها برای هر کسی که می خواهد شروع به برنامه نویسی کند ، می کند.
38
00:03:54,080 –> 00:04:00,480
رایگان: پایتون یک زبان منبع باز است و این بدان معناست که استفاده از آن برای همه رایگان است. می تواند
39
00:04:00,480 –> 00:04:07,360
آزادانه اصلاح و توزیع شود و به هر منظور ، تجاری یا غیر. کاربرد: از پایتون می توان
40
00:04:07,360 –> 00:04:15,200
تقریباً برای توسعه هر چیزی استفاده کرد: برنامه های دسک تاپ ، موبایل و وب. به علاوه ، در بسیاری از زمینه ها
41
00:04:15,200 –> 00:04:22,480
بسیار مفید است: تجزیه و تحلیل داده ها ، هوش مصنوعی ، یادگیری ماشین و غیره. کتابخانه و پشتیبانی:
42
00:04:22,480 –> 00:04:28,240
به لطف منبع باز بودن و توسعه یافتگی جامعه ، پایتون دارای پایگاه پشتیبانی گسترده ای است.
43
00:04:28,240 –> 00:04:34,320
میلیون ها توسعه دهنده همفکر روزانه با این زبان کار می کنند و آنها همچنان عملکرد اصلی خود را
44
00:04:34,320 –> 00:04:40,800
بهبود می بخشند ، که باعث می شود پایتون حتی بهتر و کارآمدتر شود. بنابراین ، این یک روش عالی برای
45
00:04:40,800 –> 00:04:47,600
برقراری ارتباط با توسعه دهندگان دیگر ، به اشتراک گذاشتن و توسعه مهارت های شماست. اکنون می توانید درک کنید که چرا مردم
46
00:04:47,600 –> 00:04:54,720
جهان اینقدر پایتون را دوست دارند! اما چه ویژگی هایی دارد؟ سادگی: پایتون زبانی است که به
47
00:04:54,720 –> 00:04:59,840
راحتی قابل یادگیری است ، به این معنی که اجرای برنامه های شما بسیار راحت تر است –
48
00:05:00,560 –> 00:05:05,360
زیرا به یک نحو ساده زیبا و مبتدی نیاز دارد که بر روی خوانایی تمرکز دارد
49
00:05:05,360 –> 00:05:10,800
. خواندن برنامه هایی که می نویسید بسیار آسان تر است ، حتی اگر یک برنامه نویس ماهر نباشید.
50
00:05:11,520 –> 00:05:16,640
این بیشتر به این دلیل است که به زبان انسان یا نحوه نوشتن کلمات نزدیکتر است – به جای
51
00:05:16,640 –> 00:05:22,960
زبانی که کامپیوترها برای خواندن آن استفاده می کنند. توسعه دهندگان می توانند کد پایتون را بسیار آسان تر
52
00:05:22,960 –> 00:05:29,360
از سایر زبانها بخوانند و ترجمه کنند. به نوبه خود ، این هزینه تعمیر و نگهداری و توسعه برنامه را کاهش می دهد زیرا
53
00:05:29,360 –> 00:05:34,560
به تیم ها امکان می دهد تا به طور مشترک و بدون زبان و موانع قابل توجه کار کنند!
54
00:05:35,760 –> 00:05:41,120
تفسیر شده: پایتون از چند الگوی برنامه نویسی پشتیبانی می کند ، از جمله: برنامه ریزی رویه ای ،
55
00:05:41,120 –> 00:05:47,680
شی گرا و کاربردی. از همه مهمتر ، پایتون یک زبان تفسیر شده است ،
56
00:05:47,680 –> 00:05:52,880
به این معنی که کد نوشته شده در واقع به یک فرمت قابل خواندن رایانه ترجمه نمی شود.
57
00:05:53,680 –> 00:05:59,120
کدهای پایتون به صورت خط به خط اجرا می شوند ، که خطاهای اشکال زدایی را بسیار آسان و کارآمد می کند.
58
00:05:59,760 –> 00:06:04,560
اما این هزینه دارد ، زیرا بسیار کندتر از سایر زبانهای برنامه نویسی کامپایل شده است.
59
00:06:05,520 –> 00:06:11,440
شی گرا: برنامه نویسی شی گرا روشی است که برای مدلسازی موجودات بتن و
60
00:06:11,440 –> 00:06:16,240
دنیای واقعی به اشیا software نرم افزاری استفاده می شود که دارای برخی داده ها با آنها هستند
61
00:06:16,240 –> 00:06:22,320
و می توانند عملکردهای خاصی را انجام دهند. به عنوان مثال ، یک شی object می تواند نمایانگر شخصی با: نام
62
00:06:22,320 –> 00:06:29,440
سن ، آدرس و غیره و رفتارهایی مانند: راه رفتن ، صحبت کردن یا دویدن باشد. هدف شی گرا
63
00:06:29,440 –> 00:06:35,280
آن کمک به برنامه نویسان برای نوشتن کد روشن و منطقی ، برای پروژه های کوچک و بزرگ است.
64
00:06:36,240 –> 00:06:42,320
قابلیت حمل و نقل: به دلیل وجود پایتون ، هیچ انحصاری وجود ندارد و همه ابزارهای لازم در همه
65
00:06:42,320 –> 00:06:48,480
سیستم عامل های اصلی در دسترس هستند . با یک لیست طولانی از رایانه ها و سیستم عامل ها سازگار است ، مانند:
66
00:06:48,480 –> 00:06:55,920
ویندوز ، لینوکس ، macOS و غیره … علاوه بر این ، از یک رابط مشابه در هر یک از آن سیستم عامل ها استفاده می کند ، به
67
00:06:55,920 –> 00:07:02,800
این معنی که می توانید به راحتی بین آنها بپرید ، و اگر لازم قابل جاسازی و توسعه: پایتون می
68
00:07:02,800 –> 00:07:07,920
تواند در یک برنامه تعبیه شود ، که یک رابط قابل برنامه ریزی برای کاربران آن برنامه فراهم می کند.
69
00:07:08,640 –> 00:07:13,500
اگر برنامه ای را برای کار با پایتون در یک ترمینال آماده کرده اید ، این یک ویژگی عالی است
70
00:07:13,500 –> 00:07:18,300
. علاوه بر این ، پایتون از استفاده از ماژول ها و بسته ها پشتیبانی می کند ،
71
00:07:18,300 –> 00:07:25,521
به این معنی که برنامه می تواند به سبک مدولار طراحی شود و کد در پروژه های مختلف مورد استفاده مجدد قرار گیرد.
72
00:07:26,320 –> 00:07:30,720
حتی بهتر ، هنگامی که ماژول یا بسته مورد نیاز خود را ایجاد کردید
73
00:07:30,720 –> 00:07:36,160
، می تواند برای استفاده در پروژه های دیگر نیز به اشتراک گذاشته و مقیاس بندی شود. وارد کردن یا صادر کردن
74
00:07:36,880 –> 00:07:43,120
کتابخانه بسیار آسان است . پایتون با کتابخانه استاندارد پایتون ارائه می شود ، پشتیبانی یکپارچه برای انواع
75
00:07:43,120 –> 00:07:49,040
وظایف معمول برنامه نویسی مانند: همگام سازی با سرورهای وب ، جستجو در متن و تغییر پرونده ها را ارائه می دهد …
76
00:07:49,840 –> 00:07:56,000
برای اکثر زبان های دیگر شما باید این محتوا را از ابتدا ایجاد کنید. پایتون همچنین دارای
77
00:07:56,000 –> 00:08:00,960
یک انجمن شگفت انگیز است که تعدادی ابزار مفید اضافی ساخته است و شما می توانید از آنها در
78
00:08:00,960 –> 00:08:08,000
برنامه های خود استفاده کنید … آیا نیاز به کار با اسناد PDF دارید؟ یک ابزار جامع برای آن وجود دارد. می خواهید
79
00:08:08,000 –> 00:08:15,120
از صفحات وب داده جمع آوری کنید؟ نیازی به شروع از ابتدا نیست ، پایتون قدرت جنگو: یک چارچوب برنامه وب منبع
80
00:08:15,120 –> 00:08:20,480
باز و کامل است که می تواند برای ساده سازی و سرعت بخشیدن به روند توسعه استفاده شود.
81
00:08:21,680 –> 00:08:27,120
منبع باز: به لطف منبع باز بودن و توسعه یافتگی جامعه ،
82
00:08:27,120 –> 00:08:33,280
پایتون دارای یک پایگاه پشتیبانی گسترده است . میلیون ها توسعه دهنده همفکر روزانه با این زبان کار می کنند.
83
00:08:34,080 –> 00:08:39,919
و آنها همچنان به بهبود عملکرد اصلی می پردازند. آخرین نسخه پایتون همچنان پیشرفت ها و به روزرسانی ها را
84
00:08:39,919 –> 00:08:45,200
دریافت می کند . بنابراین ، این یک روش عالی برای برقراری ارتباط با توسعه دهندگان دیگر ،
85
00:08:45,200 –> 00:08:51,280
به اشتراک گذاشتن و توسعه مهارت های شماست. یکی از امیدوار کننده ترین مزایای پایتون این است که مفسر
86
00:08:51,280 –> 00:08:56,800
پایتون و کتابخانه استاندارد گسترده به صورت رایگان و بصورت باینری – برای همه سیستم عامل های بزرگ
87
00:08:56,800 –> 00:09:02,640
آزادانه در دسترس هستند . می توان آن را از وب سایت پایتون بارگیری کرد ، و ممکن است
88
00:09:02,640 –> 00:09:09,280
آزادانه برای هر هدفی اصلاح شود و توزیع شود – تجاری یا غیر. از آنجایی که پایتون تحت مجوز منبع باز
89
00:09:09,280 –> 00:09:14,640
در دسترس است ، علی رغم اینکه دارای حق چاپ است ، هیچ هزینه مجوزی نیز دریافت نمی شود.
90
00:09:15,280 –> 00:09:20,400
همین سایت همچنین شامل توزیع و اشاره گرهای بسیاری از
91
00:09:20,400 –> 00:09:26,320
ماژول های رایگان پایتون شخص ثالث و همچنین برنامه ها و ابزارها است. اما همچنین مستندات اضافی و غیره.
92
00:09:27,200 –> 00:09:33,120
اگر این توصیف پایتون بالای سر شماست ، نگران نباشید: به زودی آن را درک خواهید کرد!
93
00:09:33,120 –> 00:09:37,520
آنچه واقعاً باید از این بخشها بردارید این است که پایتون یک زبان برنامه نویسی
94
00:09:37,520 –> 00:09:44,000
است که برای توسعه نرم افزارها و برنامه های وب از جمله موبایل استفاده می شود. یادگیری آن نسبتاً آسان است ،
95
00:09:44,000 –> 00:09:50,160
زیرا ابزار و مستندات لازم برای همه بصورت رایگان در دسترس است. این باعث می شود که
96
00:09:50,160 –> 00:09:56,000
پایتون تقریباً برای همه قابل دسترسی باشد. اگر وقت یادگیری دارید می توانید چیزهای شگفت انگیزی ایجاد کنید!
97
00:09:56,800 –> 00:10:02,000
بنابراین ، می توان آن را به عنوان پایه ای برای ورود به سایر زبانها و چارچوبهای برنامه نویسی مورد استفاده قرار داد.
98
00:10:02,800 –> 00:10:08,000
هر کسی می تواند کار با این زبان را شروع کند. کافی است کمی صبر ، و تمرین زیاد باشد.
99
00:10:08,880 –> 00:10:13,040
اگر یک مبتدی مطلق هستید و اولین بار است که با هر نوع
100
00:10:13,040 –> 00:10:19,760
زبان برنامه نویسی کار می کنید ، قطعاً پایتون چیزی است که می خواهید. اما ، چرا پایتون اینقدر محبوب شده است؟
101
00:10:20,480 –> 00:10:26,240
به دلیل همه ویژگی ها و مزایای آن ، ممکن است شنیده باشید که مار پیتون محبوب است. از یک
102
00:10:26,240 –> 00:10:31,120
طرف ، ممکن است به نظر برسد که چقدر محبوبیت یک زبان مهم نیست ، اگر بتوانید
103
00:10:31,120 –> 00:10:37,120
برنامه ای را که می خواهید با آن بسازید ، بسازید. اما خوب یا بد ، در توسعه نرم افزار محبوبیت
104
00:10:37,120 –> 00:10:44,300
شاخصی از کیفیت بالای کتابخانه های موجود و همچنین تعداد فرصت های شغلی است.
105
00:10:44,300 –> 00:10:48,300
آیا پایتون در واقع آنقدر محبوب است؟ آره! و بسیاری از آمارها برای پشتیبانی این یکی وجود دارد.
106
00:10:48,300 –> 00:10:56,301
بیایید برخی از تجزیه و تحلیل های موجود را که توسط stackoverflow.com ارائه شده است بررسی کنیم.
107
00:10:56,301 –> 00:11:02,700
آنها با نام “StackOverflow Trends” اجرا می شوند. در اینجا می توانید روندهای مربوط به فناوری های مختلف توسط Tag را بررسی کنید.
108
00:11:02,720 –> 00:11:07,440
وقتی پایتون را با نامزدهای احتمالی دیگری که می توانید برای یادگیری برنامه نویسی انتخاب کنید مقایسه می کنیم ،
109
00:11:07,440 –> 00:11:12,320
یکی را غیر از دیگران می بینید! به رشد خارق العاده پایتون
110
00:11:12,320 –> 00:11:18,160
در مقایسه با خط ثابت یا حتی روند نزولی سایر نامزدهای معمول توجه کنید … اگر در آینده خود
111
00:11:18,160 –> 00:11:22,800
بر روی موفقیت یک فناوری معین شرط می بندید ، کدام یک را از این لیست انتخاب می کنید؟
112
00:11:23,520 –> 00:11:28,320
فهمیدی! پایتون چیزهای زیادی برای ارائه دارد و شما به زودی می توانید اسکریپت های خود را بنویسید.
113
00:11:29,280 –> 00:11:36,320
پایتون برای چه استفاده می شود؟ در واقع ، تقریباً هر چیزی که بخواهید: بیشتر اتوماسیون ، داده کاوی و
114
00:11:36,320 –> 00:11:43,840
سیستم عامل های داده بزرگ به پایتون متکی هستند. از پایتون همچنین می توان برای پردازش متن ، نمایش اعداد یا تصاویر ،
115
00:11:43,840 –> 00:11:50,560
حتی حل معادلات علمی و صرفه جویی در داده ها استفاده کرد. به طور خلاصه ، از آن در پشت صحنه برای پردازش
116
00:11:50,560 –> 00:11:56,000
بسیاری از عناصری که ممکن است در دستگاه هایتان به آنها نیاز داشته باشید یا با آنها روبرو شوید استفاده می شود. این به این دلیل است که این
117
00:11:56,000 –> 00:12:02,960
زبان ایده آل برای کار برای کارهای عمومی است. به عنوان مثال ، برای برنامه های GUI ،
118
00:12:02,960 –> 00:12:08,960
توسعه بازی ها ، خراش داده ها از وب سایت ها ، توسعه وب و نرم افزار ، ابزارهای
119
00:12:08,960 –> 00:12:16,400
مدیریت سیستم ، تجزیه و تحلیل داده ها ، یادگیری ماشین – و موارد دیگر استفاده می شود. زبان برنامه نویسی پایتون که
120
00:12:16,400 –> 00:12:22,080
به آن زبان اسکریپت نویسی نیز گفته می شود ، در این چند سال اخیر بطور قابل توجهی تغییر کرده است. زیرا از پایتون
121
00:12:22,080 –> 00:12:28,800
به جای استفاده از برنامه های پیش پا افتاده ، اکنون برای نوشتن برنامه های بزرگ به سبک تجاری استفاده می شود. بنابراین ، این اعتماد به
122
00:12:28,800 –> 00:12:35,360
پایتون حتی بیشتر شده است ، بنابراین با محبوبیت اینترنت! نه تنها پایتون یک زبان
123
00:12:35,360 –> 00:12:40,800
دوستانه و سرگرم کننده برای یادگیری است ، بلکه فناوری پشت چندین شرکت در کلاس جهانی را نیز تقویت می کند!
124
00:12:41,600 –> 00:12:47,760
در واقع ، اکثر برنامه ها و سیستم عامل های وب اکنون به پایتون متکی هستند ، از جمله:
125
00:12:47,760 –> 00:12:53,040
موتور جستجوی گوگل ، بستر Youtube و سیستم معاملات وب گرا در بورس اوراق بهادار
126
00:12:53,040 –> 00:12:59,120
نیویورک … شما می دانید که زبان باید بسیار جدی باشد ، نوبت به تأمین انرژی
127
00:12:59,120 –> 00:13:06,320
سیستم بورس می رسد. در واقع ، بسیاری از شرکت های بزرگ دیگر نیز وجود دارند ، از جمله: Pinterest ، Instagram ، Netflix ،
128
00:13:06,320 –> 00:13:14,320
Spotify ، Disney ، Yahoo ، Nokia ، IBM ، و بسیاری دیگر – فقط چند مورد را نام ببرید. احتمالاً از خود می پرسید که چرا هر یک
129
00:13:14,320 –> 00:13:19,280
از این موارد مهم است؟ این بدان دلیل است که با یادگیری پایتون ، هرگز در استفاده از مهارت خود
130
00:13:19,280 –> 00:13:24,880
کمبود نخواهید داشت . ناگفته نماند: از آنجا که بسیاری از شرکت های بزرگ به زبان اعتماد دارند ،
131
00:13:24,880 –> 00:13:32,080
شما می توانید به عنوان یک توسعه دهنده پایتون درآمد خوبی کسب کنید. فرصت های شغلی. در این دنیای پویا که
132
00:13:32,080 –> 00:13:38,800
همه چیز با سرعت زیادی تغییر می کند ، محبوبیت پایتون هرگز متوقف نمی شود. امروزه ، صدور گواهینامه پایتون
133
00:13:38,800 –> 00:13:44,640
در کل دامنه برنامه نویسی یکی از مهارت های مورد توجه است. آیا تا به حال فکر کرده اید
134
00:13:44,640 –> 00:13:50,480
که آیا واقعاً ارزش آن را دارد؟ پایتون ، به عنوان یک زبان برنامه نویسی ، یادگیری آن آسان و ساده است.
135
00:13:51,360 –> 00:13:56,880
Ppython با ترکیب ساده خواندن و ترکیب آسان ، زمان توسعه را به نصف می رساند.
136
00:13:57,760 –> 00:14:03,680
همچنین ، کتابخانه های زیادی دارد که از تجزیه و تحلیل داده ها ، دستکاری و تجسم پشتیبانی می کنند.
137
00:14:04,480 –> 00:14:09,440
بنابراین این زبان به عنوان مطلوب ترین زبان تکامل یافته است و به عنوان نکته مهم بعدی
138
00:14:09,440 –> 00:14:15,840
و ضروری برای متخصصان در نظر گرفته می شود . در پایان سال 2019 ، پایتون در حال ارائه تعداد قابل توجهی بود …
139
00:14:16,720 –> 00:14:22,960
یک تحقیق اخیر.com واقعا حاکی از دست کم 55000 شغل پایتون در ایالات متحده ،
140
00:14:22,960 –> 00:14:29,600
با ارقام حقوق و دستمزد. شما می توانید پیشنهادات شغلی زیادی را در بازار پیدا کنید. اکثر
141
00:14:29,600 –> 00:14:36,320
شرکت ها به دنبال مشخصات زیر هستند که ممکن است به مهارت های پایتون نیاز داشته باشد: مهندس نرم افزار ،
142
00:14:36,320 –> 00:14:42,000
توسعه دهنده پایتون ، تحلیلگر تحقیق ، تحلیلگر داده ، دانشمند داده ، توسعه دهنده نرم افزار …
143
00:14:43,120 –> 00:14:47,840
آیا اکنون آماده یادگیری پایتون هستید؟ اما شما نمی دانید که چگونه شروع به کار کنید ، یا حتی چه چیزی
144
00:14:47,840 –> 00:14:53,440
یاد بگیرید؟ این که آیا شما تازه وارد برنامه نویسی هستید یا یک توسعه دهنده نرم افزار حرفه ای هستید ، به دنبال غرق
145
00:14:53,440 –> 00:14:58,480
شدن در یک زبان جدید هستید ، این دوره به شما کلیه اصول پایتون را که برای شروع کار
146
00:14:58,480 –> 00:15:05,920
در پروژه های شخصی خود نیاز دارید ، آموزش می دهد : انواع ، اپراتورها ، شرطی ها ، روش ها
147
00:15:05,920 –> 00:15:12,400
و توابع ، رسیدگی به پرونده ها ، oop و موارد دیگر … مهم نیست که اهداف نهایی شما چیست: اگر
148
00:15:12,400 –> 00:15:17,040
کلاً با رایانه کار کنید ، به زودی راه های بی پایان برای بهبود زندگی خود پیدا خواهید کرد ، با
149
00:15:17,040 –> 00:15:24,720
خودکار کردن کارها و حل مشکلات برنامه های پایتون که ایجاد خواهید کرد! بیایید اکنون تمرین کنیم …
150
00:15:24,720 –> 00:15:31,280
بیایید شروع به کار کنیم: پایتون را روی رایانه یا رایانه خود نصب کنید ، سپس محیط خود را تنظیم کنید تا بتوانید
151
00:15:31,280 –> 00:15:39,840
یادگیری را شروع کنید . و مهمتر از همه ، تمرین! Getting python: Python یک نرم افزار منبع باز رایگان است که
152
00:15:39,840 –> 00:15:46,720
روی ویندوز ، لینوکس ، مک و سیستم عامل های مختلف دیگر کار می کند. بسیاری از سیستم عامل ها مانند Mac
153
00:15:46,720 –> 00:15:52,640
OS و Linux با نصب پیش از پایتون ارائه می شوند. نسخه همراه سیستم عامل شما
154
00:15:52,640 –> 00:15:59,200
تقریباً همیشه قدیمی است و حتی ممکن است نصب کامل پایتون نباشد. بنابراین ، ضروری است
155
00:15:59,200 –> 00:16:04,080
که جدیدترین نسخه پایتون را داشته باشید تا بتوانید در کل دوره توسعه پایتون برای مبتدیان ،
156
00:16:04,080 –> 00:16:09,840
با موفقیت مثالها را دنبال کنید . اما ، شما گیج شده اید ، زیرا
157
00:16:09,840 –> 00:16:13,920
نسخه های مختلفی از پایتون وجود دارد و شما حتی نمی دانید کدام یک را انتخاب کنید؟
158
00:16:14,720 –> 00:16:20,000
نگران نباشید ، این ویدئو به شما کمک می کند تا پایتون ، به علاوه ویرایشگر مورد علاقه جدید خود را بارگیری و نصب کنید.
159
00:16:21,120 –> 00:16:27,120
انتخاب نسخه پایتون. دو نسخه اصلی از پایتون موجود است: پایتون 2 ، همچنین به عنوان پایتون قدیمی
160
00:16:27,120 –> 00:16:34,000
شناخته می شود ، و پایتون 3. این می تواند برای برنامه نویسان جدید و فصلی گیج کننده باشد ، در مورد تصمیم گیری
161
00:16:34,000 –> 00:16:40,400
بین پایتون 2 یا 3. خوشبختانه ، تفاوت های نحوی کمی بین دو مورد از آنها ،
162
00:16:41,200 –> 00:16:46,960
و شما می توانید حتی هر دو نسخه را در رایانه خود اجرا کنید. برای چک کردن اینکه آیا پایتون است در حال حاضر
163
00:16:46,960 –> 00:16:51,840
بر روی دستگاه شما، و یا نسخه فعلی خود را نصب، باز کردن ترمینال و دستور زیر را
164
00:16:52,720 –> 00:16:58,720
اجرا: پایتون -v ‘اگر شما پاسخ از پایتون را ببینید، برخی از اطلاعات بر روی صفحه نمایش ظاهر می شود،
165
00:16:58,720 –> 00:17:04,160
از جمله: word python ، به دنبال شماره نسخه و اطلاعات ساخت. پایتون از
166
00:17:04,160 –> 00:17:11,040
قبل نصب شده است ، اما ممکن است به به روزرسانی نیاز داشته باشد. کدام نسخه برای شما مناسب است؟ خوب ، این به شما بستگی دارد!
167
00:17:11,040 –> 00:17:16,400
راستش را بخواهید ، تفاوت های کمی بین این دو وجود دارد که در این مرحله اولیه شما را تحت تأثیر قرار خواهد داد. بنابراین یا
168
00:17:16,400 –> 00:17:22,000
انتخاب کافی است! بعلاوه ، هنگامی که یکی را یاد گرفتید ، یادگیری دیگری دشوار نیست.
169
00:17:22,720 –> 00:17:28,640
به طور کلی ، اگر تازه یادگیری پایتون را شروع کرده اید ، با پایتون 3 همراه باشید. این یک نسخه بهتر شناخته
170
00:17:28,640 –> 00:17:33,840
شده است ، زیرا ویژگی های اضافه شده به همراه بسیاری از رفع اشکالات و چند اصلاح وجود دارد.
171
00:17:34,640 –> 00:17:40,240
با این حال ، پایتون 2 پشتیبانی بسیار بیشتری از کتابخانه های شخص ثالث دارد. اگر می دانید که باید
172
00:17:40,240 –> 00:17:45,840
از کتابخانه ای استفاده کنید که هنوز به نسخه 3 منتقل نشده است ، بهتر است این کار را با پایتون 2 شروع کنید.
173
00:17:46,720 –> 00:17:52,240
با این وجود ، پایتون 2 در سال 2000 منتشر شد و از آن زمان به پایان زندگی خود رسیده است در تاریخ
174
00:17:52,240 –> 00:17:59,760
1 ژانویه 2020. بنابراین این کل دوره فقط به پایتون 3 متمرکز خواهد شد ، که در حال حاضر از نظر به دست آوردن ویژگی ها و قابلیت های جدید
175
00:17:59,760 –> 00:18:05,840
در حال توسعه است. همانطور که گفته شد ، بسیاری از نمونه ها با هر یک از نسخه ها به خوبی کار
176
00:18:05,840 –> 00:18:10,960
خواهند کرد ، زیرا بسیاری از ویژگی ها و به روزرسانی های اضافه شده به پایتون 3
177
00:18:10,960 –> 00:18:19,680
نیز به پایتون 2 اضافه شده اند. چگونه پایتون تهیه کنم؟ نصب پایتون روی ویندوز در
178
00:18:19,680 –> 00:18:25,760
مقایسه با نصب پایتون در linux کمی طولانی تر است. در لینوکس ، به راحتی اجرای
179
00:18:25,760 –> 00:18:31,920
دستور و تنظیم آن آسان است. با ویندوز یا macOS ، مسیری کمی متفاوت طی می شود …
180
00:18:31,920 –> 00:18:37,040
برای شروع کار با پایتون 3 ، باید به مترجم پایتون دسترسی داشته باشید …
181
00:18:37,840 –> 00:18:43,280
خوشبختانه ، چندین روش متداول برای انجام این کار وجود دارد ، صرف نظر از عملکرد شما
182
00:18:43,280 –> 00:18:49,440
سیستم. پایتون را می توانید از طریق وب سایت بنیاد نرم افزار پایتون ، در python.org تهیه کنید.
183
00:18:50,240 –> 00:18:55,040
به طور معمول ، این شامل بارگیری نصب کننده مناسب سیستم عامل شما است:
184
00:18:55,040 –> 00:19:01,760
windows ، linux یا macOS. آن را بر روی دستگاه خود اجرا کنید ، سپس دستورالعمل ها را دنبال کنید ، تا بتوانید
185
00:19:01,760 –> 00:19:07,680
پایتون را به درستی نصب کنید و محیط خود را تنظیم کنید. در ویندوز ، باید مطمئن شوید کادر
186
00:19:07,680 –> 00:19:13,040
«افزودن python3 به مسیر» را علامت گذاری کنید ، بنابراین می توانید اطمینان حاصل کنید که مفسر در
187
00:19:13,040 –> 00:19:19,600
مسیر اجرای شما قرار می گیرد . در غیر این صورت نمی توانید با پایتون تماس بگیرید. اما بسته به سیستم عامل شما
188
00:19:19,600 –> 00:19:25,920
، گزینه های متعددی نیز وجود دارد . در linux ، این یک مدیر بسته فراهم می کند که می تواند برای نصب پایتون اجرا شود. در macOS ،
189
00:19:25,920 –> 00:19:32,080
بهترین راه برای نصب پایتون 3 شامل نصب یک مدیر بسته به نام “homebrew” است.
190
00:19:32,080 –> 00:19:37,280
گزینه دیگر ، اگر از ویندوز 10 استفاده می کنید ، استفاده از قابلیتی به نام ‘windows subsystem for linux’ (WSL) است.
191
00:19:37,280 –> 00:19:43,280
که به شما امکان می دهد یک محیط لینوکس را مستقیماً در ویندوز و بدون
192
00:19:43,280 –> 00:19:50,240
سربار ماشین مجازی اجرا کنید. در سیستم عامل های تلفن همراه ، مانند android و iOS ، می توانید برنامه هایی را نصب کنید
193
00:19:50,240 –> 00:19:56,320
که یک محیط برنامه نویسی پایتون مانند pythonista را فراهم می کنند. این می تواند یک روش عالی برای تمرین
194
00:19:56,320 –> 00:20:02,880
مهارت های برنامه نویسی خود ، در حال حرکت باشد! متناوباً ، چندین وب سایت نیز وجود دارد که به شما امکان می دهد
195
00:20:02,880 –> 00:20:08,640
بدون اینکه چیزی روی رایانه خود نصب کنید ، به مترجم پایتون به صورت آنلاین دسترسی داشته باشید
196
00:20:08,640 –> 00:20:13,360
. اگر نگران بارگیری پایتون بر روی رایانه خود هستید یا به دلایلی
197
00:20:13,360 –> 00:20:20,000
نمی توانید این کار را انجام دهید ، خوش شانس هستید: بنیاد نرم افزار پایتون پوسته پایتون تعاملی را از
198
00:20:20,000 –> 00:20:26,560
پایتون در هرجای دیگر ارائه می دهد. برای استفاده از آن نیازی به نصب ندارید و از طریق python.org/shell قابل دسترسی است.
199
00:20:26,560 –> 00:20:35,840
بسیاری از گزینه های دیگر عبارتند از: پایتون در هر مکان ، reval.it و غیره. پوسته تعاملی پایتون.
200
00:20:35,840 –> 00:20:41,520
اکنون که پایتون را بارگیری و نصب کردید ، بیایید یک بررسی عقلانی سریع انجام دهیم تا اطمینان حاصل شود که همه موارد
201
00:20:41,520 –> 00:20:48,080
به درستی تنظیم شده است. آماده تماشای فیلم بعدی و تمرین ساعت ها هستید؟ محیط توسعه و یادگیری یکپارچه داخلی
202
00:20:48,080 –> 00:20:53,360
پایتون (IDLE) را که به عنوان پوسته پایتون تعاملی نیز شناخته می شود ، باز کنید.
203
00:20:54,160 –> 00:20:59,680
این یکی از واسطه های ارتباط بین مفسر پایتون و شما است.
204
00:20:59,680 –> 00:21:05,040
برای انجام این کار ، فقط ترمینال خود را باز کنید و “python” را تایپ کنید – اگر فقط یک نسخه از پایتون نصب شده دارید.
205
00:21:05,680 –> 00:21:12,480
اگر چندین نسخه از پایتون از قبل وجود داشته باشد ، ممکن است مجبور باشید که python3 را به صورت دستی اجرا کنید.
206
00:21:12,480 –> 00:21:17,600
پوسته پایتون تعاملی باید باز شود و پنجره خط فرمان یا ترمینال شما باید شبیه به این باشد.
207
00:21:18,560 –> 00:21:23,360
ممکن است دو خط اول را دقیقاً همانطور که در بالا نشان داده شده دریافت نکنید ، زیرا همه
208
00:21:23,360 –> 00:21:29,280
رایانه ها ، نسخه ها و حتی ساختهای مختلفی دارند. همانطور که احتمالاً حدس زده اید ، مفسر
209
00:21:29,280 –> 00:21:34,560
نسخه پایتون ، تاریخ ساخت ، کامپایلر مورد استفاده برای ساخت آن ، معماری رایانه
210
00:21:34,560 –> 00:21:40,560
و سیستم عامل را به ما می گوید . این اطلاعات برای رفع اشکال مهم است و برای بررسی نسخه پایتون
211
00:21:40,560 –> 00:21:45,680
بسیار مفید است. مهمترین چیزی که باید به خاطر بسپارید ، این
212
00:21:45,680 –> 00:21:51,920
سه علامت بزرگتر هستند (>>>). آنها نمادی هستند که از شما مقداری ورودی می خواهد. از آنجا که این درخواست
213
00:21:51,920 –> 00:21:56,800
در چند مثال نشان داده شده است ، باید به یاد داشته باشید که این کلمه کلیدی یا مورد خاصی نیست که شما
214
00:21:56,800 –> 00:22:02,320
باید روزانه تایپ کنید. در حالی که هنوز در پوسته پایتون تعاملی هستید ،
215
00:22:02,320 –> 00:22:08,080
ممکن است اولین خط کد خود را اجرا کنید. چاپ (“یادگیری پایتون با آکادمی دیجیتال جالب است”).
216
00:22:09,040 –> 00:22:14,000
پس از وارد کردن کد ، کد شما در حال اجرا و تفسیر است ، بنابراین باید ببینید: یادگیری
217
00:22:14,000 –> 00:22:20,160
پایتون با آکادمی دیجیتال سرگرم کننده است. همانطور که مشاهده می کنید ، یک خط جدید بدون اعلان خارج می شود.
218
00:22:20,880 –> 00:22:25,120
نکته مهم دیگری که باید بدانید این است که اگر دستور با دندانه وارد کنید
219
00:22:25,120 –> 00:22:30,240
، سریع به سه دوره تغییر می کند ، زیرا مفسر پایتون منتظر ورودی بیشتری است … به
220
00:22:30,960 –> 00:22:35,680
طور کلی ، باید مراقب باشید که درخواست را با کد واقعی پایتون اشتباه نگیرید!
221
00:22:36,400 –> 00:22:41,520
و این همه: تبریک می گویم! شما فقط اولین برنامه پایتون خود را نوشتید.
222
00:22:41,520 –> 00:22:46,720
هر بار که وارد یک خط می شوید ، پایتون بلافاصله دستور را اجرا می کند ، و مقدار
223
00:22:46,720 –> 00:22:52,960
بین نقل قول ها را نشان می دهد. پس از اتمام کار ، می توانید برای خروج از پوسته تعاملی پایتون ، “خروج” یا “ترک” را تایپ کنید.
224
00:22:53,680 –> 00:23:01,040
حتی می توانید از کلیدهای میانبر صفحه کلید ctrl + d استفاده کنید ، سپس enter را فشار دهید.
225
00:23:01,040 –> 00:23:07,200
به مرور خط تایپ کردن به پوسته خود برای شما خسته کننده خواهد شد. مانند سایر
226
00:23:07,200 –> 00:23:12,240
زبانهای برنامه نویسی تفسیر شده ، می توانید کد پایتون را از روی یک فایل ذخیره و اجرا کنید.
227
00:23:12,960 –> 00:23:19,120
دو نوع اصلی از فایل های پایتون وجود دارد: متن ساده و فایل های Bytecode.
228
00:23:19,120 –> 00:23:24,000
فایل های متنی ساده فایل های ساده ای هستند که می توانید فقط با یک ویرایشگر ساده متن ساده ایجاد و ویرایش کنید.
229
00:23:24,720 –> 00:23:30,320
به عنوان مثال ، دفترچه یادداشت ، دفترچه یادداشت ++ یا محیط توسعه یکپارچه پایتون.
230
00:23:31,040 –> 00:23:37,920
نوع دیگر پرونده ها: پرونده های bytecode ، فایل های پایتون کامپایل شده هستند … فعلاً نگران این نوع پرونده ها نباشید
231
00:23:37,920 –> 00:23:44,000
، که بعداً در این دوره بحث خواهد شد. نام فایل اسکریپت پایتون با پسوند ‘.py’ پایان می یابد
232
00:23:44,000 –> 00:23:49,920
، و هنگامی که اسکریپت را به تنهایی در اکثر سیستم عامل ها اجرا می کنید ، قابل اجرا خواهد بود
233
00:23:49,920 –> 00:23:57,600
. فایلی به نام ‘hello.py’ ایجاد کنید و کد زیر را در آن بنویسید ، اکنون اسکریپت را با
234
00:23:57,600 –> 00:24:03,360
کلیک روی آن یا با اجرای آن از پوسته سیستم خود اجرا کنید. این اسکریپت باید “سلام جهان از اسکریپت پایتون” را چاپ کند
235
00:24:03,360 –> 00:24:08,720
و “برای ادامه” را فشار دهید. سپس باید مکث کند ، تا زمانی که کلید enter را فشار دهید.
236
00:24:09,520 –> 00:24:16,480
تبریک می گویم ، شما اولین اسکریپت پایتون خود را نوشتید. محیط توسعه یکپارچه شما (IDLE) یا
237
00:24:16,480 –> 00:24:22,000
محیط توسعه و یادگیری یکپارچه ، IDE پیش فرض هنگام نصب پایتون است.
238
00:24:22,640 –> 00:24:27,840
نوشتن کد پایتون با استفاده از IDLE یا حتی پوسته پایتون برای موارد ساده بسیار عالی است.
239
00:24:27,840 –> 00:24:32,800
اما این ابزارها به سرعت پروژه های بزرگتر برنامه نویسی را به چاله های ناامیدکننده تبدیل می کنند …
240
00:24:33,600 –> 00:24:39,520
استفاده از IDE یا حتی فقط از یک ویرایشگر کد اختصاصی مناسب ، کدگذاری را فو می کند. اما کدام یک برای شما بهتر است؟
241
00:24:39,520 –> 00:24:45,840
IDE یا محیط توسعه یکپارچه ، برنامه ای است که به توسعه نرم افزار اختصاص یافته است.
242
00:24:46,640 –> 00:24:51,840
همانطور که از نام آن برمی آید IDE ها چندین ابزار را به طور خاص برای توسعه نرم افزار طراحی می کنند
243
00:24:51,840 –> 00:24:57,840
که معمولاً شامل این موارد هستند: ویرایشگری که برای مدیریت نحو کد ، برجسته سازی ، ایجاد
244
00:24:57,840 –> 00:25:04,000
تورفتگی و تکمیل ، ساخت ، اجرا و اشکال زدایی طراحی شده است. نوعی کنترل منبع …
245
00:25:04,720 –> 00:25:09,680
اکثر IDE ها از زبانهای مختلف برنامه نویسی مختلفی پشتیبانی می کنند و حاوی ویژگیهای بیشتری هستند.
246
00:25:10,480 –> 00:25:16,880
بنابراین می توانند بسیار بزرگ باشند و بارگیری و نصب آنها زمان بر است. همچنین ممکن است برای استفاده صحیح از آنها به دانش
247
00:25:16,880 –> 00:25:22,560
پیشرفته ای نیاز داشته باشید. در مقابل ، یک ویرایشگر کد اختصاصی می تواند به سادگی یک
248
00:25:22,560 –> 00:25:29,600
ویرایشگر متن ، با برجسته سازی نحوی ، اما همچنین دارای قابلیت قالب بندی کد باشد. بیشتر ویرایشگران کد خوب می توانند
249
00:25:29,600 –> 00:25:35,680
کد را اجرا کرده و اشکال زدایی را کنترل کنند. بهترین ها نیز با سیستم های کنترل منبع ارتباط برقرار می کنند.
250
00:25:36,240 –> 00:25:42,000
در مقایسه با IDE ، یک ویرایشگر کد اختصاصی خوب معمولاً کوچکتر و سریعتر است ، اما غالباً
251
00:25:42,000 –> 00:25:48,320
از ویژگی کمتری برخوردار است. بنابراین در محیط برنامه نویسی واقعاً به چه چیزهایی نیاز داریم؟ لیست ویژگی ها از
252
00:25:48,320 –> 00:25:54,640
برنامه ای به برنامه دیگر متفاوت است ، اما یک مجموعه اصلی از ویژگی ها وجود دارد که کدگذاری را آسان تر می کند: پشتیبانی از اشکال زدایی ،
253
00:25:54,640 –> 00:26:00,240
برجسته سازی نحو ، قالب بندی خودکار کد ، ذخیره و بارگیری مجدد پرونده های کد ، اجرای کد از داخل محیط …
254
00:26:00,240 –> 00:26:06,080
البته موارد وجود دارد بسیاری از ویژگی های دیگری که ممکن است بخواهید: کنترل کد منبع ،
255
00:26:06,080 –> 00:26:12,640
مدل برنامه افزودنی ، ساخت و آزمایش ابزار ، کمک به زبان و غیره. اما لیست فوق
256
00:26:12,640 –> 00:26:17,920
همان ویژگی اصلی است که یک محیط ویرایش بسیار خوب باید از آن پشتیبانی کند. با در نظر
257
00:26:17,920 –> 00:26:24,880
داشتن این ویژگی ها ، بیایید نگاهی به برخی از ابزارهای عمومی که می توانید برای توسعه پایتون استفاده کنید: vim ،
258
00:26:24,880 –> 00:26:33,760
دفترچه یادداشت atom ++ ، pycharm ، Gnu emax ، متن عالی ، گرفتگی استودیوی بصری + pydev …
259
00:26:33,760 –> 00:26:39,520
با این گفته ، اکنون چندین IDE برای انتخاب. لطفا در نظرات به من بگویید ، کدام یک برای شما بهتر است؟
260
00:26:40,480 –> 00:26:47,280
در این ویدئو جدید ، متغیرها و انواع داده ها را در پایتون کشف خواهید کرد. بیایید این ویدئو را پخش کنیم ..
261
00:26:48,720 –> 00:26:54,080
اکنون که می دانید چگونه با مفسر پایتون تعامل کرده و کد پایتون را اجرا کنید ، وقت آن است که به
262
00:26:54,080 –> 00:27:01,200
جستجوی انواع داده های اصلی در پایتون بپردازیم. انواع داده ها طبقه بندی یا طبقه بندی موارد داده است.
263
00:27:01,200 –> 00:27:07,040
این نوعی ارزش را نشان می دهد که چه عملیاتی را می توان بر روی یک داده خاص انجام داد.
264
00:27:07,040 –> 00:27:14,800
بنابراین ، هر مقدار در پایتون یک نوع داده دارد. از آنجا که همه چیز در پایتون یک شی است ، انواع داده
265
00:27:14,800 –> 00:27:21,200
در واقع کلاس هستند. و متغیرها نمونه هایی از این کلاس ها هستند. در نتیجه ،
266
00:27:21,200 –> 00:27:27,520
چندین نوع داده در پایتون وجود دارد. برخی از اساسی ترین انواع داده های ساخته شده در پایتون عبارتند از: انواع عددی ،
267
00:27:27,520 –> 00:27:34,320
رشته ای و بولی. سایر موارد بخشی از مجموعه ها هستند: مجموعه های مختلف ، لیست ها ، فرهنگ لغت ها و مجموعه ها.
268
00:27:35,360 –> 00:27:41,120
اشیا imm تغییرناپذیر قابل تغییر. هنگام انتخاب داده یا نوع جمع آوری ، درک
269
00:27:41,120 –> 00:27:46,720
ویژگی های آن نوع مفید است. انتخاب نوع مناسب برای یک مجموعه داده خاص می تواند به معنای
270
00:27:46,720 –> 00:27:53,040
افزایش کارایی یا امنیت باشد. در حقیقت ، اشیا data داده ای از این نوع برای پردازش در حافظه کامپیوتر
271
00:27:53,040 –> 00:27:59,040
ذخیره می شوند . برخی از این مقادیر می توانند در حین پردازش اصلاح شوند ، اما
272
00:27:59,040 –> 00:28:05,200
محتوای برخی دیگر قابل تغییر نیست. به محض ایجاد حافظه ، مقادیر عددی ، رشته ها
273
00:28:05,200 –> 00:28:09,600
و تاپل ها تغییرناپذیر هستند ، به این معنی که محتوای آنها پس از ایجاد تغییر نمی کند.
274
00:28:10,400 –> 00:28:15,200
از طرف دیگر ، مجموعه موارد موجود در لیست یا شی فرهنگ لغت قابل تغییر است …
275
00:28:15,840 –> 00:28:22,160
افزودن ، حذف ، درج و حتی ترتیب مجدد موارد امکان پذیر است. از این رو ، آنها اشیای قابل تغییر هستند.
276
00:28:23,840 –> 00:28:29,040
آیا تعجب می کنید: از چه نوع داده ای باید استفاده کنید؟ نگران نباشید ، در پایان این آموزش
277
00:28:29,040 –> 00:28:33,200
با شکل های این نوع و نحوه نمایش آنها آشنا خواهید شد.
278
00:28:35,120 –> 00:28:42,880
انواع داده های عددی. در پایتون ، نوع داده عددی نشان دهنده داده هایی است که دارای مقدار عددی هستند. مقدار
279
00:28:42,880 –> 00:28:49,600
عددی می تواند اعداد صحیح یا شناور باشد ، حتی اعداد مختلط. این مقادیر به عنوان کلاس int ،
280
00:28:49,600 –> 00:28:55,520
float و کلاس پیچیده تعریف می شوند. در پایتون می توانیم از تابع type () استفاده کنیم تا بدانیم متغیر یا
281
00:28:55,520 –> 00:29:02,000
مقداری به کدام کلاس تعلق دارد. و تابع isinstance () برای بررسی اینکه آیا یک شی متعلق به یک کلاس خاص است.
282
00:29:02,800 –> 00:29:07,200
با استفاده از روش های int () ، float () و پیچیده () می توانید از یک نوع به نوع دیگر تبدیل کنید.
283
00:29:09,440 –> 00:29:13,200
عدد صحیح این مقدار توسط کلاس int نمایش داده می شود.
284
00:29:13,200 –> 00:29:17,280
این شامل اعداد کامل مثبت یا منفی ، بدون کسر یا اعشار است.
285
00:29:18,080 –> 00:29:23,280
در پایتون 3 ، اعداد صحیح می توانند از هر طولی باشند. مانند همه موارد فقط به میزان حافظه سیستم
286
00:29:23,280 –> 00:29:28,000
شما محدود می شود؟ اما فراتر از آن ، یک عدد صحیح می تواند تا زمانی که شما نیاز دارید باشد.
287
00:29:28,800 –> 00:29:34,960
پایتون یک توالی از رقم اعشار را بدون هیچ پیشوندی تفسیر می کند که یک عدد اعشاری باشد ، اما
288
00:29:34,960 –> 00:29:41,360
رشته های زیر را می توان به یک عدد صحیح اضافه کرد ، تا پایه ای غیر از 10 نشان دهد
289
00:29:41,360 –> 00:29:48,080
. این مقدار که توسط کلاس float نشان داده می شود ، یک عدد نقطه شناور را نشان می دهد. این یک
290
00:29:48,080 –> 00:29:54,400
عدد واقعی با نمایش نقطه شناور است و با یک نقطه اعشاری مشخص می شود.
291
00:29:54,400 –> 00:30:00,560
در صورت تمایل ، کاراکتر e به همراه یک عدد صحیح مثبت یا منفی برای تعیین علامت علمی پیوست می شود.
292
00:30:00,560 –> 00:30:08,800
این نمایش تا 15 رقم اعشار دقیق است.
293
00:30:08,800 –> 00:30:15,120
اعداد مختلط که با کلاس پیچیده نشان داده می شوند ، در فرم قسمت واقعی به اضافه قسمت خیالی (j) نوشته می شوند.
294
00:30:15,920 –> 00:30:22,080
با استفاده از مختصات مستطیل یا دکارتی به صورت داخلی ذخیره می شود.
295
00:30:22,080 –> 00:30:28,800
اگر شما در حال مطالعه یا کار با توابع ریاضی یا مهندسی هستید ، می تواند بسیار مفید باشد. به عنوان مثال ، 2 + 3j.
296
00:30:30,080 –> 00:30:36,960
Python 3 یک نوع داده بولی ارائه می دهد. اشیا of از نوع بولی ممکن است یکی از دو مقدار را داشته باشند: درست یا نادرست.
297
00:30:36,960 –> 00:30:42,720
در برنامه نویسی ، شما اغلب باید بدانید که اصطلاح درست است یا نادرست.
298
00:30:43,520 –> 00:30:49,360
می توانید هر عبارتی را در پایتون ارزیابی کنید و یکی از این دو پاسخ را بدست آورید.
299
00:30:49,360 –> 00:30:55,040
به عنوان مثال ، هنگامی که شما دو مقدار را مقایسه می کنید: عبارت ارزیابی می شود ، سپس python پاسخ بولی را برمی گرداند.
300
00:30:55,840 –> 00:31:02,320
هنگام اجرای یک شرط در دستور if ، همین رفتار رخ می دهد … پایتون همچنین
301
00:31:02,320 –> 00:31:07,200
توابع داخلی زیادی دارد که یک مقدار بولی را برمی گرداند ، مانند تابع isinstance () که می تواند برای
302
00:31:07,200 –> 00:31:13,280
تعیین اینکه آیا یک شی از داده های خاصی است ، استفاده می شود نوع درباره آموزش ارزیابی اشیا
303
00:31:13,280 –> 00:31:19,200
in در متن بولی ، هنگامی که با عملگرهای منطقی روبرو می شوید ، در آموزش آینده در مورد عملگرها
304
00:31:19,200 –> 00:31:27,120
و عبارات در پایتون اطلاعات بیشتری کسب خواهید کرد . رشته ها: نوع رشته در پایتون توسط کلاس str نشان داده می شود.
305
00:31:28,000 –> 00:31:32,800
رشته ها تغییرناپذیر هستند ، به این معنی که محتوای آنها پس از ایجاد نمی تواند تغییر کند.
306
00:31:33,600 –> 00:31:38,960
رشته ها در پایتون مانند بسیاری دیگر از زبان های برنامه نویسی معروف ، آرایه ای از بایت هستند که
307
00:31:38,960 –> 00:31:45,200
نشانگر نویسه های یونی کد هستند ، با این حال ، پایتون نوع داده کاراکتری ندارد. یک کاراکتر
308
00:31:45,200 –> 00:31:50,880
منفرد نیز به عنوان یک رشته در نظر گرفته می شود که طول آن 1 است. شما مقادیر رشته
309
00:31:50,880 –> 00:31:57,840
را در داخل نقل قول های منفرد یا دو نقل قول نشان می دهید یا آنها را اعلام می کنید . “سلام” همان “سلام” است.
310
00:31:57,840 –> 00:32:05,040
رشته های چند خطی را می توان با استفاده از نقل قول های سه گانه ، مانند لیست و تاپ ، مشخص کرد. برای دستیابی به مقادیر می توان از عملگر برش با رشته استفاده کرد.
311
00:32:05,040 –> 00:32:11,600
در یک رشته ، از شاخص ها و براکت های مربع استفاده می کنیم. پایتون همچنین مجموعه ای از
312
00:32:11,600 –> 00:32:18,000
روش های داخلی را دارد که می توانید روی رشته ها استفاده کنید: نوار () ، پایین () ، بالا () ، جایگزین () ، تقسیم () – در میان بسیاری دیگر.
313
00:32:20,720 –> 00:32:25,920
لیست ها حالا که اعداد و رشته ها را درک کردید ، بیایید انواع داده های نسبتاً
314
00:32:25,920 –> 00:32:33,440
پیچیده تری را مرور کنیم … لیست یکی از پر کاربردترین نوع داده ها در پایتون است و بسیار انعطاف پذیر است. این
315
00:32:33,440 –> 00:32:39,440
مجموعه ای از موارد است که قابل سفارش و تغییر پذیر است ، به این معنی که مقادیر یک لیست
316
00:32:39,440 –> 00:32:45,360
حتی پس از ایجاد آن قابل تغییر است . همه عناصر موجود در یک لیست ، مطابق با توالی مشخص ، نمایه می شوند.
317
00:32:46,160 –> 00:32:52,640
نمایه سازی یک لیست با صفر بودن اولین شاخص انجام می شود. هر عنصر در لیست جای خود را
318
00:32:52,640 –> 00:32:58,800
دارد که امکان کپی از عناصر را فراهم می کند. لیست ها در پایتون دقیقاً مانند آرایه های اعلام شده در
319
00:32:58,800 –> 00:33:04,480
زبان های دیگر است. نیازی نیست که همه موارد موجود در یک لیست از یک نوع باشند ، که یکی از
320
00:33:04,480 –> 00:33:11,200
قدرتمندترین ابزارها در پایتون است. اعلام لیست بسیار ساده است: مواردی که با کاما از هم
321
00:33:11,200 –> 00:33:17,520
جدا می شوند در داخل پرانتز قرار می گیرند []. یک لیست واحد ممکن است شامل انواع داده ها مانند اعداد صحیح ،
322
00:33:17,520 –> 00:33:24,240
رشته ها و همچنین اشیا باشد. لیست ها در پایتون ترتیب داده می شوند و تعداد مشخصی دارند. You can use the
323
00:33:24,240 –> 00:33:29,520
slicing operator to access and extract an item from a list, by referring to the index number.
324
00:33:31,040 –> 00:33:36,960
Dictionaries. Dictionary in python is an unordered and indexed collection of values, used to store data.
325
00:33:36,960 –> 00:33:43,840
Unlike other data types that hold only single value as an element, dictionary holds key value pair {[k: v}
326
00:33:43,840 –> 00:33:48,880
key value is provided in the dictionary to make it more optimized for retrieving data.
327
00:33:49,600 –> 00:33:54,640
It is generally used when you have a huge amount of data, since you must know the key to retrieve the value.
328
00:33:54,640 –> 00:34:01,280
But not the other way around… In python, a dictionary can be created by placing a sequence
329
00:34:01,280 –> 00:34:07,920
of elements within curly braces {}. Dictionary can also be created by the built-in function dict().
330
00:34:07,920 –> 00:34:13,920
Values in a dictionary can be of any data type, and can be duplicated. Whereas keys cannot be repeated.
331
00:34:14,639 –> 00:34:21,119
Each key value pair is separated by a colon, whereas each entry is separated by a comma.
332
00:34:21,120 –> 00:34:27,120
Dictionary keys are case sensitive. Same name but different cases of key will be treated distinctly.
333
00:34:28,400 –> 00:34:35,120
Tuples. Tuple is an ordered collection of python objects, much like a list. The sequence of values
334
00:34:35,120 –> 00:34:40,560
stored in a tuple can be of any type and they are indexed by integers, which allows duplicate members.
335
00:34:41,280 –> 00:34:45,600
The important difference between a list and a tuple, is that tuples are immutable.
336
00:34:46,400 –> 00:34:52,960
Tuples, once created, cannot be modified. Tuples are used to write protect data, and are usually faster
337
00:34:52,960 –> 00:34:59,680
than list, as it cannot change dynamically. In python, tuples are represented by tuple class, and
338
00:34:59,680 –> 00:35:05,280
created by placing sequence of values separated by comma, with or without the use of parentheses for
339
00:35:05,280 –> 00:35:11,360
grouping of data. Tuples can contain any number of elements, and of any data type like: strings,
340
00:35:11,360 –> 00:35:20,720
integers, list, etc. You can use the slicing operator to extract items, but cannot change values.
341
00:35:20,720 –> 00:35:26,640
Sets in python is an unordered collection of data type that is iterable, mutable and has unique elements.
342
00:35:27,440 –> 00:35:32,160
The order of elements in a set is undefined, though it may consist of various elements.
343
00:35:32,880 –> 00:35:38,240
The advantage of using a set, as opposed to a list, is that it has a highly optimized method
344
00:35:38,240 –> 00:35:41,440
for checking whether a specific element is contained in the set.
345
00:35:42,240 –> 00:35:47,040
Set is defined by curly braces with unique values, which are separated by commas.
346
00:35:47,840 –> 00:35:53,760
You can perform multiple set operations like union and intersection on two sets.
347
00:35:53,760 –> 00:35:59,600
Since sets are unordered collection, indexing has no meaning. Hence, the slicing operator does not work!
348
00:36:00,720 –> 00:36:07,040
Conversion between data types in python. Data types are used to classify one particular type
349
00:36:07,040 –> 00:36:11,360
of data, determining the values that you can assign to the type and the operations you can perform on it.
350
00:36:11,360 –> 00:36:17,280
Nonetheless, there are few times you need to convert values between types,
351
00:36:17,280 –> 00:36:23,360
in order to manipulate values in a different way. For example, you may need to concatenate numeric
352
00:36:23,360 –> 00:36:29,120
values with strings, or represent decimal places in numbers that were initialized as integer values.
353
00:36:29,840 –> 00:36:35,120
As you have seen along the previous examples, you can get the data type of any object by using the type()
354
00:36:35,120 –> 00:36:41,200
function, and the isinstance() function to check whether an object belongs to a particular class.
355
00:36:42,000 –> 00:36:46,320
You also have multiple constructors, for each one of the data types in python.
356
00:36:47,040 –> 00:36:53,440
You can simply use these constructors to use the specified data type, or you can change a data type
357
00:36:53,440 –> 00:37:00,160
to another one using these constructors. Basically, type casting is the process of changing one data
358
00:37:00,160 –> 00:37:06,640
type into another. Using these constructors, you can use various data types with the functionality of the other.
359
00:37:06,640 –> 00:37:13,200
Suppose that you declare the list mentioned in the example as a tuple.
360
00:37:13,200 –> 00:37:19,040
It will become immutable for that particular operation. Similarly, we can use other constructors as well.
361
00:37:19,760 –> 00:37:24,480
In this tutorial, you have learned about the built-in data types that python provides.
362
00:37:25,280 –> 00:37:28,800
Head to the next tutorial and learn about python variables…
363
00:37:29,840 –> 00:37:36,160
In this entire new video, you will now discover syntax and variables in python. Please, do not
364
00:37:36,160 –> 00:37:40,880
forget to smash that like button for the youtube algorithm, since it does really help supporting us
365
00:37:40,880 –> 00:37:46,720
and providing new free content, once a week. Are you interested to be part of this community? All the
366
00:37:46,720 –> 00:37:52,960
links are in the description below. Now let’s play this video. You might have produced a print “hello world”
367
00:37:52,960 –> 00:37:59,200
program in your best programming language. It is all very well, but quite limited, right?
368
00:37:59,200 –> 00:38:04,480
More complex programs need a way of labeling data with a name, so it can be referenced later.
369
00:38:05,280 –> 00:38:11,280
This has the rather wonderful side effect of making computer code easier to read. Nonetheless,
370
00:38:11,280 –> 00:38:15,680
there are a certain rules we have to follow, while writing a variable…
371
00:38:16,480 –> 00:38:21,200
Let’s take a look at the variable definition, to understand how we declare a variable in python.
372
00:38:21,200 –> 00:38:28,080
Variable definition. In a programming language, a variable is a memory location.
373
00:38:28,080 –> 00:38:31,840
Think of a variable as a name attached to a particular object.
374
00:38:32,400 –> 00:38:37,920
Variables are like containers for storing data values. The value that you have stored into this
375
00:38:37,920 –> 00:38:45,280
container may change in the future, according to the specifications… Variable declaration in python.
376
00:38:45,280 –> 00:38:51,120
Variables do not need to be defined or declared in advance, with any particular type. As is the case
377
00:38:51,120 –> 00:38:57,520
in many other programming languages, variables can even change type after they have been set.
378
00:38:57,520 –> 00:39:03,840
A variable is created as soon as a value is assigned to it, then you can start using it.
379
00:39:03,840 –> 00:39:09,920
Assignment is done with a single equal sign. This is read or interpreted as n is assigned the value 300.
380
00:39:10,640 –> 00:39:16,160
Once this is done, n can be used in a statement or expression, and its value will be substituted later.
381
00:39:16,880 –> 00:39:22,320
If you change the value of n and use it again, the new value will be substituted instead.
382
00:39:23,120 –> 00:39:28,080
Python also allows chained assignment, which makes it possible to assign the same value
383
00:39:28,080 –> 00:39:35,040
to multiple variables.The chained assignment above assigns 300 to the variables a, b and c, simultaneously.
384
00:39:35,040 –> 00:39:42,800
Object references. What is actually happening when you make a variable assignment?
385
00:39:42,800 –> 00:39:47,440
This is an important question in python, because the answer differs somewhat from what you would
386
00:39:47,440 –> 00:39:53,760
find in many other programming languages. Python is a highly object-oriented language.
387
00:39:53,760 –> 00:40:00,240
In fact, virtually every item of data in a python program is an object of a specific type or class.
388
00:40:01,120 –> 00:40:07,520
This point will be reiterated many times over the course of these tutorials. Consider this code…
389
00:40:07,520 –> 00:40:12,320
when presented with the statement print 300, the interpreter does the following steps:
390
00:40:13,200 –> 00:40:17,840
creates an integer object, gives it the value 300, displays it to the console.
391
00:40:18,560 –> 00:40:23,040
You can see that an integer object is created using the built-in type() function.
392
00:40:23,840 –> 00:40:28,640
A python variable is a symbolic name that is a reference or pointer to an object.
393
00:40:29,360 –> 00:40:35,520
Once an object is assigned to a variable, you can refer to the object using that name. But the data
394
00:40:35,520 –> 00:40:41,280
itself is still contained within the object. For example, this assignment creates an integer
395
00:40:41,280 –> 00:40:45,920
object with the value 300. And assigns the variable n to point to that object.
396
00:40:46,640 –> 00:40:52,240
The following code verifies that n points to an integer object… Now consider the following statement.
397
00:40:52,240 –> 00:40:58,160
What happens when it is executed? Python does not create another object,
398
00:40:58,160 –> 00:41:04,160
it simply creates a new symbolic name or reference m, which points to the same object that n points to.
399
00:41:04,960 –> 00:41:11,200
Next, suppose you do this now. Python creates a new integer object with the value 400, and
400
00:41:11,200 –> 00:41:16,800
m becomes a reference to it. Lastly, suppose this statement is executed next…
401
00:41:16,800 –> 00:41:23,280
Now python creates a string object with the value foo, and makes n reference that. There is no longer
402
00:41:23,280 –> 00:41:29,360
any reference to the integer object 300, it is now orphaned. And there is no way to access it anymore!
403
00:41:30,080 –> 00:41:34,560
Tutorials in this series will occasionally refer to the lifetime of an object.
404
00:41:35,280 –> 00:41:40,640
An object’s life begins when it is created, at which time at least one reference to it
405
00:41:40,640 –> 00:41:47,120
is created. During an object’s lifetime, additional references to it may be created, and references to
406
00:41:47,120 –> 00:41:53,360
it may be deleted as well. An object stays alive as long as it has at least one reference to it.
407
00:41:54,080 –> 00:42:00,480
When the number of references to an object drops to zero, it is no longer accessible. At that point,
408
00:42:00,480 –> 00:42:06,720
its lifetime is over. Python will eventually notice that it is inaccessible and reclaim the allocated
409
00:42:06,720 –> 00:42:13,040
memory so it can be used for something else. In computer lingo this process is referred to as
410
00:42:13,040 –> 00:42:20,640
garbage collection. Object identity in python. Every object that is created is given a number
411
00:42:20,640 –> 00:42:26,800
that uniquely identifies it. It is guaranteed that no two objects will have the same identifier
412
00:42:26,800 –> 00:42:33,120
during any period in which their lifetimes overlap. Once an object’s reference count drops to zero and
413
00:42:33,120 –> 00:42:38,240
it is garbage collected, then its identifying number becomes available and may be used again.
414
00:42:38,960 –> 00:42:44,560
The built-in python function id() returns an object’s identifier. Using this function, you
415
00:42:44,560 –> 00:42:51,040
can verify that two variables point to the same object. Indeed, after the assignment m equals n,
416
00:42:51,040 –> 00:42:56,640
m and n both point to the same object, confirmed by the fact that id(m) and id(n) return the same
417
00:42:56,640 –> 00:43:03,520
number. Once m is reassigned to 400, m and n point to different objects with different identities.
418
00:43:04,800 –> 00:43:11,520
Variable names and syntax. The examples you have seen so far have used short variable names… But
419
00:43:11,520 –> 00:43:17,600
the truth is that variable names can be more descriptive. In fact ,it is usually beneficial
420
00:43:17,600 –> 00:43:21,840
if they are, because it makes the purpose of the variable more evident at first glance.
421
00:43:22,720 –> 00:43:27,280
At least, you can tell from the name what the value of the variable is supposed to represent.
422
00:43:28,080 –> 00:43:33,040
Officially, variable names in python can be any length and can consist of uppercase and
423
00:43:33,040 –> 00:43:38,800
lowercase letters, digits and the underscore character. An additional restriction is that
424
00:43:38,800 –> 00:43:44,160
although variable name can contain digits, the first character of a variable name cannot be one digit.
425
00:43:44,880 –> 00:43:50,000
Moreover, variables names are case sensitive, which means that age, Age and AGE are three
426
00:43:50,000 –> 00:43:55,040
different variables. For example, which of the following variable names are valid?
427
00:44:07,120 –> 00:44:12,960
You may have guessed right! If not, just playback and refer to the previous five rules. Or even the
428
00:44:12,960 –> 00:44:18,400
style guide for python code, also known as pep8, which contains naming conventions that
429
00:44:18,400 –> 00:44:24,480
list suggested standards for names of different object types. There is nothing stopping you from
430
00:44:24,480 –> 00:44:28,640
creating multiple different variables in the same program, with the same name…
431
00:44:29,280 –> 00:44:35,040
But it is probably ill-advised, it would certainly be likely to confuse anyone trying to read your code!
432
00:44:35,040 –> 00:44:40,560
And even you yourself, after you had been away from it a while. Thus, it is worthwhile to
433
00:44:40,560 –> 00:44:46,160
give a variable a name that is descriptive enough to make clear what it is being used for. You will
434
00:44:46,160 –> 00:44:52,640
see later that variables are not the only things that can be given names. Actually, you can also name
435
00:44:52,640 –> 00:44:59,920
functions, classes, modules and so on. The rules that apply to variable names also apply to identifiers.
436
00:45:01,520 –> 00:45:06,480
Reserved keywords: there is one more restriction on identifier names…
437
00:45:06,480 –> 00:45:12,000
The Python language reserves a small set of keywords that designate special language functionality.
438
00:45:12,800 –> 00:45:19,840
No object can have the same name as a reserved word. In python 3, there are currently 33 reserved keywords,
439
00:45:19,840 –> 00:45:26,240
as described in this table. You can see this list anytime by typing help keywords to the
440
00:45:26,240 –> 00:45:31,840
python interpreter. Reserved words are case sensitive and must be used exactly as shown.
441
00:45:32,800 –> 00:45:39,520
They are all entirely lowercase except for False, None and True. Trying to create a variable with
442
00:45:39,520 –> 00:45:45,920
the same name as any reserved word results in an error: syntax error.
443
00:45:45,920 –> 00:45:52,080
Variable types and scope. In many programming languages, variables are statically type, which means a variable is
444
00:45:52,080 –> 00:45:57,280
initially declared to have a specific data type, and any value assigned to it during its lifetime
445
00:45:57,280 –> 00:46:04,000
must always have that type. Variables in python are not subject to this restriction. In python,
446
00:46:04,000 –> 00:46:09,360
a variable may be assigned a value of one type and then later reassigned a value of a different type.
447
00:46:09,360 –> 00:46:15,680
If you are interested in learning basic and advanced data types in python, please check out
448
00:46:15,680 –> 00:46:21,600
the suggested video. Being able to reassign a value into a variable can be very useful
449
00:46:22,480 –> 00:46:27,520
throughout the course of a program. You may need to accept user-generated values into already
450
00:46:27,520 –> 00:46:32,480
initialized variables, or may have to change the assignment to something you previously defined.
451
00:46:33,920 –> 00:46:39,360
Variable scope. If you are familiar with python, or any other programming languages, you will
452
00:46:39,360 –> 00:46:45,360
certainly know that variables need to be defined before they can be used in your program. Depending
453
00:46:45,360 –> 00:46:50,080
on how and where it was defined, a variable will have to be accessed in different ways…
454
00:46:50,880 –> 00:46:57,200
Not all variables are accessible from all parts of your program, and not all variables exist for the
455
00:46:57,200 –> 00:47:04,000
same amount of time.Where a variable is accessible and how long it exists depend on how it is defined.
456
00:47:04,720 –> 00:47:10,560
We call the part of a program where a variable is accessible its scope, and the duration for
457
00:47:10,560 –> 00:47:17,120
which the variable exists its lifetime. Some variables are defined globally, others locally.
458
00:47:17,120 –> 00:47:21,600
This means that a variable referring to an entity, in a certain part of a program,
459
00:47:21,600 –> 00:47:26,560
may refer to something different in another part of the program, even at different points in time.
460
00:47:27,360 –> 00:47:33,360
Thus, if you define a variable at the top of your script, it will be a global variable. This means
461
00:47:33,360 –> 00:47:38,800
that it is accessible from anywhere in your script, including from a class defined in the same module.
462
00:47:39,520 –> 00:47:44,960
Or both inside and outside of a function. On the other hand, a variable which is defined
463
00:47:44,960 –> 00:47:51,120
inside a function is local to that function. It is accessible from the point at which it was defined,
464
00:47:51,120 –> 00:47:56,480
until the end of the function. And it exists for as long as the function is executing.
465
00:47:56,480 –> 00:48:02,560
For instance, the parameter names in the function definition behave like local variables, but they
466
00:48:02,560 –> 00:48:07,360
contain the values that we pass into the function when we call it. When you use the
467
00:48:07,360 –> 00:48:13,360
assignment operator equals inside a function, its default behavior is to create a new local variable,
468
00:48:13,360 –> 00:48:19,360
unless a variable with the same name is already defined in the local scope. Normally, when you
469
00:48:19,360 –> 00:48:25,120
create a variable inside a function, that variable is local and can only be used inside that function.
470
00:48:25,920 –> 00:48:30,720
If you create a variable with the same name inside a function, the global variable with
471
00:48:30,720 –> 00:48:36,560
the same name will remain as it was and will keep its original value. Thus, in order to create
472
00:48:36,560 –> 00:48:43,040
a global variable inside a function, you can use the global reserved keyword. Also use the global
473
00:48:43,040 –> 00:48:48,320
keyword if you want to change a global variable inside a function, but that was declared outside.
474
00:48:49,520 –> 00:48:55,440
This tutorial covered the basics of python variables, including object references and identity,
475
00:48:55,440 –> 00:49:01,600
as well as naming of python identifiers. You should now have a good understanding of some of python’s
476
00:49:01,600 –> 00:49:08,080
data types and know how to create variables that reference objects of those data types. In the next
477
00:49:08,080 –> 00:49:13,440
video, you will discover and may learn how to combine data objects into expressions, involving
478
00:49:13,440 –> 00:49:19,520
various conditions and operators… So do not waste any minute and join us in the next episode!
479
00:49:20,880 –> 00:49:26,000
In this new video, you will now discover multiple operators in python and how to use them.
480
00:49:27,040 –> 00:49:31,520
Please, do not forget to smash that like button for the youtube algorithm, since it does really
481
00:49:31,520 –> 00:49:36,960
help supporting us and providing new free content, once a week. Are you interested to be part of this
482
00:49:36,960 –> 00:49:43,520
community? All the links are in the description below. Now let’s play this video… Python language
483
00:49:43,520 –> 00:49:49,600
is one of the most popular programming languages. Whilst learning python is seemingly easy, there are
484
00:49:49,600 –> 00:49:55,040
certain core concepts that must be mastered before moving on with various applications of python.
485
00:49:55,840 –> 00:49:59,840
Operators in python are one of the core fundamental concepts.
486
00:49:59,840 –> 00:50:05,040
Therefore, this video will help you understand the different types of operators in python.
487
00:50:05,040 –> 00:50:10,880
Let’s stick around and watch until the end of this video.
488
00:50:10,880 –> 00:50:16,240
What is an operator? Operators in Python are special symbols you will use for operations, between 2 values or variables.
489
00:50:16,960 –> 00:50:22,400
The values that an operator acts on are called operands, and the output varies according to
490
00:50:22,400 –> 00:50:27,520
the type of operator you have used in the operation. Suppose if you want to perform
491
00:50:27,520 –> 00:50:32,880
addition of two variables or values, you can use the addition operator for this special operation.
492
00:50:33,680 –> 00:50:38,400
The values in the operands can be variable or any data type that you have in python.
493
00:50:39,120 –> 00:50:44,480
Here is an example. In this case the plus operator adds the operand a and b together (a + b).
494
00:50:45,280 –> 00:50:51,360
An operand can be either a literal value or a variable that references an object. A sequence
495
00:50:51,360 –> 00:50:58,640
of operands and operators like a plus b (a + b) is called an expression. Python supports many operators for
496
00:50:58,640 –> 00:51:04,080
combining data objects into expressions. These operators are explored in the next section…
497
00:51:04,880 –> 00:51:10,320
Types of operators. Depending upon the type of operations you want to perform, there are seven
498
00:51:10,320 –> 00:51:17,840
types of operators in python: arithmetic operators, assignment operators, comparison operators,
499
00:51:17,840 –> 00:51:25,440
logical operators, bitwise operators, identity operators, membership operators. arithmetic
500
00:51:25,440 –> 00:51:31,920
Arithmetic operators are used to perform common mathematical operations in python, like:
501
00:51:31,920 –> 00:51:36,800
addition, subtraction and multiplication but also division among so many others .
502
00:51:37,600 –> 00:51:43,040
Below are the arithmetic operators with symbols, and their meaning. These are simple mathematical
503
00:51:43,040 –> 00:51:48,800
operators that you will use, while doing an arithmetic operation in python. Apart from these
504
00:51:48,800 –> 00:51:54,800
four basic arithmetic operators, you also have exponential, modulus and floor division operators.
505
00:51:55,680 –> 00:52:00,960
Please note that the result of standard division is always a float, even if the dividend is evenly
506
00:52:00,960 –> 00:52:06,880
divisible by the divisor. Whereas the fractional portion of floor division is truncated off,
507
00:52:06,880 –> 00:52:13,120
leaving only the integer portion. Assignment operators are used to
508
00:52:13,120 –> 00:52:19,760
assign values to the variables or any other object in python. For instance, x equals 5 is
509
00:52:19,760 –> 00:52:25,040
a simple assignment operator that assigns the value 5 on the right to the variable x on the left (x = 5) .
510
00:52:25,040 –> 00:52:32,000
There are also multiple compound operators in python, like x plus equals 5 (x += 5) that adds to the
511
00:52:32,000 –> 00:52:38,400
variable and later assigns the same. Therefore, this is the equivalent of x equals x plus 5 (x = x + 5).
512
00:52:39,520 –> 00:52:45,840
Let’s move forward to the next operator: comparison operators, which are
513
00:52:45,840 –> 00:52:52,800
used to compare two values. Since it either returns true or false, comparison operators are typically
514
00:52:52,800 –> 00:52:58,960
used in boolean contexts like conditional and loop statements, in order to direct program flow.
515
00:52:58,960 –> 00:53:05,280
As you will see later in the upcoming video, following are the comparison operators that we have in python:
516
00:53:05,280 –> 00:53:12,960
greater than (>), lesser than ( 00:53:18,880
Logical operators and or and not operators are used to compare two conditional statements.
518
00:53:18,880 –> 00:53:25,280
The logical operators modify and join together expressions evaluated in boolean context, to create more complex conditions.
519
00:53:26,080 –> 00:53:31,200
When you use the and operator, the expression will return true if both operands are true.
520
00:53:32,000 –> 00:53:37,520
Whereas using the or operator will only result in true if at least one of the operands is true.
521
00:53:38,320 –> 00:53:43,840
Finally, the not operator will only give true if the boolean expression in the operand is false.
522
00:53:44,800 –> 00:53:50,880
Let’s move forward and have a look at bitwise operators, which are similar to logical
523
00:53:50,880 –> 00:53:56,320
operators. But they treat operands as sequences of binary digits, and operate on them bit by bit.
524
00:53:57,120 –> 00:54:05,680
For example, 5 is 101 in binary form, and 7 is 111. When you perform a or operation on these operands,
525
00:54:05,680 –> 00:54:12,240
it will give you the output 7 (111) in binary form, since you performed a logical operation on each
526
00:54:12,240 –> 00:54:18,480
bit of the numbers, one at a time. This is exactly the same when you perform the and operation, but
527
00:54:18,480 –> 00:54:24,480
using the appropriate logical operation bit by bit. Finally but no least, there is another
528
00:54:24,480 –> 00:54:31,600
bitwise operator in python, called xor. It performs the exclusive or operation on each bit of the
529
00:54:31,600 –> 00:54:37,840
,numbers and will return true only if one bit is true, not the both of them. Apart from these three
530
00:54:37,840 –> 00:54:43,120
operators, you have two more bitwise operators which are: right shift and left shift operators.
531
00:54:43,920 –> 00:54:49,840
What basically happens when this type of operators is used: First, the value is converted into its
532
00:54:49,840 –> 00:54:55,360
binary representation. Then, you will have to specify which type of shift you want to perform,
533
00:54:55,360 –> 00:55:01,200
using two greater than signs (>>) or two lesser than signs ( 00:55:12,720
the direction you chose, left or right, and for the number of bits you specified. Python language
536
00:55:12,720 –> 00:55:18,000
offers some special type of operators like the identity operator or the membership operator.
537
00:55:18,720 –> 00:55:24,240
They are described in the next sections, with a few examples for each one of them.
538
00:55:24,240 –> 00:55:30,960
Identity operators is and is as not are used to compare two values or variables, not if they are
539
00:55:30,960 –> 00:55:36,880
equal but if they are actually the same object located on the same part of the memory. Hence, the
540
00:55:36,880 –> 00:55:41,840
two variables that are equal does not necessarily imply that they are identical, with the same
541
00:55:41,840 –> 00:55:48,800
identity. Here is an example of two objects that are equal but not identical. x and y both refer to
542
00:55:48,800 –> 00:55:55,440
objects whose value is Pi, they are equal. But they do not reference the same object, as you can verify
543
00:55:55,440 –> 00:56:02,320
x and y do not have the same identity. And x is y returns false. You saw previously that when you
544
00:56:02,320 –> 00:56:08,320
make an assignment like x equals y, python merely creates a second reference to the same object. And
545
00:56:08,320 –> 00:56:14,160
you could confirm that fact with the id() function. You can also confirm it using the is operator.
546
00:56:15,440 –> 00:56:22,480
Uunsurprisingly, the opposite of is is is not. Let’s move forward to the last type of operators: membership operators
547
00:56:22,480 –> 00:56:28,320
Membership operators in and not in are used to test whether a value
548
00:56:28,320 –> 00:56:33,200
or variable is found in a sequence (string, list, tuple, set or dictionary).
549
00:56:34,000 –> 00:56:40,000
Following are the membership operators that we have in python and return a boolean. For instance
550
00:56:40,000 –> 00:56:48,480
if a value 3 is found in the list x = [1, 2, 3, 4, 5], the statement 3 in x will return true. On the
551
00:56:48,480 –> 00:56:54,240
other hand, the statement 3 not in x will return false, since 3 is not an element of this list.
552
00:56:55,520 –> 00:57:01,360
Operator precedence. All operators that the language supports are assigned a precedence.
553
00:57:01,360 –> 00:57:07,840
In an expression all operators of highest precedence are performed first. Once those results are obtained,
554
00:57:07,840 –> 00:57:13,680
operators of the next highest precedence are performed, so it continues until the expression
555
00:57:13,680 –> 00:57:19,440
is fully evaluated. And any operators of equal precedence are performed in left to right order.
556
00:57:20,240 –> 00:57:25,120
Here is the order of precedence of the python operators you have seen so far, from lowest to
557
00:57:25,120 –> 00:57:30,960
highest operators. At the top of the table have the highest precedence, and those at the bottom of the
558
00:57:30,960 –> 00:57:38,080
table have the lowest. Any operators in the same row of the table have equal precedence.
559
00:57:38,080 –> 00:57:44,000
Operator precedence can be overridden using parentheses. Expressions in parentheses are always performed
560
00:57:44,000 –> 00:57:49,520
first, and before expressions that are not parenthesized. There is nothing wrong with making
561
00:57:49,520 –> 00:57:54,560
liberal use of parentheses, even when they are not necessary to change the order of evaluation.
562
00:57:55,360 –> 00:58:00,560
In fact, it is considered good practice because it can make the code more readable, and it relieves
563
00:58:00,560 –> 00:58:06,240
the reader of having to recall operator precedence from memory. You now have a complete overview of
564
00:58:06,240 –> 00:58:12,080
various operators in python, but keep in mind that when two operators share an operand, the operator
565
00:58:12,080 –> 00:58:18,160
with the higher precedence goes first, so are expressions in parentheses.
566
00:58:18,160 –> 00:58:23,600
In this tutorial, you discovered multiple operators that python supports, so you can combine objects into expressions.
567
00:58:24,400 –> 00:58:29,600
You should now have quite a good understanding of the assignment, logical and comparison operators.
568
00:58:30,400 –> 00:58:35,120
This way you are fully prepared to explore conditionals statements in much more detail, so that you can
569
00:58:35,120 –> 00:58:40,320
now control the execution flow of your python program. If, else, elif statements will be covered
570
00:58:40,320 –> 00:58:46,640
in the very next upcoming video, so do not waste any minute of it and join us in the next episode.
571
00:58:47,680 –> 00:58:53,200
In this brand new video, you will now discover conditional statements in python, its syntax and
572
00:58:53,200 –> 00:58:58,320
how to properly use logical and comparison operators so you can control the execution
573
00:58:58,320 –> 00:59:03,200
flow of your program. Please do not forget to smash that like button for the youtube
574
00:59:03,200 –> 00:59:08,000
algorithm, since it does really help supporting us and providing new free content, once a week.
575
00:59:09,120 –> 00:59:13,840
Are you interested to be part of this community? All the links are in the description below.
576
00:59:13,840 –> 00:59:20,080
Now let’s play this video… Everything you have seen so far has consisted of sequential execution,
577
00:59:20,080 –> 00:59:25,680
in which statements are always performed one after the next and exactly in the order you specified
578
00:59:25,680 –> 00:59:31,600
explicitly. But the world is often more complicated than that. Frequently, a program
579
00:59:31,600 –> 00:59:36,800
needs to skip over some statements, execute a series of statements, repetitively, or even
580
00:59:36,800 –> 00:59:42,480
choose between alternate sets of statements to execute. And this is where the core concept of
581
00:59:42,480 –> 00:59:47,680
control structure comes in, so you can control which instructions are executed in your program.
582
00:59:48,400 –> 00:59:53,600
In programming languages, most of the time you want to control the execution
583
00:59:53,600 –> 00:59:58,000
flow of your program, since you want to execute some set of statements, only if the
584
00:59:58,000 –> 01:00:03,520
given condition is satisfied. And a different set of statements when it is not satisfied.
585
01:00:04,240 –> 01:00:10,320
Please note that conditional structures are also known as decision making statements. A decision
586
01:00:10,320 –> 01:00:16,320
is when a program has more than one choice of actions, depending on a variable’s value. In the
587
01:00:16,320 –> 01:00:21,760
real world, you commonly must evaluate information around you and then choose one course of action
588
01:00:21,760 –> 01:00:28,000
or another, based on what you observe, right? For instance, think of a traffic light: when it is
589
01:00:28,000 –> 01:00:33,360
green you continue your drive, when you see the light turn yellow you reduce your speed, and when
590
01:00:33,360 –> 01:00:39,280
it is red you stop. These are logical decisions that depend on the value of the traffic light!
591
01:00:40,080 –> 01:00:45,360
Luckily, when your application needs to make such decisions in python, the if statement is how you
592
01:00:45,360 –> 01:00:51,440
perform this sort of decision making. And it allows conditional execution of a statement, or a group
593
01:00:51,440 –> 01:00:57,920
of statements, based on the value of a boolean expression. If statements. In order to write useful
594
01:00:57,920 –> 01:01:03,680
programs, we almost always need the ability to test conditions and change the behavior of the program,
595
01:01:03,680 –> 01:01:09,760
accordingly. Fortunately, conditional statements give us this ability. If statements are one of
596
01:01:09,760 –> 01:01:15,920
the most commonly used conditional statements. In many programming languages, it decides whether
597
01:01:15,920 –> 01:01:21,920
certain statements need to be executed or not. If statement checks for a given condition. When the
598
01:01:21,920 –> 01:01:28,240
boolean expression is true, the set of code inside the if block is executed. On the other hand, when
599
01:01:28,240 –> 01:01:33,920
the expression is false, the set of code inside the if block is skipped over and not executed at all.
600
01:01:34,880 –> 01:01:40,400
Now let’s take a look at its syntax. The boolean expression after the if statement is called the
601
01:01:40,400 –> 01:01:46,800
condition. A few things to keep on mind about if statements are 1) the colon is significant and
602
01:01:46,800 –> 01:01:53,520
required. It separates the header of the compound statements from the body. 2) The line after the
603
01:01:53,520 –> 01:01:59,520
colon must be properly indented. In python, it is standard to use 4 spaces for indenting.
604
01:02:00,240 –> 01:02:05,760
3) All lines indented the same amount after the colon will be executed. Whenever the boolean
605
01:02:05,760 –> 01:02:12,400
expression is true, one line is statements. It is customary to write if on one line, and indented on
606
01:02:12,400 –> 01:02:18,400
the following line. But, if you only have a unique line of code to be executed, rather than multiple
607
01:02:18,400 –> 01:02:24,320
lines in the code following the condition, you can place it all in the same line. This is not a rule
608
01:02:24,320 –> 01:02:30,480
but just a common practice amongst coders. Thus, both of the following are functionally equivalent.
609
01:02:30,480 –> 01:02:37,120
If expression statement on the following line, or if expression statement on the same line. There
610
01:02:37,120 –> 01:02:43,680
can even be more than one statement on the same line, separated by semicolons. But what does it mean.
611
01:02:43,680 –> 01:02:49,360
If the expression is true, executes all of the statements, otherwise do not execute any of
612
01:02:49,360 –> 01:02:57,600
them. Here is an example if expression statement 1, statement 2, statement n, etc.
613
01:02:57,600 –> 01:03:03,120
The python pass statement. Occasionally, it is useful to have a section with no statements, usually as a place
614
01:03:03,120 –> 01:03:07,440
keeper, where you will eventually put a block of code that you have not implemented yet.
615
01:03:08,160 –> 01:03:13,520
Because python uses indentation instead of delimiters, it is not possible to specify an
616
01:03:13,520 –> 01:03:18,880
empty block. If you introduce an if statement, something has to come after it, either on the
617
01:03:18,880 –> 01:03:24,960
same line or indented on the following line. To keep the interpreter happy in any situation where
618
01:03:24,960 –> 01:03:30,480
a statement is syntactically required, you can use the pass statement, which does nothing except act
619
01:03:30,480 –> 01:03:38,400
as a placeholder. Here is an example. if true: pass. print(‘after pass’). This python code will only print
620
01:03:38,400 –> 01:03:44,880
after pass, since the pass statement is used inside the if statement. Now that you know how
621
01:03:44,880 –> 01:03:51,040
to check whether a condition is true or not, let’s get to the practice. For example, let’s say you are
622
01:03:51,040 –> 01:03:57,600
recording the score for a certain course. The total score is 100, with 50 points for the theoretical
623
01:03:57,600 –> 01:04:04,320
work and 50 for practical. You want to display an error message or warning if the score exceeds 100.
624
01:04:05,040 –> 01:04:10,400
How would you proceed? Let’s declare two variables and effect a value to each one of them.
625
01:04:10,400 –> 01:04:17,120
score theory = 40, score practical = 45. Now, let’s write a conditional statement to check
626
01:04:17,120 –> 01:04:22,080
whether the total score, which is the addition of these two variables, is greater than 100.
627
01:04:22,720 –> 01:04:28,960
If score theory + score practical > 100, print invalid score. Please check the input.
628
01:04:29,680 –> 01:04:34,320
And here you are, you just wrote your first if statement, that will evaluate the total score
629
01:04:34,320 –> 01:04:39,680
and print a warning message when the input is invalid.
630
01:04:39,680 –> 01:04:45,200
You know how to use an if statement, to conditionally execute a single statement or a block of several statements.
631
01:04:45,920 –> 01:04:50,880
It is now time to find out what else you can do to control the execution flow of your program.
632
01:04:51,760 –> 01:04:57,120
Sometimes you want to evaluate a condition and take one path if it is true, so that one thing
633
01:04:57,120 –> 01:05:02,400
or more can happen. But specify an alternative path when it is not, and something else happens.
634
01:05:03,200 –> 01:05:07,760
This is accomplished with an else clause, which executes a statement in case all the
635
01:05:07,760 –> 01:05:13,440
tested expressions are false in the default case. To understand the control flow of an
636
01:05:13,440 –> 01:05:19,280
if-else statement, take a look at the flowchart of how the execution takes place. If expression
637
01:05:19,280 –> 01:05:25,440
statements, else statements. If the expression is followed by an indented block of statements.
638
01:05:25,440 –> 01:05:30,720
If the expression is true, the first block of statements is executed, then the second is skipped.
639
01:05:31,440 –> 01:05:36,000
But after the if statement, there is an optional else statement, which is followed by another
640
01:05:36,000 –> 01:05:41,040
indented block of statements. When the if expression is false, the first block of
641
01:05:41,040 –> 01:05:46,480
statements is skipped, then the second executed instead. There is no limit on the number of
642
01:05:46,480 –> 01:05:50,960
statements that can appear under the two clauses of an if-else statement. But there has to be at
643
01:05:50,960 –> 01:05:57,360
least one statement in each block. Either way, execution then resumes after the second block.
644
01:05:57,360 –> 01:06:02,480
Also note that both suites of statements are defined by its indentation, as described earlier.
645
01:06:03,280 –> 01:06:10,400
Great let’s get back and say you wanted to go a step further in the previous example. Imagine
646
01:06:10,400 –> 01:06:16,080
you wanted to display a statement when the total score was actually within the range, less than 100.
647
01:06:16,800 –> 01:06:22,160
Cause this is exactly when the if-else statement would help. When recording the score for a certain
648
01:06:22,160 –> 01:06:27,520
coursework, you want to add together the points for the theory part and also the practical part to get
649
01:06:27,520 –> 01:06:33,440
the total score. If the total score is greater than 100, you want to display a warning message.
650
01:06:34,240 –> 01:06:41,520
And if it is less than 100, display a score valid message. Here is a solution to this problem: Declare
651
01:06:41,520 –> 01:06:48,160
2 variables, score theory = 40, score practical = 45. Then write the if else
652
01:06:48,160 –> 01:06:53,120
statement to check whether the total score is valid, or print a warning message when it is not.
653
01:06:53,840 –> 01:07:00,400
If score theory + score practical > 100, print please check the input score exceeds total possible score.
654
01:07:00,400 –> 01:07:07,200
Else, print score validated, your total is score theory + score practical.
655
01:07:08,000 –> 01:07:13,760
Sometimes, there are even more than two possibilities, so you need more than two branches.
656
01:07:13,760 –> 01:07:20,320
Fortunately, there is one more conditional statement in python, which is used to test out more
657
01:07:20,320 –> 01:07:26,720
expressions in order to optimize and increase the efficiency of your code. One way to express such a
658
01:07:26,720 –> 01:07:33,600
computation is called a chain conditionals, or else-if. Let’s take a look at its syntax, to understand
659
01:07:33,600 –> 01:07:40,800
how you can declare an else if statement block in python. If expression 1: statements, elif expression
660
01:07:40,800 –> 01:07:49,600
2: statements, elif expression 3: statements, etc. else: statements. Each condition is checked in order.
661
01:07:49,600 –> 01:07:55,920
If the first is false, the next is checked. If the second is false, the next is checked. And so on.
662
01:07:55,920 –> 01:08:00,560
If one of them is true, the corresponding branch executes and the statement ends.
663
01:08:01,360 –> 01:08:06,160
Even if more than one condition is true in the following statements, only the first true
664
01:08:06,160 –> 01:08:11,920
branch will execute. There is no limit of the number of else if statements, but only a single
665
01:08:11,920 –> 01:08:18,160
and optional final else statement is allowed. And it must be the last branch in the statement. If an
666
01:08:18,160 –> 01:08:24,319
else clause is not included and all the conditions are false, then none of the blocks will be executed.
667
01:08:24,319 –> 01:08:30,559
Let’s get back to our example and imagine a case where one of these scores exceed 50, but the
668
01:08:30,560 –> 01:08:36,640
total is still less than 100. What do you think would happen? Then I bet you guessed it right…
669
01:08:36,640 –> 01:08:43,120
This code will output score validated, your total is 95. Although this is very wrong, since you know
670
01:08:43,120 –> 01:08:48,480
that the maximum limit for individual scores for theory or practical should not exceed 50!
671
01:08:49,200 –> 01:08:55,200
How can you represent this in python code? Using if else if statement. One way is to check
672
01:08:55,200 –> 01:09:00,880
whether the individual scores for theory and practical do not exceed 50. Here is an example,
673
01:09:00,880 –> 01:09:06,240
let’s declare the same two variables, but one of them with a value greater than 50.
674
01:09:07,040 –> 01:09:12,800
score theory = 60, score practical = 20. You will now check whether the limit for
675
01:09:12,800 –> 01:09:19,200
individual scores, which is 50. If score theory > 50, print please check the input score for theory.
676
01:09:19,200 –> 01:09:24,960
elif score practical > 50, print please check the input score for practical.
677
01:09:24,960 –> 01:09:30,480
else print score validated, your total is score theory + score practical.
678
01:09:31,200 –> 01:09:36,399
And here you are, you are now able to check whether an expression is met, and evaluate another
679
01:09:36,399 –> 01:09:42,319
condition when the first one is false. Then, finally, have a default case when none of them is true!
680
01:09:43,120 –> 01:09:48,160
Nested conditionals: when you have an if statement inside another if statement, this is called
681
01:09:48,160 –> 01:09:53,840
nesting in the programming world. It does not have to always be a simple if statement, but you could
682
01:09:53,840 –> 01:09:59,440
use the concept of if, else-if and else statements, all of them combine, to form a more
683
01:09:59,440 –> 01:10:05,520
complex structure and check multiple conditions. An the given code, although the indentation of the
684
01:10:05,520 –> 01:10:10,880
statements makes the structure apparent, nested conditionals become difficult to read very quickly.
685
01:10:11,680 –> 01:10:17,120
In general, it is a good idea to avoid them as much as you can, since indentation is the only
686
01:10:17,120 –> 01:10:23,120
way to figure out what is the level of nesting. An if statement present inside another if statement,
687
01:10:23,120 –> 01:10:28,320
which is present inside another if statement itself and so on is pretty difficult to read, right?
688
01:10:28,320 –> 01:10:35,760
But let’s have a look at one example of nested conditionals; if expression 1: statements,
689
01:10:35,760 –> 01:10:42,800
else if expression 2: statements, else: statements. The outer conditional contains two branches.
690
01:10:42,800 –> 01:10:47,600
The second branch else contains another if statement, which has two branches of its own.
691
01:10:48,320 –> 01:10:53,600
Those two branches could contain conditional statements as well… You see now how quickly
692
01:10:53,600 –> 01:11:00,240
this could become difficult to read, and even code yourself.
693
01:11:00,240 –> 01:11:05,920
Python supports one additional decision-making entity, called a conditional expression, or even ternary operator.
694
01:11:06,640 –> 01:11:13,120
In its simplest form, the syntax of the conditional expression is as follows: expression 1 if conditional expression else expression 2
695
01:11:13,120 –> 01:11:19,680
This is quite different from the if statement forms listed earlier,
696
01:11:19,680 –> 01:11:25,840
because it is not a control structure that directs the flow of execution of your program.
697
01:11:26,560 –> 01:11:32,880
Instead, it acts more like an operator that defines an expression. In the example, this conditional
698
01:11:32,880 –> 01:11:39,280
expression is evaluated first. If it is true, the expression evaluates to expression one, and if
699
01:11:39,280 –> 01:11:46,080
it is false, then it evaluates to the expression 2. Notice the non-obvious order: the middle expression
700
01:11:46,080 –> 01:11:52,160
is evaluated first and, based on that result, one of the expressions or values on the ends is returned.
701
01:11:52,800 –> 01:11:57,760
As you may have guessed, a common use of the conditional expression is for variable assignment.
702
01:11:58,800 –> 01:12:03,840
Here are some other examples that will, hopefully, help you clarify this concept
703
01:12:03,840 –> 01:12:09,920
of conditional expressions. In the very first example, raining has a boolean value of false.
704
01:12:10,720 –> 01:12:16,000
Thus, the expression if not raining is true and results in returning the left value of the
705
01:12:16,000 –> 01:12:22,560
conditional expression, which is beach. Consequently, it will print let’s go to the beach. On the other
706
01:12:22,560 –> 01:12:28,480
hand, when raining has a boolean value of true, the conditional expression is false, which results in
707
01:12:28,480 –> 01:12:35,280
printing out let’s go to the library. Okay, let’s pause this video and analyze the other examples …
708
01:12:35,280 –> 01:12:41,680
It is now time to practice, so open your favorite code editor and get to your
709
01:12:41,680 –> 01:12:48,400
keyboards. In this tutorial, you have learned about the conditional statements in python. These are the
710
01:12:48,400 –> 01:12:54,560
statements which alter and control the flow of execution in your program. All of these concepts
711
01:12:54,560 –> 01:13:00,960
are crucial to develop more complex python code. If you liked this video please do not forget to
712
01:13:00,960 –> 01:13:06,880
give a thumb up and subscribe our channel. In the next two tutorials, you will discover two new
713
01:13:06,880 –> 01:13:13,120
control structures: the WHILE loop and the FOR loop. These both structures facilitate iteration,
714
01:13:13,120 –> 01:13:19,600
execution of a statement or block of statements, repeatedly. So do not waste
715
01:13:19,600 –> 01:13:26,160
any minute of it and join us in the next episode. In this brand new video you will now discover
716
01:13:26,160 –> 01:13:31,840
iterations in python, using FOR and WHILE loops so you can improve the execution flow of your program
717
01:13:32,880 –> 01:13:36,800
please do not forget to smash that like button for the youtube algorithm, since
718
01:13:36,800 –> 01:13:40,640
it does really help supporting us and providing new free content, once a week.
719
01:13:41,760 –> 01:13:45,840
Are you interested to be part of this community? All the links are in the description below.
720
01:13:46,480 –> 01:13:52,720
Now let’s play this video. Loops in python are an efficient method for optimizing your code,
721
01:13:52,720 –> 01:13:59,120
and executing multiple statements. If the same block of code has to be executed multiple times,
722
01:13:59,120 –> 01:14:03,920
over and over, you can put it into a loop to perform multiple iterations and get the same
723
01:14:03,920 –> 01:14:10,640
desired output. Consequently, it saves a lot of effort and reduces complexity of the code, as well.
724
01:14:11,440 –> 01:14:15,360
Basically, there are two types of iteration in programming languages:
725
01:14:16,160 –> 01:14:21,120
indefinite iteration, in which the code block executes until some condition is met.
726
01:14:21,920 –> 01:14:28,240
This iteration is performed using WHILE loops, and will be discussed in the very next upcoming video.
727
01:14:28,240 –> 01:14:34,080
And definite iteration, in which the number of repetitions is specified explicitly, and in advance.
728
01:14:34,880 –> 01:14:39,920
This kind of iteration is performed using FOR loops and it will be discussed in the next section.
729
01:14:41,840 –> 01:14:47,040
The FOR statement in python is a bit different from what you usually use in other
730
01:14:47,040 –> 01:14:52,960
programming languages. Rather than iterating over a numeric progression, python’s for statement
731
01:14:52,960 –> 01:14:59,120
iterates over the items of an iterable: strings, dictionaries, lists, tuples and sets, or any other
732
01:14:59,120 –> 01:15:04,720
iterable objects. FOR loops are traditionally used when you have a block of code which you want to
733
01:15:04,720 –> 01:15:10,880
repeat for a fixed number of times. The python FOR statement iterates over the members of a sequence,
734
01:15:10,880 –> 01:15:16,800
then executes the same block each time, and in order. Do not forget to contrast the FOR statement
735
01:15:16,800 –> 01:15:21,600
with the WHILE loop, used when the condition needs to be checked for every single iteration, or to
736
01:15:21,600 –> 01:15:28,320
repeat a block of code forever! Okay, but what does it look like? And how to control the execution flow
737
01:15:28,320 –> 01:15:34,480
of your program, using FOR loops? Let’s move forward and have a look at the syntax of the FOR statement.
738
01:15:35,680 –> 01:15:43,360
for var in sequence: statement 1, statement 2, etc. Here is a simple declaration of a for loop, in which
739
01:15:43,360 –> 01:15:49,920
the variable var takes the value of an item inside the sequence, and on each iteration. This loop executes
740
01:15:49,920 –> 01:15:55,840
all statements and it continues until we reach the last item of the sequence. The body of loop,
741
01:15:55,840 –> 01:16:00,640
which includes some statements, is separated from the rest of the code using indentation.
742
01:16:01,360 –> 01:16:05,840
This loop can be described entirely in terms of the concepts you have just learned about.
743
01:16:06,800 –> 01:16:13,520
Python does the following steps: calls iter() to obtain an iterator for the iterable, calls next(), repeatedly,
744
01:16:13,520 –> 01:16:19,680
to obtain each item from the iterator, executes the loop body once for each item
745
01:16:19,680 –> 01:16:26,000
and set the variable to the given item. Terminates the loop when next raises the stop iteration exception,
746
01:16:26,000 –> 01:16:32,000
then continues. Let’s have a more practical and very simple example, so you can discover and understand
747
01:16:32,000 –> 01:16:38,480
how to properly use python for loops in your program. First, declare a list of strings
748
01:16:38,480 –> 01:16:45,680
and add a few strings into this list: colors = [red, green, blue, yellow]. Then you will iterate
749
01:16:45,680 –> 01:16:52,320
through this list using for statement and print each item. for color in colors: print color.
750
01:16:52,320 –> 01:16:57,920
Very simple, right? The execution will start and look for the first item in the sequence or iterable object.
751
01:16:58,720 –> 01:17:01,840
It will check whether it has reached the end of the sequence, or not .
752
01:17:02,560 –> 01:17:07,040
after executing the statements in the block, it will look for the next item in the sequence .
753
01:17:07,760 –> 01:17:13,280
And the process will continue ,over and over again, until the execution has reached the last item in
754
01:17:13,280 –> 01:17:19,440
the sequence. In the above example, the execution started from the first item in the list colors.
755
01:17:19,440 –> 01:17:25,600
And it went on until the execution has reached its last item, which is yellow. This is a very simple example
756
01:17:25,600 –> 01:17:31,520
of how you can use for loops in python. Of course, there are many other alternatives you
757
01:17:31,520 –> 01:17:37,360
can use and go through any iterable object. Here is another example, when you want to iterate through
758
01:17:37,360 –> 01:17:42,800
every single character of a string, because a string is a list of characters. Do you remember?
759
01:17:43,680 –> 01:17:50,000
my string = ‘python’. for letter in my string: print(letter ). And here you are you just wrote your
760
01:17:50,000 –> 01:17:56,320
first python iteration using for loops. This very simple code will iterate through a list and print out
761
01:17:56,320 –> 01:18:02,800
every single item of that list. It was not that hard, right? Let’s move forward to the next section…
762
01:18:03,680 –> 01:18:09,440
In each example you have seen so far, the entire body of the for loop is executed on each iteration.
763
01:18:10,160 –> 01:18:16,160
It is all very well but quite limited, right? Fortunately, python provides two keywords: break and continue.
764
01:18:16,160 –> 01:18:23,440
That will terminate a loop iteration, prematurely. break in python is a
765
01:18:23,440 –> 01:18:28,480
flow control statement that is used to exit the execution, as soon as the break is encountered.
766
01:18:29,200 –> 01:18:34,480
The python break statement immediately terminates a loop, entirely. Consequently,
767
01:18:34,480 –> 01:18:38,960
the program execution then proceeds to the first statement following the loop body.
768
01:18:39,760 –> 01:18:46,160
Let’s understand how you can use a break statement in for loop, using an example. So, let’s say you have
769
01:18:46,160 –> 01:18:51,920
a list with strings as items. You want to exit the loop using the break statement as soon as
770
01:18:51,920 –> 01:18:58,000
the desired string is encountered. In the above example, as soon as the loop encounters the space
771
01:18:58,000 –> 01:19:02,560
character, it will enter the if statement block where the break statement will exit the loop,
772
01:19:02,560 –> 01:19:09,760
straight away. Thus, it will only print digital. Did you get it? Then let’s practice, it’s your turn now!
773
01:19:09,760 –> 01:19:15,120
Suppose you have a list of colors: red, green, blue, yellow. And you want to print each color,
774
01:19:15,120 –> 01:19:21,840
but break the loop when it encounters blue. How would you proceed, and What output would you get?
775
01:19:33,760 –> 01:19:39,840
Here is a solution: first, let’s declare a list in which you added all of the colors: red, green, blue, yellow.
776
01:19:39,840 –> 01:19:45,120
Then, iterate throughout this list using for statement. for color in colors.
777
01:19:45,920 –> 01:19:51,120
It will create an iterator and the variable color will get the next value, on each iteration,
778
01:19:51,120 –> 01:19:54,320
until the execution has reached the last item in the sequence.
779
01:19:55,120 –> 01:20:00,480
When it encounters the item blue, you do not want to execute the code that comes next, but exit the loop.
780
01:20:00,480 –> 01:20:06,480
If color == blue: break. In all the other cases, as it would be with an else
781
01:20:06,480 –> 01:20:11,920
statement, you want to print the current color. As you may have guessed, the loop will print red green
782
01:20:11,920 –> 01:20:17,840
and stop because the next item is blue! Congrats, you just wrote your first FOR loop, on your own.
783
01:20:18,800 –> 01:20:24,320
Great, let’s dig a little deeper and suppose this time you do not want to exit the loop but just
784
01:20:24,320 –> 01:20:29,360
skip the current iteration and execute the rest of the iterations anyway, with the next items.
785
01:20:30,160 –> 01:20:36,080
Continue in for loop. The python continue statement, on the other hand, will immediately terminate the
786
01:20:36,080 –> 01:20:42,240
current loop iteration. The execution jumps to the top of the loop, and re-evaluate the expression. It will
787
01:20:42,240 –> 01:20:47,360
determine whether the loop will execute again with the next iteration, or if it will terminate now and
788
01:20:47,360 –> 01:20:52,240
proceed with the first statement following the loop, like it would with the break reserved keyword that you
789
01:20:52,240 –> 01:20:57,600
have seen earlier. Let’s have a look at the same example you have seen in the break statement.
790
01:20:58,320 –> 01:21:01,520
But, instead of break, you will now use the continue statement.
791
01:21:02,320 –> 01:21:07,360
Continue is also a control statement, but the only difference is that it will only skip the current
792
01:21:07,360 –> 01:21:12,720
iteration in the loop and continues with the next iteration, then executes the rest of the iterations anyway,
793
01:21:12,720 –> 01:21:18,640
until it reaches the last item of the sequence. In the above example, as soon as the
794
01:21:18,640 –> 01:21:23,760
loop encounters the item blue, it will enter the if statement block where the continue statement will
795
01:21:23,760 –> 01:21:28,560
skip the current item in the loop then continues to the next iteration, with the item yellow.
796
01:21:29,280 –> 01:21:35,680
This will result in printing out red, green and yellow, since blue has been skipped over.
797
01:21:35,680 –> 01:21:40,320
Goof, but how can you execute some code after the for loop statement and still keep your code clear?
798
01:21:41,120 –> 01:21:48,000
else in for loop. The common construct is to run a loop and search for an item. If the item is found,
799
01:21:48,000 –> 01:21:54,160
we break out of the loop using the break statement. There are only two scenarios in which the loop may end.
800
01:21:54,160 –> 01:21:59,920
The 1st one is when the item is found and break is encountered. The 2nd scenario is when the
801
01:21:59,920 –> 01:22:05,360
loop ends without encountering a break statement. Now, we may want to know which one of these is the
802
01:22:05,360 –> 01:22:11,200
reason for a loop’s completion. One method is to set a flag, and then check it once the loop ends.
803
01:22:12,000 –> 01:22:18,240
Another one is to use an optional else clause, at the end of for loop. The else clause will be executed
804
01:22:18,240 –> 01:22:23,200
if the loop terminates naturally, through exhaustion of the iterable object.
805
01:22:23,200 –> 01:22:28,880
On the other hand, if the loop terminates prematurely with break, the else clause will not be executed at all!
806
01:22:29,760 –> 01:22:35,040
Here is the basic structure of for else loop. First let’s assume you declared a list with
807
01:22:35,040 –> 01:22:41,440
some colors inside. Now you want to search for a specific item, which in that case is the color blue.
808
01:22:42,240 –> 01:22:46,880
If you find it, you want to break this loop then continue with the next statements after the loop.
809
01:22:47,600 –> 01:22:53,360
But, on the other hand, if you didn’t find that item, you want to execute some code first then,
810
01:22:53,360 –> 01:22:58,240
and only then, execute the same code that if you did found it. And proceed with the rest of your code.
811
01:22:59,040 –> 01:23:04,320
Loops can be nested in python, as they can with other programming languages.
812
01:23:05,120 –> 01:23:11,600
The program first encounters the outer loop, executing its first iteration. This first iteration
813
01:23:11,600 –> 01:23:16,800
triggers the inner nested loop, which then runs to completion. Thus, the inner loop will
814
01:23:16,800 –> 01:23:22,880
be executed one time, for each iteration of the outer loop. Awesome, you are now able to write
815
01:23:22,880 –> 01:23:28,000
your own iterations then repeat a block of code, until you have reached the last item of a list.
816
01:23:28,720 –> 01:23:34,000
But, in most cases you would like to execute a group of statements for a fixed number of times, right?
817
01:23:34,000 –> 01:23:40,240
Fortunately, python provides a very simple way to repeat an action, for a specific number of times.
818
01:23:40,240 –> 01:23:45,200
The built-in function range (), which generates a sequence of numbers from zero
819
01:23:45,200 –> 01:23:50,880
up to, but not including, a specified number. A range function has three parameters, which are:
820
01:23:50,880 –> 01:23:57,200
starting, ending and a step parameters. You can define each one of them as range: start, stop, step size.
821
01:23:57,200 –> 01:24:02,880
This function does not store all the values in memory, it would be inefficient.
822
01:24:03,680 –> 01:24:10,720
So, it remembers the start, stop and step parameters, and generates the next number on the go!
823
01:24:10,720 –> 01:24:18,240
Let’s take a look at how range function can be used with for loop. for i in range(7): print i.
824
01:24:18,240 –> 01:24:23,920
In the above example, the sequence starts from 0 and ends at 6 because the ending parameter is 7, and is not included.
825
01:24:24,640 –> 01:24:30,880
The default step is 1, if not provided. Therefore, the for execution jumps one step for each new
826
01:24:30,880 –> 01:24:35,440
iteration and will result in printing out all the numbers from 0 through 6, included.
827
01:24:36,080 –> 01:24:41,760
As you may have noticed, the range() function starts from 0 by default. But you can start the
828
01:24:41,760 –> 01:24:48,960
range at another number, by specifying the start parameter. For instance, for i in range(2, 7): print i.
829
01:24:49,760 –> 01:24:55,680
This will result in printing out all the numbers from 2 through 6, included. You can even generate a range
830
01:24:55,680 –> 01:25:03,920
of negative numbers as well. for i in range(-5, 0):print i. This example will result in printing
831
01:25:03,920 –> 01:25:10,000
out all numbers from -5 through -1, included, since the ending parameter is never included, remember?
832
01:25:10,880 –> 01:25:16,960
Finally but not least, the range increments by 1, its default behavior. But, you can also specify a
833
01:25:16,960 –> 01:25:24,080
different increment, by specifying a step size parameter. Here is an example, for i in range(2, 7, 2):
834
01:25:24,080 –> 01:25:30,640
print i. The sequence starts from 2 and ends at 6, because the ending parameter is 7 and the step is 2.
835
01:25:30,640 –> 01:25:37,840
Therefore, the for execution jumps 2 steps after each iteration, and it will result in
836
01:25:37,840 –> 01:25:44,880
printing out the numbers 2, 4 and 6. Wow, that is a lot to take in, right? Fortunately for you, this
837
01:25:44,880 –> 01:25:50,000
is the last part of this tutorial. You have seen how to iterate through strings, lists or any other iterable objects.
838
01:25:50,000 –> 01:25:56,160
But you still don’t have any idea how to get their respective indexes?
839
01:25:56,880 –> 01:26:01,200
You can use the range() function in for loops, to iterate through a sequence of numbers.
840
01:26:02,000 –> 01:26:07,200
Plus, it can be combined with the len() function to iterate through a sequence, using the indexing as
841
01:26:07,200 –> 01:26:14,880
follows. This way you can get the exact position of the current item in the list! That is great, however,
842
01:26:14,880 –> 01:26:19,760
in most such cases, it is convenient to use the enumerate() function, which results in the
843
01:26:19,760 –> 01:26:25,600
same output but simplify the way you write your python code. Indeed, the enumerate() function will
844
01:26:25,600 –> 01:26:30,720
provide a tuple that you can then unpack to get both the index and the value of the current item…
845
01:26:31,440 –> 01:26:36,800
Here is an example with the exact same result. In this tutorial you have learned about a new
846
01:26:36,800 –> 01:26:42,560
structure in python: iterations, also known as loops. This is one of the statements
847
01:26:42,560 –> 01:26:47,840
which control the flow of execution in your program, and execute a block of code, repeatedly.
848
01:26:48,640 –> 01:26:54,320
All of these concepts are crucial to develop more advanced python code and reduce its complexity.
849
01:26:55,120 –> 01:27:01,120
In the next tutorial, you will discover another loop statement: the WHILE structure. This structure
850
01:27:01,120 –> 01:27:06,800
also facilitates iteration and repeats a statement or block of statements, till some condition is met!
851
01:27:07,600 –> 01:27:13,680
So do not waste any minute of it and join us in the next episode. If you like this video, please
852
01:27:13,680 –> 01:27:19,760
do not forget to give a thumb up and subscribe our channel. In this brand new video, you will now
853
01:27:19,760 –> 01:27:25,520
discover iterations in python using FOR and WHILE Loops, so you can improve the execution flow of
854
01:27:25,520 –> 01:27:30,640
your program. Please do not forget to smash that like button for the youtube algorithm,
855
01:27:30,640 –> 01:27:34,720
since it does really help supporting us and providing new free content, once a week.
856
01:27:35,760 –> 01:27:40,480
Are you interested to be part of this community? All the links are in the description below.
857
01:27:40,480 –> 01:27:47,120
Now let’s play this video… Loops in python are an efficient method for optimizing your
858
01:27:47,120 –> 01:27:53,840
code, and executing multiple statements. If the same block of code has to be executed multiple times,
859
01:27:53,840 –> 01:27:58,720
over and over, you can put it into a loop to perform multiple iterations and get the same
860
01:27:58,720 –> 01:28:05,360
desired output. Consequently, it saves a lot of effort and reduces complexity of the code, as well.
861
01:28:06,240 –> 01:28:11,520
Basically there are two types of iteration, in programming languages: definite iteration,
862
01:28:11,520 –> 01:28:16,880
in which the number of repetitions is specified explicitly, and in advance.
863
01:28:16,880 –> 01:28:22,000
This kind of iteration is performed using for loops and has already been discussed in the previous video.
864
01:28:22,800 –> 01:28:28,800
Indefinite iteration, in which the code block executes, until some condition is met.
865
01:28:28,800 –> 01:28:33,200
This iteration is performed using while loops and will be discussed in the next section.
866
01:28:35,600 –> 01:28:41,360
While loops. The while statement in python is a bit different from what you usually use, in other
867
01:28:41,360 –> 01:28:46,880
programming languages. Rather than iterating over a numeric progression, or over the items
868
01:28:46,880 –> 01:28:52,960
of an iterable, like: strings, dictionaries, lists, tuples and sets or any other iterable objects.
869
01:28:53,760 –> 01:28:58,000
Wwhile loops are traditionally used when you have a block of code which you want to repeat, indefinitely,
870
01:28:58,000 –> 01:29:04,640
and as long as an expression is true or a particular condition is met. You generally use
871
01:29:04,640 –> 01:29:10,720
this loop when you do not know beforehand how many iterations are going to take place. Do not forget
872
01:29:10,720 –> 01:29:15,680
to contrast the while statement with the for loop, used when you want to repeat a block of code for
873
01:29:15,680 –> 01:29:21,920
a fixed number of times. Okay, but What does it look like, and How to control the execution flow of your
874
01:29:21,920 –> 01:29:28,000
program using while loops? Let’s move forward and have a closer look at the syntax of the while statement.
875
01:29:30,000 –> 01:29:37,920
while condition: statement 1, statement 2, etc. Here is a simple declaration of while loop. First the
876
01:29:37,920 –> 01:29:43,920
execution starts and checks whether the condition is true. When the condition is true, it enters the
877
01:29:43,920 –> 01:29:50,000
while loop and executes the indented statements. Inside the while loop, after one iteration, the
878
01:29:50,000 –> 01:29:55,760
condition is checked again, and this process continues until the condition evaluates to false.
879
01:29:55,760 –> 01:30:00,880
As soon as the condition is false, its execution skips the while loop and moves to the next
880
01:30:00,880 –> 01:30:07,360
following statements in the program. Please also note that python interprets any non-zero value or
881
01:30:07,360 –> 01:30:14,240
non-empty container as true… Consequently None, zero and empty sequence will be interpreted as false!
882
01:30:14,960 –> 01:30:20,080
And if the condition is false at the start, the while loop will never be executed!
883
01:30:20,080 –> 01:30:24,960
Let’s have a more practical and very simple example, so you can discover and understand
884
01:30:24,960 –> 01:30:31,680
how to properly use python loops in your program. First declare an integer i with the value of 6.
885
01:30:32,480 –> 01:30:37,520
Then you will iterate and execute the block of code inside the while loop, which will print i, and decrement the variable.
886
01:30:37,520 –> 01:30:43,920
Thus on each iteration, this expression will be evaluated again, until it
887
01:30:43,920 –> 01:30:50,240
becomes false or zero. In that specific case, if you are not used to decrement a variable with
888
01:30:50,240 –> 01:30:56,640
this syntax variable -= 1, please check the suggested video on the operators in python.
889
01:30:57,280 –> 01:31:03,760
In the above example, the execution starts with the variable i = 6, then it tests whether the
890
01:31:03,760 –> 01:31:10,480
expression i is true or i = 0. As long as the condition is true, it will enter and execute
891
01:31:10,480 –> 01:31:16,240
the statements inside the loop, until it becomes false. Then, it will proceed with the statements
892
01:31:16,240 –> 01:31:21,680
after the loop. It is a very simple example of how you can use while loops in python.
893
01:31:22,480 –> 01:31:27,440
Of course, there are many other alternatives you can use, and go through any iterable object as well.
894
01:31:27,440 –> 01:31:33,040
Here is another example, when you want to iterate through every single item of a list…
895
01:31:33,840 –> 01:31:37,920
First, let’s declare a list of strings and add a few strings to this list.
896
01:31:38,720 –> 01:31:42,640
Then, you will iterate through the list using while statement and print each item.
897
01:31:43,360 –> 01:31:48,800
Very simple, right? The execution will start and check whether the expression is true.
898
01:31:48,800 –> 01:31:54,960
In that case, if the list is not empty yet, then it will look for the first item in the sequence or any iterable object.
899
01:31:55,680 –> 01:32:00,880
After executing the statements in the block, which pop the current item into a variable and print it out,
900
01:32:00,880 –> 01:32:06,080
it will re-evaluate the condition and the process will continue, over and over again.
901
01:32:06,080 –> 01:32:11,520
Looks for the next item in this sequence and print it out, until it reaches the last item of that list, which is yellow,
902
01:32:11,520 –> 01:32:17,280
and becomes empty. Did you get it? Here is a last example, when you want to iterate through every
903
01:32:17,280 –> 01:32:22,800
single character of a string, because a string is a list of characters. This code will print out
904
01:32:22,800 –> 01:32:27,840
every single character of my string, until the condition letter different than exclamation point
905
01:32:27,840 –> 01:32:34,640
evaluates to false. Or if the opposite letter equals exclamation point becomes true! And here you
906
01:32:34,640 –> 01:32:40,960
are, you just wrote your first python iteration using while loops. It was not that hard!
907
01:32:40,960 –> 01:32:46,000
Let’s move forward to the next section, so you that can alter the behavior of your while loop during its execution.
908
01:32:46,960 –> 01:32:51,680
In each example you have seen so far the entire body of the while loop is executed, on each iteration.
909
01:32:51,680 –> 01:32:58,480
it is all very well, but quite limited, right? Fortunately, python provides two
910
01:32:58,480 –> 01:33:03,840
keywords: break and continue, which terminate the current loop iteration, prematurely, without even
911
01:33:03,840 –> 01:33:09,120
checking the test expression. Please note that break and continue work the same way with for
912
01:33:09,120 –> 01:33:14,960
loops as with while loops. Now let’s take a look at how you can use these control statements.
913
01:33:15,760 –> 01:33:22,160
Break in python is a control flow statement that is used to exit the execution,
914
01:33:22,160 –> 01:33:27,920
as soon as the break is encountered. The python break statement immediately terminates a loop,
915
01:33:27,920 –> 01:33:33,760
entirely. Consequently, the program execution then proceeds to the first statement following the loop body.
916
01:33:33,760 –> 01:33:41,200
Let’s understand how you can use a break statement in while loop, using an example.
917
01:33:41,200 –> 01:33:46,880
So, Let’s say you have a list with strings as items. You want to exit the loop using the break statement,
918
01:33:46,880 –> 01:33:52,960
as soon as the desired string is encountered. In the above example, as soon as the loop encounters
919
01:33:52,960 –> 01:33:58,080
the space character it will enter the if statement block, where the break statement will exit the loop
920
01:33:58,080 –> 01:34:05,120
straight away. Thus, it will only print digital. Did you get it? Then let’s practice, it’s your turn now…
921
01:34:05,120 –> 01:34:10,480
Suppose you have a list of colors: red, green, blue, yellow, and you want to print each color
922
01:34:10,480 –> 01:34:17,840
but break the loop when it encounters blue. How would you proceed, What output would you get?
923
01:34:26,880 –> 01:34:31,680
Here is a solution: first let’s declare a list, in which you added all of the colors:
924
01:34:31,680 –> 01:34:37,920
red, green, blue, yellow. Then iterate throughout this list, using while statement. while colors:
925
01:34:38,720 –> 01:34:44,080
it will create an iterator, and the variable color will get the first value inside the list, on each
926
01:34:44,080 –> 01:34:49,520
iteration, until the execution has reached the last item in the sequence and that the list becomes empty.
927
01:34:50,240 –> 01:34:55,200
When it encounters the item blue, you do not want to execute the code that comes next, but
928
01:34:55,200 –> 01:35:01,280
exit the loop. if color == blue: break. In all the other cases, as it would be with an
929
01:35:01,280 –> 01:35:05,760
else statement, you want to print out the current col