Facebook Pixel
Searching...
فارسی
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Learning Python

Learning Python

توسط Mark Lutz 2013 1214 صفحات
4.01
3k+ امتیازها
گوش دادن
گوش دادن

نکات کلیدی

1. انواع داده‌های اصلی پایتون: اعداد، رشته‌ها، لیست‌ها و دیکشنری‌ها

اشیای داخلی نوشتن برنامه‌ها را آسان می‌کنند.

بلوک‌های ساختاری چندمنظوره. انواع داده‌های اصلی پایتون پایه‌ای قوی برای برنامه‌نویسی فراهم می‌کنند. اعداد عملیات ریاضی را پشتیبانی می‌کنند، در حالی که رشته‌ها پردازش متن را انجام می‌دهند. لیست‌ها مجموعه‌های مرتب از آیتم‌ها ارائه می‌دهند و دیکشنری‌ها نگاشت‌های کلید-مقدار را فراهم می‌کنند. این انواع می‌توانند قابل تغییر یا غیرقابل تغییر باشند که بر نحوه تغییر آن‌ها تأثیر می‌گذارد.

عملیات قدرتمند. هر نوع دارای مجموعه‌ای از عملیات و متدهای داخلی است. برای مثال:

  • رشته‌ها: برش، الحاق و متدهایی مانند split() و join()
  • لیست‌ها: اندیس‌گذاری، برش، append() و extend()
  • دیکشنری‌ها: دسترسی مبتنی بر کلید، update() و متد keys()

انعطاف‌پذیری و کارایی. انواع اصلی پایتون برای سهولت استفاده و عملکرد طراحی شده‌اند. آن‌ها می‌توانند برای ایجاد ساختارهای داده پیچیده تو در تو شوند و بسیاری از عملیات برای سرعت بهینه‌سازی شده‌اند. این ترکیب از سادگی و قدرت، پایتون را برای طیف گسترده‌ای از وظایف برنامه‌نویسی مناسب می‌سازد.

2. تایپ پویا و ارجاعات در پایتون

نام‌ها نوع ندارند، اما اشیا دارند.

انعطاف‌پذیری نوع. پایتون از تایپ پویا استفاده می‌کند، به این معنی که متغیرها می‌توانند به اشیای هر نوعی اشاره کنند. این امکان کد انعطاف‌پذیرتر و مختصرتر را فراهم می‌کند، زیرا همان متغیر می‌تواند در زمان‌های مختلف انواع مختلفی از داده‌ها را نگه دارد. با این حال، نیاز به توجه دقیق دارد تا اطمینان حاصل شود که در طول عملیات سازگاری نوعی وجود دارد.

مدل ارجاع. در پایتون، متغیرها اساساً نام‌هایی هستند که به اشیاء در حافظه اشاره می‌کنند. هنگامی که مقداری را به یک متغیر اختصاص می‌دهید، در حال ایجاد ارجاع به یک شیء هستید. این موضوع پیامدهای مهمی دارد:

  • چندین نام می‌توانند به یک شیء اشاره کنند
  • اشیای قابل تغییر می‌توانند در محل تغییر کنند و بر تمام ارجاعات تأثیر بگذارند
  • اشیای غیرقابل تغییر هنگام "تغییر" اشیای جدیدی ایجاد می‌کنند

درک این مدل ارجاع برای نوشتن کد پایتون کارآمد و بدون اشکال، به ویژه هنگام کار با آرگومان‌های تابع و ساختارهای داده مشترک، بسیار مهم است.

3. نحو و ساختار دستورات پایتون

مرزهای بلوک و دستور به‌طور خودکار تشخیص داده می‌شوند.

تورفتگی مهم است. برخلاف بسیاری از زبان‌های دیگر، پایتون از تورفتگی برای تعریف بلوک‌های کد استفاده می‌کند. این به‌طور طراحی شده کد تمیز و خوانا را تحمیل می‌کند. کاراکتر دو نقطه (:) برای معرفی بلوک‌ها در دستورات مرکب مانند if، for، while و تعریف توابع استفاده می‌شود.

انواع دستورات. پایتون انواع مختلفی از دستورات را ارائه می‌دهد:

  • دستورات انتساب (=، += و غیره)
  • دستورات شرطی (if، elif، else)
  • دستورات حلقه (for، while)
  • تعریف توابع و کلاس‌ها (def، class)
  • مدیریت استثناها (try، except، finally)

سادگی و خوانایی. نحو پایتون به‌گونه‌ای طراحی شده است که واضح و شهودی باشد. بسیاری از عملیات‌هایی که در زبان‌های دیگر به چندین خط نیاز دارند، می‌توانند به‌طور مختصر در پایتون بیان شوند، مانند درک لیست و عبارات شرطی.

4. تکرار و ساختارهای حلقه در پایتون

حلقه‌های for و درک لیست اغلب ساده‌ترین و سریع‌ترین راه برای انجام کار واقعی در پایتون هستند.

ابزارهای تکرار قدرتمند. پایتون چندین روش برای تکرار بر روی داده‌ها ارائه می‌دهد:

  • حلقه‌های for برای توالی‌ها و دیگر قابل تکرارها
  • حلقه‌های while برای تکرار مبتنی بر شرط
  • درک لیست برای تبدیل داده مختصر
  • عبارات مولد برای تکرار کارآمد از نظر حافظه

پروتکل قابل تکرار. مدل تکرار پایتون بر اساس پروتکل قابل تکرار است که به اشیای سفارشی اجازه می‌دهد قابل تکرار باشند. این رویکرد یکپارچه به این معنی است که بسیاری از توابع و ساختارهای داخلی به‌طور یکپارچه با انواع تعریف‌شده توسط کاربر که پروتکل را پیاده‌سازی می‌کنند، کار می‌کنند.

کارایی و خوانایی. تکرار در پایتون به‌گونه‌ای طراحی شده است که هم کارآمد و هم آسان برای خواندن باشد. درک لیست، به‌عنوان مثال، می‌تواند اغلب حلقه‌های for چند خطی را با یک خط کد بیانی جایگزین کند. توابع داخلی range()، enumerate() و zip() قدرت و انعطاف‌پذیری ابزارهای تکرار پایتون را بیشتر می‌کنند.

5. توابع: بلوک‌های کد قابل استفاده مجدد در پایتون

توابع ابتدایی‌ترین ساختار برنامه‌ای هستند که پایتون برای به حداکثر رساندن استفاده مجدد از کد و به حداقل رساندن تکرار کد ارائه می‌دهد.

سازماندهی کد مدولار. توابع در پایتون به شما اجازه می‌دهند تا قطعات قابل استفاده مجدد از کد را محصور کنید. این ترویج می‌کند:

  • استفاده مجدد از کد و کاهش تکرار
  • نگهداری و اشکال‌زدایی آسان‌تر
  • بهبود خوانایی و سازماندهی

آرگومان‌های انعطاف‌پذیر. توابع پایتون از انواع مختلف پارامترها پشتیبانی می‌کنند:

  • آرگومان‌های موقعیتی
  • آرگومان‌های کلیدی
  • مقادیر پیش‌فرض آرگومان
  • لیست‌های آرگومان با طول متغیر (*args، **kwargs)

مقادیر بازگشتی و اثرات جانبی. توابع می‌توانند به‌طور صریح با استفاده از دستور return مقادیر را بازگردانند، یا به‌طور ضمنی None را بازگردانند. آن‌ها همچنین می‌توانند با تغییر اشیای قابل تغییر یا متغیرهای جهانی اثرات جانبی ایجاد کنند. درک تفاوت بین مقادیر بازگشتی و اثرات جانبی برای نوشتن کد واضح و قابل پیش‌بینی بسیار مهم است.

6. حوزه‌ها و فضای نام‌ها در پایتون

هنگامی که از نامی در برنامه استفاده می‌کنید، پایتون نام را در چیزی که به عنوان فضای نام شناخته می‌شود ایجاد، تغییر یا جستجو می‌کند—مکانی که نام‌ها در آن زندگی می‌کنند.

قانون LEGB. پایتون از قانون LEGB برای حل نام استفاده می‌کند:

  • محلی: نام‌هایی که در تابع فعلی تعریف شده‌اند
  • محصور: نام‌هایی در حوزه محلی هر تابع محصور
  • جهانی: نام‌هایی که در سطح بالای ماژول تعریف شده‌اند
  • داخلی: نام‌هایی در ماژول داخلی

انتساب نام و دستور global. به‌طور پیش‌فرض، اختصاص مقداری به یک نام در داخل یک تابع یک متغیر محلی ایجاد یا تغییر می‌کند. دستور global به شما اجازه می‌دهد تا به‌طور صریح با متغیرهای جهانی در داخل حوزه تابع کار کنید.

پیامدهای فضای نام. درک حوزه‌ها و فضای نام‌ها برای:

  • اجتناب از تعارضات نام‌گذاری
  • مدیریت طول عمر و قابلیت مشاهده متغیر
  • نوشتن کد قابل نگهداری و مدولارتر

استفاده صحیح از حوزه‌ها می‌تواند به ایجاد تعاریف تابع خودکفا و قابل استفاده مجدد کمک کند.

7. ماژول‌ها و سازماندهی کد در پایتون

ماژول‌ها به سادگی بسته‌هایی از متغیرها هستند—یعنی فضای نام‌ها.

سازماندهی کد. ماژول‌ها در پایتون به عنوان وسیله اصلی سازماندهی برنامه‌های بزرگ‌تر عمل می‌کنند:

  • هر فایل .py یک ماژول است
  • ماژول‌ها می‌توانند متغیرها، توابع و کلاس‌ها را شامل شوند
  • ماژول‌ها می‌توانند ماژول‌های دیگر را وارد کنند

مدیریت فضای نام. ماژول‌ها فضای نام‌های جداگانه ایجاد می‌کنند که به جلوگیری از تعارضات نام‌گذاری در پروژه‌های بزرگ کمک می‌کند. این رویکرد مدولار ترویج می‌کند:

  • استفاده مجدد از کد
  • سازماندهی منطقی عملکرد
  • نگهداری و همکاری آسان‌تر

مکانیسم‌های وارد کردن. پایتون روش‌های انعطاف‌پذیری برای وارد کردن و استفاده از ماژول‌ها ارائه می‌دهد:

  • import module
  • from module import name
  • from module import *
  • import module as alias

درک این مکانیسم‌های وارد کردن و پیامدهای آن‌ها برای ساختاردهی مؤثر برنامه‌های پایتون و مدیریت وابستگی‌ها بین بخش‌های مختلف کد شما بسیار مهم است.

آخرین به‌روزرسانی::

FAQ

What's Learning Python about?

  • Comprehensive Guide: Learning Python by Mark Lutz is a detailed introduction to the Python programming language, covering both basic and advanced topics.
  • Core Concepts: It focuses on Python's core concepts, including syntax, data types, and object-oriented programming (OOP) principles.
  • Practical Approach: The book includes practical examples and exercises to help readers apply concepts in real-world scenarios, making it suitable for both beginners and experienced programmers.

Why should I read Learning Python?

  • Authoritative Resource: Written by Mark Lutz, a leading figure in the Python community, the book is well-respected and widely used in educational settings.
  • Structured Learning Path: It is organized in a logical progression, starting from basic concepts and gradually introducing more advanced topics, making it suitable for self-study.
  • Updated Content: The third edition includes updates on Python 3.x features, ensuring that readers learn the most current practices and tools available in the language.

What are the key takeaways of Learning Python?

  • Core Concepts Mastery: Readers will gain a solid understanding of Python's fundamental concepts, including data types, control structures, functions, and modules.
  • OOP Principles: The book provides in-depth coverage of object-oriented programming, teaching readers how to design and implement classes and objects.
  • Practical Skills Application: Through exercises and examples, readers will learn how to apply their skills in real-world programming tasks, preparing them for actual coding challenges.

What are the best quotes from Learning Python and what do they mean?

  • "Python is a language that emphasizes readability.": This highlights Python's design philosophy, prioritizing clear and understandable code for easier collaboration and maintenance.
  • "Functions are the most basic way of avoiding code redundancy.": This emphasizes the importance of functions in programming for code reuse and organization.
  • "Classes provide new local scopes.": This points out that classes create their own namespaces, helping avoid name clashes and maintain clarity in larger programs.

How does Learning Python approach Object-Oriented Programming?

  • In-Depth OOP Coverage: The book dedicates significant sections to explaining OOP concepts such as inheritance, encapsulation, and polymorphism.
  • Practical Class Design: Readers learn how to design and implement their own classes, including using special methods for operator overloading.
  • Real-World Examples: Numerous examples demonstrate OOP in action, bridging the gap between theory and practice.

What is dynamic typing in Python according to Learning Python?

  • No Type Declarations: Variables do not require explicit type declarations; types are determined automatically at runtime based on the objects they reference.
  • Flexibility in Coding: This allows for greater flexibility, as variables can reference objects of different types throughout the program's execution.
  • Supports Polymorphism: Dynamic typing supports polymorphism, meaning the same operation can be applied to different types of objects, enhancing code reusability.

How do Python lists differ from strings in Learning Python?

  • Mutability: Lists are mutable, meaning they can be changed in-place, while strings are immutable and cannot be altered after creation.
  • Data Structure: Lists can hold a collection of items of any type, including other lists, whereas strings are specifically sequences of characters.
  • Operations: Lists support a variety of operations, such as appending and removing items, while strings support operations like concatenation and slicing.

How does exception handling work in Learning Python?

  • try/except Structure: The book explains the try/except structure for catching and handling exceptions, allowing programs to recover from errors gracefully.
  • Raising Exceptions: Readers learn how to raise exceptions manually using the raise statement, useful for signaling errors in their own code.
  • Using finally for Cleanup: The book discusses using finally clauses to ensure cleanup actions are always performed, critical for resource management.

What is the significance of the import statement in Python according to Learning Python?

  • Module Access: The import statement allows access to functions, classes, and variables defined in other modules, promoting code reuse and organization.
  • Namespace Management: Importing a module creates a separate namespace, preventing name collisions between variables in different modules.
  • Dynamic Loading: Python modules can be imported dynamically, allowing for flexible program structures where components can be loaded as needed.

How does Learning Python explain the difference between mutable and immutable types?

  • Mutable Types: Mutable types, such as lists and dictionaries, can be changed in place, allowing modification without creating a new object.
  • Immutable Types: Immutable types, like strings and tuples, cannot be changed once created, with any modification resulting in a new object.
  • Impact on Performance: Understanding the difference affects memory management and performance, as mutable types can lead to unintended side effects if not handled carefully.

How do I define and call a function in Python according to Learning Python?

  • Defining Functions: Functions are defined using the def keyword, followed by the function name and parentheses containing any parameters.
  • Calling Functions: To call a function, use its name followed by parentheses, passing any required arguments.
  • Example: For instance, def add(a, b): return a + b defines a function, and calling add(2, 3) would return 5.

How does Learning Python help with debugging?

  • Error Messages and Stack Traces: Python provides detailed error messages and stack traces when exceptions occur, aiding in debugging efforts.
  • Using try/except for Debugging: Try/except blocks can catch exceptions during development, allowing testing and debugging without terminating the program.
  • Testing Frameworks: The book introduces testing frameworks like PyUnit and Doctest, which help automate testing and debugging processes.

نقد و بررسی

4.01 از 5
میانگین از 3k+ امتیازات از Goodreads و Amazon.

کتاب یادگیری پایتون نظرات متفاوتی دریافت می‌کند. بسیاری از جامعیت و وضوح آن تمجید می‌کنند و آن را برای مبتدیان و برنامه‌نویسان با تجربه ارزشمند می‌دانند. با این حال، برخی از طولانی بودن، تکراری بودن و کندی پیشرفت آن انتقاد می‌کنند. خوانندگان توضیحات دقیق و مقایسه‌های آن با زبان‌های دیگر را می‌پسندند، اما برخی آن را پرحرف و ضعیف سازمان‌دهی شده می‌دانند. این کتاب به خاطر پوشش تفاوت‌های پایتون 2.x و 3.x شناخته شده است. در حالی که برخی آن را منبعی ضروری می‌دانند، دیگران روش‌های یادگیری جایگزین یا کتاب‌های دیگری را برای رویکردی عملی‌تر به برنامه‌نویسی پایتون توصیه می‌کنند.

درباره نویسنده

مارک لوتز یکی از چهره‌های برجسته در جامعه پایتون است که به خاطر کارهای پیشگامانه‌اش در زمینه آموزش و ادبیات پایتون شناخته می‌شود. او نویسنده چندین کتاب پرفروش پایتون از جمله "برنامه‌نویسی پایتون"، "مرجع جیبی پایتون" و "یادگیری پایتون" است که همگی در ویرایش چهارم خود قرار دارند. لوتز از سال 1992 شروع به استفاده از پایتون کرد و از سال 1997 به تدریس آن پرداخته است. او بیش از 250 جلسه آموزشی پایتون برگزار کرده و به حدود 4,000 دانشجو آموزش داده است. کتاب‌های او حدود 250,000 نسخه فروش داشته و به چندین زبان ترجمه شده‌اند. لوتز دارای مدارک علوم کامپیوتر از دانشگاه ویسکانسین است و تجربه گسترده‌ای به عنوان یک توسعه‌دهنده نرم‌افزار حرفه‌ای دارد.

0:00
-0:00
1x
Dan
Andrew
Michelle
Lauren
Select Speed
1.0×
+
200 words per minute
Create a free account to unlock:
Requests: Request new book summaries
Bookmarks: Save your favorite books
History: Revisit books later
Ratings: Rate books & see your ratings
Try Full Access for 7 Days
Listen, bookmark, and more
Compare Features Free Pro
📖 Read Summaries
All summaries are free to read in 40 languages
🎧 Listen to Summaries
Listen to unlimited summaries in 40 languages
❤️ Unlimited Bookmarks
Free users are limited to 10
📜 Unlimited History
Free users are limited to 10
Risk-Free Timeline
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on Feb 25,
cancel anytime before.
Consume 2.8x More Books
2.8x more books Listening Reading
Our users love us
50,000+ readers
"...I can 10x the number of books I can read..."
"...exceptionally accurate, engaging, and beautifully presented..."
"...better than any amazon review when I'm making a book-buying decision..."
Save 62%
Yearly
$119.88 $44.99/year
$3.75/mo
Monthly
$9.99/mo
Try Free & Unlock
7 days free, then $44.99/year. Cancel anytime.
Settings
Appearance
Black Friday Sale 🎉
$20 off Lifetime Access
$79.99 $59.99
Upgrade Now →