Python Basics: Tokens, Variables, Types
Informatics Practices के लिए Python क्यों?
Informatics Practices पूरी तरह data के साथ काम करने के बारे में है — उसे इकट्ठा करना, store करना, analyse करना और दिखाना। Data के काम के लिए Python सबसे popular language है क्योंकि यह पढ़ने में आसान, free है, और इसकी NumPy (numbers), Pandas (tables) और Matplotlib (charts) जैसी powerful libraries हैं, जो आप Class XI और XII में use करेंगे।
Execution modes
Interactive mode
>>>prompt वाला Python shell- एक statement type करें, result तुरंत
- Calculator जैसा; कुछ save नहीं होता
Script mode
- Program
.pyfile में लिखें - Save करके run करें (IDLE में F5)
- पूरे programs के लिए
>>> 45.5 * 3 136.5 >>> 'IP' * 4 'IPIPIPIP' >>> 100 / 8 12.5
पहला program
# Shop bill – our first IP program
item = 'Notebook'
price = 45.5
qty = 3
bill = price * qty
print('Item:', item)
print('Bill: Rs.', bill)Tokens: सबसे छोटी units
जैसे वाक्य शब्दों और विराम चिह्नों से बनता है, वैसे ही Python statement tokens से बनता है।
| Token type | मतलब | Examples |
|---|---|---|
| Keyword | तय मतलब वाला reserved word | if, else, for, while, True, None |
| Identifier | Programmer का दिया नाम | bill, price, total_marks |
| Literal | Fixed value | 20, 45.5, 'Pen', True |
| Operator | Operation करने वाला symbol | + - * / == and |
| Punctuator | Code को ढाँचा देने वाले symbols | ( ) [ ] { } , : . ' # |
Keywords और identifiers
Keywords reserved words हैं (नए Python में 35) — इन्हें नाम के रूप में use नहीं कर सकते। Identifiers variables, functions वगैरह के नाम हैं। Identifier के rules: सिर्फ़ letters, digits और underscore; digit से शुरू नहीं; keyword नहीं; case-sensitive।
Valid identifiers
pricetotal_bill_taxitem2Qty(qtyसे अलग)
Invalid identifiers
1item– digit से शुरूtotal bill– spacewhile– keywordrate%– symbolgst-rate– hyphen
Literals
Literal program में सीधे लिखी गई raw value है।
| Literal | Examples |
|---|---|
| Numeric – integer | 25, -7, 0o17 (octal), 0x1F (hex), 0b101 (binary) |
| Numeric – float | 3.14, -0.5, 2.5e3 |
| Numeric – complex | 3+2j |
| String | 'Pen', "Delhi", '''multi-line''' |
| Boolean | True, False |
| Special | None |
print(0o17, 0x1F, 0b101) # octal, hex, binary integer literals
print(2.5e3, 3 + 2j) # float in exponent form, complex
print('It\'s', "say \"hi\"") # escape quotes with backslash
print('Line1\nLine2\tTabbed') # \n new line, \t tab
print(True, False, None) # boolean and special literalsEscape sequences backslash से शुरू होते हैं: \n नई line, \t tab, \\ backslash, \' और \" quotes।
Variables और assignment
Variable memory में रखी value को refer करने वाला नाम है। पहली बार assign करने पर बनता है। Python dynamically typed है:
score = 50 print(score, type(score)) score = 'fifty' # same name, now a string (dynamic typing) print(score, type(score))
a = b = c = 100 # same value to many variables x, y, z = 'Red', 'Green', 'Blue' print(a, b, c) print(x, y, z) print(id(a) == id(b)) # both names refer to one object
Comments
Comments इंसानों को code समझाते हैं और Python उन्हें ignore करता है। Comment # से शुरू करें। Formulas और ज़रूरी steps के लिए comments लिखें, जैसे # GST at 18% is added to the price।
Data types
item, price, qty, in_stock = 'Notebook', 45.5, 3, True
print(type(item), type(price), type(qty), type(in_stock))
runs = [45, 102, 8] # list
player = {'name': 'Virat', 'runs': 102} # dictionary
location = (28.61, 77.21) # tuple
print(runs, player['runs'], location[0])
print(type(runs), type(player), type(location))Mutable types बनने के बाद बदल सकते हैं (list, dictionary, set); immutable नहीं (int, float, bool, str, tuple)।
सारांश और practice
- Data के काम के लिए Python मुख्य language है (NumPy, Pandas, Matplotlib)।
- Tokens: keywords, identifiers, literals, operators, punctuators।
- Variables assignment से बनते हैं; Python dynamically typed है।
- Types: int, float, complex, bool, str, list, tuple, dict, set, None।
1) avg = (a + b) / 2 में tokens गिनें। 2) हर literal का type बताइए: '2026', 2026, 20.26, False। 3) दो items का नाम, price और quantity store करके total bill print करने वाला program लिखें।
Answers: 1) 9 tokens: avg = ( a + b ) / 2। 2) str, int, float, bool।
