Pravar Agrawal Technology & Travel

Python Learning Part 3

In our last post, we left while discussing looping techinques in Python. Today, we’ll continue with discussing on For loops and their uses in Python programming language.

In Python, the for statement works differently then in C. Here for statement, iterates over a sequence of items which can be a list or a string. Like,

>>> x = ['Ball', 'Bat', 'Cat']
>>> for i in x:

We also have continue statements in Python, which skips the execution of code after it and goes back to the start. By using which, we can skip a part of the loop. For example,

while True:
   if n<0:
   elif n==0:
   print("The value of n is ", n)

Data structures are an important part of any programming language. In Python, there are multiple data structures available like List, Tuples, Dictionaries, Sets. Let’s take a look at each of these one-by-one. Lists, are a type of data structures where we can store multiple values. And those multiple values are accessible through indices of those items inside the list. For example,

>>> a = [12, 3, 4, 5]
>>> a[0]
>>> a[2]

Also we can use, append(), insert(), count() methods on the lists to append, insert and get the total count of items in the list. Example,

>>> a.insert(0,99)
>>> a
>>> a.append(45)
>>> a
[99, 12, 3, 4, 5, 45]
>>> a.count(3)

There are many other operations which we can carry out on lists and can be found here Lists. Another data structures are Tuples, which we have seen earlier as well. These actually hold data separated by commas. For example,

>>> a = 'Banana', 'Apple', 'Sitafal', 'Blueberry'
>>> a
('Banana', 'Apple', 'Sitafal', 'Blueberry')

Tuples are immutable meaning, the values at indices can’t be deleted, added, or edited inside it. We can even create a tuple with just one value and a comma like,

>>> a = 3,
>>> a

So, it’s the comma which makes a Tuple and not the paranthesis brackets. Another data structure is Sets, which does not consists of duplicate items. We can even apply mathematical set operations on these sets. For example,

>>> a = set('hjhjjdkfj')
>>> a
set(['h', 'k', 'j', 'd', 'f'])

One of the most important data structure is Dictionaries in Python and it’s most widely used as well. Dictionaries are a key-value based data structure where keys are unique. These are defined using {} braces. For example,

>>> dict_1 = {'Name':'Pravar', 'Surname':'Agrawal'}
>>> dict_1
{'Name':'Pravar', 'Surname':'Agrawal'}

We can declare an empty dictionary as well by using {} braces and assign key-value pairs to it by,

>>> data = {}
>>> data['key-1'] = 'value-1'
>>> data['key-2'] = 'value-2'
>>> data
{'key-2':'value-2', 'key-1':'value-1'}

In Python, we can even play with strings using some easy string manipulation. Strings are simple text which are declared between "" or ''. To understand this,

>>> s = "I'm an angel"
>>> s
"I'm an angel"
>>> print(s)
I'm an angel

There are a variety of methods available for strings as well. Such as, title(), uppper(), lower(), islower(), istitle() etc. For example,

>>> s = "My name is Pravar"
>>> s.title()
My Name Is Pravar
>>> s.upper()
>>> s.lower()
my name is pravar

Also, we can use methods like find(), startswith(), endswith() to search for values in a string. Like,

>>> s = "Hello hi howdy"
>>> s.find('hi')
>>> s.startswith("He")

We have seen some splendid features of Python ranging from variety of data structures to some easy handling of Strings. Now, in coming posts we’ll see how Functions, Exceptions are defined which will take us to a level up.