bytes

tutorials

python

data types in python

Data Types in Python

Module - 4 Data Structures in Python
Data Types in Python

Overview:

Python offers several data types, including numbers (integers and floating-point), lists , dictionaries (for key-value pairs), booleans (True or False ), and sets (for unique values ). These data types help store and process data efficiently. 

Data Types in Python-min.png

What are Data Types?

Saranya is a data scientist working on a project to gather and analyze data. As part of her analysis, she needed to understand the different data types she would be dealing with. Saranya came across a book ???? that contained information about the different data types.

The book explained that data could be classified as one of four types: integers , floats, strings 🔤 and booleans. Saranya discovered that an integer is a whole number like 3 or -25; a float is a number with decimals like 3.1415; strings are words or phrases contained within quotation marks like "Hello World"; and booleans can only have two possible values: true and false .

The book also explained how each type of data has its unique properties and how they can be used in various ways. For example, Saranya learned that integers could be added together to find the sum of two integers, but multiplying an integer by another integer will produce another integer. Saranya found a similar book 📚 that talked about the types of data that can be classified as resources.

The resource type in the book is called a "resource locator" because it will tell Saranya where to find the resources she needs, which make up her project . Resources are divided into four categories: strings, floats, booleans, and dates . A string is a text contained within quotation marks like "Hello World,"  whereas booleans can only have two values: true or false . At the same time, dates are numerical representations of a specific date .

💡 Did you Know ? In Python, the data types are also classified based on their mutability. Mutable data types can be changed after creation, while immutable data types cannot be changed after creation.

What are the different Types of Data in Python?

Python is a high-level, object-oriented programming language that provides a wide range of data types to aid in developing numerous applications.

List of data types in Python

  • Numeric Data types
  • Sequence Data types
  • Dictionaries in Python
  • Booleans in Python
  • Sets in Python
  1. Numeric Data types

Numeric data types are data types that represent numbers, such as integers and floating point numbers. They are used in programming and databases to store and manipulate numerical values. The most common numeric data types in Python are:

  1. integers: whole numbers without decimals, positive or negative, of unlimited length
  2. floating point numbers: numbers with decimals, positive or negative, of limited length
  3. complex numbers: combinations of real and imaginary numbers, like a + bj

Integers (int) represent whole numbers. Floating point numbers (float) have decimals. Complex numbers (complex) include real and imaginary parts.

variable_name = value of variable

Example:

int_var = -5
float_var = 3.14159
complex_var = 3+2j
print(type(int_var))
print(type(float_var))
print(type(complex_var))

2. Sequence Data types in Python

In Python, sequence data types refer to objects that hold a collection of items, such as strings, lists, and tuples. These objects can be indexed and sliced and have various methods for modifying and manipulating their contents.

  1. Strings: Strings are sequences of characters enclosed in single or double quotes.
  2. Lists: Lists are ordered lines of values enclosed in square brackets. They can include any data type.
  3. Tuples: Tuples are immutable, ordered sequences of values defined using parentheses.

Syntax:

variable_name=string_value
variable_name=[list values]
variable_name=(tuple values)

Examples:

str_var = "Data scientists don't just build models; they build solutions"
list_var=['Harry potter','Hermione','Ron']
tupple_var=('Harry potter','Hermione','Ron')
print(type(str_var))
print(type(list_var))
print(type(tupple_var))

3. Dictionaries in python

A dictionary in Python is an unordered set of key-value pairs. Keys identify elements in the dictionary . Values can be any Python object. Dictionaries are mutable , changeable, and indexed for easy, fast access and modification . Dictionaries map keys to values like a dictionary maps words to definitions .

Syntax:

variable_name={key1:value1,key2:value2}

Examples:

my_dict = {'name':'John', 'age':25}
my_dict2 = {1:'a', 2:'b', 3:'c'}
my_dict3 = {'fruits':['apple', 'banana', 'mango'], 'vegetables':['carrot', 'potato', 'cucumber']}
print(type(my_dict3))

4. Booleans in Python

In Python, booleans represent either True or False values. They are crucial for controlling program flow with conditional statements like if and else.

Example:

x = True
y = False
if x:
    print("x is True!")
else:
    print("x is False!")

5. Sets in Python

  • Sets in Python are unordered collections of unique elements.  They are mutable and useful for storing and manipulating unique, unordered data. 
  • Sets enable set operations like union, intersection, difference, and symmetric difference. They are useful in mathematics and for removing duplicates from sequences. 
  • Sets are mutable, meaning they can be changed. However, they remain unordered and unindexed.

Syntax:

variable_name={set values}

Example:

my_set = {1,2,3,4,5} # This is a set of numbers

Conclusion:

Saranya has understood different data types in Python, such as numeric, sequence, dictionary, boolean, and set. She can work with these data types to analyze data and draw meaningful insights. She is familiar with the basic operations of each data type and can use them for her data science projects.

Key Takeaways:

  1. Python has various built-in data types, including integers, floats, strings, booleans, lists, tuples, dictionaries, and sets.
  2. Each data type has a specific purpose and should be used for the appropriate task.
  3. Integers represent whole numbers. Floats represent real numbers. Strings represent text. Booleans represent truth values. Lists represent collections of values. Tuples represent immutable collections of values. Dictionaries define key-value pairs. Sets represent collections of unique values.
  4. Type conversion can convert one data type into another.
  5. When working with data, remember to consider the data types used and ensure they suit the task.

Quiz:

  1. Which of the following is a data type in Python?
    1. String 
    2. Integer 
    3. Float 
    4. All of the above

Answer: D. All of the above

  1. What data type would you use to store a list of values?
    1. Integer 
    2. Float 
    3. String
    4. List

Answer: D. List

  1. What data type would you use to store a single character?  
    1. Integer 
    2. Float 
    3. String 
    4. Boolean

Answer: C. String

Related Programs
Full Stack Data Science with Placement Guarantee of 5+ LPA
Course
20,000 people are doing this course
Become a job-ready Data Science professional in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.
Related Tutorials

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter.

avatar
Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter
Vikash SrivastavaCo-founder & CPTO AlmaBetter
Vikas CTO
Related Tutorials to watch
Top Articles toRead
AlmaBetter
Made with heartin Bengaluru, India
  • Location
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2022 AlmaBetter