Python Native Data Types
1. Numbers
Python supports various numeric types:
- Integers (
int
): Whole numbers (e.g.,10
,-3
) - Floating-point numbers (
float
): Decimal numbers (e.g.,3.14
,-2.71
) - Complex numbers (
complex
): Numbers with a real and imaginary part (e.g.,2+3j
)
Operations on Numbers:
-
Arithmetic:
+, -, *, /, // (floor division), % (modulo), ** (power)
a = 10 b = 3 print(a + b) # 13 print(a / b) # 3.333 print(a // b) # 3 print(a % b) # 1 print(a ** b) # 1000
-
Type Conversion:
int()
,float()
,complex()
print(int(5.7)) # 5 print(float(3)) # 3.0 print(complex(2, 3)) # (2+3j)
-
Built-in Functions:
abs(x)
: Returns absolute value.pow(x, y)
: Power calculation (x^y).round(x, n)
: Rounds to n decimal places.divmod(x, y)
: Returns quotient and remainder.
print(abs(-7)) # 7 print(pow(2, 3)) # 8 print(round(3.14159, 2)) # 3.14 print(divmod(10, 3)) # (3, 1)
2. Lists
A list is an ordered, mutable collection that can store heterogeneous items.
Operations on Lists:
-
Creating Lists:
lst = [1, 2, 3, 'a', 'b', 'c']
-
Accessing Elements:
- Indexing:
lst[0]
(first element) - Slicing:
lst[1:4]
(elements from index 1 to 3)
- Indexing:
-
List Methods:
append(x)
: Adds item at the end.extend(iterable)
: Extends list with items.insert(i, x)
: Inserts x at positioni
.remove(x)
: Removes the first occurrence of x.pop([i])
: Removes item at positioni
(last by default).index(x)
: Returns index of x.count(x)
: Counts occurrences of x.sort()
: Sorts the list.reverse()
: Reverses the list.
lst = [10, 20, 30] lst.append(40) # [10, 20, 30, 40] lst.insert(1, 15) # [10, 15, 20, 30, 40] lst.pop() # [10, 15, 20, 30] lst.sort() # [10, 15, 20, 30]
3. Tuples
A tuple is an ordered, immutable collection of items.
Characteristics:
- Immutable: Once created, cannot be changed.
- Faster than lists for iteration.
Creating and Accessing Tuples:
tup = (1, 2, 3, 'a')
print(tup[0]) # 1
print(tup[1:3]) # (2, 3)
Tuple Methods:
count(x)
: Counts occurrences of x.index(x)
: Returns index of x.
4. Sets
A set is an unordered, mutable collection with unique elements.
Operations on Sets:
-
Creating Sets:
s = {1, 2, 3, 3} # {1, 2, 3}
-
Set Methods:
add(x)
: Adds element x.update(iterable)
: Adds multiple elements.remove(x)
: Removes x; raises an error if not found.discard(x)
: Removes x; no error if not found.pop()
: Removes and returns an arbitrary element.clear()
: Clears the set.union()
: Combines sets.intersection()
: Finds common elements.difference()
: Finds differences.
s1 = {1, 2, 3} s2 = {3, 4, 5} print(s1.union(s2)) # {1, 2, 3, 4, 5} print(s1.intersection(s2)) # {3}
5. Dictionary
A dictionary is an unordered collection of key-value pairs.
Creating Dictionaries:
d = {'name': 'Alice', 'age': 25, 'city': 'NY'}
Dictionary Methods:
keys()
: Returns keys.values()
: Returns values.items()
: Returns key-value pairs.get(key)
: Returns value for key.update(dict)
: Updates dictionary.pop(key)
: Removes key and returns value.clear()
: Clears dictionary.
d = {'name': 'Alice', 'age': 25}
print(d.keys()) # dict_keys(['name', 'age'])
print(d.get('name')) # Alice
d.update({'city': 'NY'}) # {'name': 'Alice', 'age': 25, 'city': 'NY'}
d.pop('age') # Removes 'age'
6. Strings
A string is an immutable sequence of characters.
String Operations:
- Concatenation:
+
- Repetition:
*
- Indexing and Slicing:
s = "hello" print(s[0]) # h print(s[1:4]) # ell
String Methods:
- Case Conversion:
upper()
,lower()
,capitalize()
,title()
,swapcase()
- Searching:
find(sub)
,index(sub)
,count(sub)
- Modification:
replace(old, new)
,strip()
,lstrip()
,rstrip()
- Splitting and Joining:
split()
,join(iterable)
- Validation:
isalpha()
,isdigit()
,isspace()
,isalnum()
s = "hello world" print(s.upper()) # HELLO WORLD print(s.replace("world", "Python")) # hello Python print(s.split()) # ['hello', 'world'] print("-".join(['a', 'b'])) # a-b
This overview provides a clear understanding of Python's native data types, operations, and methods. Let me know if you need further explanations or code examples!
Comments
Post a Comment