STD.10 Ai Assignment-4
STD.10 Ai Assignment-4
SUBJECT:--ARTIFICIAL INTELLIGENCE(417)
Name:--______________________________________
Class/div:--_____________
Roll no:--___________
*Recap: PYTHON
Python is a high level object –oriented programming language developed by
Guido Van Rossum in 1991
♦ FEATURES OF PYTHON
1] Easy to Learn and Use
Python is easy to learn as compared to other programming languages. Its syntax is
straightforward and much the same as the English language.
2] Interpreted Language
Python is an interpreted language; it means Python program is executed one line at
a time. The advantage of being interpreted language, it makes debugging easy and
portable.
3] Platform Independent
Python can run on any platform such as Windows, Linux/UNIX, Macintosh etc. It
is a portable language.
♦PYTHON TOKENS
• The smallest individual unit in a program is known as a Token or a lexical
unit.
• Python provides the following 5 types of tokens:
TOKENS
2] IDENTIFIERS
3] LITERALS
• Literals also called as constants are the fixed values that do not change
during the execution of a program. Different types of literals provided by
Python are :
LITERALS
1] String Literals
• Sequence of characters surrounded by quotes (single or double or triple quotes) are called
string literals.
• Example: 'V', "Guido Van Rossum",'''Python is a high level language'''
2] Numeric Literals
• Numeric Literals are the fixed numeric values used in Python programs
• Different types of numeric literals:
o Integer:Whole numbers without any fractional part.
Eg- 123,+90,-89
o Floating-Point:Numbers having fractional parts.
Eg- 2.0,17.5, -13.5, -.25,52.
o Complex :A complex number is in the form A+Bi where i is the imaginary
number and A and B are real numbers.Python represents complex numbers in the
form A+Bj.
Eg- 3+5j, 5.2+4j
3] Boolean Literal
• A Boolean literal in Python is used to represent one of the two Boolean values i.e True or
False
4] Special Literal
• Python has one special literal, which is None. It indicates absence of value.
5] Collections
• Includes collection of more than one literal.
Different types of collection literal are:
o List- [10,20,’abc’]
o Tuple – (90,100,20)
o Sets-{‘a’,’b’,’c’}
o Dictionary-{‘a’:’apple’,’b’:’ball’}
4] OPERATORS
Operators are mathematical or other symbols that work on the operands and
are used for calculations
Eg- 5*10
ARITHMETIC OPERATORS
To perform arithmetic operations, Python supports the following operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor Division/Integer Division- It gives the floor value of the
quotient produced by dividing the two operands.
** Exponentiation- it calculates the first operand power to the
second operand.
% Remainder/Modulus -It returns the reminder after dividing the
first operand by the second operand.
5] PUNCTUATORS
♦DATATYPES IN PYTHON
• Data type identifies the type of data values a variable can hold and the
operations that can be performed on that data.
• There are various data types in Python. Some of the important types are
mentioned below :
1] Numbers: Number data type stores Numerical Values such as integers
(1,23,56,-98) ,float (2.45,-8.9,3.14) and complex numbers (2+3j,4-5j)
3] Lists: Collection of data which are ordered and changeable. List is enclosed
in square brackets. Example: dob = [19,"January",1990]
4] Tuples: Tuples are a sequence of values of any type. They are immutable.
Tuples are enclosed in ().Example: t = (5,'program',2.5)
♦ STATEMENTS IN PYTHON
A statement is a programming instruction that does something i.e. some action
takes place. Different types of statements in Python:
1] Input Statement: These statements are written to accept input from the user. In
Python, input() function is used to get input from user.
Example:
name=input(“enter your name”)
num=input("Enter a number:")
2] Output Statement: These statements are written to display output to the user.
In Python, print () function is used to display output.
Example:
print("hello")
print(age)
print(90)
♦COMMENTS IN PYTHON
Comments are non-executable statements. They are used to explain the code
written in a program. Types of comments in Python:
1] Single Line Comment: Comments written on a single –line beginning with #
symbol. Example-
# this is an example of inline line comment
2] Multiline comment: Comments written on a multiple lines. They are written
inside triple quotes. Example-
''' Multi-line comments are useful for detailed additional
information related to the program in question.
It helps clarify certain important things
'''
♦PYTHON LIST
List is 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 [ ].
Example:
Nested List
A list containing list as an element is called nested list.
x=[10,100,['a','abc'],1000]
1) List Index
A list index is the position at which any element is present in the list. Index in the
list starts from 0, so if a list has 5 elements the index will start from 0 and go on till
4. In order to access an element in a list we need to use index operator [].
Example
CODE
language =
['p','y','t','h','o','n']
print(my_list[0])
print(my_list[4])
OUTPUT
p
o
2) Negative Indexing
Python allows negative indexing for its sequences. The index of -1 refers to the last
item, -2 to the second last item and so on.
Example
Code
day = ['f','r','i','d','a','y']
print(a[-1])
print(a[-6])
Output
y
f
Adding Element to a List
We can add an element to any list using the following methods :
1) Using append() method –adds an element to the end of list
2) Using insert() method- inserts an item at the defined index
3) Using extend() method- adds all the elements of a list to the end of current list
Example
CODE OUTPUT
#append method ['apple', 'pear', 'orange', 'mango']
L=['apple','pear','orange'] [10, 20, 30, 40]
L.append('mango') [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
print(L)
# insert method
L1=[10,20,40]
L1.insert(2,30)
print(L1)
#extend method
L2=[2,4,6,8,10]
L2.extend([12,14,16,18,20])
print(L2)
# pop method
L1=[10,20,40]
L1.pop()
print(L1)
#clear method
L2=[2,4,6,8,10]
L2.clear()
print(L2)
************************************************************************************************************