Topic 5:- Python Data types

What are Python Data Types?

Data types define the kind of value a variable can hold  for example, numbers, text, lists, etc.
Python automatically determines the data type based on the value assigned (it’s dynamically typed).

1. Numeric Data Types

Type                 Description Example
int            Integer numbers (no decimal) x = 10
float Decimal or floating-point numbers y = 3.14
complex Complex numbers with real and imaginary parts z = 2 + 3j

 
Python Datatype


Example:

a = 10        # int
b = 3.14      # float
c = 2 + 5j    # complex

print(type(a))  # <class 'int'>
print(type(b))  # <class 'float'>
print(type(c))  # <class 'complex'>

2. String Data Type (str)

Used for text data, enclosed in quotes ' ' or " ".

Syntax:

string_variable = "Hello, Python!"

Example:

name = "Kavi"
print(name.upper())     # KAVI
print(name[0])          # K

3. List Data Type (list)

Ordered, mutable (can be changed), and can hold different data types.

Syntax:

list_name = [item1, item2, item3]

Example:

fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(fruits)  # ['apple', 'banana', 'cherry', 'mango']

4. Tuple Data Type (tuple)

Ordered but immutable (cannot be changed after creation).

Syntax:

tuple_name = (item1, item2, item3)

Example:

colors = ("red", "green", "blue")
print(colors[1])  # green

5. Dictionary Data Type (dict)

Stores key-value pairs, unordered and mutable.

Syntax:

dict_name = {"key1": value1, "key2": value2}

Example:

student = {"name": "Kavi", "age": 23, "course": "Python"}
print(student["name"])  # Kavi

6. Set Data Type (set)

Unordered, no duplicates allowed, used for mathematical operations like union and intersection.

Syntax:

set_name = {item1, item2, item3}

Example:

numbers = {1, 2, 3, 3, 4}
print(numbers)  # {1, 2, 3, 4}

7. Boolean Data Type (bool)

Represents True or False values.

Syntax:

bool_var = True  # or False

Example:

x = 10
y = 20
print(x > y)   # False

 Summary

<\br>
Data Type  Example Mutable Ordered
int 10 No  N/A
float 3.14                 No                     N/A
complex 2 + 3j No N/A
str "Hello" No Yes
list [1, 2, 3]  Yes Yes
tuple (1, 2, 3) No Yes
dict {"a": 1} Yes  No (unordered till Python 3.6+)
set {1, 2, 3} Yes  No
bool True / False No N/A


Comments

Popular posts from this blog

Topic1 :- What is Python Programming?

Topic2: -Why Python?

Topic7: What is Numpy?