Data Analysis With Python, String, List, Tuples, Dictionary
Data Analysis With Python, String, List, Tuples, Dictionary
Strings
A string is a sequence of characters.
For example, "hello" is a string containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.
OUTPUT
Python programming
OUTPUT
2. Negative Indexing:
OUTPUT
3. Slicing:
OUTPUT
ytho
OUTPUT
yhnpo
OUTPUT
False
True
2. Join Two or More Strings: We can join (concatenate) two or more strings using the + operator.
str1 = "Hello"
str2 = "World"
OUTPUT
HelloWorld
3. Finding length of String: We can find the length of a string using len() function.
str1 = "Hello"
OUTPUT
OUTPUT
H
e
l
l
o
LIST
Lists are used to store multiple data at once.
For example: Suppose we need to record the ages of 5 students. Instead of creating 5 separate variables, we can
simply create a list:
for i in range(len(age)):
print(age[i], end=" ")
OUTPUT
17 18 15 19 14
2. Negative Indexing:
age= [17, 18, 15, 19, 14]
print(age[-3])
OUTPUT
15
3. Slicing
age= [17, 18, 15, 19, 14]
print(age[2:4])
OUTPUT
[15, 19]
Add Elements to a Python List:
1. Using append(): The append() method adds an item at the end of the list.
age= [17, 18, 15, 19, 14]
age.append(90)
print(age)
OUTPUT
even_numbers = [4, 6, 8]
print("List2:", even_numbers)
# join two lists
prime_numbers.extend(even_numbers)
OUTPUT
List1: [2, 3, 5]
List2: [4, 6, 8]
List after append: [2, 3, 5, 4, 6, 8]
3. Using insert(): The insert() method inserts an element to the list at the specified index.
# create a list of vowels
vowel = ['a', 'e', 'i', 'u']
print('List:', vowel)
OUTPUT
1. Using del: We can use the del statement to remove one or more items from a list.
languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']
OUTPUT
2. Using remove(): We can also use the remove() method to delete a list item.
languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']
# remove 'Python' from the list
languages.remove('Python')
print(languages)
OUTPUT
3. Using pop(): The pop() method removes the item at the given index from the list and returns the removed
item.
# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]
OUTPUT
Removed Element: 5
Updated List: [2, 3, 7]
4. Using clear():The clear() method removes all items from the list.
prime_numbers = [2, 3, 5, 7, 9, 11]
OUTPUT
Tuple
A tuple in Python is similar to a list. The difference between the two is that we cannot change the elements of a tuple
once it is assigned whereas we can change the elements of a list.
Creating a Tuple:
# Empty tuple
my_tuple = ()
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
OUTPUT
()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))
Accessing a Tuple:
Like a list, tuples can be accessed using:
1. Index
2. Negative Index
3. Slicing
Python Tuple Methods:
DICTIONARY
Python dictionary is an ordered collection of items. It stores elements in key/value pairs. Here, keys are unique
identifiers that are associated with each value.
Example:
If we want to store information about countries and their capitals, we can create a dictionary with country names
as keys and capitals as values.
Create a dictionary:
capital_city = {"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}
print(capital_city)
OUTPUT
OUTPUT
Fatima
Ali
Add Elements to a Dictionary:
capital_city = {"Nepal": "Kathmandu", "England": "London"}
print("Initial Dictionary: ",capital_city)
capital_city["Japan"] = "Tokyo"
OUTPUT
student_id[112] = "Zahra"
OUTPUT
del student_id[111]
OUTPUT
FILES
A file is a container in computer storage devices used for storing data.
When we want to read from or write to a file, we need to open it first. When we are done, it needs to be closed so
that the resources that are tied with the file are freed.
Hence, in Python, a file operation takes place in the following order:
1. Open a file
2. Read or write (perform operation)
3. Close the file
OUTPUT
Mode Description
r Open a file for reading. (default)
w Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
x Open a file for exclusive creation. If the file already exists, the operation fails.
a Open a file for appending at the end of the file without truncating it. Creates a new file if it does not
exist.
t Open in text mode. (default)
b Open in binary mode.
+ Open a file for updating (reading and writing)
OUTPUT
THE END