Python

Chapter 2: Variables and Data Types in Python

Python is a versatile and beginner-friendly programming language. One of the first concepts you need to understand is Variables and Data Types, as they form the foundation of any program.

In this chapter, we will cover:

  1. What are Variables?
  2. Rules for Naming Variables
  3. Data Types in Python
  4. Type Conversion

Let’s dive in!

1. What are Variables?

variable is like a container that stores data values. Unlike some other programming languages, Python does not require explicit declaration of variables—they are created the moment you assign a value to them.

Example:

name = "Alice"  
age = 25  
height = 5.9  

Here:

  • name stores a string ("Alice")
  • age stores an integer (25)
  • height stores a float (5.9)

Variables can be reassigned to different values anytime.

age = 25  
age = 26  # Updated value  
print(age)  # Output: 26  

2. Rules for Naming Variables

Python has some rules and conventions for variable names:

Rules:

✔ Must start with a letter (a-z, A-Z) or underscore (_).
✔ Can contain letters, numbers (0-9), and underscores.
✔ Case-sensitive (Age and age are different).
✔ Cannot use Python keywords (ifforwhile, etc.).

Examples:

✅ Valid:

name = "Bob"  
_age = 30  
user1 = "Alice"  

❌ Invalid:

1user = "John"  # Starts with a number  
for = 5         # Uses a keyword  
first-name = "Sam"  # Hyphen not allowed  

Best Practices:

  • Use snake_case (e.g., user_name).
  • Choose meaningful names (e.g., student_age instead of sa).

3. Data Types in Python

Python has several built-in data types:

Data TypeExampleDescription
int5-10Whole numbers (positive/negative)
float3.14-0.5Decimal numbers
str"Hello"'Python'Text (enclosed in quotes)
boolTrueFalseBoolean (True/False)
list[1, 2, 3]Ordered, mutable collection
tuple(1, 2, 3)Ordered, immutable collection
dict{"name": "Alice"}Key-value pairs
set{1, 2, 3}Unordered, unique elements

Examples:

# Integer  
age = 25  

# Float  
price = 19.99  

# String  
greeting = "Hello, Python!"  

# Boolean  
is_student = True  

# List  
fruits = ["apple", "banana", "cherry"]  

# Tuple  
coordinates = (10.0, 20.0)  

# Dictionary  
person = {"name": "Alice", "age": 25}  

# Set  
unique_numbers = {1, 2, 3, 3}  # {1, 2, 3} (duplicates removed)  

4. Type Conversion

Sometimes, we need to convert one data type to another. Python provides built-in functions for this:

FunctionExampleConverts to
int()int("5") → 5Integer
float()float(3) → 3.0Float
str()str(100) → "100"String
bool()bool(1) → TrueBoolean

Examples:

# String to Integer  
num_str = "10"  
num_int = int(num_str)  # 10  

# Integer to String  
age = 25  
age_str = str(age)  # "25"  

# Float to Integer  
price = 9.99  
price_int = int(price)  # 9 (truncates decimal)  

# Boolean Conversion  
print(bool(1))  # True  
print(bool(0))  # False  

Summary

✔ Variables store data and can be reassigned.
✔ Follow naming rules for variables.
✔ Python has multiple data types (intfloatstrbool, etc.).
✔ Type conversion allows changing one data type to another.

In the next chapter, we’ll explore Operators and Expressions in Python!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *