My Second Week Of Learning Python by Hrithik Sharma


Hello, I am back with another blog. So, in my previous blog, I told You about how amazing, advanced and powerful the python programming is. In my previous blog, I talked about some useful keywords in python. So, in the second week of learning python, I learned some more new and advanced things regarding the programming language in another past week which is my second week of learning python for Artificial Intelligence and Machine Learning. In my second week of learning python, I learned new topics such as strings, data structures in python such as lists, tuples, dictionaries, and sets, and functions also.

As I talked in my previous blog that in the field of Artificial Intelligence and Machine Learning the python language is widely used similarly in the field of computer vision. Computer Vision is a subset of the field of Machine Learning which consists of the study of how to teach the machines to see like as humans see and recognise the objects and things in a similar way it is done by the machines. Like in the above picture the things and objects such as cars, people, trucks, buses, traffic lights, and even handbags are seen and recognised by the computer or machine. A few days back I saw a Ted Talk by Fei Fei Li which is totally on the field of computer vision.

So, In today's blog, I am going to talk about the list data structure in detail. Lists are ordered and changeable and allow the duplicate members. In python, lists are written in square brackets []. For Example:  

Cities=['New York', 'Bronx', 'Los Angeles', 'Las Vegas', 'San Francisco']

print(Cities)

['New York', 'Bronx', 'Los Angeles', 'Las Vegas', 'San Francisco']


Here In the above example, I created a list with a object named Cities in which I listed the five cities of the USA and then I printed the list then I get the desired output of Cities. Here In the above example, I created a list with a object named Cities in which I listed the five cities of the USA and then I printed the list then I get the desired output of Cities. In Lists, there are many operations can be performed by the use of many functions that are made for lists.  

MyList=[12,'Python','Java',54,'x','y',[1,2,3,4],9.9,10.52]

print(MyList)

[12,'Python','Java',54,'x','y',[1,2,3,4],9.9,10.52]


In lists, we can store the different examples of different data types for example integers, floats, characters, strings and even we can create nested lists. 

Let's talk about some important functions which can be used on lists.

Let's take our very first list of Cities.

['New York', 'Bronx', 'Los Angeles', 'Las Vegas', 'San Francisco']



  • append() function

Cities.append('San Jose')

print(Cities) 

['New York', 'Bronx', 'Los Angeles', 'Las Vegas', 'San Francisco','San Jose']


By the use of append() function, we can insert a member at the last index of the list. The append function takes one argument which is to be inserted. The append() function appends an element to the end of the list.

  • insert() function

Cities.insert(3, 'Chicago')

print(Cities)

['New York', 'Bronx', 'Los Angeles', 'Chicago', 'Las Vegas', 'San Francisco', 'San Jose']



By the use of insert() function, we can insert an element at any specific index. The insert function takes two arguments one is for index and one for the element to be inserted. The insert() function inserts the specified value at the specified position.

  • extend() function

Cities.extend(['Queens', 'Boston'])


print(Cities)

['New York', 'Bronx', 'Los Angeles', 'Chicago', 'Las Vegas', 'San Francisco', 'San Jose', 'Queens', 'Boston']


By the use of extend() function, we can insert an element as well as the list of the elements as illustrated in the example. The extend() function takes one argument. The extend() function adds the specified list elements to the end of the current list.

  • index() function

print(Cities.index('Los Angeles'))

2


By the use of index() function, we can find the index of a specific element in the list. The index() function returns the position at the first occurrence of the specified value.

  • count() function

print(Cities.count('San Francisco')

1


The count() method returns the number of elements with the specified value.

  • pop() function

print(Cities.pop())

Boston

print(Cities)

['New York', 'Bronx', 'Los Angeles', 'Chicago', 'Las Vegas', 'San Francisco', 'San Jose', 'Queens']

# WHEN THE INDEX IS SPECIFIED

print(Cities.pop(2))

Los Angeles

print(Cities)

['New York', 'Bronx', 'Chicago', 'Las Vegas', 'San Francisco', 'San Jose', 'Queens']


By the use of the pop() function, the last element of the list is popped i.e the element at the last index by default unless the index is not specified as an argument. The pop() function removes the element at the specified position.

  • remove() function

print(Cities.remove('Bronx')

print(Cities)

['New York', 'Chicago', 'Las Vegas', 'San Francisco', 'San Jose', 'Queens']


By the use of remove() function, the specified element as the argument is removed at the first occurrence. The remove() function removes the first occurrence of the element with the specified as a argument.

  • reverse() function

print(Cities)

['New York', 'Chicago', 'Las Vegas', 'San Francisco', 'San Jose', 'Queens']

Cities.reverse()

print(Cities)

['Queens', 'San Jose', 'San Francisco', 'Las Vegas', 'Chicago', 'New York']



By the use of reverse() function, the list is reversed. The reverse() function reverses the sorting order of the elements.

  • copy() function

print(Cities)


['Queens', 'San Jose', 'San Francisco', 'Las Vegas', 'Chicago', 'New York']

x=cities.copy()

print(x)


['Queens', 'San Jose', 'San Francisco', 'Las Vegas', 'Chicago', 'New York']


By the use of copy() function, the copy of the list is generated with a different memory object. The copy() function returns a copy of the specified list.

  • clear() function

print(Cities)

['Queens', 'San Jose', 'San Francisco', 'Las Vegas', 'Chicago', 'New York']

Cities.clear()

print(Cities)

[]


By the use of clear() function, the whole list is get emptied. The clear() function removes all the elements from a list.

  • sort() function

n = [45,98,89,1,50,38,56,75,25]

print(n)

[45,98,89,1,50,38,56,75,25]

n.sort()

print(n)

[1, 25, 38, 45, 50, 56, 75, 89, 98]


The sort() function sorts the list ascending by default.

List indexing, slicing, finding the length and reversing of list

# FINDING THE LENGTH OF LIST

l=[45,98,89,1,50,38,56,75,25]

print(len(l))

9

# INDEXING

print(l[1])

25

print(l[2])

38

# SLICING OF LISTS

print(l[::])

[1, 25, 38, 45, 50, 56, 75, 89, 98]

print(l[2:])

[38, 45, 50, 56, 75, 89, 98]

print(l[:6])

[1, 25, 38, 45, 50, 56]

print(l[2:7])

[38, 45, 50, 56, 75]

# REVERSING OF LIST

print(l[::-1])

[98, 89, 75, 56, 50, 45, 38, 25, 1]




This is all about my second blog of learning python for Machine Learning and Artificial Intelligence as soon I learn different concepts of python I'll keep posting. So, there are more to go as I am learning.

Bye-Bye, See You in my next blog. Until then PEACE.

1 view

Contact Us:

A-116, The Corenthum A-65,

Sector 62

Noida 201301

UP, India

Phone: +91 8882050481

Email: info@theikigailab.com

© 2020 TheIkigaiLab  Terms of Use Privacy and Security Statement