Introduction to Python

6. Writing and Executing First Python Program

Python सीखने का सबसे अच्छा तरीका है कि आप तुरंत अपना पहला प्रोग्राम लिखें और run करें। यह chapter step-by-step आपको पहला Python program बनाने और execute करने की प्रक्रिया सिखाएगा।


6.1 Python Program लिखना

Python program एक text file होती है जिसका extension .py होता है।
Example (Hello World Program):

# पहला Python प्रोग्राम
print("Hello, World!")

6.2 Python Program Execute करने के तरीके

(a) Using Python IDLE

  1. Python install करने के बाद IDLE open करें।
  2. Shell window में लिखें: print("Hello, Python!") और Enter दबाएँ → output मिलेगा।
  3. File → New File → code लिखें → Save as program.py → Run → Run Module (F5)।

(b) Using Command Prompt / Terminal

  1. Program को किसी text editor (Notepad, VS Code आदि) में लिखें और save करें, जैसे hello.py
  2. Command Prompt (Windows) या Terminal (Linux/Mac) open करें।
  3. File location पर जाएँ और type करें: python hello.py Output: Hello, World!

(c) Using Jupyter Notebook

  1. Anaconda या pip से Jupyter install करें। pip install notebook jupyter notebook
  2. Browser में notebook open होगा।
  3. Cell में code लिखें और Run करें: print("Hello, Jupyter!")

6.3 Inline Execution (Interactive Mode)

Python interpreter को सीधे run करके भी code लिखा जा सकता है।

python

फिर लिखें:

>>> print("Hello, Interactive Mode!")
Hello, Interactive Mode!

6.4 Example Programs

Example 1: Two Numbers Add करना

a = 10
b = 20
print("Sum =", a + b)

Output:

Sum = 30

Example 2: User Input लेना

name = input("Enter your name: ")
print("Welcome,", name)

Output:

Enter your name: Anand
Welcome, Anand

📌 Quick Note for Students:

  • हमेशा program को .py extension से save करें।
  • Beginners के लिए सबसे आसान तरीका है कि IDLE या VS Code से program run करें।
  • Execution के दौरान error आने पर error message ध्यान से पढ़ें – वही debugging की पहली step है।