Notes

Variables and Data Types in Python [ English ]

< Prev Next >

1. Introduction

In Python, variables are used to store data, and data types define the type of data that a variable can hold. Python is a dynamically typed language, which means you do not need to declare the type of a variable explicitly.

2. Variables in Python

2.1 Definition

A variable is a name given to a memory location that stores a value.

2.2 Creating Variables

In Python, variables are created when a value is assigned to them using the assignment operator (=).

x = 10
name = "Python"
price = 99.5

2.3 Rules for Naming Variables

2.4 Examples

age = 20
student_name = "Rahul"
_total = 100

2.5 Multiple Assignment

a, b, c = 1, 2, 3

2.6 Assigning Same Value

x = y = z = 10

2.7 Dynamic Typing

x = 10
x = "Hello"

Explanation

3. Data Types in Python

3.1 Definition

A data type specifies the type of value a variable holds.

4. Basic Data Types

4.1 Numeric Data Types

1. Integer (int)

x = 10

2. Float (float)

y = 3.14

3. Complex (complex)

z = 2 + 3j

4.2 Boolean Data Type (bool)

a = True
b = False

4.3 String Data Type (str)

name = "Python"
message = 'Hello'

5. Collection Data Types

5.1 List (list)

numbers = [1, 2, 3, 4]

5.2 Tuple (tuple)

data = (1, 2, 3)

5.3 Set (set)

values = {1, 2, 3}

5.4 Dictionary (dict)

student = {"name": "Rahul", "age": 20}

6. Checking Data Type

Use the type() function:

x = 10
print(type(x))

Output

<class 'int'>

7. Type Conversion

7.1 Implicit Conversion

x = 5
y = 2.5
result = x + y
print(result)

7.2 Explicit Conversion

x = "10"
y = int(x)
print(y)

8. Summary

< Prev Next >