Python Variables क्या हैं? Variables के Rules, Examples & Data Types | RBSE Class 12
Python Variable क्या है?
Variable किसी value को program में refer करने के लिए दिया गया नाम है। Python में variable का उपयोग data को program के दौरान store और access करने के लिए किया जाता है।
उदाहरण के लिए:
name = "Rahul"
age = 18
marks = 85
यहाँ name, age और marks variables
हैं।
Python में Variable कैसे बनाते हैं?
Python में variable बनाने के लिए अलग से data type declare करने की
आवश्यकता नहीं होती। Assignment operator = की सहायता से
value assign की जाती है।
x = 10
name = "Rahul"
price = 99.50
Python value के आधार पर उसके type को determine कर सकता है।
Variable का Basic Syntax
variable_name = value
उदाहरण:
student_name = "Amit"
student_marks = 85
Python Variable के Examples
name = "Amit"
age = 17
marks = 92.5
passed = True
| Variable | Value | Data Type |
|---|---|---|
| name | "Amit" | str |
| age | 17 | int |
| marks | 92.5 | float |
| passed | True | bool |
क्या Python में Variable का Data Type Declare करना पड़ता है?
नहीं। Python में सामान्य variable assignment के समय अलग से data type लिखना जरूरी नहीं होता।
x = 100
name = "Python"
price = 25.50
इस उदाहरण में Python values के आधार पर types को determine करता है।
Python में Variable का Type कैसे Check करें?
किसी variable का data type जानने के लिए type() function का
उपयोग किया जाता है।
x = 100
print(type(x))
Output:
<class 'int'>
एक और example:
name = "Python"
print(type(name))
Output:
<class 'str'>
Python Variable Naming Rules
Variable का नाम रखते समय Python के कुछ rules follow करने होते हैं।
- Variable name में letters, digits और underscore का उपयोग किया जा सकता है।
- Variable name किसी digit से शुरू नहीं हो सकता।
- Variable name में space का उपयोग नहीं किया जाता।
- Python के keywords को सामान्य variable name के रूप में उपयोग नहीं करना चाहिए।
- Python में variable names case-sensitive होते हैं।
- Variable name में special characters जैसे
@,$और%का उपयोग नहीं किया जाता।
Valid Variable Names
नीचे दिए गए variable names valid examples हैं:
name = "Rahul"
age = 18
student_name = "Amit"
marks1 = 85
_total = 100
Invalid Variable Names
नीचे दिए गए examples valid variable names नहीं हैं:
1name = "Rahul"
student name = "Amit"
student-name = "Ravi"
class = 12
पहले example में variable digit से शुरू हो रहा है। दूसरे में space है।
तीसरे में hyphen का उपयोग हुआ है। चौथे में class Python
keyword है।
Variable Names Case-Sensitive होते हैं
Python में uppercase और lowercase letters को अलग माना जाता है।
name = "Rahul"
Name = "Amit"
NAME = "Ravi"
print(name)
print(Name)
print(NAME)
यहाँ name, Name और NAME तीन अलग
identifiers हैं।
Multiple Variables को एक साथ Assign करना
Python में एक ही statement में multiple variables को values assign की जा सकती हैं।
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
एक ही Value को Multiple Variables में Assign करना
a = b = c = 100
print(a)
print(b)
print(c)
Variables की Value बदलना
Python में variable की value को बाद में दूसरी value से replace किया जा सकता है।
x = 10
print(x)
x = 50
print(x)
पहले x की value 10 थी और बाद में उसे 50 assign किया गया।
Different Data Types के Variables
एक Python program में अलग-अलग types की values को variables में store किया जा सकता है।
name = "Rahul"
age = 18
height = 5.8
is_student = True
| Data Type | Example |
|---|---|
| int | age = 18 |
| float | height = 5.8 |
| str | name = "Rahul" |
| bool | is_student = True |
String Variable
Text को store करने के लिए string variable का उपयोग किया जाता है।
name = "Rahul"
print(name)
Integer Variable
age = 18
print(age)
Float Variable
percentage = 85.50
print(percentage)
Boolean Variable
is_passed = True
print(is_passed)
Variable में User Input Store करना
Python में input() function से प्राप्त value को variable
में store किया जा सकता है।
name = input("Enter your name: ")
print("Hello", name)
Integer Input को Variable में Store करना
input() से प्राप्त value सामान्यतः string होती है। यदि
integer value चाहिए तो int() conversion का उपयोग किया जा
सकता है।
age = int(input("Enter your age: "))
print(age)
Float Input को Variable में Store करना
marks = float(input("Enter your marks: "))
print(marks)
Variables के साथ Arithmetic Operations
a = 20
b = 10
addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b
print(addition)
print(subtraction)
print(multiplication)
print(division)
Variables के साथ Comparison
age = 18
print(age >= 18)
Output:
True
Variable को Delete करना
Python में del statement का उपयोग variable को delete करने
के लिए किया जा सकता है।
x = 100
del x
del के बाद उस variable को reference करने पर error आ सकता है
क्योंकि variable का binding हट चुका है।
Variable और Constant में अंतर
Python में सामान्य assignment से बनाई गई values को variables के रूप में use किया जाता है। Constants के लिए Python में कोई अलग mandatory constant declaration syntax नहीं है। Convention के अनुसार constant values के नाम uppercase में लिखे जा सकते हैं।
PI = 3.14159
MAX_MARKS = 100
Variable Naming में Best Practices
- Variable का नाम meaningful रखें।
- एक से अधिक words के लिए
snake_caseउपयोग करना readable हो सकता है। - बहुत छोटे और अस्पष्ट names से बचें, जब तक उनका context स्पष्ट न हो।
- Python keywords को variable names के रूप में उपयोग न करें।
उदाहरण:
student_name = "Rahul"
student_marks = 85
total_marks = 100
Variable और Identifier में अंतर
| Term | अर्थ |
|---|---|
| Identifier | Program में किसी object, function, variable आदि को refer करने वाला नाम। |
| Variable | ऐसा नाम जो किसी value/object के reference के रूप में उपयोग किया जाता है। |
Python Variables का Complete Example
student_name = "Rahul"
student_age = 18
student_marks = 87.5
student_passed = True
print("Name:", student_name)
print("Age:", student_age)
print("Marks:", student_marks)
print("Passed:", student_passed)
print(type(student_name))
print(type(student_age))
print(type(student_marks))
print(type(student_passed))
Common Mistakes
- Variable name को digit से शुरू करना।
- Variable name में spaces का उपयोग करना।
- Python keyword को variable name बनाना।
- Case sensitivity को ignore करना।
- String value को quotes के बिना लिखना।
RBSE Class 12 Important Questions
- Python variable क्या है?
- Python में variable कैसे create किया जाता है?
- क्या Python में variable का data type पहले से declare करना पड़ता है?
- Python में
type()function का उपयोग क्या है? - Variable naming के कोई पाँच rules लिखिए।
- Valid variable name क्या है?
- Invalid variable name क्या है?
- Python में variable names case-sensitive क्यों कहलाते हैं?
- Multiple variables को एक साथ कैसे assign करते हैं?
- एक ही value को multiple variables में कैसे assign किया जाता है?
- Variable की value को कैसे change किया जाता है?
delstatement का उपयोग क्या है?- Python में user input को variable में कैसे store करते हैं?
- Python में integer और float variables के उदाहरण दीजिए।
- Python variables पर एक छोटा program लिखिए।
Quick Revision
| Concept | Example |
|---|---|
| Variable | x = 10 |
| String Variable | name = "Rahul" |
| Integer Variable | age = 18 |
| Float Variable | marks = 85.5 |
| Boolean Variable | passed = True |
| Type Check | type(x) |
| Delete Variable | del x |
Frequently Asked Questions (FAQs)
Python Variable क्या है?
Python variable program में किसी value या object को refer करने के लिए उपयोग किया जाने वाला नाम है।
Python में variable कैसे बनाते हैं?
Assignment operator = का उपयोग करके variable में value
assign की जा सकती है।
क्या Python में variable का data type declare करना जरूरी है?
नहीं। सामान्य Python assignment में अलग से data type declaration की आवश्यकता नहीं होती।
Python में variable का data type कैसे पता करें?
type() function का उपयोग करके variable का type check किया
जा सकता है।
क्या Python variable name digit से शुरू हो सकता है?
नहीं। Python identifier का नाम digit से शुरू नहीं किया जा सकता।
क्या Python में variable names case-sensitive होते हैं?
हाँ। name, Name और NAME अलग
identifiers हैं।
Related Python Topics
type(), multiple assignment और value reassignment के
examples जरूर practice करें।
Python Variables और identifiers से संबंधित detailed information के लिए Python की official documentation देख सकते हैं।