0% found this document useful (0 votes)
22 views

Pythona

The document discusses Python lists. Lists are mutable ordered sequences that can contain elements of different types. The document covers list operations like accessing, slicing, modifying, looping through lists and more.

Uploaded by

Subha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Pythona

The document discusses Python lists. Lists are mutable ordered sequences that can contain elements of different types. The document covers list operations like accessing, slicing, modifying, looping through lists and more.

Uploaded by

Subha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Python

Python Basics
• Object Oriented Programming Language
• Everything is object except control flow
• Installation
Python Basics
Python List
• List is an ordered sequence of items.
• All the items in a list do not need to be of the same type.
• [ ]
• a = [1, 2.2, 'python’]
a = [5,10,15,20,25,30,35,40]

# a[2] = 15
print("a[2] = ", a[2])

# a[0:3] = [5, 10, 15] list1 = ['physics', 'chemistry', 1997, 2000];


print("a[0:3] = ", a[0:3]) print "list1[0]: ", list1[0]

# a[5:] = [30, 35, 40] Output


print("a[5:] = ", a[5:])
list1[0]: physics

Output

a[2] = 15
a[0:3] = [5, 10, 15]
a[5:] = [30, 35, 40]
Lists are mutable, meaning, the value of elements of a list can be altered.

a = [1, 2, 3]
a[2] = 4
print(a)

Output

[1, 2, 4]

list = ['physics', 'chemistry', 1997, 2000];


print "Value available at index 2 : “
print list[2]
Output:
Value available at index 2 :
1997
thislist = ["apple", "banana", "cherry"]
print(len(thislist))

lists are defined as objects with the data type 'list’:


mylist = ["apple", "banana", "cherry"]
print(type(mylist))

Negative Indexing: start from the end

-1 refers to the last item, -2 refers to the second last item etc.
Example

Print the last item of the list:


thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
Range
• thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

• thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]


print(thislist[:4])

• thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]


print(thislist[2:]):

• thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]


print(thislist[-4:-1])
This example returns the items from index -4 (included) to index -1 (excluded):
['orange', 'kiwi', 'melon']
Deletion

list1 = ['physics', 'chemistry', 1997, 2000];


print list1
del list1[2];
print "After deleting value at index 2 : "
print list1

['physics', 'chemistry', 1997, 2000]


After deleting value at index 2 :
['physics', 'chemistry', 2000]

thislist = ["apple", "banana", "cherry"]


thislist.pop(1)
print(thislist)

thislist = ["apple", "banana", "cherry"]


thislist.pop()
print(thislist)

thislist = ["apple", "banana", "cherry"]


thislist.clear()
print(thislist)
1 cmp(list1, list2): Compares elements of both lists. 1 list.append(obj)
2 len(list):Gives the total length of the list. Appends object obj to list
3 max(list):Returns item from the list with max value. 2 list.count(obj)
4 min(list)
Returns count of how many times obj occurs in list
3 list.extend(seq)
Returns item from the list with min value.
5 list(seq) Appends the contents of seq to list
4 list.index(obj)
Converts a tuple into list. Returns the lowest index in list that obj appears
5 list.insert(index, obj)

Inserts object obj into list at offset index


6 list.pop(obj=list[-1])

Removes and returns last object or obj from list


7 list.remove(obj)

Removes object obj from list


8 list.reverse()

Reverses objects of list in place


9 list.sort([func])

Sorts objects of list, use compare func if given


thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)

thislist = ["apple", "banana", "cherry"]


for i in range(len(thislist)):
print(thislist[i])
thislist = ["apple", "banana", "cherry"]
i=0
while i < len(thislist):
print(thislist[i])
i=i+1
The general form of an if/else statement is import functions from a module:

if condition : • Import one or more specific functions:


if block
else: from math import sqrt, log
else block In this case, only the sqrt and log functions will
be available.

• Import everything the module has to offer:


The general form of the conditional expression is
Expression1 if condition else expression2 from math import *
The * symbol represents “everything.”

• Import the module itself instead of just its


The while statement has the general form: components:
while condition :
Block import math
In this case, to use a function we must use the
range( begin,end,step ) following notation:
y = math.sqrt(x)
print(math.log10(100))

You might also like