This post explains the data structures used in Python. It is essential to understand the data structures in a programming language. In python, there are many data structures available. They are as follows :
READ MORE »
- strings
- lists
- tuples
- dictionaries
- sets
Python Data Structures
1. Strings
Python String is a sequence of characters.
How to create a string in Python
You can create Python string using a single or double quote.
Output:
mystring = "Hello Python3.6"
print(mystring)
Hello Python3.6
Can I use multiple single or double quotes to define string?
Answer is Yes. See examples below -
Multiple Single Quotes
Output:
mystring = '''Hello Python3.6'''
print(mystring)
Hello Python3.6
Multiple Double Quotes
Output:
mystring = """Hello Python3.6"""
print(mystring)
Hello Python3.6
How to include quotes within a string?
Output:
mystring = r'Hello"Python"'
print(mystring)
Hello"Python"
How to extract Nth letter or word?
You can use the syntax below to get first letter.
Output
mystring = 'Hi How are you?'
mystring[0]
'H'mystring[0] refers to first letter as indexing in python starts from 0. Similarly, mystring[1] refers to second letter. To pull last letter, you can use -1 as index.
mystring[-1]
To get first word
Output : Hi
mystring.split(' ')[0]
How it works -
1. mystring.split(' ') tells Python to use space as a delimiter.
Output : ['Hi', 'How', 'are', 'you?']2. mystring.split(' ')[0] tells Python to pick first word of a string.
2. List
Unlike String, List can contain different types of objects such as integer, float, string etc.
- x = [142, 124, 234, 345, 465]
- y = [‘A’, ‘C’, ‘E’, ‘M’]
- z = [‘AA’, 44, 5.1, ‘KK’]
Get List Item
We can extract list item using Indexes. Index starts from 0 and end with (number of elements-1). Syntax : list[start : stop : step]
- start : refers to starting position.
- stop : refers to end position.
- step : refers to increment value.
k = [124, 225, 305, 246, 259]
k[0]
k[1]
k[-1]
k[0]
124
k[1]
225
k[-1]
259
Explanation :
To select multiple elements from a list, you can use the following method :
k[0] picks first element from list. Negative sign tells Python to search list item from right to left. k[-1] selects the last element from list.
k[:3] returns [124, 225, 305]
k[0:3] also returns [124, 225, 305]
k[::-1] reverses the whole list and returns [259, 246, 305, 225, 124]
Sort list
sorted(list)
function arranges list in ascending order. sorted(list, reverse=True)
function sorts list in descending order.
sorted(k) returns [124, 225, 246, 259, 305]
sorted(k, reverse=True) returns [305, 259, 246, 225, 124]
Add 5 to each element of a list
In the program below, len() function is used to count the number of elements in a list. In this case, it returns 5. With the help of range() function, range(5) returns 0,1,2,3,4.
x = [1, 2, 3, 4, 5]
for i in range(len(x)):
x[i] = x[i] + 5
print(x)
[6, 7, 8, 9, 10]It can also be written like this -
for i in range(len(x)):
x[i] += 5
print(x)
Combine / Join two lists
The '+' operator is concatenating two lists.
X = [1, 2, 3]
Y = [4, 5, 6]
Z = X + Y
print(Z)
[1, 2, 3, 4, 5, 6]
from Planet Python
via read more
No comments:
Post a Comment