AING0BJECTIVES
LEARNING In this chapter,
students will learn
CHAPTER 7 Introduction to Python
Using input () function
Using int () function
Using print () function
Working with Operators
Operator Precedence
More in Python
ca
edureka!
Python
Programming
Python is apopular programming language. Itis used in machine learning, web development,
desktop applications and many other fields. It allows you to write programs in fewer lines
of code
than most of the programming languages.
105)
INTRODUCTION TO PYTHON
Python is ahigh-level programming language. It has simple language syntax which
It easy to read & write. It was developed by Guido Van Rossum in 1991 at
the maes,
Research Institute for Mathematics and Computer Science in the Netherlands. Nation
versions of Python have been released till date starting from version 10.
in various application areas such as: Python is
Artificial Intelligence
Machine Learning
Web Development
GUIDO
Scientific and Numeric Calculations
VAN
Computational Games ROSSUM
Image Processing and Graphic Designing
Applications
Network Programming
In the previous class, you have learnt how to declare, initialise, and print
variables.
Now, let us recall the use of the input() function and learn more
commands in Python
programming.
USING input( ) FUNCTION
In Python, we have the input( ) function for taking the user
input. The input( ) tuncuoi
prompts the user to enter data. It accepts all user input as a string. The
user may ente o
number or a string but the input( ) function treats them as
strings only.
Syntax:
input ([Prompt])
Prompt is the message we may like to display on the screen
and it is optional. When a prompt is specified, first it is displavedprior to taking e
on the screen alno
then the user can enter data.
The input( )takes exactly what is typed from the keyboard,
converts it into a(-).striyd
assigns it to the variable on the left-hand side of the assignment operator Entering
data for the input function is terminated by pressing the
Enter key.
106
ample1 :
Prompt
Edit Format Run Optións Window Help
File
a=input
(" Enter your NAME ")
print (a)
O u t p u t :
iDLE Shell 3.10.0
Options Window Help
File Edit Shell Debug /v3.10.0:b494159, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (
Python 3.10.0 (tags
) on win32
AND64)
Type "help, "copyright", "credits" or "license () " 2or more information.
>>>
DESTART : c:/Users/vikaa/AppData/Local/Programs /Python/Python310/nput_statemen
t.py Vadhavan
Enter your NAME Advit
Advit Vadhavan
>>>
L: 7 Cot: 0
Here is an example of the input () function without any prompt.
Example 2:
File Edit Format Run Options Window Help
Print("Enter roll, name and percentage') Tech Info
E,,p=int (input ()),input (),£loat (input ())
PZint('Roll:',z)
Pzint('Nane:',n) Python uses the print) function to output
Print(' Percentage: ,p)
data to the standard output device the
Screen.
Ln: 6 Col: 0
Output:
IDLE Shell 3.10.0
File Edit Shell Debug Options
Window Help
C:/Users/vikaa/AppData/Local/Programs /Python/Python310/Input without
AK:
msg.pY
Enter roll, nane and percentage
2
Amit
98.2
Roll: 2
Name : Amit
>>>\||Percentage: 98.2
Ln: 49 Col: 0
107
USING int( )FUNCTION
The int( )function converts a specified value into an integer. In the example shown
arithmetic operators '+ (addition), and -' (subtraction) have been used to belon
calculations on values entered by the user. The int( )function has been used for perforrn
the input entered by the user into an integer type. This is because any input
entered in
converting
Python is considered as a string type, and you cannot perform arithmetic
strings. Therefore, it hasto be converted into a number type before performingoperations
on it. calculations
Example 3: Output:
npyUsersManinder/AppData/Loca/Programs/Python/Python310/n.py (3.10.2) X
Ede Fomat Run Options Window Help Entez First Number:10
int (input ("Enter Frst Number:"))
int (Snput ("Enter Second Number:"))
Entex Second Numbez:8
The sum is l8
print (The sum is", c) The difterence is 2
print (The ditterence is", d)
Example 4:
Input statement_diff.py- C:/Users/vikaa/AppData/Local/Pr.
X
File Edit Format Run Qptions Window Help
input (" Enter your NAME ")
age = int (input ("Enter your agge:
"))
beight float (input ("Enter your
|print (" height:"))
- ")
Print("Your name is", a)
print ("Your age is", age)
print ("Your height is", height)
print ( -")
Ln: 8 Col: 1
Output:
IDLE Shell 310.0
File Edit Shell Debug Opions
Window Help
= RESTART:
t dift.py C:/Users/vikaa/AppData/Logal/Programs / Py thon/ Py thon310/ Input statemen
Enter your NAME Parhan Akta
Enter your age:24
Enter your hei ght:5.5
Your name is Pahan Aktar
Your age is 24
Your height is 5.5
108 Ln: 17 Co: 0
SAVE As
( ) function in Python is used to convert a string into 'a floating point
The float
(decimal) number.
USING print( ) FUNCTION
function is used to send data to the standard output device the screen. The
The print()
nt)function evaluates the expresion before displaying it on the screen. The print( )
function sends a complete line and then moves to the next line on the screen for the
next output.
Syntax:
print(value [, ... sep = ', end = \n'])
8 sep: The optional parameter sep is a separator between the output values. We can use
a character, integer or string as a separator. The default separator is space.
end: This is also an optional parameter and it allows us to specify any string to be
appended after the last value. The default is a new line.
Below are some examples showing the use of print( ):
Example 5:
Print sep.py - C/Users/vikaa/AppData/Local/Prog.
File Edit Format Run Options Window Help
print (122)
print('hello India')
print (' Computer','saience ')
print ('Computer',' Science',sep' & ')
print(Computer','Science',sep=' & ,end'.)
Ln: 5 Co: 22
Output:
IDLE Shell 3.10.0
File Edit Shell Debug Options Window
|>>> Help
|>>>
RSTART: C:/Usera/vikaa/AppData/Looal/ Programs/Python/Python310/ Print sep.py
122
hello India
Computer Science
Computer & Saience
|Computer & Soienoe.
Ln: 29 Col: 11
109)
WORKING WITH OPERATORS
Operators are symbols that represent arithmetic and SAVE As
logicaloperations onoperands and provide a meaningful Operators can be defined .
result. For performing any operation, the operator symbols that are used to perform
requires one or more operands. The valid combination operations on operands.
of operators and operands makes an expression that
returns a computed result.
Examples of some operators are t, *, / %. and so on.
Let us learn about Arithmetic Operators, Relational Operators, Assignment Operators
and Logical Operators with examples.
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations like addition, multiplication.
division etc.
Operator Description Example
perform addition of two numbers X= a + b
perform subtraction of two numbers X = a -b
perform division of two numbers X = a/b
perform multiplication of two numbers X= a * b
% modulus returns remainder X =a % b
Floor division remove digits after the decimal point |X = a//b
Exponent perform raise to power X = a ** b
Example 6:
Arithmetic,py C/Users/vikaa/AppData/Local/Pro.
File Edit Format Run Options Window Help
y 2
print('x + y =',x+y)
print ('x - Y =,X-y)
print ('x * y =',x*y)
print ('x / y =',x/y)
Print ('x // y =',*//y)
print ('x ** y =', *y)
Ln:9 Col: 0
110)
Output:
IDLE Shell 3.100
Options Window Heln
Shell Debug
Ele Edit Ont
Python 3.10.0 (tags /v3.10.0:b494f59, 4 2021, 19:00:18) (MgC v.1929 64 bit (
win32
AMD64 )) on 'ored1 ta" or "1icenme () " for more information
"help", "oopyright",
Type
>>> - RESTART C:/Users /vikaa/AppData/Logal/ Programs/ Python/Py thon310/Ari thmetia.py
xy 12
20
x/y 5.0
y 100
x *
>>>|
Ln: 11 Col: 0
Operators/Comparison Operator
, Relational
provide the result as either
Relational operators are used to Compare the values. They
True or False
Example
Operators Description
a == b
Equal to, return true if a equals to b
a! = b
Not equal, return true if a is not equals to b
a> b
Greater than, return true if a is greater than b
V
Greater than or equal to, return true if a is greater a > = b
>=
than b or a is equals to b
a<b
<
Less than, return true if a is less than b
than or equal to, return true if a
is less than b or a <= b
<= Less
a is equals to b
Example 7:
Relational.py - C:/Users/vikaa/AppData/Local/Pro.
File Edit Format Run Options Window Help
10
y 5
print ('x > y is',x>y)
print('x < y is' ,x<y)
print('x =s y is' , x y )
print('x != y is' ,x!=y)
print('x = y is',x>Y)
Print('* <= y is' ,x<=y)
Ln: 9 Col: 0
111
Output:
IDLE Shell 3.10.0
File Edit Shell Debug Options Window Help
>>>
RESTART: C:/Users/vikaa/AppData/Local /Programs/ Py thon/Py thon310/Relationa 1
x>y is True
x< y is False
x y is False
x ! y is True
x > y is True
x < y is False
>>>
Ln: 25 Cot: 0
Assignment Operators
Assignment operators are used to assign values to the variables.
Operators Description Example
Assigns values from right side operand to left side a=b
operand
+=
Add 2 numbers and assigns the result to left a+= b or a = a + b
operand.
Divides 2 numbers and assigns the result to left
a/= b or a = a/b
operand.
* Multiply 2 numbers and assigns the result to left a*= b or a = a *b
operand.
Subtracts 2 numbers and assigns the result to left
a-= b or a = a -
operand.
%=
modulus 2 numbers and assigns the result to left
|operand. a%= b or a = a % b
Perform floor division on 2 numbers and assigns
the result to left operand. a//= b or a = a//b
*. calculate power and assigns the result to left
operand. a**= b or a = a**b
112
Example 8:
x 20
print (" Value of x + y is ". 2)
print (" Value of x is , z)
print (" Value of z mx is , 2)
print (" Value of z / x is ", 2) Output:
2 Value of x + y is 30
Value of z 4* % is SO
print (" Value of z x is ". z) Value of z = x is 1000
Value of z /= % is 50.0
Value of z $= x is 2
print ( Value of z *Am x is 2) Value of z **= x is 1048576
Value of z /l= x îis 52428
print (" Value of z /l= x is ", z)
Logical Operators Tech Info
Logical operators are used to perform
logical operations on the given two Boolean operators are named after George Boole,
variables or values. Logical operators an English Mathematician, who developed many
are also called Boolean operators. logical methods that we use in programming
today.
|Operator Description Example
and return True if both the conditions are True X and Y
or return True if either or both the Conditions are True Xor Y
not reverse the condition not (X>Y)
OPERATOR PRECEDENCE
Operator precedence in Python programming is a rule that describes PE MDAS
Wnich operator is solved first in an expression.
Python follows the PEMDAS rule. The PEMDAS Rule (an acronym
for "Please Excuse My Dear Aunt Sally") is a set of rules that
Pioritize the order of calculations, that is, which operation is to
be performed first.
NH
113
PEMDAS stands for Parentheses, Exponents, Multiplication and Division, and Addition
and Subtraction.
Here is an example that shows the application of PEMDAS,
Step 1: Parentheses 6+ (8-2) +3- 5 x2
Step 2: Exponents 62+ 6 +3-5 x 2
Sbep 3: Multiply and 36 + 6 + 3- 5 x 2
Bivide 36 + 2-5x2
(oform the operakon kal comes frt)
Step 4: Add and 36+ 2 - 10
Subtract
(Perform the operation that comes frt)
38- 10
Solution: 28
Here are some examples of PEMDAS rule:
Operations
Example 9: Simplify the following expression using the Order of
(a) 5 - 2 + 3 x 4 = 5 -2 + 3x 4
=5 -2 + 3x 4 Multiply
= 5- 2 + 12
=5 -2 + 12 Subtract
= 3 + 12
= 3 + 12 Add
= 15
(b) (18 ÷6 x 5) - 14 + 7 = (18 ÷ 6 x 5) - 14 + 7
=(18 + 6 x 5) - 14 ÷ 7
= (3 x 5) -14 + 7
= (15) 14 +7
= 15 - 14 +7
= 15 - 2
= 13
114