Book Image

Python Fundamentals

By : Ryan Marvin, Mark Nganga, Amos Omondi
Book Image

Python Fundamentals

By: Ryan Marvin, Mark Nganga, Amos Omondi

Overview of this book

After a brief history of Python and key differences between Python 2 and Python 3, you'll understand how Python has been used in applications such as YouTube and Google App Engine. As you work with the language, you'll learn about control statements, delve into controlling program flow and gradually work on more structured programs via functions. As you settle into the Python ecosystem, you'll learn about data structures and study ways to correctly store and represent information. By working through specific examples, you'll learn how Python implements object-oriented programming (OOP) concepts of abstraction, encapsulation of data, inheritance, and polymorphism. You'll be given an overview of how imports, modules, and packages work in Python, how you can handle errors to prevent apps from crashing, as well as file manipulation. By the end of this book, you'll have built up an impressive portfolio of projects and armed yourself with the skills you need to tackle Python projects in the real world.
Table of Contents (12 chapters)
Python Fundamentals
Preface

List Methods


The list data type has some built-in methods that can be used with it. These methods are as follows:

  • list.append(item)

  • list.extend(iterable)

  • list.insert(index, item)

  • list.remove(item)

  • list.pop([index])

  • list.clear()

  • list.index(item [, start [, end]])

  • list.count(item)

  • list.sort(key=None, reverse=False)

  • list.reverse()

  • list.copy()

Let's take a closer look at what these methods can do.

list.append(item)

The list.append(item) method adds a single item to the end of a list. This doesn't return a new list – it only modifies the original. The following is an example of this in use:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
   things = ["first"]
   
   things.append("another thing")
   
   things
   
=> ['first', 'another thing']

list.extend(iterable)

The list.extend(iterable) method takes one argument, which should be an iterable data type. It then extends the list by appending all of the items from the iterable to the list. What would happen is we would extend a list with another...