Python Tutorials

Python Data Types

In programming, a data type specifies the type of value a variable can hold. It tells the compiler or interpreter how the programmer intends to use the data.

Python is dynamically typed, meaning you do not need to explicitly declare a data type; Python automatically assigns it based on the value you provide. To check the exact data type of any variable, we use the built-in type() function.

Python provides various standard data types to handle different kinds of data. In this section, we will cover Numeric, Boolean, Sequence Types, Dictionary, and Set.

1. Numeric Types

Numeric data types store numerical values. In Python, there are three distinct numeric types: Integer, Float, and Complex Number.

A. Integer (int)

The int data type represents a whole number without any fractional or decimal parts. It can be positive or negative. In Python 3, there is no limit to how long an integer value can be.

positive_num = 150
negative_num = -55

print(positive_num, type(positive_num))
print(negative_num, type(negative_num))
Code language: PHP (php)

Output:

150 <class 'int'>
-55 <class 'int'>
Code language: HTML, XML (xml)

B. Float (float)

The float data type represents real numbers that contain a decimal point.

decimal_num = 10.5
print(decimal_num, type(decimal_num))
Code language: PHP (php)

Output:

10.5 <class 'float'>
Code language: HTML, XML (xml)

C. Complex Number (complex)

A complex number consists of a real part and an imaginary part, written in the format a + bj. Python uses j instead of i for the imaginary part.

comp_num = 3 + 5j
print(comp_num, type(comp_num))
Code language: PHP (php)

Output:

(3+5j) <class 'complex'>
Code language: HTML, XML (xml)

2. Boolean Type (bool)

The bool data type is used to represent the truth value of an expression. It can only hold one of two possible values: True or False. Internally, Python treats True as 1 and False as 0.

is_python_fun = True
print(is_python_fun, type(is_python_fun))
Code language: PHP (php)

Output:

True <class 'bool'>
Code language: PHP (php)

3. Sequence Types

Sequence types are used to store multiple values in an organized and efficient manner. The standard sequence types in Python are String, List, and Tuple.

A. String (str)

A String is a sequence of characters enclosed within single quotes '' or double quotes "". Strings in Python are immutable, meaning they cannot be changed after they are created.

message = "Welcome to Python"
print(message, type(message))
Code language: PHP (php)

Output:

Welcome to Python <class 'str'>
Code language: HTML, XML (xml)

B. List (list)

A List is an ordered collection of data. It can hold items of different data types. Lists are created using square brackets []. They are mutable, meaning you can add, remove, or change items after the list is created.

my_list = [10, "Rahul", 25.5, True]
print(my_list, type(my_list))
Code language: PHP (php)

Output:

[10, 'Rahul', 25.5, True] <class 'list'>
Code language: PHP (php)

C. Tuple (tuple)

A Tuple is very similar to a list, but it is created using parentheses (). The main difference is that tuples are immutable. Once a tuple is created, you cannot modify its elements.

my_tuple = (10, "Rahul", 25.5)
print(my_tuple, type(my_tuple))
Code language: PHP (php)

Output:

(10, 'Rahul', 25.5) <class 'tuple'>
Code language: JavaScript (javascript)

4. Dictionary Type (dict)

A Dictionary is an unordered collection of data stored as key-value pairs. It is highly optimized for retrieving data when you know the key. Dictionaries are created using curly braces {} with a colon : separating the key and value.

student_info = {"name": "Amit", "age": 20, "course": "Python"}
print(student_info, type(student_info))

# Accessing a specific value using its key
print("Student Name:", student_info["name"])
Code language: PHP (php)

Output:

{'name': 'Amit', 'age': 20, 'course': 'Python'} <class 'dict'>
Student Name: Amit
Code language: HTML, XML (xml)

5. Set Type (set)

A Set is an unordered collection of unique elements. It is created using curly braces {}. Because sets only store unique items, any duplicate values passed during creation will be automatically removed.

# Notice that 10 and 20 are repeated, but the set removes duplicates
my_set = {10, 20, 30, 10, 20}
print(my_set, type(my_set))
Code language: PHP (php)

Output:

{10, 20, 30} <class 'set'>
Code language: HTML, XML (xml)

Type Conversion (Type Casting)

Sometimes, you need to convert a variable from one data type to another. This process is called Type Casting. Python provides built-in functions like int(), float(), str(), and list() to achieve this.

Example of Type Casting:

a = 10       # int
b = 5.5      # float

# Converting float to int
converted_b = int(b)
print(converted_b, type(converted_b))

# Converting int to float
converted_a = float(a)
print(converted_a, type(converted_a))
Code language: PHP (php)

Output:

5 <class 'int'>
10.0 <class 'float'>
Code language: HTML, XML (xml)
Tags: #basic Python standard data types #beginner guide to Python variables #converting float to int in Python #difference between list and tuple in Python #how to check exact data type in Python #learn Python programming #Python boolean type True False #Python data types #Python dictionary key-value pairs #Python dynamic typing explained #Python for beginners #Python numeric types integer float complex #Python sequence types string list tuple #Python set unique elements #Python type conversion and type casting

Leave a Comment