Concepts of Interpreter based programming language, structure, indentation, execution

Python आपके code को line-by-line तुरंत पढ़ता और execute करता है, code के blocks group करने के लिए curly brackets के बजाय ख़ाली spaces इस्तेमाल करते हुए।

10 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

ग़ायब Semi-Colons का रहस्य

आपके Semester 1 BCA104 C programming labs में, आपने घंटे एक अकेला ग़ायब semi-colon या एक ग़लत जगह रखे curly brace { } को ढूँढने में बिताए जो आपके पूरे program को compile होने से रोकता था। अब, Python को देखिए। lines के अंत में कोई semi-colons नहीं, functions या loops mark करने के लिए कोई curly braces नहीं, और execute करने से पहले आपको एक compile command चलाने की ज़रूरत नहीं। computer कैसे जानता है कि एक statement कहाँ ख़त्म होता है, कौन सी lines एक loop के अंदर हैं, और एक EXE file के बिना code कैसे चलाएँ?

Theory

Live Translator बनाम Book Publisher

एक compiler को एक book publisher की तरह सोचिए: यह English में लिखी एक पूरी manuscript लेता है, पूरी किताब को Hindi में translate करता है, और एक स्थायी copy (EXE file) print करता है। अगर page 50 पर एक error है, printing press पूरी तरह रुक जाती है। एक interpreter एक live tourist guide की तरह है: वे English में एक वाक्य सुनते हैं, इसे तुरंत Hindi में translate करते हैं, वह action execute करते हैं, और फिर अगले वाक्य पर जाते हैं। अगर page 50 पर एक error होता है, guide रुकने से पहले पहले 49 वाक्य perfectly सँभालता है।

Theory

मूल Concepts: Interpreters और Structure

एक interpreter एक program है जो source code को पहले एक machine code binary में compile किए बिना सीधे line by line execute करता है। इसके कारण, Python code की एक अनोखी structure है। एक newline character एक statement के अंत को mark करता है, semi-colons की ज़रूरत ख़त्म करते हुए। code के blocks, जैसे एक loop या function का body, पूरी तरह indentation (एक line की शुरुआत में ख़ाली spaces) से परिभाषित होते हैं, braces के बजाय। लिखने से execution तक का workflow तत्काल है, testing को तेज़ बनाते हुए।

At a glance

Table 1: C और Python execution models के बीच मुख्य अंतर।

FeatureBCA104 C Language (Compiled)BCA204 Python (Interpreted)
Execution Modeपूरे code को एक बार में एक EXE file में translate करता है।runtime पर code को line by line translate और run करता है।
Block Markerscode group करने के लिए curly braces { } इस्तेमाल करता है।code group करने के लिए white spaces (indentation) इस्तेमाल करता है।
Statement Endinglines ख़त्म करने के लिए एक semi-colon ; चाहिए।lines ख़त्म करने के लिए एक newline character इस्तेमाल करता है।
Error Detectionचलने से पहले सारे syntax errors report करता है।बिल्कुल उस line पर रुकता है जहाँ एक error होता है।

Theory

एक Worked Example: Pocket Money Track करना

आइए इसे हमारे चल रहे subject project: PocketMoney के साथ काम में देखें। हम अपना balance जाँचना चाहते हैं और अगर यह 100 rupees से नीचे गिरे तो एक warning print करना चाहते हैं।

देखिए कि Python इस logic को कैसे संरचित करता है जो आपने C में किया उसकी तुलना में। condition line के अंत में colon : पर ग़ौर कीजिए, उसके बाद indented lines जो block बनाती हैं।

Practical

PocketMoney Structure Example

balance = 500
spend = 450
balance = balance - spend

if balance < 100:
    print("Warning!")
    print("Low PocketMoney balance.")

print("Transaction completed.")

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Output को कदम-दर-कदम ट्रेस करना

ऊपर के Python script का मन में विश्लेषण कीजिए। यह code चलने पर screen पर बिल्कुल क्या print होगा? tap करने से पहले इसे line by line track करने की कोशिश कीजिए।

Show the answer

output होगा:

Warning!

Low PocketMoney balance.

Transaction completed.

क्यों? Line 1 और 2 values set करती हैं। Line 3 balance को 50 के रूप में calculate करती है। Line 5 50 < 100 को True के रूप में evaluate करती है। क्योंकि यह true है, interpreter दो indented lines (lines 6 और 7) execute करता है। Line 9 unindented है, यानी यह block के बाहर है, तो यह अंत में सामान्य रूप से execute होती है।

Quiz

क्या होगा अगर Python interpreter एक 10-line program की line 4 पर एक syntax error से टकराए?

  1. program बिल्कुल चलना शुरू नहीं होगा।
  2. Lines 1, 2, और 3 execute होंगी, फिर program line 4 पर crash होगा।
  3. interpreter line 4 skip करता है और बाक़ी lines execute करता है।
  4. program पूरी तरह चलता है पर अंत में एक warning message दिखाता है।
Show the answer

Lines 1, 2, और 3 execute होंगी, फिर program line 4 पर crash होगा।

क्योंकि Python interpreter-based है, यह code को क्रमिक रूप से line by line execute करता है। यह line 4 पर syntax error से टकराने और execution रोकने से पहले lines 1, 2, और 3 सफलतापूर्वक execute करेगा। यह एक classic university exam viva सवाल है!

Quiz

निम्न code snippet को ध्यान से पढ़िए: if balance < 100: print("Low balance!") print("Check expenses.") Python execution engine क्या report करेगा?

  1. यह perfectly चलेगा और दोनों lines print करेगा।
  2. यह एक Compilation Error फेंकेगा क्योंकि कोई curly braces नहीं हैं।
  3. यह एक IndentationError फेंकेगा।
  4. यह सिर्फ़ पहला message print करेगा।
Show the answer

यह एक IndentationError फेंकेगा।

Python के लिए ज़रूरी है कि एक ही block के भीतर सारी lines का बिल्कुल एक जैसा indentation हो। पहले print में 4 spaces हैं, पर दूसरे print में 2 spaces हैं। यह mismatch एक सीधी IndentationError पैदा करता है।

Watch out

Classic जाल: Tabs और Spaces मिलाना

university lab exams में, सबसे बार-बार marks-गँवाने वाली ग़लती अदृश्य code layout error है। अगर आप एक line के लिए TAB key दबाते हैं और अगली line के लिए Spacebar चार बार दबाते हैं, code आपके monitor पर perfectly aligned दिख सकता है, पर Python interpreter एक IndentationError: unindent does not match any outer indentation level फेंकेगा। सुरक्षित रहने के लिए हमेशा अपने text editor को Tabs को Spaces में अपने-आप convert करने के लिए configure कीजिए।

Theory

आपके भविष्य के Semesters से जोड़ना

Python के line-by-line interpreter model पर महारत debugging को तेज़ बनाती है। Semester 3 (BCA303) में, आप SQLite databases से तुरंत interact करने के लिए Python scripts लिखेंगे। BCA302 में, आप statistics के लिए R इस्तेमाल करेंगे, जो एक बहुत मिलती-जुलती interactive terminal execution शैली पर निर्भर करता है। कोई compilation प्रतीक्षा समय नहीं मतलब तेज़ development!

Summary

Key takeaways

  • Python एक interpreted language है जो runtime के दौरान code को line by line translate करती है।
  • Statements एक newline character से ख़त्म होते हैं, semi-colons की ज़रूरत हटाते हुए।
  • code के blocks curly brackets के बजाय indentation spaces से परिभाषित होते हैं।
  • code blocks के अंदर असंगत spacing एक तुरंत IndentationError trigger करती है।
  • Errors बिल्कुल उस line पर execution रोकते हैं जहाँ वे होते हैं, पिछले चरण execute किए हुए छोड़ते हुए।
  • Memory Hook: सुसंगत spaces curly braces की जगह लेते हैं!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Fundamentals

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concepts of Interpreter based programming language, structure, indentation, execution · Programming Skills · Gri-Learn