AI CHAPTER 5 PYTHON NOTES - Class IX
AI CHAPTER 5 PYTHON NOTES - Class IX
What is Python?
Python Statement
Multi-line statement
Variables
Note:
Floating Point:
2) None
This is special data type with single value. It is used to signify the
absence of value/false in a situation. It is represented by None.
3) Sequence:
a) Strings
b) Lists
c) Tuples
String
Lists
List is also a sequence of values of any type. Values in the list are
called elements / items. These are indexed/ordered. List is enclosed
in square brackets.
Tuples:
4) Sets
Dictionaries
Example 2:
In all the examples till now, we have been using the calculations on
known values (constants). Now let us learn to take user’s input in
the program. In python, input () function is used for the same
purpose.
Type Conversion
The process of converting the value of one data type (integer, string,
float, etc.) to another data type is called type conversion. Python
has two types of type conversion.
1. Implicit Type Conversion
2. Explicit Type Conversion
Syntax:
(required_datatype)(expression)
In above program,
Comparison operators
Introduction to Lists
List is one of the most frequently used and very versatile data type
used in Python. A number of operations can be performed on the
lists.
Example:
a = [1,2.3,"Hello"]
It can have any number of items and they may be of different types
(integer, float, string etc.).
How to access elements of a list?
1) List Index
2) Negative Indexing
List Index
Negative Indexing
append() method only works for addition of elements at the end of the
List, for addition of element at the desired position, insert() method
is used. Unlike append() which takes only one argument, insert()
method requires two arguments(position, value).
Other than append() and insert() methods, there's one more method
for Addition of elements, extend(), this method is used to add multiple
elements at the same time at the end of the list.
Removing Elements from a List
Note – Remove method in List will only remove the first occurrence
of the searched element.
Pop() function can also be used to remove and return an element from
the set, but by default it removes only the last element of the set, to
remove an element from a specific position of the List, index of the
element is passed as an argument to the pop() method.
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].
Slicing using negative index of list :
Introduction to Tuples
Example
Accessing of Tuples
Example
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.
Example
num = (0, 1, 2, 3, 4)
del num
● if statement
● if..else statements
● if-elif ladder
If Statement
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.
Python if...else Statement
Python if...elif...else Statement
Python Nested if statements
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.
Exercises:
Python program that ask for our name and age, and then displays
it on
the screen.
Program:
Python program that accepts a number and then prints a table for that number.
Python code to Print each fruit in a fruit list using for loop.
Python code to Print all numbers from 1 to 10 using for, and
print a message when the loop has ended.
Program:
list = [10,11,12,13,14,15,16,17,18,19,20]
print ("Elements of the List")
print (list)
list[3]=list[1]+list[2]
print("Added 2nd & 3rd items and store the added result as 4th
item")
print (list)
list.pop(3)
print("List after deleting 4th item")
print (list)
list.insert(3,13)
print ("List after adding 13 as the 4th item")
print (list)
Program: