Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Variables & Data Types

Lesson 17 of 37 in the free Data Science notes on Siksha Sarovar, written by Rohit Jangra.

Variables & Data Types in Python

Variables

Definition: A variable is a named container that stores a value in memory.In Python, you do not need to declare the type — it is automatically inferred.

Rules for Variable Names:

  • Must start with a letter or underscore(_).
  • Cannot start with a number.
  • Can contain letters, numbers, and underscores.
  • Case-sensitive (age and Age are different).
  • Cannot be a Python keyword (if, for, class, etc.).

Naming Conventions:

ConventionStyleExampleUsed For
snake_caselowercase with underscoresstudent_nameVariables, functions
PascalCaseEach word capitalizedStudentNameClasses
UPPER_CASEAll uppercaseMAX_SIZEConstants

---

Data Types in Python

Python has several built-in data types. Understanding them is critical because the type of data determines what operations can be performed on it.

Numeric Types

TypeDescriptionExample
intInteger (whole numbers)x = 10
floatDecimal numbersy = 3.14
complexComplex numbersz = 2 + 3j

Text Type

TypeDescriptionExample
strString (text)name = "Alice"

String Operations:

  • Concatenation: "Hello" + " " + "World" → "Hello World"
  • Repetition: "Ha" * 3 → "HaHaHa"
  • Slicing: "Python"[0:3] → "Pyt"
  • Length: len("Hello") → 5
  • Methods: .upper(), .lower(), .strip(), .split(), .replace()

Boolean Type

TypeValuesExample
boolTrue or Falseis_active = True

---

Collection Data Types

1. List

Definition: An ordered, mutable (changeable), indexed collection that allows duplicate elements.

FeatureDetail
Syntaxmy_list = [1, 2, 3, "hello"]
OrderedYes (maintains insertion order)
MutableYes (elements can be changed)
DuplicatesAllowed
IndexingZero-based (my_list[0] = first element)

Common Methods: .append(), .remove(), .pop(), .sort(), .reverse(), .extend()

2. Tuple

Definition: An ordered, immutable (unchangeable), indexed collection.

FeatureDetail
Syntaxmy_tuple = (1, 2, 3)
OrderedYes
MutableNo (cannot be changed after creation)
DuplicatesAllowed
Use CaseStoring fixed data (e.g., coordinates, RGB colors)

3. Dictionary

Definition: An unordered (Python 3.7+ ordered), mutable collection of key-value pairs.

FeatureDetail
Syntaxmy_dict = {"name": "Alice", "age": 25}
OrderedYes (from Python 3.7+)
MutableYes
DuplicatesKeys must be unique; values can repeat
AccessBy key: my_dict["name"] → "Alice"

Common Methods: .keys(), .values(), .items(), .get(), .update(), .pop()

4. Set

Definition: An unordered, mutable collection of unique elements. No duplicates allowed.

FeatureDetail
Syntaxmy_set = {1, 2, 3}
OrderedNo
MutableYes (can add/remove elements)
DuplicatesNot allowed
Use CaseRemoving duplicates, set operations (union, intersection)

---

Comprehensive Comparison Table

TypeOrderedMutableDuplicatesSyntaxUse Case
List✅ Yes✅ Yes✅ Yes[1, 2, 3]General-purpose collection
Tuple✅ Yes❌ No✅ Yes(1, 2, 3)Fixed/constant data
Dictionary✅ Yes✅ YesKeys: ❌{"a": 1}Key-value mappings
Set❌ No✅ Yes❌ No{1, 2, 3}Unique collections

---

Type Conversion (Casting)

FunctionConverts ToExample
int()Integerint("10") → 10
float()Floatfloat("3.14") → 3.14
str()Stringstr(100) → "100"
list()Listlist((1,2,3)) → [1, 2, 3]
tuple()Tupletuple([1,2,3]) → (1, 2, 3)
set()Setset([1,1,2]) → {1, 2}

Checking Type

Use the type() function to check a variable's data type: type(42) → <class 'int'> type("hello") → <class 'str'>

Summary

  • Variables are dynamically typed in Python — no type declaration needed.
  • Python has numeric (int, float, complex), text (str), boolean (bool), and collection types.
  • Lists are mutable and ordered; Tuples are immutable and ordered.
  • Dictionaries store key-value pairs; Sets store only unique values.
  • Type conversion functions (int(), str(), etc.) allow flexible data manipulation.