Asm 114478
Asm 114478
LISTS IN PYTHON
List is a sequence of values of any type.
Values in the list are called elements / items.
List is enclosed in square brackets.
Example:
a = [1,2.3,"Hello"]
List is one of the most frequently used and very versatile data type used in Python.
In order to access elements using negative indexing, we can use the negative index as mentioned in the above
figure.
Elements can be removed from the List by using built-in remove() function but an Error arises if element doesn't
exist in the set. Remove() method only removes one element at a time, to remove range of elements, iterator is
used. The remove() method removes the specified item.
Slicing of a List
In Python List, there are multiple ways to print the whole List with all the elements, but to print a specific range
of elements from the list, we use Slice operation.
Slice operation is performed on Lists with the use of colon(:).
To print elements from beginning to a range use [:Index], to print elements from end use [:-Index], to print
elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index: End
Index] and to print whole List with the use of slicing operation, use [:]. Further, to print whole List in reverse
order, use [::-1].
Note – To print elements of List from rear end, use Negative Indexes.
Accessing of Tuples
We can use the index operator [] to access an item in a tuple where the index starts from 0.
So, a tuple having 6 elements will have indices from 0 to 5.
Trying to access an element outside of tuple (for example, 6, 7,...) will raise an IndexError.
The index must be an integer; so, we cannot use float or other types. This will result in TypeError.
Example
fruits = ("apple", "banana", "cherry")
print(fruits[1])
The above code will give the output as "banana"
Deleting a Tuple
Tuples are immutable and hence they do not allow deletion of a part of it. Entire tuple gets deleted by the use of
del() method.
If Statement
The if statement is used to check a condition: if the condition is true, we run a block of statements (called the if-
block).
Syntax:
if test expression:
statement(s)
Here, the program evaluates the test expression and will execute statement(s) only if the text expression is True.
If the text expression is False, the statement(s) is not executed.
Note:
1) In Python, the body of the if statement is indicated by the indentation. Body starts with an indentation and the
first unindented line marks the end.
2) Python interprets non-zero values as True. None and 0 are interpreted as False.
Example :
#Check if the number is positive, we print an appropriate message
num = 3
if num > 0:
print(num, “is a positive number.”)
print(“this is always printed”)
num = -1
if num > 0:
print(num, “is a positive number.”)
print(“this is always printed”)
Syntax of if...else
if test expression:
Body of if
else:
Body of else
The if..else statement evaluates test expression and will execute body of if only when test condition is True.
If the condition is False, body of else is executed. Indentation is used to separate the blocks.
Example of if...else
#A program to check if a person can vote
age = input(“Enter Your Age”)
if age >= 18:
print(“You are eligible to vote”)
else:
print(“You are not eligible to vote”)
In the above example, when if the age entered by the person is greater than or equal to 18, he/she can vote.
Otherwise, the person is not eligible to vote
Syntax of if...elif...else
if test expression:
Body of if
©Lotus Valley Int. School, Gurugram
~ 10 ~
elif test expression:
Body of elif
else:
Body of else
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, body of else is executed. Only one block among the several if...elif...else blocks is
executed according to the condition.
The if block can have only one else block. But it can have multiple elif blocks.
Example of if...elif...else
#To check the grade of a student
Marks = 60
if marks > 75:
print("You get an A grade")
elif marks > 60:
print("You get a B grade")
else:
print("You get a C grade")
We can have an if...elif...else statement inside another if...elif...else statement. This is called nesting in computer
programming.
Any number of these statements can be nested inside one another. Indentation is the only way to figure out the
level of nesting. This can get confusing, so must be avoided if it can be.
Python Nested if Example
# In this program, we input a number
# check if the number is positive or
# negative or zero and display
# an appropriate message
Iteration Statements
The For Loop
The for..in statement is another looping statement which iterates over a sequence of objects i.e. go through each
item in a sequence.
Syntax of for Loop-
for val in sequence:
Body of for
Here, val is the variable that takes the value of the item inside the sequence on each iteration.
The while statement allows you to repeatedly execute a block of statements as long as a condition is true.
A while statement is an example of what is called a looping statement. A while statement can have an optional
else clause.
Syntax of while Loop in Python
while test_expression:
Body of while
In while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates
to True. After one iteration, the test expression is checked again. This process continues until the test_expression
evaluates to False. In Python, the body of the while loop is determined through indentation. Body starts with
indentation and the first unindented line marks the end. Python interprets any non-zero value as True. None and
0 are interpreted as False.
Example:
# Program to add natural
# numbers upto
# sum = 1+2+3+...+n
# To take input from the user,