Python Data Types
In Python, data types are used to define the type of values that a variable can hold. Python supports several built-in data types including numbers, strings, lists, tuples, dictionaries, and booleans.
Numbers Data Types
Python supports three types of numbers: integers, floating-point numbers, and complex numbers. Integers are whole numbers, floating-point numbers have a decimal point, and complex numbers have a real and imaginary component. For example,
1integer_number = 10 2float_number = 3.14 3complex_number = 3 + 4j
Strings Data Type
In Python, strings are used to represent a sequence of characters. They can be written using single or double quotes. For example,
1string_literal = "Hello, World!" 2string_literal_single_quote = 'Hello, World!'
Lists Data Type
In Python, lists are used to store a collection of items. They are written using square brackets and separated by commas. For example,
1list_literal = [1, 2, 3, 4, 5]
Tuples Data Type
In Python, tuples are similar to lists, but they are immutable, meaning their values cannot be changed once they are created. Tuples are written using parentheses and separated by commas. For example,
1tuple_literal = (1, 2, 3, 4, 5)
Dictionaries Data Type
In Python, dictionaries are used to store key-value pairs. They are written using curly braces and key-value pairs separated by colons. For example,
1dictionary_literal = {'name': 'William', 'age': 30}
Booleans Data Type
In Python, booleans are used to represent true or false values. They are written using the keywords True
and False
. For example,
1bool_literal = True
In addition to the built-in data types, Python also supports user-defined data types through classes and objects. These allow you to create custom data types that can have their own properties and methods.