Variable and Data Types
Python tutorial · PySpark.in
Variable
A variable is a name that refers to a memory location where a value is stored.
Assigning Values to Variables
Use the assignment operator (=) to store a value in a variable.
Rules for Naming Variables
Rule | Example |
|---|---|
Can contain letters (A–Z, a–z), digits (0–9), and underscores (_) | Allowed student_name |
Cannot start with a digit | Not allowed 1student |
Cannot contain spaces | Not allowed student name |
Cannot use special characters (except _) | Not allowed student@name |
Cannot use hyphens (-) | Not allowed student-name |
Variable names are case-sensitive | Name ≠ name |
Avoid leading/trailing underscores unless needed | Reserved patterns in Python. |
Naming Conventions
Convention | Example | Used For |
|---|---|---|
snake_case | student_name | Common in Python |
camelCase | studentName | Used by some developers |
UPPER_CASE | PI = 3.14 | Used for constants |
Multiple Assignments
Python allows you to assign multiple variables in one line:
You can also assign the same value to multiple variables:
Python is Dynamically Typed
Python determines it automatically based on the assigned value.
```
x = 10 # int
x = "Hello" # str (same variable, new type)
print(x)
```
Python Data Types
Data types define the kind of value a variable can hold.
Basic (Primitive) Data Types
1. Integers (int)
Whole numbers (positive or negative).
2. Floating Point Numbers (float)
Numbers with decimal points.
3. Strings (str)
Text enclosed in single, double, or triple quotes.
Strings are immutable and support slicing, concatenation, and built-in methods like .upper(), .lower(), .replace().
4. Boolean (bool)
Represents logical values — True or False.
5. NoneType (None)
Represents the absence of a value.
Python Collection (Container) Data Types
List
Ordered, mutable, allows duplicates.
- Access by index: fruits[0] → 'apple'
- Supports slicing: fruits[1:3] → ['banana', 'cherry']
Tuple
Ordered, immutable, allows duplicates.
Set
Unordered, no duplicates, mutable.
Dictionary (dict)
Stores key-value pairs, mutable, unordered.
Data Types
Type | Description | Example |
|---|---|---|
range | Sequence of numbers | range(1, 6) → [1,2,3,4,5] |
bytes | Immutable binary data | b = b"Hello" |
bytearray | Mutable binary data | bytearray(b"Hi") |
memoryview | View/edit binary data without copying | memoryview(bytearray(b"Python")) |
frozenset | Immutable set | frozenset({1, 2, 3}) |
array (module) | Efficient numeric array | from array import array |
Type Conversion (Casting)
Convert one data type to another using functions like int(), float(), str(), list(), etc.
Difference Between Mutable and Immutable Objects
Feature | Mutable Objects | Immutable Objects |
|---|---|---|
Definition | Can be changed after creation | Cannot be changed after creation |
Memory Address | Remains same even after modification | Changes when a new value is assigned |
Examples | List, Dictionary, Set, bytearray | int, float, string, tuple, frozenset, bytes |
Can Add / Remove Elements | Yes | No |
Performance | Slightly slower (extra memory for flexibility) | Faster (no modification tracking) |
Use Case | When data needs to change (e.g., dynamic lists) | When data must remain constant (e.g., keys in dicts) |
Example of a Mutable Object (List)
The same list object was modified — so list is mutable.
Example of an Immutable Object (String)
name = "Python"
print(id(name)) # Memory address before change
The old string wasn’t modified — instead, Python created a new string in memory.
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges