Field of Technological Industries: Computer Science (ETP211) : (Lec)
Field of Technological Industries: Computer Science (ETP211) : (Lec)
Example:
>>> str1 = input('Please Enter the String:')
Please Enter the String:amit
>>> print(str1)
amit
Introduction to Computer and Python Programming
The input() function
The input() function is used to accept an input from a user. A programmer can ask a user to
input a value by making use of input().
Output
Type your name: Ahmed
Type your ID: 221100218
Hello Ahmed
Your Id = 221100218
Introduction to Computer and Python Programming
PROGRAM: Write a program to calculate your age
Output
Enter your year of birth: 2002
You age is 22 years old
Introduction to Computer and Python Programming
PROGRAM: Write a program to calculate the area of a rectangle
L1=int(input(‘The length= ’))
L2=int(input(‘The width= ’))
a=L1*L2
print(‘Length=’, L1, ‘width= ’, L2, ‘area= ‘, a)
Output
Length = 2, width = 5, area = 10
Introduction to Computer and Python Programming
PROGRAM: Write a program to calculate the area of a circle.
L=int(input(‘The radius= ’))
a=3.14*L**2
print(‘radius=’, L, ‘area= ‘, a)
Output
radius = 10, area = 314
Introduction to Computer and Python Programming
PROGRAM: Write a program to print name, age and hight for a person using input function.
Output
Ahmed is 20 years old.
His height is 185.5 cm.
Introduction to Expression and Operators
An expression in Python is a block of code that produces a result or
value upon evaluation.
Example:
In the expression 6 + 3 the ‘+’ act as operator. Where operator
requires operands to perform operations. Therefore 6 and 3 act as
operands.
Arithmetic Operators
There are two types of Arithmetic Operators
a) Unary Operator b) Binary Operator
Unary Operators:
Unary arithmetic operators perform mathematical operations on one operand only. The ‘+’ and ‘-’ are two
unary operators.
Example:
>>> x = -5 #Negates the value of X
>>> x
-5
Binary Operators
Binary operators are operators which require two operands .
Operator is written in between two operands.
Example:
>>> 3 + 10
>>>13
>>> 10 – 7
>>> 3
Arithmetic Operators
Division Operator
If the division operator is applied in between two operands, it returns the
result as the arithmetic quotient of the operands.
Example:
>>> 4/2
>>> 2.0
>>> 2/3
0.6666666666666666
Arithmetic Operators
Modulo (%) Operator
When the second number divides the first
number, the modulo operator returns the
remainder.
Example:
>>> 21 % 9
3
Arithmetic Operators
The Exponent ** Operator
The ‘**’ exponent operator is used to calculate the power or exponent
of a number
Example
>>> 5**4
625
Operator Precedence and Associativity
• The operator precedence determines the order in which the python interpreter evaluates the
operators in an expression.
• Where as associativity property decides which operation to be performed first.
• Associativity is of two types.
a) Left to Right
Example:
4+6–3+2 = 9
b) Right to Left
X = Y = Z = Value
Note:
When the operators of same priority are found in the expression, precedence is given to the
left most operator.
Operator Precedence and Associativity
Example:
Z=4*6+8/2
= 28
In the above expression * is evaluated first, even though * and / have the
same priorities. The operator * occurs before / and hence the evaluation
starts from left to right.
Precedence Operators Associativity
Highest () Innermost to Outermost
** Highest
* , / , // , % Left to Right
+- Left to Right
Lowest
Compound Assignment Operator
• The operators +, *, //, /, % and ** are used with assignment operator (=) to
form compound or augmented assignment operator.
Example:
X=X+1
But python allows programmer to combine assignment and addition operator.
Thus the above statement X = X + 1 can also be written as
X+=1
Various other Compound Assignment operators are as follows
*= , /= //=, %=, **=, -= , and !=
Some Examples
Example
>>> z=(5+6)*10
>>> z
110
Explanation
In the above example, z is initialized with one expression (5+6)*10. The
sub expression (5 + 6) is
evaluated first, followed by the multiplication operation.
Some Examples