Understanding Data Types in Python: Variables and Operators Explored

 

We have discussed variables and operators in Python.If you not notice, so you can read here. If you notice, you may notice that the variables are different here, even though we have been dealing with numeric values ​​so far.

But in order to work normally, we have to work with different types of data, that is, the variables are also different.  Just as we buy a liter of water instead of a kilogram of water, programming languages ​​use specific types of data for specific tasks. And these different types of data are called data types.

Understanding Data Types in Python: Variables and Operators Explored


Every programming language has certain provided or built-in data types.  Also in Python, but since Python's data type is very rich and for ease of understanding, it can be divided into some basic parts –


Boolean

•bool

Numeric

•int
•float
•complex

String

•str

Sequence

•list
•tuple

Mapping

•dict

Set

•set
 
Now we will know these data types in detail.

Boolean

Boolean data type is very limited A data type that is limited to True and False only. That is, only these two values ​​can exist here.


Bool (bool)

Boolean data type is denoted by bool in Python. Boolean data is used to evaluate the value of a conditional statement, the message of a statement or evaluate a variable, etc.


a=10

b=5

# As a Conditional Statement

print(a>b)

print(a<b)

 print(bool("python")) # As a validator

# As a message
if a>b:
print('a is greater than b')
else:
print('a is less than b')


The result will be –

True
False
True
a is greater than b


Special Note:

Since Python is case sensitive, boolean values ​​are only True and False; true and not false. Hope you understand the difference.

If you look at line #As a Validator , you will see that the result is True. Why True?  Because Boolean operator indicates True if a variable has a value and False if it is not i.e. empty.


Numeric data type

We already know more or less about numerical data. That is, numbers will be discussed here.

Int (integer)

The int data type holds any integer, positive or negative number. Python defines types dynamically so there is no need to worry about large numbers;  Just be careful that it doesn't become a fraction.

Float (float)

The float data type can also take decimal numbers. This is the only difference with int. A float number can be positive or negative.

Complex (complex)

Complex numbers are used in many complex mathematical problems. And complex data is used for this complex number. Complex numbers are denoted by j.

Now let's see all these together with an example –


a=10

b=55555555555555555555555555

c=15.155

d=3+4j

 print("The value of a is {} and type {}".format(a,type(a)))

print("The value of c is {} and type {}".format(c,type(c)))

print("The value of c is {} and type {}".format(d,type(d)))


Result:

The value of a is 10 and type <class 'int'>
The value of c is 15.155 and type <class 'float'>
The value of c is (3+4j) and type <class 'complex'>


Note: ( .format)  is used to display the result here. Its usage is where the value should be left blank with {}, then the variable inside the format should be given. Instead of {}, the variables will sit one by one.


String (string)

A sequence of characters or characters is called a 'string'. 'String' is a widely used data type in Python.  From writing simple programs to building complex software, the use of strings is essential everywhere. Here we will look at how to use strings as well as some built-in member functions.

str

Strings are represented by str in Python. Let's look at an example –


name="Henry"

print("Your name is {} and the data type is {}".format(name,type(name)))

Result: Your name is Henry and the data type is <class 'str'>


Here 'Henry' is a string data type.

As mentioned earlier, strings are sequential insertions of characters.  So, the characters are arranged according to a specific index, and these indexes can be specified separately. For example –


name="Henry"

print("Your name is {} and the 3rd character is {}".format(name,name[2]))


Result:
Your name is Henry and the 3rd character is n

Note here that index starts from 0 and never from 1. Here name[2] means the third element.

Since the scope of the discussion of strings is very large, We will end here, more details can be discussed later.


Sequence

Python's sequence data type maintains a specific order and stores data of the same or similar types. Note that String is a sequence data type; But considering its prevalence is discussed separately.

List (list)

A list is a collection of data, in other programming languages ​​(C, C++ etc.) it is called an array. There is no separate method or function to define the list. The data must be placed inside the brackets one by one with commas. List values ​​can be changed.


a=[20,40,80] #Declaring a list

 print("The value of a is {} and type {}".format(a,type(a)))


Result :
The value of a is [20, 40, 80] and type <class 'list'>


"list" elements like 'strings' are accessed by "index". To update the value of the list under the specified "index"-


a=[20,40,80] #Declaring a list

 print("The Third value of this list is {}".format(a[1])) # Accessing a specific element in a list

 a[1]=50 #update list for specific index

 print("The value of updeted list is {}".format(a))


Result: The third value of this list is 40

The value of updated list is [20, 50, 80]

Notice in line 3 that the value is changed using a specific index.


Tuple (tuple)

"Tuples" and "lists" are almost the same data type; The only difference is that values ​​in the list can be updated or deleted and not deleted. That is tuple immutable data type.

Tuple elements must be enclosed in parentheses () with commas.


a=(20,40,80) #Declairng a tpple

 print("Third value of this tuple is {}".format(a[1])) # Accessing a specific element in a tuple

 a[1]=50 #update list for specific index


Result:
1. Third value of this list is 40

2. Traceback (most recent call last):

3. File "main.py", line 15, in <module>

a[1]=50 #update list for specific index

4. TypeError: 'tuple' object does not support item assignment


In the above result we see an error because we tried to update the value of the tuple in the 4th line which is not acceptable in Python.


Mapping (mapping)

Sometimes hashes or mappings are needed to increase program efficiency.  Most conveniently, Python has a built-in mapping data type known as a dictionary.

Dict

"dict" is Python's unordered data type.  Generally speaking, just like in a Dutch or English dictionary the words are inserted under a letter, the Python dictionary also has a 'key, value' pair.  One has to access a particular value with its key. The structure is – key:value, as we will see below with an example.


a={1:'Snake',2:'Lion',4:'Tiger'} #Declaring a dict

 print("Third value of this dict is {}".format(a)) # Accessing dict

print("The first value of this dict is {}".format(a[1])) # Accessing a


Result:
Third value of this dict is {1: 'Snake', 2: 'Lion', 4: 'Tiger'}


The first value of this dict is Snake

If you look at the 3rd line, you can see that the value is accessed with the key of the existing dictionary.

Understanding Data Types in Python: Variables and Operators Explored



Set (set)

The set data type was created in Python to represent the concept of mathematical sets. Duplicate values ​​can never be added to a set. Sets are much faster than lists to find a specific element. Union, intersection, operations can be done in the set.

Example :


a={"Snake","Lion","Tiger"} #Declaring a set

 print("Third value of this set is {}".format(a)) # Accessing set

print("The value Snake exists in this set a :{}".format("Snake" in a)) # Accessing a specific element in a dict

 a.add("Zebra") #adding element in set

a.remove("Lion") #remove and element from a set

 print("Third value of this set is {}".format(a)) # Accessing set


Result:
Third value of this set is {'Lion', 'Tiger', 'Snake'}

The value Snake exists in this set a :True

Third value of this set is {'Tiger', 'Zebra', 'Snake'}


A new element "Zebra" has been added to the 4th line.

An element "Lion" is deleted in the 5th line.

From the result in line 6 we can see that "Zebra" have been added to our set and "Lion" has been removed.


Conclusion

This article provides an overview of variables and operators in Python, emphasizing the importance of different data types.

It highlights that programming languages, including Python, utilize specific data types to handle various tasks effectively.

The article explores different data types in Python, such as Boolean, numeric, string, sequence, mapping, and set.

Each data type is explained with examples, demonstrating their usage and characteristics.

Boolean operators, integers, floats, complex numbers, strings, tuples, dictionaries, and sets are all covered.

In general, this article provides a useful guide for working with various data types in Python programming.


FAQs


Q: Is the order of elements preserved in a set?

A: No, sets are unordered collections, so the order in which elements are added to a set is not preserved.


Q: Can I update or delete elements in a tuple?

A: No, tuples are immutable, meaning their elements cannot be updated or deleted once defined.


Q: Can I access values in a dictionary using keys?

A: Yes, in a dictionary, you can access values by providing their corresponding keys.


Q: What is a dictionary in Python?

A: A dictionary is a mapping data type in Python that stores data in key-value pairs. It allows efficient retrieval of values based on their associated keys.


Q: What is the purpose of a set in Python?

A: A set is a data type in Python used to represent a collection of unique elements. It is useful for mathematical operations such as union, intersection, and finding specific elements efficiently.


Q: How are strings represented in Python?

A: Strings are represented by the keyword "str" in Python. They are sequences of characters and are widely used in programming for various purposes.


Q: How is the Boolean data type represented in Python?

A: The Boolean data type is represented by the keyword "bool" in Python.


Q: What is the difference between "int" and "float" data types?

A: The int data type holds any integer, positive or negative, while the float data type can hold decimal numbers. Integers do not have fractional parts, whereas floats can represent fractional values.

Post a Comment

0 Comments