Data Types Python
Sure! Let’s go through the main data types in Python: numbers, lists, sets, tuples, and dictionaries, along with their methods and examples.
1. Numbers
Python supports three types of numeric data:
- Integers: Whole numbers, e.g.,
5
,-3
- Floats: Decimal numbers, e.g.,
3.14
,-0.001
- Complex Numbers: Numbers with a real and imaginary part, e.g.,
2 + 3j
Example:
a = 5 # Integer
b = 3.14 # Float
c = 2 + 3j # Complex number
print(type(a)) # Output: <class 'int'>
print(type(b)) # Output: <class 'float'>
print(type(c)) # Output: <class 'complex'>
2. Lists
A list is an ordered collection of items which can be of different types. Lists are mutable, meaning they can be changed after creation.
Methods:
append()
: Adds an item to the end of the list.extend()
: Adds all items of a list to another list.insert()
: Inserts an item at a given position.remove()
: Removes the first occurrence of an item.pop()
: Removes and returns an item at a given position.clear()
: Removes all items from the list.index()
: Returns the index of the first occurrence of an item.count()
: Returns the count of the number of items.sort()
: Sorts the list in ascending order.reverse()
: Reverses the order of the list.copy()
: Returns a shallow copy of the list.
Example:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
3. Sets
A set is an unordered collection of unique items. Sets are mutable but do not allow duplicate elements.
Methods:
add()
: Adds an element to the set.update()
: Adds multiple elements to the set.remove()
: Removes an element from the set. Raises KeyError if the element is not found.discard()
: Removes an element from the set if it is a member. Does nothing if the element is not found.pop()
: Removes and returns an arbitrary set element. Raises KeyError if the set is empty.clear()
: Removes all elements from the set.union()
: Returns a set containing all elements from both sets.intersection()
: Returns a set containing only elements that are in both sets.difference()
: Returns a set containing elements that are in the first set but not in the second.symmetric_difference()
: Returns a set containing elements that are in either set but not in both.
Example:
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits) # Output: {'banana', 'cherry', 'apple', 'orange'}
4. Tuples
A tuple is an ordered collection of items which can be of different types. Tuples are immutable, meaning they cannot be changed after creation.
Methods:
count()
: Returns the number of times a specified value occurs in a tuple.index()
: Searches the tuple for a specified value and returns the position of where it was found.
Example:
fruits = ("apple", "banana", "cherry")
print(fruits[1]) # Output: banana
5. Dictionaries
A dictionary is an unordered collection of key-value pairs. Keys must be unique and immutable, while values can be of any type.
Methods:
get()
: Returns the value for a specified key.keys()
: Returns a list containing the dictionary’s keys.values()
: Returns a list of all the values in the dictionary.items()
: Returns a list containing a tuple for each key-value pair.update()
: Updates the dictionary with the specified key-value pairs.pop()
: Removes the element with the specified key.popitem()
: Removes the last inserted key-value pair.clear()
: Removes all elements from the dictionary.copy()
: Returns a copy of the dictionary.
Example:
person = {"name": "John", "age": 30, "city": "New York"}
print(person["name"]) # Output: John
person["email"] = "john@example.com"
print(person) # Output: {'name': 'John', 'age': 30, 'city': 'New York', 'email': 'john@example.com'}
Comments
Post a Comment