0% found this document useful (0 votes)
13 views16 pages

Python Is Also - This Means That It Is Not Converted To Computer-Readable Code Before The Program Is Run But at Runtime

The document provides an introduction to Python basics including data types, variables, operators, and comments. It discusses Python's interactive mode using examples and explains fundamental concepts such as creating variables and naming conventions. Rules for variables and examples of data types, arithmetic operators, and precedence are also covered.

Uploaded by

Mohammed Fahad
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)
13 views16 pages

Python Is Also - This Means That It Is Not Converted To Computer-Readable Code Before The Program Is Run But at Runtime

The document provides an introduction to Python basics including data types, variables, operators, and comments. It discusses Python's interactive mode using examples and explains fundamental concepts such as creating variables and naming conventions. Rules for variables and examples of data types, arithmetic operators, and precedence are also covered.

Uploaded by

Mohammed Fahad
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/ 16

Python Notes Class XI Python Basics

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.

Fundamental / Primitive / Built-in data types of Python and Literals (constants):


Data Type Literals (Constants)
int (integer) …, -4, -3, -2, -1, 0, 1, 2, 3, 4, …
float (floating point) …, -3.7, -1.0, -0.8, 0, 0.3, 1.8, 2.0, 3.9, …
complex …, 5+2j, 2-5j, …
'AMIT', '2.65', '***', 'GH-14/783, Paschim Vihar',
str (string)
"RUPA", "1995", "$$$", "49 South St, PO Box-9951"
bool True, False (To be discussed with logical expression)

Arithmetic operators of Python


Operator Operands Output Remarks
>>> 10+20 30 int + int is int (adds two numbers)
>>> 2.5+3.8 6.3 float + float is float (adds two numbers)
+
>>> 3.6+8 11.6 float + int is float (adds two numbers)
>>> 'MAN'+'GO' MANGO Joins two strings (concatenation)
>>> 35-17 18 int - int is int (subtracts two numbers)
- >>> 7.2-3.8 3.4 float - float is float (subtracts two numbers)
>>> 8-4.2 3.8 int - float is float (subtracts two numbers)
>>> 5*4 20 int * int is int (multiplies two numbers)
* >>> 3.5*4.5 15.75 float * float is float (multiplies two numbers)
>>> 4*2.5 10.0 int * float is float (multiplies two numbers)
>>> 12/4 3.0 int / int is float (divides two numbers)
Real >>> 2/3 0.6667 int / int is float (divides two numbers)
Division >>> 12.5/2.5 5.0 float / float is float (divides two numbers)
/ >>> 8.3/12.6 0.6588 float / float is float (divides two numbers)
>>> 10/3.5 2.857 int / float is float (divides two numbers)

FAIPS, DPS Kuwait Page 2 / 16


Python Notes Class XI Python Basics
>>> 12//4 3 int // int is int (divides two numbers)
>>> 2//3 0 int // int is int (divides two numbers) Answer
Floor
>>> 12.5//2.5 5.0 float // float is float (divides two numbers) is
Division
>>> 8.3//12.6 0.0 float // float is float (divides two numbers) the
//
>>> 10.5//4 2.0 float // int is float (divides two numbers) quotient
>>> 10//3.5 2.0 int // float is float (divides two numbers)
>>> 12%4 0 int % int is int (finds remainder of two numbers)
>>> 2%3 2 int % int is int (finds remainder of two numbers)
Modulo
>>> 12.5%2.5 0.0 float % float is float (finds remainder of two numbers)
%
>>> 8.3%12.6 8.3 float % float is float (finds remainder of two numbers)
>>> 10.5%4 2.5 float % int is float (finds remainder of two numbers)
>>> 10%3.5 3.0 int % float is float (finds remainder of two numbers)
>>> 2**3 8 int ** int is int (finds base raised to the exponent)
Power >>> 2.5**3.0 15.625 float ** float is float (finds base raised to the exponent)
** >>> 4**3.0 64.0 int ** float is float (finds base raised to the exponent)
>>> (-2)**5 -32 int ** int is int (finds base raised to the exponent)

Precedence of operators in Python


Highest ** (works from right to left)
Intermediate *, /, //, % (works from left to right)
Lowest +, - (works from left to right)
() parenthesis is used to override the precedence of an operator

Variable (Object) in Python


A variable is name given to a memory location to store value in the computer’s main storage (in RAM).
It is a name used in the program that represents data (value). The program can always access the current
value of the variable by referring to its name. In Python variable is created when it is assigned a value. It
means, in Python to create a variable, a value must be assigned to it. In Python, every variable is an Object.

Rules for naming a Python variable (identifier)


1. Variable name should start either with an alphabet (letter) or an underscore. Variable name starting
with _ (underscore) has special meaning.
2. Variable name may contain more than one characters. Second characters onwards we may use alphabet
or digit or underscore.
3. No special characters are allowed in a variable name except underscore.
4. A variable name in Python is case sensitive. Uppercase and lowercase letters are distinct.
Sum, sum, SUM, suM, Sum and sUm are treated as six different variable names in Python.
5. A variable name cannot be a keyword.
False, True, and, break, class, def, elif, else, for, if, import, in, not, or,
return and while are commonly used keywords and hence cannot be a variable name.

Examples of correct variable names are given below:


marks,m1,m2,Father_Name,Max_Score,sub1code,ans,Roll,Val,Input_Scr,
For, CSMarks, false, true, CLASS,…

List of incorrect variable names are given below:


In correct Variable Name Reasons
1m, 2ndst, #No, %Att Variable name starts with either digit or special character
Stu-name, name$, val% Variable names contain special characters
pass, break, while, for Keywords cannot be variable names

FAIPS, DPS Kuwait Page 3 / 16


Python Notes Class XI Python Basics
Python comment: a non-executable statement which is ignored by the Python interpreter. It is also called
remark. Every Python comment starts with #. For example:
>>> #This is a comment
>>>
Python interpreter will ignore the statement
>>> This is a comment
Without #, Python interpreter will flag the statement as syntax error

Creating Python variable


As mentioned earlier, a variable is a name given to a memory location to store a value. To use a variable,
variable must be created. To create a variable, a value should be assigned to it. Every variable that is being,
some value must be assigned to it. Data type of a variable depends on the data type of the constant
(expression) that is being assigned to the variable.

Rule for creating variable


VarName=Value
VarName1, VarName2, VarName3=Value1, Value2, Value3

>>> roll=21 21 is an int constant. By assigning 21 to variable


>>> name='RUPA JAIN' the roll, roll becomes an int variable.
>>> marks=89.5 'RUPA JAIN' is a str constant. By assigning
>>> roll, name, marks 'RUPA JAIN' to the variable name, name
Displays 21, 'RUPA JAIN', 89.5 becomes a str variable.
89.5 is a float constant. By assigning 89.5 to
>>> type (roll) the variable marks, marks becomes a float
Displays <class 'int'> variable.
>>> type (name)
Data type of the variables can checked by type()
Displays <class 'str'> function.
>>> type (marks)
Displays <class 'float'>

We can use single statement to create three separate variables.

>>> roll, name, marks=21, 'RUPA JAIN', 89.5


>>> roll, name, marks
Displays (21, 'RUPA JAIN', 89.5) on the screen.

Creating variables for arithmetic calculation:


>>> a, b=20, 8
>>> su, di, pr, di1, di2, re=a+b, a-b, a*b, a/b, a//b, a%b
>>> su, di, pr, di1, di2, re
Displays (28, 12, 160, 2.5, 2, 4)

>>> a, b=12.5, 5.0


>>> su, di, pr, di1, di2, re=a+b, a-b, a*b, a/b, a//b, a%b
>>> su, di, pr, di1, di2, re
Displays (17.5, 7.5, 62.5, 2.5, 2.0, 2.5)

print() function of Python


Till now we only used the variable name to display output on the screen. Now we will use print() function
to display output on the screen. print() function can be used to display constant value (literal), value of
an expression and value stored in a variable on the screen.

FAIPS, DPS Kuwait Page 4 / 16


Python Notes Class XI Python Basics
Syntax for print()
print(Value1, Value2, Value3, …)

>>> roll, name, marks=21, 'RUPA JAIN', 89.5


>>> print(roll, name, marks)
Displays 21 RUPA JAIN 89.5
print() does not display the single quote (') when displaying a string. By default, space is the separator.
Later we will learn how to change the default separator.
Displaying output with prompt
>>> print('Roll =',roll)
Roll = 21
>>> print('Name =',name)
Name = RUPA JAIN
>>> print('Marks =',marks)
Marks = 89.5
>>> num1,num2=10.5,7.25
>>> print(num1,num2)
Displays 10.5 7.25
>>> print(num1+num2,num1-num2)
Displays 17.75 3.25
>>> print('Sum =',num1+num2,'Difference =',num1-num2)
Displays Sum = 17.75 Difference = 3.25
>>> print('Sum =',num1+num2,' , Difference =',num1-num2)
Displays Sum = 17.75 , Difference = 3.25
>>> print('FAIPS,'+'DPS-'+'Kuwait')
Displays FAIPS,DPS-Kuwait

Converting data type in Python


In Python it is possible to convert data from one type another type using functions int(), float() and str().
• Function int() will convert either floating point (float) type or string (str) type to integer (int) type.
>>> int(2.5)
Displays 2
>>> int(-7.3)
Displays -7
>>> int(10.8)
Displays 10
>>> int(0.6)
Displays 0
>>> int(2.5+3.7)
Displays 6
>>> int(3.5*2.2)
Displays 7
>>> int(20.5%3.5)
Displays 3
>>> int(2.0**3.5)
Displays 11
>>> int(10/5) #Same as 10//5
Displays 2
>>> int(15/4) #Same as 15//4
Displays 3
int() function just extracts the integer part from a floating point value and discards the fractional part.

FAIPS, DPS Kuwait Page 5 / 16


Python Notes Class XI Python Basics
>>> int('2018')
Displays 2018
>>> int('-45')
Displays -45
>>> int('20.75')
Displays syntax error because 20.75 is not an integer constant.
>>> int('10+20')
Displays syntax error because 10+20 is not an integer constant.

• 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'

input() function of Python


We have already discussed use of assignment operator (=) to store value in a variable. Now we will learn
how to use input() function to store value in a variable by inputting data from a keyboard.

Syntax for input()


VarName=input(['Prompt'])
Prompt is an optional string. If Prompt is present, Prompt will appear on the screen and input()
function waits for some input from the keyboard. If Prompt is absent input() function only waits for
some input from the keyboard.

FAIPS, DPS Kuwait Page 6 / 16


Python Notes Class XI Python Basics
Keyboard input using input() without prompt
>>> x=input()
100
>>> y=input()
2.5
>>> z=input()
MANGO
>>> x,y,z
Displays ('100', '2.5', 'MANGO')
input() function can only returns string type. So inputting a string using input() function is not a
problem. But to input either an integer type or a floating point type we have to use int() and float()
function respectively.
>>> x=int(input())
200
>>> y=float(input())
16.75
>>> z=input()
APPLE
>>> x,y,z
Displays (200, 16.75, 'APPLE')

Keyboard input using input() with prompt


>>> roll=int(input('Roll? '))
Roll? 21
>>> name=input('Name? ')
Name? Sandip Kr Gupta
>>> marks=float(input('Marks? '))
Marks? 75.5
>>> roll, name, marks
Displays (21, 'Sandip Kr Gupa',75.5)
>>> x=int(input('Integer? '))
Integer? 2.5
Displays syntax error because 2.5 is not an integer constant
>>> x=int(input('Integer? '))
Integer? 2+3
Displays syntax error because 2+3 is not an integer constant

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)

√𝑎 a**0.5, a**(1/2), pow(a, 0.5), pow(a, 1/2)

√𝑎 + 𝑏 (a+b)**0.5, (a+b)**(1/2), pow(a+b, 0.5)


(a*a+b*b)**0.5,(a**2+b**2)**0.5, pow(a*a+b*b,0.5)
√𝑎2 + 𝑏 2 pow(a**2+b**2,0.5), pow(pow(a,2)+pow(b,2), 0.5)

FAIPS, DPS Kuwait Page 7 / 16


Python Notes Class XI Python Basics
𝑎4 a*a*a*a, a**4, pow(a,4)
3
√𝑎4 (a**4)**(1/3),a**(4/3),pow(a,4/3),pow(pow(a,3),1/4)
4
√𝑎3 (a**3)**(1/4),a**(3/4),a**0.75,pow(a,3/4),pow(a,0.75)
𝑎+𝑏 𝑎+𝑏
, (a + b) / c , (a + b) / (c – d)
𝑐 𝑐−𝑑
3 3 (a*a*a+b*b*b)/(c*c–d*d), (a**3+b**3)/(c**2–d**2),
𝑎 +𝑏
𝑐 2 − 𝑑2 (pow(a,3)+pow(b,3))/(pow(c,2)–pow(d,2))
4𝜋𝑟𝑎𝑑 2 4*3.14*rad*rad, 4*3.14*rad**2, 4*3.14*pow(rad,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 ...

Some built-in functions of Python as given in the syllabus


abs()
Returns the magnitude of number. Return number without sign.
>>> abs(-34), abs(-2.5), abs(45), abs(7.25)
(34, 2.5, 45, 7.25)
>>> w,x,y,z=10,-20, 5.7, -6.2
>>> abs(w), abs(x), abs(y), abs(z)
(10, 20, 5.7, 6.2)

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'

float() already explained earlier

input() already explained earlier

int() already explained earlier

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)

print() already explained earlier

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)

str() already explained earlier

type() already explained earlier

Python Floor Division and Remainder (Modulo) Operator


We have discussed floor division and remainder operator earlier with positive numbers (operands). But
when we use floor division and remainder operator with negative numbers then things are little different.
But before we discuss floor division and remainder with negative numbers, we need to learn a small
concept called floor of a number. What is a floor of a number? It is the closest integer value which is less
than or equal to the specified number.

For example, floor of positive numbers:


• What is the floor of 7.1? Answer is 7. 7.1 is greater than 7
• What is the floor of 7.0? Answer is 7. 7.0 is equal to 7
• What is the floor of 7.9? Answer is 7. 7.9 is greater than 7

FAIPS, DPS Kuwait Page 12 / 16


Python Notes Class XI Python Basics
For example, floor of negative numbers:
• What is the floor of -7.1? Answer is -8. -7.1 is greater than -8
• What is the floor of -7.0? Answer is -7. -7.0 is equal to -7.0
• What is the floor of -7.9? Answer is -8. -7.9 is greater than -8

>>> 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?

Dividend = Quotient (Dividend // Divisor) * Divisor + Remainder


=> Remainder = Dividend - Quotient (Dividend // Divisor) * Divisor

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

Python Syntax error


Syntax error arises when Python is unable to understand a line of code (Python statement) due to the
grammatical error in Python. Error that violates the rules of the programming language. Syntax error is
detected before the execution of a program, during the compilation time. Line of code or Python statement
with syntax error will never be executed. Example: Most syntax errors are typographical errors, incorrect
indentation, …

FAIPS, DPS Kuwait Page 13 / 16


Python Notes Class XI Python Basics
Python Run-time Error
Syntactically correct Python line of code (Python statement) performs illegal operation during execution
of a program (during the run-time). Illegal operation is performed when the Python code encounters an
unexpected data during the execution of the program. Run-time error causes the Python program to
terminate abnormally. Examples: Division by zero, Square root of a negative number, Log of zero or
negative number, …

Python Logic error


Syntactically correct line of code giving unexpected output or result due to error or flaw in the design or
the logic of the code. A Python line of code (Python statement) with a logical error may not terminate a
program abnormally. If a logical error triggers a run-time error, then the Python program will terminate
abnormally. Example: Missing parenthesis when it is required, Counter/Accumulator initialized
incorrectly, …
Python Script (Program)

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.

print('My first 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:

My first Python script!

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)

4. Write a Python script to input the following:


Employee code integer
Employee name string
Basic salary floating point
Calculate the following:
House rent floating point (40% of basic salary)
Perks floating point (80% of basic salary)
Gross salary floating point (basic salary + house rent + perks)
PF deduction floating point type (10% of gross salary)
IT deduction floating point type (15% of gross salary)
Net salary floating point type (gross salary – PF deduction – IT deduction)
Display all inputted values and all the calculated values on the screen.
code=int(input('Code? '))
name=input('Name? ')
FAIPS, DPS Kuwait Page 15 / 16
Python Notes Class XI Python Basics
bsal=float(input('Basic Sal? '))
hra=0.4*bsal
perks=0.8*bsal
gsal=bsal+hra+perks
pf=0.1*gsal
itax=0.15*gsal
nsal=gsal-pf-itax
print('Code=',code)
print('Name=',name)
print('Basic Salary=',bsal)
print('House Rent=',hra)
print('Perks=',perks)
print('Gross Salary=',gsal)
print('PF=',pf)
print('I Tax=',itax)
print('Net Salary=',nsal)

5. Write a Python script to input the following:


name name of a person
mob mobile number (8 digit integer)
local number minutes of local calls (floating point)
inter number minutes of international call (floating point)
sms number of SMS’s (integer)
Rate Minutes (float) Total
Local call is @ 0.25 per minute Local Call 0.25*Local Call
International call is @ 1.25 per minute International Call 1.25*International Call
SMS is @ 0.50 per message sms 0.50*sms
Amount due is calculated as sum of column Total. Display all inputted values and all the calculated
values on the screen.
name=input('Name? ')
mob=int(input('Mobile? '))
local=float(input('Local Calls? '))
inter=float(input('International Calls? '))
sms=float(input('SMS? '))
amt1=0.25*local
amt2=1.25*inter
amt3=0.50*sms
amount=amt1+amt2+amt3
print('Name=',name)
print('Mobile=',mob)
print('Local Calls=',local)
print('International Calls=',inter)
print('SMS=',sms)
print('Local Calls Charges=',amt1)
print('International Calls Charges=',amt2)
print('SMS Charges=',amt3)
print('Amount Dues=',amount)

FAIPS, DPS Kuwait Page 16 / 16

You might also like