0% found this document useful (0 votes)
120 views13 pages

Samrat Ashok Technological Institute: Cs/It Department, S.A.T.I Vidisha

This document contains an assignment submission for a Python course. It discusses various Python data types including numeric, list, tuple, dictionary, set, and string types. It also covers Python operators, loops (for, while), conditional statements (if/else), and the break statement. Examples are provided for each concept to demonstrate their usage. The assignment was submitted by Khushi Deshmukh for the CS/IT department at Samrat Ashok Technological Institute.

Uploaded by

K Deshmukh
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)
120 views13 pages

Samrat Ashok Technological Institute: Cs/It Department, S.A.T.I Vidisha

This document contains an assignment submission for a Python course. It discusses various Python data types including numeric, list, tuple, dictionary, set, and string types. It also covers Python operators, loops (for, while), conditional statements (if/else), and the break statement. Examples are provided for each concept to demonstrate their usage. The assignment was submitted by Khushi Deshmukh for the CS/IT department at Samrat Ashok Technological Institute.

Uploaded by

K Deshmukh
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/ 13

SAMRAT ASHOK TECHNOLOGICAL

INSTITUTE

CS/IT DEPARTMENT, S.A.T.I VIDISHA

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. '''

Python has the following data types built-in by


default:

1. Numeric Types: '''

#int data type


x = 20
print(x)
print(type(x))
# EXTRACTING THE TYPE OF DATA TYPE

#float data type

y = 20.5
print(y)
print(type(y))
# EXTRACTING THE TYPE OF DATA TYPE

#LIST DATA TYPE

l = ["utensil", "spoon", "plate"]


print(l)
print(type(l))
# EXTRACTING THE TYPE OF DATA TYPE
#TUPLE DATA TYPE

t = ("BMW", "Mercedes", "Tata")


print(t)
print(type(t))
# EXTRACTING THE TYPE OF DATA TYPE

# DICTIONARY DATA TYPE

d = {"name" : "Gita", "age" : 30}


print(d)
print(type(d))
# EXTRACTING THE TYPE OF DATA TYPE

# SET DATA TYPE


s = {"almond", "cashew", "cherry"}
print(s)
print(type(s))

# EXTRACTING THE TYPE OF DATA TYPE

# STRING DATA TYPE


string = "Have a nice day"
print(string)
print(type(string))

# 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

# using the subtraction operator


res = val1 - val2
print(res)

val1 = 2
val2 = 3

# using the multiplication operator


res = val1 * val2
print(res)

Output:
6
val1 = 3
val2 = 2

# using the division operator


res = val1 / val2
print(res)
Output :
1.5
val1 = 3
val2 = 2

# using the modulus operator


res = val1 % val2
print(res)
Output :
1
val1 = 2
val2 = 3

# using the exponentiation operator


res = val1 ** val2
print(res)
Output :
8

'''QUESTION 3

LISTS-->
Lists are used to store multiple items in a
single variable.
Lists are created using square brackets.

list methods are as follows '''

#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()

#Returns whether two sets have a intersection or


not
setexample.difference()
#Returns a set containing the difference between
two or more sets

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.'''

stationary = ["paper", "pencil", "ballpen"]


for x in stationary:
print(x)
#With the break statement we can stop the loop
before it has looped through all the items:

fruits = ["paper", "pencil", "ballpen"]


for x in fruits:
print(x)
if x == "pencil":
#IF X = PENCIL IS REACHED THE LOOP IS STOPPED
break

#With the continue statement we can stop the


current iteration of the loop, and continue with
the next:
fruits = ["paper", "pencil", "ballpen"]
for x in fruits:
if x == "paper":

#HERE IF X = PAPER IS REACHED THE CURRENT


ITERATION STOPS ONLY.
continue
print(x)

'''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

'''IF-ELSE CONDITIONAL STATEMENT'''


a = 200
b = 33
if b > a:

# IF CONDITION IS FULFILLED THEN THE STATEMENT


WOULD BE PRINTED
print("b is greater than a")

elif a == b:
print("a and b are equal")
#ELSE THIS WOULD BE PRINTED
else:
print("a is greater than b")

'''In this example a is greater than b, so the


first condition is not true,
also the elif condition is not true, so we go to
the else condition and print to screen that "a is
greater than b".'''

# Python program to demonstrate


# decision making
#Nested if else loop

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

print("Out of for loop" )


print()

i = 0

# Using while loop


while True:
print(s[i])

# break the loop as soon it sees 'e'


# or 's'
if s[i] == 'e' or s[i] == 's':
break
i += 1

You might also like