Friday, June 2, 2023

Data Types of Python with Examples

Data types are an essential part of any programming language as they define the type of data that can be stored in a variable. Python supports several built-in data types, including numeric, boolean, sequence, dictionary, and set data types.
डेटा प्रकार किसी भी प्रोग्रामिंग भाषा का एक अनिवार्य हिस्सा हैं क्योंकि वे डेटा के प्रकार को परिभाषित करते हैं जिसे एक चर में संग्रहीत किया जा सकता है। पायथन कई अंतर्निहित डेटा प्रकारों का समर्थन करता है, जिनमें संख्यात्मक, बूलियन, अनुक्रम, शब्दकोश और सेट डेटा प्रकार शामिल हैं।

In Python, data types are dynamic, meaning the data type of a variable can change at runtime depending on the type of value assigned to it. For example, a variable declared as an integer can later be assigned a string value, and Python will change its data type to a string automatically.
पायथन में, डेटा प्रकार गतिशील होते हैं, जिसका अर्थ है कि किसी वेरिएबल का डेटा प्रकार उसे निर्दिष्ट मान के प्रकार के आधार पर रनटाइम पर बदल सकता है। उदाहरण के लिए, पूर्णांक के रूप में घोषित एक चर को बाद में एक स्ट्रिंग मान सौंपा जा सकता है, और पायथन अपने डेटा प्रकार को स्वचालित रूप से एक स्ट्रिंग में बदल देगा।

1.) Numeric Data Types:-

Numeric data types are used to store numeric values such as integers, floating-point numbers, and complex numbers.
संख्यात्मक डेटा प्रकारों का उपयोग संख्यात्मक मानों जैसे पूर्णांक, फ़्लोटिंग-पॉइंट संख्या और जटिल संख्याओं को संग्रहीत करने के लिए किया जाता है।

a) Integer इन्टिजर :-
Integers are whole numbers with no fractional part, and they are represented in Python using the `int` data type.
पूर्णांक पूर्ण संख्याएँ हैं जिनमें कोई भिन्नात्मक भाग नहीं होता है, और उन्हें `int` डेटा प्रकार का उपयोग करके पायथन में दर्शाया जाता है। 
Here is an example यहाँ एक उदाहरण है:-

x = 5
print(type(x))  #Output: <class 'int'>


b) Float फ्लोट:-
Floating-point numbers are used to represent decimal numbers, and they are represented in Python using the `float` data type. 
फ्लोटिंग-पॉइंट संख्याओं का उपयोग दशमलव संख्याओं को दर्शाने के लिए किया जाता है, और उन्हें `फ्लोट` डेटा प्रकार का उपयोग करके पायथन में दर्शाया जाता है। 
Here is an example यहाँ एक उदाहरण है:-

x = 3.14

print(type(x)) # Output: <class 'float'>

c) Complex काम्प्लेक्स:-
Complex numbers are used to represent numbers with both real and imaginary parts, and they are represented in Python using the `complex` data type. 

Here is an example:

x = 2 + 3j

print(type(x)) # Output: <class 'complex'>

```

Boolean Data Type:-

Boolean data types are used to represent the truth values `True` and `False`. In Python, boolean values are represented using the `bool` data type. Here is an example:

```

x = True

print(type(x)) # Output: <class 'bool'>

```

2.) Sequence Data Types:-

Sequence data types are used to store sequences of elements such as characters, lists, and tuples.

String:-

Strings are used to represent text data in Python, and they are represented using the `str` data type. Here is an example:

```

x = "Hello, World!"

print(type(x)) # Output: <class 'str'>

```

List:-

Lists are used to store a collection of elements, and they are represented using the `list` data type. Lists are mutable, meaning their elements can be changed after they are created. Here is an example:

```

x = [1, 2, 3, 4]

print(type(x)) # Output: <class 'list'>

```

Tuple:-

Tuples are used to store a collection of elements, and they are represented using the `tuple` data type. Tuples are immutable, meaning their elements cannot be changed after they are created. Here is an example:

```

x = (1, 2, 3, 4)

print(type(x)) # Output: <class 'tuple'>
## Tuples
Tuples are a type of sequence in Python that can hold any number of elements of different data types, just like lists. However, tuples are immutable, which means that once you create a tuple, you cannot modify its contents. The syntax for creating a tuple is to enclose a sequence of values in parentheses, separated by commas.


### Example:
```python
my_tuple = (1, "hello", 3.14)
```


### Accessing Tuple Elements
Tuple elements can be accessed using their index, just like lists. The first element of a tuple has an index of 0, the second element has an index of 1, and so on.


### Example:
```python
my_tuple = (1, "hello", 3.14)
print(my_tuple[0]) # Output: 1
print(my_tuple[1]) # Output: "hello"
```


### Tuple Operations
While you cannot modify the contents of a tuple, you can perform operations on tuples, such as concatenation and repetition.


#### Concatenation:
```python
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
tuple3 = tuple1 + tuple2
print(tuple3) # Output: (1, 2, 3, 4, 5, 6)
```


#### Repetition:
```python
tuple1 = (1, 2, 3)
tuple2 = tuple1 * 3
print(tuple2) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
```


### Advantages of Tuples
- Tuples are immutable, which means that once you create a tuple, you cannot modify its contents. This makes tuples a good choice for storing data that should not be modified, such as configuration settings or constant values.
- Tuples are faster than lists when it comes to indexing and iterating over large collections of data.
- Tuples can be used as keys in dictionaries, while lists cannot.


## Sets
A set is an unordered collection of unique elements in Python. In other words, a set contains no duplicate elements. You can create a set by enclosing a sequence of values in curly braces, separated by commas.


### Example:
```python
my_set = {1, 2, 3, 4}
```


### Adding Elements to a Set
You can add elements to a set using the `add()` method.


### Example:
```python
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
```


### Removing Elements from a Set
You can remove elements from a set using the `remove()` method.


### Example:
```python
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4}
```


### Set Operations
Sets support a variety of operations, such as union, intersection, and difference.


#### Union:
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = set1.union(set2)
print(set3) # Output: {1, 2, 3, 4}
```


#### Intersection:
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = set1.intersection(set2)
print(set3) # Output: {2, 3}
```


####

Input and Output statements in Python

In Python, input and output are handled using built-in functions input() and print(). पायथन में, इनपुट और आउटपुट को अंतर्निहित फ़ंक्शन इनपुट...