Working with strings, numbers, lists, tuples, dictionaries, and sets in Python

Python is a versatile programming language that provides various data types to work with. In this article, we will explore how to work with strings, numbers, lists, tuples, dictionaries, and sets in Python.

Working with Strings

Strings are sequences of characters enclosed in either single ('') or double ("") quotes. Python provides numerous string manipulation methods to perform operations such as concatenation, slicing, and formatting.

name = "John Doe"
print(len(name)) # prints the length of the string
print(name.upper()) # converts the string to uppercase
print(name.lower()) # converts the string to lowercase
print(name.split()) # splits the string into a list of words

Working with Numbers

Python supports different numeric types such as integers, floating-point numbers, and complex numbers. It also offers various mathematical operations and built-in functions to work with numbers.

x = 10
y = 5
print(x + y) # addition
print(x - y) # subtraction
print(x * y) # multiplication
print(x / y) # division

import math
print(math.sqrt(x)) # square root
print(math.pow(x, y)) # exponentiation

Working with Lists

Lists are ordered and mutable collections of items that can hold values of different types. Lists are denoted by square brackets ([]). Python provides methods to manipulate lists efficiently.

numbers = [1, 2, 3, 4, 5]
names = ['Alice', 'Bob', 'Charlie']
mixed_list = [1, 'Alice', 3.14, True]

print(len(numbers)) # prints the length of the list
print(names[0]) # access the first element of the list
print(numbers + names) # concatenates two lists
numbers.append(6) # appends an element to the list
numbers.remove(3) # removes an element from the list

Working with Tuples

Tuples are similar to lists, but they are immutable, meaning their values cannot be modified after creation. Tuples are denoted by parentheses (()).

point = (3, 7)
dimensions = (10, 20, 30)
person = ('John', 25, 'Designer')

print(point[0]) # access the first element of the tuple
print(len(dimensions)) # prints the length of the tuple
print(person.count('John')) # counts the occurrences of 'John'
print(person.index('Designer')) # finds the index of 'Designer' in the tuple

Working with Dictionaries

Dictionaries are key-value pairs where each value is associated with a unique key. Dictionaries are denoted by curly braces ({}) and allow efficient lookup and retrieval of values.

student = {'name': 'Alice', 'age': 20, 'major': 'Computer Science'}

print(student['name']) # access the value using the key
print(len(student)) # prints the number of key-value pairs
student['age'] = 21 # modifies the value associated with the key 'age'
student['grade'] = 'A' # adds a new key-value pair to the dictionary
del student['major'] # deletes the key-value pair from the dictionary

Working with Sets

Sets are unordered collections of unique elements. They are useful for performing mathematical set operations like union, intersection, and difference.

fruits = {'apple', 'banana', 'orange'}
vegetables = {'carrot', 'celery'}

print('apple' in fruits) # checks if an element is present in the set
print(fruits.union(vegetables)) # returns a new set with all elements from both sets
print(fruits.intersection(vegetables)) # returns a new set with common elements
print(fruits.difference(vegetables)) # returns a new set with elements in fruits but not in vegetables

In this article, we have covered the basics of working with strings, numbers, lists, tuples, dictionaries, and sets in Python. These data types and their respective methods provide powerful capabilities for handling and manipulating data effectively.

noob to master © copyleft