Vaiables in python
Python variables are like containers that store values in a program. When you create a variable, Python sets aside some space in memory to hold the value. The type of value you store (like number, text, or decimal) decides how much space is needed. So, using different data types, variables can store things like whole numbers, decimal numbers, or text.
Python में variables ऐसे container होते हैं जो values को स्टोर करते हैं। जब आप कोई variable बनाते हैं, तो Python मेमोरी में उसके लिए जगह बना देता है। आप जो type की value उसमें डालते हैं (जैसे संख्या, दशमलव, या टेक्स्ट), उसी के हिसाब से मेमोरी दी जाती है। इस तरह variables में integer, float, और string जैसी values रखी जा सकती हैं।
In Python, variables are used to store data values. You don't need to declare the variable type.
Python में variables का उपयोग डेटा वैल्यू स्टोर करने के लिए किया जाता है। यहां टाइप डेक्लेयर की जरूरत नहीं है।
Example 1: Basic Variable Assignment
x = 5
y = 'Hello'
print(x)
print(y)
Output:
5 Hello
Assigns integer and string values to variables and prints them.
यह प्रोग्राम एक integer और एक string वैल्यू को variable में store करता है और print करता है।
Example 2: Multiple Assignment
a, b, c = 1, 2, 3
print(a, b, c)
Output:
1 2 3
Assigns multiple variables in one line.
यह प्रोग्राम एक ही लाइन में कई variables को value assign करता है।
Example 3: Variable Types
x = 10 # int
y = 10.5 # float
z = "Python" # str
print(type(x), type(y), type(z))
Output:
<class 'int'> <class 'float'> <class 'str'>
Prints the data types of different variables.
यह प्रोग्राम अलग-अलग variables के डेटा टाइप को print करता है।
Example 4: Type Casting
a = int('10')
b = float('5.5')
c = str(100)
print(a, b, c)
Output:
10 5.5 100
Casts string to int and float, int to string.
यह प्रोग्राम string को int और float में बदलता है, और int को string में।
Example 5: Global and Local Variables
x = 'global'
def myFunc():
x = 'local'
print('Inside:', x)
myFunc()
print('Outside:', x)
Output:
Inside: local Outside: global
Shows the difference between global and local variables.
यह प्रोग्राम global और local variables में फर्क दिखाता है।