LearningPython TutorialVariables & Data Types
CHAPTER 3 · PYTHON BASICS

Python Variables & Data Types

Variables program ke data ko names dete hain. Is chapter me assignment, naming rules, integers, floats, complex numbers, strings, booleans, None, type(), dynamic typing aur value references ko practical examples ke saath samjhenge.

English + Hinglish48 min readOnline practice included

What is a variable?

A variable is a name bound to a value. Python me variable create karne ke liye separate type declaration required nahi hoti.

variables.pyPython
name = "Broun"
age = 21
is_learning = True

print(name)
print(age)
print(is_learning)
Hinglish Explanation

name, age aur is_learning variables hain. Right side wali value evaluate hoti hai aur left side ka name us value se bind ho jata hai.

Try it Yourself →

Assignment with =

= is the assignment operator. It does not mean mathematical equality.

assignment.pyPython
score = 70
score = 85

print(score)

Output 85 hoga because second assignment name ko new value se bind karta hai.

Variable naming rules

  • Name letters ya underscore se start ho sakta hai.
  • Digits name me aa sakte hain, but first character nahi ho sakte.
  • Spaces allowed nahi hain.
  • Python keywords variable names nahi ho sakte.
  • Names case-sensitive hote hain.
names.pyPython
student_name = "Aman"
chapter2_score = 92
_private_value = "internal"

print(student_name, chapter2_score)
Style: Normal variables/functions ke liye snake_case common Python convention hai.

Choose meaningful names

meaningful.pyPython
# Weak
x = 1200

# Clearer
monthly_fee = 1200

Short names small formulas/loops me useful ho sakte hain, but business data ke liye intent clear rakhna better hai.

Check a value with type()

type() value ka runtime type object return karta hai.

types.pyPython
print(type(10))
print(type(3.5))
print(type("Python"))
print(type(True))
print(type(None))

Run type() examples →

Integer: int

int whole numbers represent karta hai.

integers.pyPython
students = 45
balance = -250
zero = 0

print(type(students))

Python integers arbitrary precision support karte hain, so fixed 32-bit/64-bit limit jaisi restriction normal Python int par directly apply nahi hoti; available memory practical limit hoti hai.

Decimal numbers: float

float floating-point numbers represent karta hai.

floats.pyPython
price = 199.99
temperature = -2.5

print(price)
print(type(price))
Precision note: Binary floating-point exact decimal arithmetic nahi guarantee karta. Money ya exact decimal use cases me later decimal module relevant ho sakta hai.

Complex numbers: complex

Python built-in complex type bhi provide karta hai. Imaginary part ke liye j notation use hota hai.

complex.pyPython
value = 2 + 3j
print(value)
print(type(value))

Beginner everyday applications me complex numbers less common hain, but built-in numeric type ke roop me jaana useful hai.

Text: str

Python text ko str type me represent karta hai. Single, double aur triple-quoted literals different situations me useful ho sakte hain.

strings.pyPython
course = "Python"
message = 'Learn by practice'
multiline = """Line one
Line two"""

print(course)
print(message)
print(multiline)

Strings ko Chapter 8 me indexing, slicing, methods aur formatting ke saath detail me padhenge.

Boolean: bool

bool ke two values hain: True and False.

boolean.pyPython
is_logged_in = True
has_completed = False

print(type(is_logged_in))

Capital T aur F required hain; true/false Python boolean literals nahi hain.

None and NoneType

None absence of a value ko represent karne ke liye commonly use hota hai.

none.pyPython
selected_course = None

print(selected_course)
print(type(selected_course))

None empty string, zero ya False ke same nahi hai.

Python is dynamically typed

Name ka type permanently declare nahi hota. Same name different times par different types ke values se bind ho sakta hai.

dynamic.pyPython
value = 10
print(type(value))

value = "ten"
print(type(value))
Use with care: Dynamic typing flexible hai, but same variable ka meaning/type randomly change karna code ko hard to understand bana sakta hai.

Multiple assignment

Python ek line me multiple names bind kar sakta hai.

multiple.pyPython
name, age, active = "Aman", 22, True
print(name, age, active)

Values ki count names ki count se match honi chahiye, unless advanced unpacking syntax intentionally use ho.

Assign the same value to multiple names

same-value.pyPython
a = b = c = 0
print(a, b, c)

Immutable numbers ke case me simple hai. Mutable objects ke saath chained assignment later important reference-sharing behavior create kar sakta hai.

Names refer to objects

Python variables ko beginner-friendly shorthand me “boxes” kehna useful ho sakta hai, but technically names objects ko reference karte hain.

references.pyPython
first = "Python"
second = first

print(first)
print(second)

Mutable objects ke references ka practical effect lists/dictionaries chapters me clear hoga.

Constants are a convention

Python me normal variable ko JavaScript const jaisa enforce karne wala general keyword nahi hai. Uppercase naming convention constant intent signal karti hai.

constants.pyPython
MAX_ATTEMPTS = 3
APP_NAME = "BrounStack"

Uppercase name runtime reassignment ko prevent nahi karta; it is a convention.

Delete a name with del

del current namespace se name binding remove kar sakta hai.

delete.pyPython
temporary = "remove me"
print(temporary)

del temporary
# print(temporary)  # NameError

Normal code me variables ko manually delete karna frequently necessary nahi hota.

Mutable vs immutable preview

Some Python objects can be changed in place and some cannot. This distinction later lists, dictionaries, sets aur function behavior me important hoga.

  • Common immutable examples: int, float, bool, str, tuple.
  • Common mutable examples: list, dict, set.
For now: Sirf concept note karo. Mutation/reference behavior ko relevant data structure chapters me detail me cover karenge.

Common beginner mistakes

  • 1name jaisa identifier use karna.
  • Variable name me spaces rakhna.
  • Keyword jaise class ko variable banana.
  • True/False ki capitalization galat karna.
  • = ko comparison operator samajhna.
  • None ko empty string ya zero samajhna.
  • Variable type ko permanently fixed assume karna.
  • Float ko exact decimal arithmetic samajhna.
  • Meaningless names everywhere use karna.
  • Uppercase constant convention ko runtime protection samajhna.

Beginner best practices

  • Descriptive snake_case names use karo.
  • Same variable ka meaning stable rakho.
  • Expected type confirm karne ke liye learning/debugging me type() use karo.
  • Boolean names ko is_, has_, can_ jaise prefixes se readable banao where appropriate.
  • None ko explicit “no value yet” state ke liye intentionally use karo.
  • Examples edit karke different values test karo.

Chapter checklist

  • Variable aur assignment ka difference samajh aaya?
  • Valid Python naming rules yaad hain?
  • int, float, complex, str, bool aur None identify kar sakte ho?
  • type() use kar sakte ho?
  • Dynamic typing ka basic meaning clear hai?
  • Multiple assignment samajh aaya?
  • Constant uppercase convention aur actual enforcement ka difference clear hai?
  • Mutable vs immutable ka preview samajh aaya?

Practice Task — Student Profile Variables

BrounStack Playground me ek small student profile banao.

  1. student_name me apna naam store karo.
  2. age me integer value rakho.
  3. percentage me float value rakho.
  4. is_enrolled boolean banao.
  5. scholarship ko initially None set karo.
  6. Har variable ka value print karo.
  7. type() se har value ka type print karo.
  8. age ko new integer se reassign karke output observe karo.
  9. Ek variable ko intentionally string se number me change karke dynamic typing observe karo.
  10. COURSE_NAME uppercase constant-style name banao.
  11. Ek invalid variable name try karke syntax error read karo.
  12. Final clean version me meaningful naming maintain karo.

Open practice starter →