Samrat Ashok Technological Institute: Cs/It Department, S.A.T.I Vidisha
Samrat Ashok Technological Institute: Cs/It Department, S.A.T.I Vidisha
INSTITUTE
CS-1846
PYTHON ASSIGNMENT
Submitted By
Khushi Deshmukh
CSE-A
0108CS211066
''' QUESTION 1
'''Data types are the classification or
categorization of data items.
It represents the kind of value that tells what
operations can be performed on a particular data.
Since everything is an object in Python
programming,
data types are actually classes and variables are
instances (object) of these classes. '''
y = 20.5
print(y)
print(type(y))
# EXTRACTING THE TYPE OF DATA TYPE
#OPERATORS
#USING THE GREATER THAN OPERATOR
a = 33
b = 200
if b > a:
print("b is greater than a")
# using the addition operator
res = val1 + val2
print(res)
val1 = 2
val2 = 3
val1 = 2
val2 = 3
Output:
6
val1 = 3
val2 = 2
'''QUESTION 3
LISTS-->
Lists are used to store multiple items in a
single variable.
Lists are created using square brackets.
#EXAMPLE
fruits = ['pineapple', 'blackberry', 'cherry']
#SLICING RANGE FOR LOOP ALL TYPES
fruits.append("orange")
#APPEND IS USED TO ADD AN ELEMENT
fruits.clear()
#IT WILL CLEAR THE LIST
fruits.count()
#COUNT METHOD IS USED TO COUNT THE DUPLICATE
ELEMENTS
fruits.remove("pineapple")
#IT WILL REMOVE THE DESIRED ELEMENT
fruits.insert(1, "orange")
#IT WILL INSERT THE DESIRED ELEMENT INTO THE
DESIRED INDEX
fruits.sort()
# IT WILL SORT THE LIST
fruits.pop(1)
#POP METHOD IS USED TO REMOVE 1 st ELEMENT
fruits.reverse()
#Reverses the order of the list
fruits.clear()
#Removes all the elements from the list
fruits.extend()
#Add the elements to the end of the current list
'''TUPLES-->
Tuples are used to store multiple items in a single
variable.
A tuple is a collection which is ordered and
unchangeable.
Tuples are written with round brackets.'''
#EXAMPLE
Employee = ("Arjun","Rahul","Arju","Arjun")
Employee.index("Rahul")
#INDEX METHOD IS USED TO FIND INDEX OF GIVEN KEY
Employee.count()
#COUNT METHOD IS USED TO COUNT THE DUPLICATE
ELEMENTS
Employee.len()
#LENGTH METHOD IS USED TO FIND THE NUMBER OF
ELEMENTS
Employee.append("Bhaskar")
#APPEND IS USED TO ADD AN ELEMENT
Employee.add("Shyam")
#ADD METHOD IS USED TO ADD ELEMENT BUT IT CREATES
A NEW COPY
print(Employee)
'''SET-->
Sets are used to store multiple items in a single
variable.
Set is one of 4 built-in data types in Python used
to store collections of data,
the other 3 are List, Tuple, and Dictionary, all
with different qualities and usage.
A set is a collection which is unordered,
unchangeable*, and unindexed.'''
#EXAMPLE
setexample = {"carrot","tomato","Radish","Onion"}
print(setexample)
setexample.len()
#LENGTH METHOD IS USED TO FIND THE NUMBER OF
ELEMENTS
setexample.add("Pepper")
#ADD METHOD IS USED TO ADD ELEMENT BUT IT CREATES
A NEW COPY
setexample.discard("carrot")
# THE CHOSEN ELEMENT WILL BE DISCRDED
setexample.pop(1)
#POP METHOD IS USED TO REMOVE 1 st ELEMENT
setexample.clear()
#IT WILL CLEAR THE LIST
setexample.remove("onion")
#IT WILL REMOVE THE DESIRED KEY
setexample.isdisjoint()
setexample.copy()
#Returns a copy of the set
'''DICTIONARY-->
Dictionaries are used to store data values in
key:value pairs.
A dictionary is a collection which is ordered*,
changeable and do not allow duplicates.'''
#EXAMPLE
dict = {
"brand": "Godrej",
"type": "Almirah",
"year": 2016
}
print(dict)
print(dict["year"])
#METHODS
dict.clear()
#IT WILL CLEAR THE LIST
dict.copy()
#IT WILL COPY INTO NEW DICT
dict.get("brand")
#IT WILL GET THE VALUES
dict.pop(2)
#POP METHOD IS USED TO REMOVE 1 st ELEMENT
dict.values()
#Returns a list of all the values available in a
given dictionary
dict.items()
#Return the list with all dictionary keys with
values
dict.setdefault()
#Returns the value of a key if the key is in the
dictionary else inserts the key with a value to the
dictionary
dict.update()
#Updates the dictionary with the specified key-
value pairs
dict.fromkeys()
#Returns a dictionary with the specified keys
'''QUESTION 2
FOR LOOP -->
A for loop is used for iterating over a sequence.
With the for loop we can execute a set of
statements, once for each item in a list, tuple,
set etc.'''
'''WHILE LOOP-->
With the while loop we can execute a set of
statements as long as a condition is true.'''
i = 1
while i < 6:
# LOOP WILL EXECUTE TILL I < 6 .
print(i)
if i == 3:
# IF I=3 IS ACHIEVED IT WILL BREAK THE LOOP
break
i + = 1
elif a == b:
print("a and b are equal")
#ELSE THIS WOULD BE PRINTED
else:
print("a is greater than b")
i = 20;
if (i < 15):
print ("i is smaller than 15")
print ("i'm in if Block")
else:
print ("i is greater than 15")
print ("i'm in else Block")
print ("i'm not in if and not in else Block")
#conditional statement
#Break statement in Python
'''Break Statement is a loop control statement that
is used to terminate the loop.
As soon as the break statement is encountered from
within a loop,
the loop iterations stop there,
and control returns from the loop immediately to
the first statement after the loop. '''
s = 'Khushi'
# Using for loop
for letter in s:
print(letter)
# break the loop as soon it sees 'e'
# or 's'
if letter == 'e' or letter == 's':
break
i = 0