Python Basics

Data Types in Python: A Short Guide

Table of Contents

One of the essential concepts in Python, as in any other programming language, is data types. In this article, we will explore the different data types available in Python, their characteristics, and how to use them.

Built-in Data Types

Python has several built-in data types that cater for different types of data:

Data Type Description Example
Int Represents whole numbers. 4, -2, 0
Float Represents real numbers with decimal points. 2.14, -4.5
String Represents a sequence of characters. "Hello", "Developer"
List Represents an ordered collection of items. [1, 2, 3], ["bread", "crumbs"]
Tuple Similar to a list but immutable. (1, 2), ("bread", "crumbs")
Dictionary Represents an unordered collection of key-value pairs. {‘name’: ‘Michael’, ‘age’: 20, ‘city’: ‘Berlin’}

Integer (Int)

An integer is a whole number without decimals or fractions. Python supports both positive and negative integers, as well as very large numbers. For example:


x = 10
y = -5
z = 9223372036854775807

Floating-point (Float)

A floating-point number is a number with decimals or fractions. Python uses the float data type to represent these numbers. For example:


x = 3.14
y = -0.025
z = 1e-8

String (Str)

A string is a sequence of characters, enclosed in single quotes or double quotes. Strings can be used to represent text or other data types. For example:


x = 'Hello, World!'
y = "This is a string with double quotes."
z = "Python \t is \n an \t awesome \t language!"

Boolean (Bool)

A boolean value can be either True or False. Boolean values are used to represent logical conditions and decisions in Python. For example:


x = True
y = False
z = 5 > 3

List (List)

A list is a collection of items, which can be of any data type. Lists are mutable, meaning their contents can be changed after creation. For example:


x = [1, 'raspberry', 3.14, ['lion', 'zebra']]
y = ['Python', False, 42]
z = list('Hello')

Tuple (Tuple)

A tuple is similar to a list but immutable, meaning its contents cannot be changed after creation. Tuples are useful for storing data that should not be modified. For example:


x = (1, 'raspberry', 3.14, ('lion', 'zebra'))
y = ('Python', False, 42)
z = tuple('Hello')

Dictionary (Dict)

A dictionary is a collection of key-value pairs. Dictionaries are mutable and can be used to store data in an associative manner. For example:


x = {'name': 'Michael', 'age': 30, 'city': 'Berlin'}
y = {'Python': 3.9, 'Java': 11.0, 'Ruby': 2.7}
z = dict(name='Michael', age=30, city='Berlin')

Custom Data Types (Classes)

Python also allows you to create custom data types using classes. This is an advanced topic and will be covered in a future article.

In conclusion, understanding the various data types available in Python is crucial for effective programming. By mastering these concepts, you’ll be well on your way to creating powerful and efficient applications with Python. Stay tuned for more articles on advanced topics!

To top