AMITY INTERNATIONAL SCHOOL, SECTOR 6,
VASUNDHARA
CLASS XI – PYTHON
ARTIFICIAL INTELLIGENCE
Features of Python
High Level language
Interpreted Language
Free and Open Source
Platform Independent (Cross-Platform) – runs virtually in every platform if
a
compatible python interpreter is installed.
Easy to use and learn – simple syntax similar to human language.
Variety of Python Editors – Python IDLE, PyCharm, Anaconda, Spyder
Python can process all characters of ASCII and UNICODE.
Widely used in many different domains and industries.
PYTHON TOKENS
Keywords: Reserved words used for special purpose. List of keywords are given
below.
b. Identifier: An identifier is a name used to identify a variable, function, class, module
or other object. Generally, keywords (list given above) are not used as variables.
Identifiers cannot start with digit and also it can’t contain any special characters except
underscore.
c. Literals: Literals are the raw data values that are explicitly specified in a program.
Different types of Literals in Python are String Literal, Numeric Literal (Numbers),
Boolean Literal (True & False), Special Literal (None) and Literal Collections.
d. Operators: Operators are symbols or keywords that perform operations on
operands to produce a result. Python supports a wide range of operators:
Arithmetic operators (+, -, *, /, %)
Relational operators (==, !=, <, >, <=, >=)
Assignment operators (=, +=, -=)
Logical operators (and, or, not)
Bitwise operators (&, |, ^, <<, >>)
Identity operators (is, is not)
Membership operators (in, not in)
e. Punctuators: Common punctuators in Python include: ( ) [ ] { } , ; . ` ‘ ‘ ” ” / \ & @ ! ? |
~ etc.
Data Types
Data types are the classification or categorization of data items. It represents the kind
of value that tells what operations can be performed on a particular data. Python
supports Dynamic Typing.
Data Type Description
Integer Stores whole number a=10
Boolean is used to represent the truth
Boolean values of the expressions. It has two values Result = True
True & False
Floating
Stores numbers with fractional part x=5.5
point
Stores a number having real and imaginary
Complex num=a+bj
part
Immutable sequences (After creation values
cannot be changed in-place)
String name= “Ria”
Stores text enclosed in single or double
quotes
Mutable sequences (After creation values
can be changed in-place)
List lst=[ 25, 15.6, “car”, “XY”]
Stores list of comma separated values of
any data type between square [ ]
Immutable sequence (After creation values
cannot be changed in-place)
Tuple tup=(11, 12.3, “abc”)
Stores list of comma separated values of
any data type between parentheses ( )
Set is an unordered collection of values, of
Set s = { 25, 3, 3.5}
any type, with no duplicate entry.
Unordered set of comma-separated dict= { 1 : “One”, 2: “Two”, 3:
Dictionary
key:value pairs within braces {} “Three”}
Accepting values from the user
The input() function retrieves text from the user by prompting them with a string
argument. For instance:
name = input("What is your name?")
Sample Program,
Write a program to read name and marks of a student and display the total mark.
In the above example float( ) is used to convert the datatype into floating point. The
explicit conversion of an operand to a specific type is called type casting.
Control flow statements in Python
Control flow is the order in which statements, instructions or functions are executed.
Selection Statement
The if/ if..else statement evaluates test expression and the statements written below
will execute if the condition is true otherwise the statements below else will get
executed. Indentation is used to separate the blocks.
Sample Program,
Asmita with her family went to a restaurant. Determine the choice of food according to
the options she chooses from the main menu.
Looping Statements
Looping statements in programming languages allow you to execute a block of code
repeatedly. In Python, there are mainly two types of looping statements: for loop and
while loop.
For loop
The “for” keyword is used to start the loop. The loop variable takes on each value in the
specified sequence (e.g., list, string, range). The colon (:) at the end of the for
statement indicates the start of the loop body. The statements within the loop body are
executed for each iteration.
The for loop iterates over each item in the sequence until it reaches the end of the
sequence or until the loop is terminated using a break statement.
Simple program,
Q1. Write a program to display even numbers and their squares between 100 and 110.
Q2. Write a program to read a list, display each element and its type. (use type( ) to
display the data type.)
Q3. Write a program to read a string. Split the string into list of words and display each
word.
Q4. Write a simple program to display the values stored in dictionary
PYTHON MODULES
in the programming world, a library is a collection of precompiled codes
that can be used later on in a program for some specific well-defined
operations.
A Python library is a collection of related modules. It contains bundles of
code that can be used repeatedly in different programs.
EXAMPLE OF FEW LIBRARIES
Random
Math
Statistics
Numpy
Scipy
Matplotlib
Pandas
Tensorflow
Scikit-learn
Example
import math
A = 16
print([Link](A))
from math import sqrt, sin
A = 16
B = 3.14
print(sqrt(A))
print(sin(B))