Professional Documents
Culture Documents
Abu Hamour Branch, Doha - Qatar: M.E.S Indian School (Mesis)
Abu Hamour Branch, Doha - Qatar: M.E.S Indian School (Mesis)
Keywords :
Keywords are the words that have special meaning reserved by programming language.
They are reserved for special purpose and cannot be used for normal identifier names.
E.g. in, if, break, class, and, continue, True, False
Identifiers :
Literals :
String Literals
text2 = ‘ ’ ’Welcome
to
MESIS’ ’ ’
print(text2)
Welcome
to
MESIS
Size of Strings :
8
15
17
3
Operator :
Operators are the symbol, which triggers some computation when applied on operand.
Unary Operator : those operators that require only one operand.
Unary Plus +
Unary Minus –
Bitwise Complement ~
Logical Negation not
Binary Operator : those operators that require only two operand.
Arithmetic Operator +, -, *, /, %
Bitwise Operator &, ^, |
Shift Operator >> , <<
Identity Operator is , is not
Relational Operators
Less Than <
Greater Than >
Less Than or Equal to <=
Greater than or Equal to >=
Equal To ==
Not Equal to !=
Eg :
a=3>6
print (a)
b = 13 < 60
print (b)
d = 3 == 6
print (d)
False
True
False
Logical Operators
Logical AND and
Logical OR or
ACD-105, REV 0, 27.03.2021
Eg :
a = 3 > 6 and 13 < 60
print (a)
a = 3 > 6 or 13 < 60
print(a)
False
True
Assignment Operator
Assignment =
Assign Quotient /=
Assign Sum +=
Assign Product *=
Assign Remainder %=
Assign Difference -=
Assign Exponent **=
Assign Floor Division //=
Eg :
n = 10
n/=2
print(n)
n+=5
print(n)
n*=2
print(n)
n%=3
print(n)
n//=2
print(n)
5.0
10.0
20.0
2.0
1.0
Membership Operator
Whether variable in sequence in
Whether variable not in sequence not in
Eg :
sub = ‘a’ in ‘ram’
print(sub)
sub = ‘a’ not in ‘ram’
print (sub)
sub = ‘t’ in ‘ram’
print(sub)
sub = ‘t’ not in ‘ram’
print(sub)
Punctuator :
• Punctuators are the symbols used in programming language to organise the statements of the
program.
• Common Punctuators are ‘ “ # \ ( ) [ ] { } @ , : . =
Named Label whose value can be changed and processed during the program run.
Creating Variable :
name = ‘Anant’
age = 9
age = 30
Python do not have fixed memory locations, when the value changes they change their
location.
Lvalue : expression that can come on left hand side of an assignment.
Rvalue : expression that can come on right hand side of an assignment.
Dynamic Typing :
The process in which the variable pointing to a value of certain type, can be made to point to
the value of other type.
Eg :
age = 10
print(age)
age = “ten”
print(age)
Eg :
name = ‘anant’
print(name)
name = 60
print(name)
anant
60
In Dynamic Typing the datatype attached with the variable can change during the program run
where as in Static Typing the data type defined for the first time is fixed for the variable.
Python Support Dynamic Typing.
Multiple Assignment :
Variable Definition :
input( ) function is used for taking value for a variable during runtime.
Eg :
name = input(“Enter your name : “)
print(“Name : “, name)
To overcome the above error we need to convert the string type object to integer or any
desired numeric type.
input( ) function always returns a value of string type.
Use int( ) or float( ) function to convert the type string to int and float respectively.
Eg :
age = int(input(“Enter your age : “)
age1 = age + 10
print(“Age after 10 years”, age1)
Enter your age : 20
Age after 10 years 30
My name is Aisha
Age : 18
sep and end argument in print statement
Eg :
print(“My”, “name”, “is Amaira”, sep = “$”, end = “…”)
print(“Age : 18”, sep = “$”, end = “…”)
My$name$is Amaira…Age : 18…
Dictionary :
Dictionary in Python represents unordered set of comma-separated key : value pair within { },
with the requirement that within a dictionary, no two keys can be the same.
Eg : A = {‘a’ : 1, ‘b’ : 2, ’c’ : 3, ’d’ : 4}
>>>A[‘b’]
2
>>> A[‘d’]
4
Immutable Types :
The immutable types are those that can never change their value in place.
Immutable Data Types are
Integer
Floating Point Number
Boolean
String
Tuples
Eg : >>>a = 10 >>>print(a) 10
>>>a = 20 >>>print(a) 20
Note : We see that the above code is changing the value of the variable, values are not
changing in place. Variable names are stored as references to a value-object, each time
you change the value, the variable’s reference memory address changes.
Mutable Types :
Mutable types are those whose values can be changed in place.
Mutable Data Types are
List
Dictionary
Sets
Thus Mutability means that in the same memory address, new value can be stored as and when
you want.
Expression :
Expression is the valid combination of operators, literals and variables.
Expression is formed by atoms and operators.
Atoms are something which has value.
Eg : Literals, identifier, string, list, tuples, sets and dictionary etc.
Types of Expression
Arithmetic Expression : a + 5 – 6
Relational Expression : x > 90
Logical Expression : a > b or 6 < p
String Expression : “computer” + “science”, “web” *3
ACD-105, REV 0, 27.03.2021
Evaluating Arithmetic Expression :
During evaluation if the operand are of same data type then the resultant will be of the data
type which is common.
In case, during evaluation if the operand are of different data type then the resultant will be of
the data type of the largest operand. We need to promote all the operand to the largest type
through type promotion.
Implicit Type Conversion / Coercion
Implicit conversion is performed by the compiler without programmer’s intervention.
Eg :
>>>2 + 4.0 #note that the type coercion is done automatically
6.0
>>>2 + 4.0
6.0
>>>1 + 2j + 1
(2 + 2j)
Note :
Division operator will result in floating point number, even if both operands are integer type.
Eg :
a = 10
b=2
i = a/2
print(i)
5.0
Type Casting :
Python internally changes the data type of some operand, so that all operand have same data
type. This is called Implicit Type Conversion.
Explicit Type Conversion / Type Casting : user defined conversion that forces an expression to
be of specific type.
Type casting is done by <datatype> (expression)
Eg : amt = int(amount)
Different data types are int(), float(), complex(), str(), bool()
Types of statements in Python
if Statement :
In Python, if Statement is the simplest of all decision making statements.
It will run the body of code only when IF statement is true.
When you want to justify one condition while the other condition is not true, then you use "if
statement".
Syntax :
if <expression/condition> :
statement block1
Example :
a = 33
b = 200
if b > a:
print("b is greater than a")
if else Statement :
The if else statement provides control to check the true block as well as the false block.
Syntax :
if <expression/condition> :
statement block1
else :
statement block2
Example :
a = int(input(“Enter any number : “))
if a%2 == 0 :
print(a, " is even")
else :
print(a, “ is odd”)
if..elif..else Statement :
When we need to construct a chain of if statement(s) then “elif” clause can be used instead of
“else”.
“elif” clause combines if..else-if..else statements to one if..elif..else.
“elif” can be considered to be abbreviation of “else if”.
In an “if” statement there is no limit of “elif” clause that can be used, but an “else” clause if
used should be placed at the end.
There are many situations when we need to repeat certain task or functions some given
number of times or based on certain condition.
Loop :
Loops can execute a block of code number of times until a certain condition is satisfied. Their
usage is fairly common in programming. Unlike other programming language that have for loop,
while loop, do while, etc.
To carry out repetitive task python provides following looping / iterative statements :
Conditional Loop while
Counting Loop for
while loop is used to run a block of code until a certain condition is satisfied.
Syntax :
while <logical expression> :
loop body
Anatomy of a while Loop
Initialization
Test Expression
The Body of the Loop
Update Expression
Example :
n=1
while n < 5 :
print(“Square of “, n, “ is “, n*n)
n += 1
print(“Thank you”)
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Thank you
for loop :
Both the loops of Python has else clause. The else of a loop executes only when the loop end
normally (i.e. only when the loop is ending because the while loop’s test condition has become
false and for loop has executed the last sequence)
Note : The else clause of the Python Loop executes when the loop terminates normally, not
when the loop is terminating because of break statements.
Jump Statement :
You might face a situation in which you need to exit a loop completely when an external
condition is triggered or there may also be a situation when you want to skip a part of the loop
and start next execution.
Python provides break and continue statements to handle such situations and to have good
control on your loop.
The break Statement :
The break statement in Python terminates the current loop and resumes execution at the next
statement.
The continue statement in Python returns the control to the beginning of the while loop. The
continue statement rejects all the remaining statements in the current iteration of the loop
and moves the control back to the top of the loop.
Nested Loop :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
*********