0% found this document useful (0 votes)
7 views10 pages

CH - 5 TB Ex - Python Pgms

The document discusses invalid identifier names in Python and provides explanations for each case. It includes Python assignment statements, logical expressions, error categorization, and several programming exercises with outputs. Additionally, it covers data types for various values and provides sample Python programs for temperature conversion, interest calculation, arithmetic operations, and more.

Uploaded by

salirnaqvi2006
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)
7 views10 pages

CH - 5 TB Ex - Python Pgms

The document discusses invalid identifier names in Python and provides explanations for each case. It includes Python assignment statements, logical expressions, error categorization, and several programming exercises with outputs. Additionally, it covers data types for various values and provides sample Python programs for temperature conversion, interest calculation, arithmetic operations, and more.

Uploaded by

salirnaqvi2006
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/ 10

EXERCISE

1. Which of the following identifier names are invalid and why?

i Serial_no. v Total_Marks
ii 1st_Room vi total-Marks
iii Hundred$ vii _Percentage
iv Total Marks viii True

i. Invalid - Identifier in python can't contain any exceptional character aside from underscore
(_).
ii. Invalid - Identifier in Python can’t begin with a number.
iii. Invalid - Identifier in Python can't contain any uncommon character aside from underscore
(_).
iv. Invalid - Identifier in Python can't contain any exceptional character aside from underscore
(_). In the event that more than a single word is utilized as a variable, at that point it tends to
be isolated utilizing underscore (_), rather than space.
v. Valid
vi. Invalid - Identifier in Python cannot contain any special character except underscore (_). If
more than one word is used as a variable then it can be separated using an underscore (_),
instead of hyphen (-).
vii. Valid
viii. Invalid - Identifier in Python should not be a reserved keyword.

2. Write the corresponding Python assignment statements:

a) Assign 10 to variable length and 20 to variable


breadth.
length, breadth=10,20

b) Assign the average of values of variables length and


breadth to a variable sum.
Sum=(length +breadth)/2

c) Assign a list containing strings ‘Paper’, ‘Gel Pen’,


and ‘Eraser’ to a variable stationery.
Stationery=[‘Paper’,’Gelpen’,’Eraser’]

d) Assign the strings ‘Mohandas’, ‘Karamchand’, and


‘Gandhi’ to variables first, middle and last.
first,middle,last = “Mohandas”,”Karamchand”,”Gandhi”

e) Assign the concatenated value of string


variables first, middle and last to
variable fullname. Make sure to incorporate
blank spaces appropriately between different parts
of names.
fullname = first +” ” +middle+” ”+last
2. Write logical expressions corresponding to the following statements in Python and
evaluate the expressions (assuming variables num1, num2, num3, first, middle, last
are already having meaningful values):

a) The sum of 20 and –10 is less than 12.

(20+(-10))<12
10<12
True

b) num3 is not more than 24.

num3 <=24 or not (num3>24)

c) 6.75 is between the values of integers num1 and num2.

(6.75>=num1) and (6.75<=num2)

d) The string ‘middle’ is larger than the string ‘first’ and smaller than the string ‘last’.

(middle>first) and (middle<last)

e) List Stationery is empty.

len(Stationery)==0

4. Add a pair of parentheses to each expression so


that it evaluates to True.
a) 0 == 1 == 2

(0==(1==2)
1==2 (false)
False can be represented as 0
0==0 (True)

b) 2 + 3 == 4 + 5 == 7

(2+(3==4)+5)==7

3==4 (false)
2 +0 + 5 =7
7==7 (True)

c) 1 < -1 == 3 > 4

(1<-1)==(3>4)
(False) (False)
0==0 (True)
5. Write the output of the following:

a) num1 = 4
num2 = num1 + 1
num1 = 2
print (num1, num2)

Output: 2,5

b) num1, num2 = 2, 6
num1, num2 = num2,
num1 + 2
print (num1, num2)

Output: 6,4

c) num1, num2 = 2, 3
num3, num2 = num1,
num3 + 1
print (num1, num2, num3)

Output: Error, because num3 is used in RHS of line 2 (num3,num2=num1,num3+1) before defining
it earlier.

6. Which data type will be used to represent the following data values and why?

a) Number of months in a year- Integer datatype

b) Resident of Delhi or not – Boolean datatype

c) Mobile number - Integer datatype

d) Pocket money – Floating point datatype

e) Volume of a sphere – Floating point datatype

f) Perimeter of a square – Floating point datatype

g) Name of the student – String datatype

h) Address of the student – String datatype

7. Give the output of the following when num1 = 4, num2 = 3, num3 = 2

a) num1 += num2 +num3


print (num1)
Output: 4+3+2 =9

b) num1 = num1 ** (num2 + num3)


print (num1)
4(3+2) = 45 = 1024
c) num1 **= num2 + num3

4(3+2) = 45 = 1024

d) num1 = '5' + '5'


print(num1)

55

e) print (4.00/(2.0+2.0))

(4.00/4.0)=1.0

f) num1 = 2+9*((3*12)-8)/10
print(num1)

2+9*(36-8)/10
=2+9*28/10
=2+252/10
=2+25.2
=27.2
g) num1 = 24 // 4 // 2
print(num1)

6//2=3

h) num1 = float(10)
print (num1)

10.0

i) num1 = int('3.14')
print (num1)

Error, because we cannot pass string representation of float to an int function.

j) print('Bye' == 'BYE')

As Python compares string character to character and when different characters are found then
their Unicode value is compared. The character with a lower Unicode value is considered to be
smaller. Here, ‘y’ has Unicode 121 and ‘Y’ has 89. Output will be False.

k) print(10 != 9 and 20 >= 20)

True

l) print(10 + 6 * 2 ** 2 != 9//4 -3 and 29>= 29/9)

LHS=10+6*4!=9//4-3
=10+6*4!=2-3
=10+6*4!=-1
=10+24!=-1
=34!=-1 (True)

RHS= 29>=29/9
29>=3.2 (True)

Output: True

m) print(5 % 10 + 10 < 50 and 29 <= 29)

LHS=5 +10 < 50


=15<50 (True)

RHS= true

Output: True

n) print((0 < 6) or (not(10 == 6) and (10<0)))


(True) or (True) and (False)
True and False
Output : True

8. Categorize the following as syntax error, logical error or runtime error:

a) 25 / 0 - Run-time Error

b) num1 = 25; num2 = 0; num1/num2 – Run-time Error


TB Exercise -Important programs
1. Write a program to convert temperature in degree Celsius to
degree Fahrenheit.
TC=float(input("Enter the Celsius temperature="))
TF=TC*9/5+32
print("Fahrenheit Temperature =",TF)
Output :
Enter Celsius temperature=36.5
Fahrenheit Temperature = 97.7
2. Write a program to calculate the amount payable,
Amount=Principle + SI
P=float(input(“Enter the principle amount=”))
R=float(input(“Enter the rate of interest=”))
T=float(input(“Enter the time in years=”))
SI=(P*R*T)/100
A=P+SI
print(“Simple interest=”,SI)
print(“Amount=”,A)
Output:
Enter the principle amount=10000
Enter the rate of interest=7
Enter the time in years=2
Simple interest=1400.0
Amount=11400.0
3. Write a program to enter two integers and perform all arithmetic
operations on them.
num1=int(input(“Enter the first number=”))
num2=int(input(“Enter the second number=”))
print(“Sum=”,num1+num2)
print(“Difference=”,num1-num2)
print(“Product=”,num1*num2)
print(“Quotient=”,num1/num2)
print(“Integer Quotient=”,num1//num2)
print(“Remainder=”,num1%num2)
print(“Exponent=”,num1**num2)
OUTPUT:
Enter the first number=2
Enter the second number=5
Sum=7
Difference=-3
Product=10
Quotient=0.4
Integer Quotient=0
Remainder=2
Exponent=32
4. Write a program to find average of three numbers.
num1 = float(input("Enter 1st number: "))
num2 = float(input("Enter 2nd number: "))
num3 = float(input("Enter 3rd number: "))
avg = (num1 + num2 + num3) / 3
print("The average of the three numbers is”,avg)
OUTPUT:
Enter 1st number: 6.5
Enter 2nd number: 2.5
Enter 3rd number: 1.5
The average of the three numbers is 3.5
5. Write a program to swap two numbers using a third variable.
x = input('Enter value of x: ')
y = input('Enter value of y: ')
temp = x
x=y
y = temp
print('The value of x after swapping:',x)
print('The value of y after swapping:',y)
OUTPUT:
Enter value of x: 5
Enter value of y: 10
The value of x after swapping: 10
The value of y after swapping: 5
6. Write a program to swap two numbers without using a third
variable.
x = input('Enter value of x: ')
y = input('Enter value of y: ')
x, y = y, x
print('The value of x after swapping',x)
print('The value of y after swapping',y)
OUTPUT:
Enter value of x: 4
Enter value of y: 2
The value of x after swapping 2
The value of y after swapping 4

7. The volume of a sphere with radius r is 4/3πr3. Write a Python


program to find the volume of spheres with radius 7 cm, 12 cm,
16 cm, respectively.
r1 = 7
r2 = 12
r3 = 16
volume1 = (4/3*22/7*r1**3)
volume2 = (4/3*22/7*r2**3)
volume3 = (4/3*22/7*r3**3)
print("When the radius is",r1,"cm, the volume of the sphere will
be", volume1)
print("When the radius is",r2,"cm, the volume of the sphere will
be", volume2)
print("When the radius is",r3,"cm, the volume of the sphere will
be", volume3)
OUTPUT:
When the radius is 7 cm, the volume of the sphere will be
1437.3333333333335
When the radius is 12 cm, the volume of the sphere will be
7241.142857142858
When the radius is 16 cm, the volume of the sphere will be
17164.190476190477

8. Write a program that accepts the mass of an object and


determines its energy.(E = mc2 where, E ⇒ energy measured in Joule, m⇒
mass in Kilogram, c ⇒ speed of light in m/s.)

mass = float(input("Enter the mass of object(in grams): "))


c = 3 * 10 ** 8
Energy = (mass/1000) * c ** 2
print("The energy of an object is :",Energy,"Joule.")

OUTPUT:

Enter the mass of object(in grams): 2


The energy of an object is : 180000000000000.0 Joule.

***************************

You might also like