Python: Programming For Beginners: Learn The Fundamentals of Python in 7 Days by Michael Knapp

Python: Programming For Beginners: Learn The Fundamentals of Python in 7 Days by Michael Knapp

Author:Michael Knapp [Knapp, Michael]
Language: eng
Format: azw3
Published: 2017-05-31T04:00:00+00:00


Method two: [a] * d

In this example, you are going to be assuming that the variable that has to be repeated needs to be repeated not in one list, but in two different lists and the number of variables that have to be repeated is going to be different for each list.

Example

[m] * 4

M, m, m, m

[m] * 3

M, m, m

Removing elements

When you are taking elements off a list, there are going to be two methods that you are going to be able to use. First is the del statement which should only be used when you have to delete a specific element that should be removed from your list. The remove technique, on the other hand, is going to be used when all the elements need to be taken off your list.

Example

List a = [ ‘title’, ‘name’, ‘animal’,]

Print list a

Del list a [1]

Print everything that falls after the first index

Print list a

Result

List a [ ‘title’, ‘name’ ]

Sort

The sort method can be used for any element that you find on your list. You are not going to be using the function method, but the sort method is going to sort method will work similarly to this method.

Syntax:

List. Sort([func])

The function that is being used in the example that follows will not have any parameters.

Example

#! / usr/ bin/ Python

A list= [46, apple, moon, lie, flea];

A list.sort()

Print list

Result

A list [46, apple flea lie moon];

The count () method

With the count method, you are going to be able to get a result of how many times something is listed in the list that you are working with.

Syntax

List. Count(obj)

You are going to have to follow the parameters that are set into place for this method though, and that parameter will be the object parameter. It is this parameter that is going to give you the answer as to how many times objects are found on the list that you are currently working with.

Example

#! / usr/ bin/Python

Zlist = [46, apple flea lie moon, moon, moon]

Print “number of times moon appears zlist. count (moon)

Result: number for moon: 3



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.