Python Is Also - This Means That It Is Not Converted To Computer-Readable Code Before The Program Is Run But at Runtime
Python Is Also - This Means That It Is Not Converted To Computer-Readable Code Before The Program Is Run But at Runtime
Introduction
Python is an open-source, object-oriented programming language. Python is also an interpreted language.
This means that it is not converted to computer-readable code before the program is run but at runtime.
Written by Guido van Rossum. All the examples in this document are using Python 3.10.4.
Initially we will learn the basics of Python in the interactive mode by using IDLE Python shell. Python
IDLE looks like:
>>> is the Python prompt. If something is typed and it is syntactically correct, then Python will display
some output on the screen, otherwise it will display an error message. Some examples are given below:
>>> 10
Displays 10 on the screen.
In Python 10 is int (integer – a number without any digit after the decimal point). We can use type()
to check the data type.
>>> type(10)
Displays <class 'int'> on the screen. In Python data type and class can be used inter
changeably.
>>> 25.6
Displays 25.6 on the screen
In Python 25.6 is float (floating point – a number without at least one digit after the decimal point).
We can use type() to check the data type.
>>> type(25.6)
Displays <class 'float'> on the screen.
>>> 'FAIPS'
Displays 'FAIPS' on the screen.
>>> "FAIPS"
FAIPS, DPS Kuwait Page 1 / 16
Python Notes Class XI Python Basics
Displays 'FAIPS' on the screen.
In Python 'FAIPS' / "FAIPS" is str (string – sequence characters enclosed within '/"). We can use
type() to check the data type.
>>> type('FAIPS')
Displays <class 'str'> on the screen.
A string has to be enclosed within ' or ". But represent a string by starting with ' and ending " or vice-
versa will be flagged as syntax error.
>>> 'FAIPS"
Will display following error message on the screen:
SyntaxError: EOL while scanning string literal
>>> "FAIPS'
Will display following error message on the screen:
SyntaxError: EOL while scanning string literal
>>> 5+2j
Displays (5+2j) on the screen.
In Python (5+2j) is complex (complex number: 5 is the real part and 2j is the imaginary part).
We can use type() to check the data type.
>>> type(5+2j)
Displays <class 'complex'> on the screen. Data type complex is not in the syllabus.
• Function float() will convert either integer (int) type or string (str) type to floating point (float)
>>> float(35)
Displays 35.0
>>> float(-12)
Displays -12.0
>>> float(0)
Displays 0.0
>>> float(12+13)
Displays 25.0
>>> float('7.25')
Displays 7.25
>>> float('-10.8')
Displays -10.8
>>> float('10')
Displays 10.0
>>> float('3.2+4.7')
Displays syntax error because 3.2+4.7 is not a floating point constant.
• Function str() will convert either integer (int) type or floating point (float) type to string (str)
>>> str(134)
Displays '134'
>>> str(17+18)
Displays '35'
>>> str(-163)
Displays '-163'
>>> str(5.75)
Displays '5.75'
>>> str(5.5*3.2)
Displays '17.6'
>>> str(-25.8)
Displays '-25.8'
Algebraic Expression
In Algebra In Python
a+b a + b
a-b a - b
ab a * b
𝑎
a / b, a // b
𝑏
𝑎𝑏 a**b, pow(a, b)
a*a + 2*a*b + b*b, a**2+2*a*b+b**2
𝑎2 + 2𝑎𝑏 + 𝑏 2 pow(a,2) +2*a*b + pow(b,2)
Python Token
Building block of a program is called a token. It is also called program element. Tokens of a Python can
be classified as Keyword, Identifier, Operator, Literal (Constant) and Delimiter.
a) Keyword: It is component of a program which has special meaning for Python. Python contains list
of all the keywords. List of keywords vary from version to version. A keyword cannot be redefined,
that is, we cannot use a keyword as a variable name (identifier name). List of Python keywords for
version 3.11.3 are given below:
False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else,
except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise,
return, try, while, with, yield
All the keywords are in lowercase except for False, True and None. That means ELSE, Else, ELse,
false, FALSE are not keywords and can be used as a variable name (identifier name) in a Python
script.
>>> pass=10
Displays a syntax error because we are trying to use a keyword as a variable name.
>>> Pass=20.5
>>> Pass
Displays 20.5 on the screen because Pass is not a keyword.
b) Identifier: Identifier is a component of a script which is identified by Python. There are two broad
categories of identifiers:
Built-in: It is name of built-in functions or built-in objects. Some built-in functions and objects
can be used directly. But some built-in functions and objects needs appropriate module
to be imported. We will discuss module later. A built-in identifier can be redefined
(built-in identifier can be used as variable name).
>>> pow(36,0.5)
Displays 6.0 on the screen
pow() function (built-in identifier) is as an alternative to ** operator
>>> pow=25 #using pow as a variable name
>>> pow
Displays 25 on the screen
>>> pow(4,2)
Displays an error message on the screen since pow is used as a variable name. Hence
it cannot calculate 4**2. Therefore, it is a bad practice to use a built-in identifier
as a variable name or to use as an identifier.
User-defined: Identifiers created by the programmer like variable names, user-defined function
names, and class names. User-defined identifiers can only be used after they have
created or declared. Till now variables are the only user-defined identifiers, being
created in a Python script. User defined function and class will be discussed later.
FAIPS, DPS Kuwait Page 8 / 16
Python Notes Class XI Python Basics
c) Literal: As discussed earlier, a literal is a program element whose value remains same (constant)
through the execution of the Python script. Examples of different types of literals are given below:
Data Type Literal
int …, -4, -3, -2, -1, 0, 1, 2, 3, 4, …
float …, -3.7, -1.0, -0/8, 0, 0.3, 1.8, 2.0, 3.9, …
bool False(0), True(1)
"AMIT",'2.5',"***",'Delhi',"2021",'1995',"-5.8"
str '''Python was created """Python was created
By Guido van Rossum''' By Guido van Rossum"""
d) Operator: Operators are used in Python to carry out various functions. Mostly operators are used in
arithmetic calculations and in logical (Boolean) expressions. Examples of operators are given below:
Operator Expression Meaning Rank
** a**b a raised to the power b 1
Unary + +a Sign of value stored in a remains unaltered 2
Unary - -a Changes sign of value stored in a 2
* a*b Multiply a and b 3
/ a/b Divide a by b 3
// a//b Divide a by b 3
% a%b Remainder (Modulo) of a divided by b 3
Binary + a+b Adds a and b 4
Binary - a–b Subtract b from a 4
= a=10 a is assigned a value 10 5
+= a+=b b is added to a and the result is assigned a 5
-= a-=b b is subtracted from a and the result is assigned a 5
*= a*=b a is multiplied by b and the result is assigned a 5
/= a/=b a is divided by b and the result is assigned a 5
%= a%=b a is assigned a value of a%b 5
**= a**=b a is assigned a value of a**b 5
== a==b a is equal to b 6
!= a!=b a is not equal to b 6
> a>b a is greater than b 6
>= a>=b a is greater than equal to b 6
< a<b a is less than b 6
<= a<=b a is less than equal to b 6
not not(a<b) Negate the condition a is less than or equal to b 7
and a>0 and a<9 a's value lies between 0 and 9 7
or a<0 or a>9 a's value is either less than 0 or greater than 9 7
List of operators mentioned above are the ones that we already used and the ones that we will be using
in the immediate future. Bu there are few more operators supported by Python. They will be introduced
later, when the need arises. Operators requiring multiple symbols, there should not be any space
between the symbols.
Unary operator: An operator that needs one operand.
Examples: Unary -, unary + and not
Binary operator: An operator that needs two operands. Most of the operators of Python are
binary operators. Operators and, or and not are also keywords.
Examples: Binary +, /, +=, *=, >=, !=, and, or
Operators working form Left to Right Operators working form Right to Left
+, -, *, /, //, %, >, >=, >, <=, **, =, +=, -=, *=, /=, %=, =**
==, !=, and, or, not
FAIPS, DPS Kuwait Page 9 / 16
Python Notes Class XI Python Basics
e) Delimiter: are special symbol(s) that perform three special roles in Python: grouping, punctuation
and assignment. List of Python delimiters are given below:
= += -= *= /= //= %= **= assignment (also used as shorthand operator)
( ) [ ] { } grouping (More about grouping later)
. , : ; punctuation
() uses with function name but not for grouping
. used in a float literal, calling function from a module, calling function of an object
, used to assign multiple values to multiple variables in a single statement
: used in if-else statement, loops, function header ...
chr()
Returns the single character whose ASCII code or Unicode (integer) is the parameter to the function. If
the parameter to chr() is float, then it is syntax error.
>>> x=97
>>> chr(x), chr(65)
('a', 'A')
>>> chr(1000)
'Ϩ'
Displays Unicode character whose Unicode is 1000
>>> chr(67.2)
Displays syntax error because parameter is float.
eval()
Evaluates an expression inside a string. An expression inside a string could be either of int type or float
type or str type or bool type.
>>> eval('10+20'), eval('2.5+7.8')
(30, 10.3)
>>> a, b, c, d=7, 4, 4.5, 2.5
>>> eval('a*b'), eval('c*d')
(28, 11.25)
>>> eval("'DEL'+'HI'")
'DELHI'
len()
Returns the length of a string/list/tuple/dictionary. Data types list/tuple/dictionary will be explained in
the Second Term. But for now, len() function will be used with only string.
>>> len('FAIPS, DPS-Kuwait'), len('')
(17, 0)
FAIPS, DPS Kuwait Page 10 / 16
Python Notes Class XI Python Basics
>>> x='49 South Street, PO Box-9951, Ahamdi-61010'
>>> len(x), len('India, '+'Delhi')
(42, 12)
max()
Returns maximum value from the list of values passed as parameters to the function. List may contain:
• All values are integer
• All values are floating point
• All values are numbers (either int or float))
• All values are string
But using max() with a list of values containing is either integers and strings or floating points and strings
will trigger syntax error.
>>> max(10, 20, 30, 15, 25)
30
>>> max(2.5, 5.6, 7.8, 9.2, 4.8)
9.2
>>> max(25, 54.6, 78, 91.2, 48)
91.2
>>> a, b, c, d=22, 44, 55, 66
>>> max(a, b, c, d)
66
>>> max('DDD', 'AAA', 'FFF', 'ZZZ', 'CCC')
'ZZZ'
>>> a, b, c, d, e='GGG', 'TTT', 'DDD', 'BBB', 'FFF'
>>> max(a, b, c, d, e)
'TTT'
>>> max(100, 'DDD', 400, 'AAA', 200,'FFF')
Displays syntax error list of values include integers and strings.
min()
Returns minimum value from the list of values passed as parameters to the function. List may contain:
• All values are integer
• All values are floating point
• All values are numbers (either int or float))
• All values are string
But using min() with a list of values containing is either integers and strings or floating points and strings
will trigger syntax error.
>>> min(35, 20, 30, 15, 25)
15
>>> min(3.8, 5.6, 7.8, 1.2, 4.8)
1.2
>>> min(25, 56.5, 78, 73.2, 14)
14
>>> min(12.4, 50, 40.3, 60.2, 2.2, 33)
2.2
>>> a, b, c, d=77, 44, 55, 66
>>> min(a, b, c, d)
44
>>> min('DDD', 'AAA', 'FFF', 'ZZZ', 'CCC')
'AAA'
>>> a, b, c, d, e='GGG', 'TTT', 'DDD', 'BBB', 'FFF'
>>> min(a, b, c, d, e)
'BBB'
FAIPS, DPS Kuwait Page 11 / 16
Python Notes Class XI Python Basics
>>> min(100, 'DDD', 400, 'AAA', 200,'FFF')
Displays syntax error list of values include integers and strings.
ord()
Returns the ASCII code / Unicode of the string containing single character.
>>> x='D'
>>> ord(x), ord('T')
(68, 84)
>>> ord('AB')
Displays syntax error because string contains more than one character.
pow()
Returns base raised to the power of exponent.
>>> pow(5, 4), pow(81, 0.5), pow(-1, 3), pow(8, 1/3)
(625, 9.0, -1, 2.0)
>>> a, b, c, d=81, 0.5, 7, 3.0
>>> pow(a, b), pow(c, d)
(9.0, 343.0)
>>> pow(25, 0.5), pow(25, 1/2), pow(25, 1//2)
(5.0, 5.0, 0)
round()
Returns a number rounded to nearest integer or rounded to fixed number of decimal places.
>>> round(10.7), round(7.2), round(9.5)
(11, 7, 10)
>>> a, b, c, d=25.3, 26.8, 23.5, 34.5
>>> round(a), round(b), round(c), round(d)
(25, 27, 24, 34)
>>> round(12.3475,2), round(7.2714,2), round(9.1754,2)
(12.35, 7.27, 9.18)
>>> a, b, c=-5.12345, -2.12382, -3.12356
>>> round(a,3), round(b,3), round(c,3)
(-5.123, -2.124, -3.124)
>>> round(1234.25,-1), round(1282.25,-3), round(1282.25,-2)
(1230.0, 1000.0, 1300.0)
>>> 10/3
#Answer is 3.333333333333333
>>> 10//3
#Answer is 3
Why? This is because floor of 10/3 (3. 333333333333333) is 3.
>>> -10/3
#Answer is -3.333333333333333
>>> -10//3
#Answer is -4
>>> 10//-3
#Answer is -4
Why? This is because floor of -10/3 or floor of 10/-3 (-3. 333333333333333) is -4.
>>> -10/-3
#Answer is 3.333333333333333
>>> -10//-3
#Answer is 3
Why? This is because floor of -10/-3 (3. 333333333333333) is 3.
In real division, there is a dividend (numerator), there is a divisor (denominator) and a quotient but no
remainder. But in Euclidian division, there is a dividend (numerator), there is a divisor (denominator),
a quotient and a remainder. What is the relation between all the four components of Euclidian division?
>>> 10%3
#Answer is 1
Why? This is because Remainder = 10 - 10//3 * 3 = 1
>>> -10%3
#Answer is 2
Why? This is because Remainder = -10 - (-10//3) * 3 = 2
>>> 10%-3
#Answer is -2
Why? This is because Remainder = 10 - (10//-3) * 3 = -2
>>> -10%-3
#Answer is -1
Why? This is because Remainder = -10 - (-10//-3) * 3 = -1
Till now we had tested and checked different concepts using Python shell (interactive) mode using Python
IDLE (Integrated Development and Learning Environment). It will be very difficult to write a complete
Python program using Python IDLE. Therefore, we have to switch to Python script (programing) mode.
To start Python script mode, first we have to start Python IDLE and then click New File (CTRL+N) from
the File menu. This will open a new window as shown below:
The new window will be titled as Untitled. A Python script is a collection of many Python statements.
Python script must contain at least one Python line of code. We will start with one line Python script.
This script needs to be saved first. To save a Python script (program), there are two options:
1. Click File menu and then click Save
2. Use keyboard short cut CRTL+S
Either of the options will open a dialog box, where you have type a file name and hit <ENTER>. Suppose
you type filename as first, then the Python script will be saved as first.py. Every Python script must be
saved first before it can be executed (Run). It is better to create a folder say Python (or any other folder
FAIPS, DPS Kuwait Page 14 / 16
Python Notes Class XI Python Basics
name) preferably in the D:\ to save all your Python scripts. If your computer does not have D:\ drive, then
you can create the folder in the C:\ drive. Once the Python script has been saved, you can execute the
script by clicking the Run Menu and from the Run menu and click Run Module (keyboard short cut
function key F5). If the Python script is free from syntax error, Python script will be executed. Running
of the Python scripts is also called Run Module. Running of the script first.py will display:
1. Write a Python script to input radius and height of a solid cylinder; calculate the surface area and the
volume of the solid cylinder. Display the surface area and the volume on the screen.
Surface Area=2πh+2πr2, Volume=πr2h, r is radius, h is height, π is 3.14
rad=float(input('Radius? '))
ht=float(input('Height? '))
sa=2*3.14*ht*rad+2*3.14*rad*rad #2*3.14*ht*rad+2*3.14*rad**2
vol=3.14*rad*rad*ht #3.14*rad**2*ht
print('Radius=', rad)
print('Height=', ht)
print('Surface Area=', sa)
print('Volume=', vol)
2. Write a Python script to input length of three side of a triangle; calculate the area of a triangle using
Heron’s formula. Display the area of the triangle on the screen.
a=float(input('Length of 1st side? '))
b=float(input('Length of 2nd side? '))
c=float(input('Length of 3rd side? '))
s=(a+b+c)/2 #semi perimeter
area=(s*(s-a)*(s-b)*(s-c))**0.5
print('Area=', area)
3. Write a Python script to input 3 coefficients (a, b, c) of a quadratic equation (ax2+bx+c=0); calculate
the discriminant and the two roots of the quadratic equation. Display the two roots on the screen.
a=float(input('Coefficient of x^2? '))
b=float(input('Coefficient of x ? '))
c=float(input('Constant Term ? '))
d=b*b-4*a*c #d=b**2-4*a*c
x1=(-b+d**0.5)/(2*a)
x2=(-b-d**0.5)/(2*a)
print('1st root=', x1)
print('2nd root=', x2)