0% found this document useful (0 votes)
15 views37 pages

UNIT 2 List

this pdf contains important topics of python programming.

Uploaded by

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

UNIT 2 List

this pdf contains important topics of python programming.

Uploaded by

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

UNIT 2

LIST
List items are ordered, changeable, and allow
duplicate values.
List items are indexed, the first item has
index [0], the second item has index [1] etc.

List Indexing and Splitting:


The indexing procedure is carried out
similarly to string processing. The slice
operator [] can be used to get to the List's
components.
Sub-list of the list
list_varible(start:stop:step)
Example:
list = [1,2,3,4,5,6,7]
print(list[0])
print(list[1])
print(list[2])
print(list[3])
# Slicing the elements
print(list[0:6])
print(list[:])
print(list[2:5])
print(list[1:6:0])
O/P:
1
2
3
4
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5]
ValueError: slice step cannot be zero
Negative Indexing
In contrast to other programming languages,
Python lets you use negative indexing as well.
# negative indexing example
list = [1,2,3,4,5]
print(list[-1])
print(list[-3:])
print(list[:-1])
print(list[-3:-1])
O/P:
5
[3, 4, 5]
[1, 2, 3, 4]
[3, 4]
Updating List Values
list = [1, 2, 3, 4, 5, 6]
print(list)
# It will assign value to the value to the second
index
list[2] = 10
print(list)
# Adding multiple-element
list[1:3] = [89, 78]
print(list)
# It will add value at the end of the list
list[-1] = 25
print(list)
O/P:
[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]
List Comprehension
List comprehension offers a shorter syntax
when you want to create a new list based on
the values of an existing list.
Example:
Based on a list of fruits, you want a new list,
containing only the fruits with the letter "a" in
the name.
Without list comprehension you will have to
write a for statement with a conditional test
inside:
Example:
fruits =
["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []

for x in fruits:
if "a" in x:
newlist.append(x)

print(newlist)
O/P: ['apple', 'banana', 'mango']
With list comprehension you
can do all that with only one
line of code:
Example:
fruits = ["apple", "banana", "cherry", "kiwi",
"mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
O/P:
['apple', 'banana', 'mango']
The Syntax of List
Comprehension
newlist = [expression for item in iterable if
condition == True]
The return value is a new list, leaving the old
list unchanged.
List Built-in Methods
Dictionary
Dictionaries are used to store data values in
key:value pairs.
A dictionary is a collection which is ordered*,
changeable and do not allow duplicates.
Dictionaries are written with curly brackets, and have
keys and values:
# Example:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
O/P: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Dictionary Methods
Use of while loops in python
The Python while loop iteration of a code
block is executed as long as the given
Condition, i.e., conditional_expression, is
true.
Syntax of the Python while loop
Statement
while Condition:
Statement
i=1
while i<=10:
print(i, end=' ')
i+=1
O/P: 1 2 3 4 5 6 7 8 9 10
--------
i=0
while i< 3:
print(i)
i += 1
else:
print(0)
O/P:
0
1
2
0
----------------
i=1
while i<51:
if i%5 == 0 or i%7==0 :
print(i, end=' ')
i+=1
O/P: 5 7 10 14 15 20 21 25 28 30 35 40 42 45
49 50
Write a program to check an input
number is prime or not.
n= int(input("Enter Number:"))
count=0
i=1
while(i<=n):
if(n%i==0):
count=count+1
i=i+1
if(count==2):
print("prime number")
else:
print("not prime number")

O/P: Enter Number:7


prime number
Python Program to check
whether a given number is a
palindrome.
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

O/P: Enter number:525


The number is a palindrome!
How to check if a given number is
Fibonacci number?
n=int(input("Enter the number: "))
c=0
a=1
b=1
if n==0 or n==1:
print("Yes")
else:
while c<n:
c=a+b
b=a
a=c
if c==n:
print("Yes")
else:
print("No")
O/P:
Enter the number: 34
Yes
Continue Statement in Python
Continue statement is opposite to that of the
break statement, instead of terminating the
loop, it forces to execute the next iteration of
the loop.
As the name suggests the continue statement
forces the loop to continue or execute the
next iteration.
Syntax of Continue Statement
The continue statement in Python has the
following syntax:

for / while loop:


# statement(s)
if condition:
continue
# statement(s)
Working of Python Continue
Statement
Continue Statement in Python
Example: Continue to the next iteration if i is 3:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
O/P:
1
2
4
5
6
The else Statement
With the else statement we can run a block of
code once when the condition no longer is
true.
Example:
i=1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
O/P:
1
2
3
4
5
i is no longer less than 6
The Break Statement
The break statement in Python is used to
terminate the loop or statement in which it is
present.
After that, the control will pass to the
statements that are present after the break
statement, if available.
The break statement in Python has the
following syntax:

for / while loop:


# statement(s)
if condition:
break
# statement(s)
# loop end
The working of the break statement
in Python
The break Statement
Example:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
O/P:
1
2
3
Pass Statement in Python
The pass statement in Python is used when a
statement is required syntactically but you do
not want any command or code to execute.
It is like a null operation, as nothing will
happen if it is executed. Pass statements can
also be used for writing empty loops.
Syntax of Pass Statement
function/ condition / loop:
pass
When the user does not know what code to
write, So user simply places a pass at that
line.
Why Python Needs “pass” Statement?
If we do not use pass or simply enter a
comment or a blank here, we will receive
an IndentationError error message.
Example: n = 26
if n > 26:
# write code your here
print('Geeks')
O/P: IndentationError: expected an indented block
after 'if' statement
Use of pass keyword in Python
Loop
n = 10
for i in range(n):
# pass can be used as placeholder
# when code is to added later
pass

You might also like