Saturday, June 29, 2019

ListenData: Python Data Structures

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 :
  1. strings
  2. lists
  3. tuples
  4. dictionaries
  5. 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.

mystring = "Hello Python3.6"
print(mystring)
Output:
Hello Python3.6
Can I use multiple single or double quotes to define string?
Answer is Yes. See examples below -
Multiple Single Quotes

mystring = '''Hello Python3.6'''
print(mystring)
Output:
Hello Python3.6
Multiple Double Quotes

mystring = """Hello Python3.6"""
print(mystring)
Output:
Hello Python3.6
How to include quotes within a string?

mystring = r'Hello"Python"'
print(mystring)
Output:
Hello"Python"
How to extract Nth letter or word?
You can use the syntax below to get first letter.

mystring = 'Hi How are you?'
mystring[0]
Output
'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

mystring.split(' ')[0]
Output : Hi
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.
  1. x = [142, 124, 234, 345, 465]
  2. y = [‘A’, ‘C’, ‘E’, ‘M’]
  3. 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]
  1. start : refers to starting position.
  2. stop : refers to end position.
  3. 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 :

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.
To select multiple elements from a list, you can use the following method :
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]
READ MORE »

from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...