0% found this document useful (0 votes)
34 views11 pages

STD.10 Ai Assignment-4

The document discusses various topics related to Python programming language including its features, limitations, tokens, variables, data types, statements, comments and lists. Python is an interpreted, object-oriented programming language that is easy to learn and use. It supports various data types and has statements for input, output and assignments.
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)
34 views11 pages

STD.10 Ai Assignment-4

The document discusses various topics related to Python programming language including its features, limitations, tokens, variables, data types, statements, comments and lists. Python is an interpreted, object-oriented programming language that is easy to learn and use. It supports various data types and has statements for input, output and assignments.
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/ 11

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.

4] Free & Open Source:


Python is available on the Internet free of cost and can be downloaded from
official website www.python.org. It is open-source, so source code may be
available for redistribution.

5] Offers a variety of applications:


Nowadays Python is used in many devices or applications such as Scripting, Web
Development, Gaming, AI, Database Development etc.
♦ LIMITATIONS OF PYTHON
1] Slow execution: Python uses an interpreter that executes code line by line
which is slower in execution compared to a compiler-based programming
language.

2] Not strong for mobile development: It is good for desktop or server-based


applications but not efficient for mobile apps.

3] Memory Consumption: Python offers flexible data types so there is high


memory consumption.

4] Weak in database connectivity: Python has limitations while working with


database applications. Its database access layer has some barriers.

♦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

Keywords Identifiers Literals Operators Punctuators


1] KEYWORDS

• A keyword is a word having special meaning reserved by programming


language. Python provides the following keywords

2] IDENTIFIERS

• A Python identifier is a user-defined name given to parts of a program viz. a


variable, function, class, module or other object.
• Naming Conventions for Identifiers
1] An identifier may contain letters (a-z,A-Z), digits (0-9) and underscore
symbol (_).
2] An identifier must begin with a letter or underscore symbol.
3] An identifier cannot contain any special character except for underscore
symbol.
4] An identifier must not be a keyword.
5] Identifier can be of any length. However it is preferred to keep it short
and meaningful.
6] Python is case sensitive as it treats upper and lower-case characters
differently.(Eg- P and p are different)

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

STRING NUMERIC BOOLEAN SPECIAL COLLECTION

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

• Punctuators are symbols that are used to organize programming-sentence


structures and indicate the rhythm and emphasis of expressions, statements
and program structure.Some common punctuators are:
' " # \ ( ) [ ] { } @ , : .` =
♦VARIABLES IN PYTHON
 A variable in Python is an identifier(a name) that refers to a value or
element that is stored in memory.Values stored in a variable can be changed
during program run.
 EXAMPLE: num=10

Declaring variable and assigning value to it


 In Python we use assignment statement to create new variables and assign
specific values to them.
 Example
CODE
name="Alice"
age=23
marks=500
print(name)
print(age)
print(marks)
OUTPUT
Alice
23
500

Multiple Assignment in Python


 In Python we can assign a single value to multiple variables at the same
time.
 Example
CODE
x=y=z=30
print(x)
print(y)
print(z)
OUTPUT
30
30
30

♦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)

2] Strings: String is an ordered sequence of letters/characters. They are


enclosed in single quotes (‘ ‘) or double (“ “). Eg-“AI”,’ArtIntegration’

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)

5] Sets: Set is an unordered collection of values, of any type, with no duplicate


entry.

6] Dictionary: Dictionary is an unordered collection of key-value pairs. In


Python, dictionaries are defined within braces {}

♦ 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)

3] Assignment Statement: These statements assign values to different


variables.It takes the following form:
variable=constant value or expression
Example-
num=90
name=’blue house’
s=a+b

♦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]

Accessing Elements Of List


A list is made up of various elements which need to be individually accessed on
the basis of the application it is used for. There are two ways to access an
individual element of a list:
1) List Index
2) Negative Indexing

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)

Removing Elements from a List


Elements from a list can removed using methods :
1) Using remove() method –removes an item from the list
2) Using pop() method- removes and returns an element at the given index
3) Using clear() method- removes all items from the list
CODE OUTPUT
#remove method ['pear', 'orange']
L=['apple','pear','orange'] [10, 20]
L.remove('apple') []
print(L)

# pop method
L1=[10,20,40]
L1.pop()
print(L1)

#clear method
L2=[2,4,6,8,10]
L2.clear()
print(L2)

Finding length of the list


len() method is used to determine number of elements in the list
CODE OUTPUT
#len method 3
L1=[10,20,40]
print(len(L1))

************************************************************************************************************

You might also like