Variables and Data Types in Python
Python में Variables और Data Types
Introduction
In Python, a variable is a name that points to a value stored in memory. Unlike C or Java, you do not declare the type — Python figures it out automatically from the value you assign. This is called dynamic typing.
Variables are the foundation of every program. Marks, names, totals, flags — everything your program remembers is stored in a variable. Master this first; the rest of Python builds on it.
What is a Variable?
Think of a variable as a labelled box. The label is the name, and the box holds a value. When you write marks = 90, Python creates a box named marks and puts 90 inside it.
The = sign is the assignment operator — it does not mean "equal to" (that is ==). It means "store the value on the right into the name on the left".
Built-in Data Types
Python has several built-in data types. These five are used most often at Class 12 to B.Tech level:
| Type | Example | Used For |
|---|---|---|
| int | age = 17 | Whole numbers |
| float | pct = 91.5 | Decimal numbers |
| str | name = "Aman" | Text |
| bool | passed = True | True / False |
| NoneType | result = None | "No value yet" |
Program 1: Declaring Variables
# Each line creates a variable of a different type
name = "Aman" # str
age = 17 # int
percentage = 91.5 # float
is_pass = True # bool
print("Name:", name)
print("Age:", age)
print("Percentage:", percentage)
print("Passed:", is_pass)Line-by-line:
name = "Aman"— text goes inside quotes, so Python stores it as a str.age = 17— a whole number with no decimal becomes an int.percentage = 91.5— the decimal point makes Python store it as a float.is_pass = True—True/False(capital T/F) are bool values.print(...)— the comma adds a space between the label and the value automatically.
Program 2: Checking & Converting Types
Use type() to check a type, and int()/float()/str() to convert (called type casting).
marks = "85" # this is a STRING, not a number! print(type(marks)) # <class 'str'> # Convert string to integer before doing maths marks_int = int(marks) print(marks_int + 5) # now arithmetic works print(type(marks_int))
Why this is important: data from input() is always a string. If you forget to convert it, "85" + 5 gives an error. Program 3 shows the real fix.
Program 3: Student Report Card
A practical program that uses every type together:
name = input("Enter student name: ")
maths = int(input("Maths marks: "))
science = int(input("Science marks: "))
english = int(input("English marks: "))
total = maths + science + english
percentage = total / 3
is_pass = percentage >= 33
print("\n----- REPORT CARD -----")
print("Name :", name)
print("Total :", total, "/ 300")
print("Percentage :", round(percentage, 2), "%")
print("Result :", "PASS" if is_pass else "FAIL")int(input(...))reads text and converts it to a number in one step.percentage >= 33produces a bool stored inis_pass.round(percentage, 2)keeps only 2 decimal places."PASS" if is_pass else "FAIL"is a one-line conditional (ternary).
Variable Naming Rules
- Can contain letters, digits and underscore (
_), e.g.total_marks. - Cannot start with a digit:
2marksis invalid,marks2is fine. - Case-sensitive:
Nameandnameare different. - Cannot be a keyword (
if,class,for...). - Use clear names:
percentage, notp.
Common Mistakes
- Forgetting to convert
input()tointbefore maths. - Using
=(assign) when you meant==(compare). - Writing
true/falsein lowercase — Python needsTrue/False. - Wrong indentation (Python is strict about spaces).
Practice Tasks
- Create variables for your name, class and three subject marks; print a formatted report.
- Take two numbers from the user and print their sum, difference and product.
- Store your height in metres (float) and print whether it is above 1.5 (bool).
- Use
type()on five different values and note the output.
Summary
- A variable is a name pointing to a value; Python detects the type automatically.
- Main types: int, float, str, bool, None.
input()always returns a string — convert withint()/float().type()checks a type;=assigns,==compares.
परिचय
Python में variable एक नाम है जो memory में रखी value की ओर इशारा करता है। C या Java की तरह आपको type declare नहीं करना पड़ता — Python value देखकर खुद type समझ लेता है। इसे dynamic typing कहते हैं।
Variables हर program की नींव हैं। Marks, names, totals, flags — जो कुछ program याद रखता है, वह variable में रहता है। इसे पहले master करें; बाकी Python इसी पर बनता है।
Variable क्या है?
Variable को एक label वाला डिब्बा समझें। Label है name, और डिब्बे में रहती है value। जब आप marks = 90 लिखते हैं, Python marks नाम का डिब्बा बनाकर उसमें 90 रख देता है।
= चिह्न assignment operator है — इसका मतलब "बराबर" नहीं (वह == है)। इसका मतलब है "दाईं ओर की value को बाईं ओर के नाम में store करो"।
Built-in Data Types
Python में कई built-in data types हैं। ये पाँच Class 12 से B.Tech level पर सबसे ज़्यादा use होते हैं:
| Type | Example | किसके लिए |
|---|---|---|
| int | age = 17 | पूर्ण संख्याएं |
| float | pct = 91.5 | दशमलव संख्याएं |
| str | name = "Aman" | Text |
| bool | passed = True | True / False |
| NoneType | result = None | "अभी कोई value नहीं" |
Program 1: Variables बनाना
# हर line अलग type का variable बनाती है
name = "Aman" # str
age = 17 # int
percentage = 91.5 # float
is_pass = True # bool
print("Name:", name)
print("Age:", age)
print("Percentage:", percentage)
print("Passed:", is_pass)Line-by-line:
name = "Aman"— text quotes में है, इसलिए Python इसे str रखता है।age = 17— बिना दशमलव वाली पूर्ण संख्या int बनती है।percentage = 91.5— दशमलव बिंदु इसे float बना देता है।is_pass = True—True/False(बड़े T/F) bool values हैं।print(...)— comma अपने आप label और value के बीच space देता है।
Program 2: Type Check और Convert
Type check करने के लिए type(), और convert करने के लिए int()/float()/str() (इसे type casting कहते हैं)।
marks = "85" # यह STRING है, number नहीं! print(type(marks)) # <class 'str'> # maths करने से पहले string को integer में बदलें marks_int = int(marks) print(marks_int + 5) # अब arithmetic चलेगा print(type(marks_int))
यह क्यों ज़रूरी है: input() से आया data हमेशा string होता है। अगर convert करना भूल गए, तो "85" + 5 error देगा। Program 3 असली fix दिखाता है।
Program 3: Student Report Card
एक practical program जो हर type एक साथ use करता है:
name = input("Enter student name: ")
maths = int(input("Maths marks: "))
science = int(input("Science marks: "))
english = int(input("English marks: "))
total = maths + science + english
percentage = total / 3
is_pass = percentage >= 33
print("\n----- REPORT CARD -----")
print("Name :", name)
print("Total :", total, "/ 300")
print("Percentage :", round(percentage, 2), "%")
print("Result :", "PASS" if is_pass else "FAIL")int(input(...))text पढ़कर एक ही step में number बना देता है।percentage >= 33एक bool बनाता है जोis_passमें रहता है।round(percentage, 2)सिर्फ 2 दशमलव रखता है।"PASS" if is_pass else "FAIL"एक-line का conditional (ternary) है।
Variable Naming Rules
- letters, digits और underscore (
_) हो सकते हैं, जैसेtotal_marks। - digit से शुरू नहीं हो सकता:
2marksगलत,marks2सही। - Case-sensitive:
Nameऔरnameअलग हैं। - keyword नहीं हो सकता (
if,class,for...)। - साफ नाम रखें:
percentage, न किp।
सामान्य गलतियाँ
- maths से पहले
input()कोintमें convert करना भूलना। ==(compare) की जगह=(assign) लगाना।true/falselowercase में लिखना — Python कोTrue/Falseचाहिए।- गलत indentation (Python spaces को लेकर सख्त है)।
Practice Tasks
- अपने नाम, class और तीन subjects के marks के variables बनाकर formatted report print करें।
- User से दो numbers लेकर उनका sum, difference और product print करें।
- अपनी height metres (float) में रखकर print करें कि वह 1.5 से ऊपर है या नहीं (bool)।
- पाँच अलग values पर
type()use करके output नोट करें।
सारांश
- Variable एक नाम है जो value की ओर इशारा करता है; Python type अपने आप पहचानता है।
- मुख्य types: int, float, str, bool, None।
input()हमेशा string लौटाता है —int()/float()से convert करें।type()type check करता है;=assign,==compare।