sahil,11 -a,ip
th
vd1h
FINAL TERM PRATICAL FILE
*Working with python (3.1.1)
1. Open python shell
(Type Python 3 for entering the shell)
2. Exit / Quit the shell
(Type Exit () / Quit () for exiting the shell)
Execution modes (3.1.2)
3. Interactive Modes
4. Script Modes
Variables (3.4)
5. gender= ‘M’ message=
“Keep smiling” price=
987.9
Program 3-2
6. Write a python program to find the sum of
two numbers
# program 3-2
# To find the sum of two given
numbers num1=10
num2=20
result= num1+num2
print(result)
OUTPUT:
30
Program 3-3
7. Write a python program to find the sum of a rectangle
given that its length is 10 units and breadth is 20 units
Sequence(3.5.2)
8. String
str1= ‘Hello Friends’
str2= “452”
9. List(Example3.2)
list1[5,3.4, “New Delhi”, “20C”,
45] print(list1)
10. Tuple(Example3.3)
tuple1= (10,20, “Apple”,3.4, ‘a’)
print(tuple1)
10,20, “Apple”,3.4, ‘a’
Mapping
11. Dictionary
#Create a dictionary
>>> dic1 = { ‘Fruit’ : ‘Apple’ , ‘Climate’ : ‘Cold’ ,
‘Price(kg)’ : 120 }
>>> print (dic1 )
{‘Fruit’ : ‘Apple’ , ‘Climate’ : ‘Cold’ , ‘Price(kg)’ :
120 }
Operators
12.Arithmetic operator
# >>>str1= “Hello”
>>>str2=”India”
>>>str1 + str2
‘Hello India
>>>str1 = ‘India’
>>>str1 * 2
‘IndiaIndia’
13. *Addition
>>> num1 = 5
>>> num2 = 6
>>> num1 + num2
11
• 14. *Subtraction
>>> num1 = 5
>>> num2 = 6
>>> num1 – num2
-1
• 15. * Multiplication
>>> num1 = 5
>>> num2 = 6
>>> num1 * num2
30
16. *Division
>>> num1= 5
>>> num2 = 2
>>> num1 /
num2 2.5
*17. Modulus
>>> num1 = 13
>>> num2 = 5
>>> num1 % num2
3
18. *Floor Division
>>> num1 = 5
>>> num2 = 2
>>> num1 //
num2 2
>>> num2 //
*19. Exponent
>>> num1 = 3
>>> num2 = 4
>>> num1 ** num2
81
A) 20. Relational
operator Consider
num1= 10 num 2 = 0
num 3 = 10 str1 =
“Good” str2 =
“Afternoon”
21. # Equals to
>>> num1 == num2
False
>>> str1 ==
str2 False
22. # Not equal to
>>> num1 ! = num2
True
>>> str1 != str2
True
>>> num1 != num3
False
23. # Greater than
>>> num1 >
num2 True
>>> str1 >
str2 True
ASSIGNMENT QUESTIONS
24. Consider the value of x variable to predict
the output of following expression
x=10
a)x+=100
b)x-=10
c)x/=2
c) x>10
d)x<10
e) x*=2
f) x**=2
24. Write a python program to declare 3
variables with 10, 20,30 values display their
addition , subtraction and multiplication
25. Write a python program to create a
basic calculator
26. Write a python statement to take user’s name
and display it
27. Write a python statement to get your
favourite teacher name
28. Write a python statement to take user’s
favourite destination for outstation trip
29. Write a python program to display the folowing
: age=int(input(“Enter your age”))
if age>=18:
print(“You are not eligible for voting”)
LOOPS
30. Display 1 to 10 no.s through python program
31. Write a python program to obtain starting number
and ending number of a number series . Display
these series of number using loop
32. Write a python program to display table of 5
33. Write a python program to obtain a number
from user to display using loop
34. Write a python program to repeat the string
“GOOD MORNING” n times . Here n is an
integer entered by the user’s
35. Find the output of following program
segments a) for I in range (20,30,2)
print(i)
b) country= ‘INDIA’
for i in country
print(i)
c) for i in range ‘INFORMATICS
PRACTICES’ print(2)
d) x=2
for i in ‘Hello’
print(x*x)
e) x=5
for i in range
print(x*i)
NESTED IF AND IF ELSE
STATEMENT(PRACTICE
QUESTIONS)
36. Write a python program to check English
alphabets giving by the user and display it is a
vowel if the alphabet is a ,i,e,o,u . Give provision of
taking alphabet in any case
37. Write a program to accept an integer and display it
corresponding day of weeks i.e. 1 for MONDAY , 2 for
TUESDAY and so on . If the number is not in 1to7 it should
display a message “Invalid day number”.
38. Write a program to find out whether a given year
is leap year
39. Write a python program to find out whether
a number is odd or even
40. Write a program to calculate simple interest
using formula SI=P*R*T/100 . Rate will be 5% if
the principal amount is less than 25000 otherwise
rate will 8%.
METHODS OF LIST
1. len()
2.list()
3.Append()
>>> list1 = [10,20,30,40]
>>> list1.append(50)
>>> list1 [10, 20, 30, 40, 50]
>>> list1 = [10,20,30,40]
>>> list1.append([50,60])
>>> list1 [10, 20, 30, 40, [50, 60]
4.Extend
>>> list1 = [10,20,30]
>>> list2 = [40,50]
>>> list1.extend(list2)
>>> list1 [10, 20, 30, 40, 50]
5. Count
>>> list1 = [10,20,30,10,40,10]
>>> list1.count(10)
3
>>> list1.count(90)
0
6. Find
>>> list1 = [10,20,30,20,40,10]
>>> list1.index(20)
1
>>> list1.index(90)
ValueError: 90 is not in list
7. Remove
>>> list1 = [10,20,30,40,50,30]
>>> list1.remove(30)
>>> list1 [10, 20, 40, 50, 30]
>>> list1.remove(90)
ValueError:list.remove(x):x not in list
8. Pop
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop(3)
40
>>> list1 [10, 20, 30, 50, 60]
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop()
60
>>> list1 [10, 20, 30, 40, 50]
9.Reverse
>>> list1 = [34,66,12,89,28,99]
>>> list1.reverse()
>>> list1 [ 99, 28, 89, 12, 66, 34]
>>> list1 = [ 'Tiger' ,'Zebra' , 'Lion'
, 'Cat' ,'Elephant' ,'Dog']
>>> list1.reverse()
>>> list1 ['Dog', 'Elephant', 'Cat', 'Lion',
'Zebra', 'Tiger']
10. Sort
>>>list1 = ['Tiger','Zebra','Lion',
'Cat', 'Elephant' ,'Dog']
>>> list1.sort()
>>> list1 ['Cat', 'Dog', 'Elephant', 'Lion',
'Tiger', 'Zebra']
>>> list1 = [34,66,12,89,28,99]
>>> list1.sort(reverse = True)
>>>list1 [99,89,66,34,28,12]
11. Sorted
>>>list1 = [23,45,11,67,85,56]
>>> list2 = sorted(list1)
>>> list1 [23, 45, 11, 67, 85, 56]
>>> list2 [11, 23, 45, 56, 67, 85]
12. Min()
>>> list1 = [34,12,63,39,92,44]
>>> min(list1)
12
13.Max()
>>> max(list1)
92
14. Sum()
>>> sum(list1)
284
METHODS OF DICTIONARY
1. len()
>>> dict1 = {'Mohan':95,'Ram':89, 'Suhel':92,
'Sangeeta':85}
>>> len(dict1)
4
2.keys()
>>> dict1 = {'Mohan':95, 'Ram':89,
'Suhel':92, 'Sangeeta':85}
>>> dict1.keys()
dict_keys(['Mohan', 'Ram', 'Suhel', 'Sangeeta'])
3.values()
>>> dict1 = {'Mohan':95, 'Ram':89,
'Suhel':92, 'Sangeeta':85}
>>> dict1.values()
dict_values([95, 89, 92, 85])
4.items()
>>> dict1 = {'Mohan':95, 'Ram':89,
'Suhel':92, 'Sangeeta':85}
>>> dict1.items() dict_items([( 'Mohan', 95), ('Ram',
89), ('Suhel', 92), ('Sangeeta', 85)])
5.get()
>>> dict1 = {'Mohan':95, 'Ram':89,
'Suhel':92, 'Sangeeta':85}
>>> dict1.get('Sangeeta')
85
>>> dict1.get('Sohan’)
>>>
6. Update
>>> dict1 = {'Mohan':95, 'Ram':89,
'Suhel':92, 'Sangeeta':85}
>>> dict2 = {'Sohan':79,'Geeta':89}
>>> dict1.update(dict2)
>>> dict1 {'Mohan': 95, 'Ram': 89, 'Suhel':
92, 'Sangeeta': 85, 'Sohan': 79, 'Geeta': 89}
7. Clear()
>>> dict1 = {'Mohan':95,'Ram':89,
'Suhel':92, 'Sangeeta':85}
>>> dict1.clear()
>>> dict1
{}
8. del()
>>> dict1 = {'Mohan':95,'Ram':89,
'Suhel':92, 'Sangeeta':85}
>>> del dict1['Ram']
>>> dict1
{'Mohan':95,'Suhel':92, 'Sangeeta': 85}
STRUCTURED QUERY LANGUAGE
STEPS TO CREATE TABLE
1. create database
2. use database
3. create table
SYNTAX:
CREATE TABLE tablename( attributename1 datatype
constraint, attributename2 datatype constraint, : attributenameN
datatype constraint);
NOW ,WE ARE GOING TO LEARN ABOUT THE COMMANDS
WHICH WILL HELP IN MySQL
Following are the commands
1. CREATE
2.INSERT
3.SELECT
4.ALTER
5.DESCRIBE
6.DROP
1.Create
MySQL > CREATE TABLE STUDENT
( -> RollNumber INT,
-> SName VARCHAR(20),
-> SDateofBirth DATE, );
2. Insert
SYNTAX:
Insert into tablename values
(value1,value2…)
3.Select
Syntax:
SELECT attribute1, attribute2, ...
FROM table_name
WHERE condition
We can select command not for displaying whole table but also
for displaying specific column
1.Retrieve selected column
2.Renaming any column
4.Alter
5.Describe
6.Update
Example 8.1 Create table STUDENT
. mysql> CREATE TABLE
STUDENT ( -> RollNumber INT,
-> SName VARCHAR(20),
-> SDateofBirth DATE);
Query OK, 0 rows affected (0.91 sec)
Example 8.2 To display the name and date of birth
of student with roll number 2, we write the following
query: mysql> SELECT SName, SDateofBirth ->
FROM STUDENT
-> WHERE RollNumber = 1;
| SName | SDateofBirth |
| Atharv Ahuja | 2003-05-15 |
1 row in set (0.03 sec)
Example 8.3 Display names of all employees .
While displaying query result, rename EName as
Name. mysql> SELECT EName AS Name, ->
FROM EMPLOYEE;
Example 8.4 Display all the employees who are
earning more than 34
mysql> SELECT *
-> FROM EMPLOYEE
-> WHERE Salary > 5000
Example 8.5 The following query displays records of all
the employees except Joseph Think and Reflect What
will happen if
mysql> SELECT *
-> FROM EMPLOYEE
-> WHERE NOT EName = Joseph
Example 8.6 The following query displays name of all
those employees who are earning salary between 48
and 600 .
mysql> SELECT Ename
-> FROM EMPLOYEE
-> WHERE Salary>= 48 AND Salary<=600;
Example 8.7 The following query displays details of all
the employees who are working either in DeptId 1, 4
or 5.
mysql> SELECT *
-> FROM EMPLOYEE
-> WHERE DeptId = '1' OR DeptId = '4' OR DeptId
= '5';
Example 8.8 The following query displays details of all
the employees except those working in department
number 1 or 4 .
mysql> SELECT *
-> FROM EMPLOYEE
-> WHERE DeptId NOT IN('1', '5’);
Example 8.9 The following query displays details of
all the employees in ascending order of their salaries.
mysql> SELECT *
-> FROM EMPLOYEE
-> ORDER BY Salary
Example 8.10 The following query displays details of all
the employees in descending order of their salaries.
mysql> SELECT *
-> FROM EMPLOYEE
-> ORDER BY Salary DESC;
Example 8.11 The following query displays details of
all those employees who have not been given a bonus.
This implies that the bonus column will be blank.
mysql> SELECT *
→FROM EMPLOYEE
-> WHERE Bonus IS NULL;
Example 8.12 The following query displays names of all
the employees who have been given a bonus. This
implies that the bonus column will not be blank.
mysql> SELECT EName
-> FROM EMPLOYEE
-> WHERE Bonus IS NOT NULL
Example 8.13 The following query displays details of
all those employees whose name starts with 'K'. mysql>
SELECT *
-> FROM EMPLOYEE
-> WHERE Ename LIKE 'K%';
Example 8.14 The following query displays details of all
those employees whose name ends with 'a'. mysql>
SELECT *
→ FROM EMPLOYEE
-> WHERE Ename LIKE '%a';
Example 8.15 The following query displays details of all
those employees whose name consists of exactly 5
letters and starts with any letter but has ‘ANYA’ after
that. mysql> SELECT *
-> FROM EMPLOYEE
-> WHERE Ename LIKE '_ANYA';
Example 8.16 The following query displays names of
all the employees containing 'se' as a substring in
name. mysql> SELECT Ename
-> FROM EMPLOYEE
-> WHERE Ename LIKE '%se ;
Example 8.17 The following query displays names of
all employees containing 'a' as the second character.
mysql> SELECT EName -> FROM EMPLOYEE - >
WHERE Ename LIKE '_a%';