GeneralBlog

Python Data Types

In Python, variables can be used to store values of different data types. A data type is a set of rules for the possible values of a variable and the operations that can be applied to these values. The following are some examples of data types in Python:

Python has the following standard or built-in data types:

Python has the following standard or built-in data types:

  • Numbers
  • String
  • List
  • Tuple
  • Set
  • Dictionary

1. Numbers

Python has two numeric types: integers (whole numbers with no fractional part) and floating-point numbers. Integers are denoted in the execution of a program by small, plain decimal values such as -5, 0, 1, 2, 3 and 4. Floating-point numbers can include a fractional part. Floating-point values are written as some number followed by an exponent; for example: 3.14159265358979323846264338327950288419713089793238462643383280.

The difference between the two numeric types is important because integers are stored in a different way than floating-point values. Integers can be stored as binary numbers, which are made up of only 0s and 1s. Floating-point values require more storage space because they need to store both the value and its exponent separately.

2. String

A string is an immutable sequence of characters. You can create a string by enclosing characters in quotation marks (“), or you can use single or triple quotes (” or ”’).

Strings are objects, which means they have methods and attributes. The most useful methods for working with strings are:

  • chop() – Removes the last character from a string if it exists; otherwise, returns None.
  • endswith() – Checks whether the end of a string ends with another specified character(s).
  • find() – Finds one substring within another, starting at the specified position, or returns None if no match is found.
  • index() – Returns the index where one specified substring appears within another; raises ValueError when there is no match when searching from left to right.
  • lower() – Converts all uppercase letters in a string to lowercase as it returns it; if passed no argument, returns itself; if given an int argument N, also changes its first N bytes/characters to lowercase after returning itself (but only if they were originally all uppercase).
  • lstrip(), rstrip(), strip() – Remove leading whitespace from each line before returning them (by default); any other combination of arguments will remove trailing whitespace only instead.”

3. List

List is another data type in Python, which stores multiple values in the form of an ordered collection. The elements of a list are separated by commas and enclosed in square brackets ([]). A list can contain any data type such as integers, floats, strings and Booleans along with other objects that you create using classes. List is mutable because it allows us to add values at any position or remove them from the existing one. We can also change the value of an element in a list by changing its index or slice out an element from the beginning or end of the sequence.

List is an ordered sequence because each element appears at its particular position within a sequence; for example [“a”, “b”]. Indexing accesses elements of a list based on their positions from 0 up to (but not including) len(list). Similarly slicing extracts ranges of elements starting from specified indices i.e., startIndex..endIndex-1

The syntax for declaring a list is as follows: variable_name = [element1, element2, element3]

4. Tuple

A tuple is an immutable sequence of immutable items, which means that once you create a tuple, it can never be changed or altered. Tuples are used to store a group of related values and have many uses in Python programming:

  • They can be used as records
  • They can be passed around like lists but don’t change if their items are modified (e.g. for UI elements)
  • They allow you to return multiple values from a function

5. Set

The set type is a collection of unique items. It does not allow duplicate items, and order is not defined. Set elements are unordered. Therefore, it doesn’t matter what the order of the elements stored in a set are when you iterate over them.

The Python library provides two built-in functions to work with sets:

  • filter() – Returns a new list containing only those items that satisfy the given predicate function; returns an empty list if no values satisfy it
  • map() – Returns its argument as modified by applying each of its input nodes to a corresponding node in another sequence or iterable object (for example, file objects), producing zero or more output nodes

6. Dictionary

The dictionary is a collection of key-value pairs. The key can be any type and the value is also any type.

It’s important to note that both keys and values are unique within a given dictionary. This means that if you have two different dictionaries, they may have the same value for a certain key but they won’t have the same keys.

For example, let’s say we want to store information about cars: their make, model, year and color. We would create four different dicts called car_details1 through 4 with their respective values as follows: car_details1 = {‘make’:’Toyota’, ‘model’:’Camry’, ‘year’:2018,’color’:’silver”}

In this article, we have learned what are data types in python. We have gone through Python’s list of built-in data types as well as how to create custom data types. We also looked at the difference between immutable and mutable objects and how to handle them when working with lists in Python.

Reading more about Python!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button