SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2022-2023 Semester: II
PRACTICAL 1
Part A (To be referred by students)
Data Types, Input / Output Statements and Operators (Arithmetic, Relational, Logical,
Bitwise)
SAVE THE FILE AND UPLOAD AS (RollNo_Name_Exp1)
Problem Statement: Write Python program to
1. Swap two variables.
1. Using temp variable
2. Using comma operator
3. Using bitwise XOR operator
2. Compute areas of the following (Find the equation and implement in python):
1. Circle
2. Square
3. Sphere
4. Triangle
5. Rectangle
3. Solve quadratic equation ax2+bx+c=0
4. Convert Celsius to Fahrenheit
5. Generate a Random Number using random(), randint(), sample()[import random]
Topic covered: input(), print().
Learning Objective: Learner would be able to
1. Analyze the scenario to write script declaring variables, printing output and using
expressions.
2. Infer the features of various operators for different operations as per variable data
type.
3. Evaluate the working of the algorithm using variables, printing and inputting user
values and expressions.
Theory:
Data types:
Number: int, float, complex
String
a = 11.80
x = 100
y = “Hello”
z = 3+5.j
print(a)
print(x)
1|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2022-2023 Semester: II
print(y)
print(z)
Output:
11.8
100
Hello
(3+5j)
Accepting Input from the Console:
# if input() used it accepts input as string
num=input(‘Enter a number: ‘)
print(num)
# to accept numbers
num=int(input(‘Enter a number: ‘))
print(num)
num=float(input(‘Enter a number: ‘))
print(num)
Output:
Enter a number: 10
‘10’
Enter a number: 45
45
Enter a number: 78
78.0
Output using print() with formatting:
x=6
y=89
print('The value of x is {1} and y is {0}'.format(x,y))
x = 12.3456789
print('The value of x is %3.2f' %x)
print('The value of x is %3.4f' %x)
print ('The %.2f and %.1f ' %(x, y))
Output:
The value of x is 89 and y is 6
The value of x is 12.35
The value of x is 12.3457
The 12.35 and 456.8
2|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2022-2023 Semester: II
Operators:
Operators are used to perform operations on variables and values.
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
Operator Example
+ x+y
- x-y
* x*y
/ x/y
% x%y
** x ** y
// x // y
Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator Example
= x=5
+= x += 3
-= x -= 3
*= x *= 3
/= x /= 3
3|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2022-2023 Semester: II
%= x %= 3
//= x //= 3
**= x **= 3
&= x &= 3
|= x |= 3
^= x ^= 3
>>= x >>= 3
<<= x <<= 3
Python Comparison Operators
Comparison operators are used to compare two values:
Operator Example
== x == y
!= x != y
> x>y
< x<y
>= x >= y
<= x <= y
Python Logical Operators
Logical operators are used to combine conditional statements:
Operato Description Example
r
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is not(x < 5 and x < 10)
true
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
4|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2022-2023 Semester: II
Operator Description Example
is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:
Operator Description Example
in Returns True if a sequence with the specified value is x in y
present in the object
not in Returns True if a sequence with the specified value is not x not in y
present in the object
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operator Name Description
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left Shift left by pushing zeros in from the right and
shift let the leftmost bits fall off
>> Signed right Shift right by pushing copies of the leftmost bit
shift in from the left, and let the rightmost bits fall
off
Operators:
x1 = 5
y1 = 5
x2 = ‘Hello’
y2 = ‘Hello’
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
5|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2022-2023 Semester: II
x = 'Hello world'
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
Output:
False
True
True
True
PRACTICAL 1
6|Page
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Course: Python Programming
PROGRAMME: B.(Tech.)/MBA(Tech.)
First Year AY 2022-2023 Semester: II
Part B (to be completed by students)
Data Types, Input / Output Statements and Operators (Arithmetic, Relational, Logical,
Bitwise)
(Students must submit the soft copy as per the following segments. A soft copy containing
Part A and Part B answered must be uploaded on the platform specified by the Practical
Teacher. The filename should be RollNo_Name_Exp1)
Roll No.: Name:
Prog/Yr/Sem: Batch:
Date of Experiment: Date of Submission:
1. Program Code along with Sample Output: (Paste your programs [1,2,3,4,5], input and
output screen shot for programs [1,2,3,4,5])
2. Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down
your learnings about the Topic: Data Types, Input / Output Statements and Operators.
7|Page