IX AI Python Notes
IX AI Python Notes
IX AI Python Notes
Python programming language was created by Guido van Rossum and was released in 1991.
Features of Python:
1. Python is interpreted language i.e., each line of code is read by the interpreter and is
translated to machine code one by one.
2. Python is free and open-source software i.e., it can be downloaded at free of cost from the
official website of python and the program code can be read, modified/customized as per
requirement and used by anyone.
3. Python supports Unicode character set i.e., symbols of all the recognized languages in the
world. Therefore, the program code, written in one language need not be translated to
another before execution and saves time.
4. Python is platform independent i.e., can be supported by all types of hardware and software.
5. Python is portable language i.e., program written in one system can be modified and
executed in any other system.
6. Python supports both procedural and object-oriented programming approach.
7. Python is case sensitive language.
8. Python can handle big data applications.
Application of Python:
Modes of Python
Tokens of Python:
Token is an indivisible and individual part of a programming language that can be identified
and translated by the language translator. Every program consists of a set of tokens only.
Classification of Tokens:
Token
Q. 2. Identify the tokens from the following program code and count the total no. of tokens:
a = 10
b=7
c = a + b*2
print('Result =', c)
Keywords
Keywords are the reserved words for the language translators which have special meanings and
usage and cannot be redefined by the programmers.
Data types
Data types are the type of values stored in a memory location. Every data stored in memory
can be classified as a particular data type.
Data types
int literals
Invisible or Non- printable characters like Blank space, Tab (8 blank spaces), New line etc.
The first 33 characters of Unicode are non-printable.
Escape Sequence => A combination of 2 or more characters starting with ‘\’ (back slash)
having a special meaning and purpose are not displayed on the screen as it is.
Identifiers
Identifier is the name of a programming construct given by the programmer.
Identifiers can be variables, functions, objects etc.
An identifier can contain alphabets (both lower and upper case), digits (0-9) and
only one special character _ (underscore).
An identifier can start with either alphabet or _ (underscore) only, but not digit.
Variable => Variable is a named memory location which can store a value of any data type
during the execution of a program.
For example, consider the following program code
No1 = 23
No2 = 45
Sum = No1 + No2
Here variables are No1, No2 and Sum. Literals are 23 and 45.
Function => Function is a named piece of program code or sub-program which can be called
and executed independently.
A function can take 0 or more parameter and returns 0 or more values.
Advantage of using function:
A function once defined can be called and executed any no. of times. No need to write the same
piece of code multiple times for repeated use, hence saves time and effort.
Examples:
input() takes an input from the user through the input device in the form of
string and returns the same. Input can take a string as parameter and display the
same in the screen for the ease of user.
eval() takes a value in the form of a string and returns the value into the
designated data type.
Library functions – math library functions like sin(), floor(), sqrt(), pow() etc.
User defined functions
Comment Line:
Comment lines are the line(s) of code which are skipped by the language translator and hence
are not read and executed.
Comment lines are mostly used to describe the purpose, working functionality of any statement
or a block of statements. It is used for documentation of the program code which helps to
analyze and modify the code in future.
Comment Line
# This line will not be executed ''' These lines will not
be executed at all. '''
Q. Write a program in python to input 3 words from the user and display the same.
Ans-
PROGRAM SAMPLE OUTPUT
# DISPLAY RESULT
print("The words are: ", a, b, c)
Operators
Classification
• Arithmetic : +, -
Unary • Logical : not
• Arithmetic : +, -
= , += , -= , *= , /= , //= , %= , **=
or , and , not
in , not in
Lowest
Commutative Property:
i. Among arithmetic operators +, * are commutative, all others are not.
ii. Among relative operators ==, != are commutative, others are not.
iii. Among logical operators or, and both are commutative
a+b 8
a>b True
a-b 2
a>=b True
a*b 15
a<b False
a/b 1.67 (Real quotient)
a//b 1 (Integer quotient) a<=b False
a%b 2 (remainder) a==b False
a**b 125 (a=base, b=exponent) a!=b True
a+=1 Logical Operators Membership Operators
6
a-=1
4 5<3 or 4<=6 : 'i' in "String"
True : True
a*=2
10
5<3 and 4<=6 : 'ri' not in "String" :
a/=2
2.5 False False
a//=2
2 not 5>3 : 'x' in "String"
False : False
a%2
1
'x' not in "String" :
a**=2 True
25
Q. Write a program to input 2 numbers from the user and display their sum,
difference, product, quotient (both integer and real), remainder and exponential
value.
Ans –
PROGRAM SAMPLE OUTPUT
# DISPLAY RESULT
PRINT("SUM = ", SUM)
PRINT("DIFFERENCE = ", DIFF)
PRINT("PRODUCT = ", PROD)
PRINT("INTEGER QUOTIENT = ", QTNI)
PRINT("REAL QUOTIENT = ", QTNF)
PRINT("REMAINDER = ", REM)
PRINT("EXPONENT = ", EXP)
Multiple Assignment Operators
x = y = z = 10
• At first 10 is assigned to z, then the same value is assigned to y
and x respectively. (Right to Left)
x, y, z = 3, 5, 4
• This statement is equivalent to x=3;y=5;z=4 and can be expressed
in a single line of code.
Q. Write a program in python to initialize a with 10, b with 20. Write statement
to swap the contents of the variables.
Ans –
Option 1: Using 3rd variable Option 2: Without using 3rd variable
a = 10 a = 10
b = 20 b = 20
t=a a, b = b, a
a=b print("a = ", a)
b=t print("b = ", b)
print("a = ", a)
print("b = ", b)
Q. Write a program in python to input the altitude (L) and base (B) of a right-
angled triangle and find its hypotenuse (H). (H =√𝐿2 + 𝐵2 )
Ans -
# CALCULATION
H = math.sqrt(L**2 + B**2)
# DISPLAY RESULT
print("Hypotaneous = ", H)
Data type Conversion
Automatic / Explicit /
Implicit Type casting
If an expression consists of data of various data types (Mostly numeric), during the
operation the lower precision operand is promoted to the matching higher precision data
type and the result is of the higher precision data type.
Example: In the expression F = True + 5 - 3.1, True (bool data type) is internally
converted to 1 (integer), so True+5 is 6 (integer). Next, 6 (integer) is internally
converted to 6.0 (float), so 6.0 – 3.1 is 2.9 (float)
Punctuators
, Separators of values / expressions
; End of statement
. Accessing Member
: Branch / Nested statement
"" or '' String literals
() Tuple
[] List
{} dictionary