Header Ads Widget

Ticker

6/recent/ticker-posts

Python Syllabus And Assignments

 Syllabus


Assignment:-1

1.What is list in Python? Demonstrate use of any three methods of list. 

A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements,lists are defined by having values between square brackets [ ] .

List Method 1) sort():- method sorts the list ascending by default.

cars = ['Ford', 'BMW', 'Volvo']

cars.sort()

print(cars)


Output:-

['BMW', 'Ford', 'Volvo']

List Method 2) append() :- appends an element to the end of the list.

a = ["apple", "banana", "cherry"]

b = ["Ford", "BMW", "Volvo"]

a.append(b)

print(a)

 
Output:-

['apple', 'banana', 'cherry', ['Ford', 'BMW', 'Volvo']]

List Method 3)  clear() :- removes all the elements from a list.

fruits = ["apple", "banana", "cherry"]

fruits.clear()

print(fruits)


Output:-

[]

2 Give the syntax and significance of string functions: title() and strip(). 

The title() method returns a string where the first character in every word is upper case. Like a header, or a title.If the word contains a number or a symbol, the first letter after that will be converted to upper case.

Syntax:-
string.title()

The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove).

Syntax:-
string.strip(characters)

3 What is lambda function in python? Explain with any one example.

A lambda function is a small anonymous function.
A lambda function can take any number of arguments,but can only have one expression.
Syntax
lambda arguments : expression
The expression is executed and the result is returned

Example:-lambda function that adds 10 to the number passed in as an argument

x = lambda a : a + 10
print(x(5)) 

Output:-

15

4 Is tuple mutable? Demonstrate any two methods of tuple.

No,tuple has immutable nature i.e., tuple can’t be changed or modified after its creation.

Method 1) The count() method returns the number of times a specified value appears in the tuple.

Syntax
tuple.count(value)

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.count(7)

print(x)


Output:-

2

Method 2) The index() method finds the first occurrence of the specified value.

The index() method raises an exception if the value is not found.

Syntax

tuple.index(value)

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)

x = thistuple.index(1)

print(x)


Output:

0

5 What is dictionary in Python? Explain with an example.

Dictionary is a collection which is unordered,changeable and indexed. No duplicate members.dictionary is as simple as placing items inside curly braces {}separated by comma.

An item has a key and the corresponding value expressed as a pair, key: value.

# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary with Integer Keys
Dict = {1: 'abc', 2: 'pqr', 3: 'xyz'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict) 

Output:-

Empty Dictionary: {} Dictionary with the use of Integer Keys: {1: 'abc', 2: 'pqr', 3: 'xyz'}

6.How append() and extend() are different with reference to list in Python?

append()
extend()
The element passed as an argument is appended to the end of the list
Each element of the iterable passed as an argument gets appended to the end of the list.
An iterable passed as an argument appends without any change as a single element to the end of the list.
An iterable passed as an argument will append each of its elements to the end of the list
Length of the list increases by 1

Length of the list increases by the number of elements in the iterable.



7 Is String a mutable data type? Also explain the string operations length, indexing and slicing in detail with an appropriate example.

String is an example of an immutable type.

Index Operator:The indexing operator (Python uses square brackets to enclose the index) selects a single character from a string. The characters are accessed by their position or index value. For example, in the string shown below, the 14 characters are indexed left to right from postion 0 to position 13.


place = "Saskatoon Sask"
some_char = place[4]
print(some_char)

last_char = place[-1]
print(last_char)


Output:-

a k

Length:- The len function, when applied to a string, returns the number of characters in a string.

fruit = "Banana"
print(len(fruit))

Output:-

6

The Slice Operator:A substring of a string is called a slice. Selecting a slice is similar to selecting a character:

places = "Regina, Estevan, and Melville"
print(places[0:6])
print(places[8:15])
print(places[21:29])


Output:-
Regina Estevan Melville

8 Differentiate filter and map in Python using example.

map(): Applies a function to every item of an iterable.

filter(): Returns all elements of an iterable for which a function is true.

Multiplies each element by 2 print(list(map(lambda x: x*2, [1,2,3,4])))

OUTPUT: [2, 4, 6, 8]

Returns all elements greater than 2 print(list(filter(lambda x: x>2, [1, 2, 3, 4])))

OUTPUT: [3, 4]

9 Can we add and delete element in set? Explain it in detail. Explain any 2 methods with example.

Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.

insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective position.

remove() :- This function is used to delete the first occurrence of number mentioned in its arguments.

# Python code to demonstrate the working of
# insert() and remove()

# initializing list
lis = [2, 1, 3, 5, 3, 8]

# using insert() to insert 4 at 3rd pos
lis.insert(3, 4)

# displaying list after inserting
print("List elements after inserting 4 are : ", end="")
for i in range(0, len(lis)):
  print(lis[i], end=" ")
  
print("\r")

# using remove() to remove first occurrence of 3
# removes 3 at pos 2
lis.remove(3)

# displaying list after removing
print ("List elements after removing are : ", end="")
for i in range(0, len(lis)):
  print(lis[i], end=" ")


Output:-

List elements after inserting 4 are : 2 1 3 4 5 3 8 List elements after removing are : 2 1 4 5 3 8

10 Discuss any 2 built in modules of Python

Math Module:-
Some of the most popular mathematical functions are defined in the math module. 
• These include trigonometric functions, representation functions, logarithmic functions, angle conversion functions, etc. 
• In addition, two mathematical constants are also defined in this module. – pi and Euler’s number e  

 Pi is a well-known mathematical constant, which is defined as the ratio of the circumference to the diameter of a circle and its value is 3.141592653589793. 

>>> import math
>>> math.pi
3.141592653589793
Another well-known mathematical constant defined in the math module is e. It is called Euler's number and it is a base of the natural logarithm. Its value is 2.718281828459045.

>>> math.e 
2.718281828459045 
random module

Python’s standard library contains random module which defines various functions for handling randomization. Python uses a pseudo-random generator based upon Mersenne Twister algorithm that produces 53-bit precision floats. Functions in this module depend on pseudo-random number generator function random() which generates a random float number between 0.0 and 1.0.

random.random(): Returns a random float number between 0.0 to 1.0. The function doesn’t need any arguments

>>> import random
>>> random.random()
0.755173688207591

Other functions in random module are described here:

random.randint(): Returns a random integer between the specified integers

>>> import random
>>> random.randint(1,100)
58
>>> random.randint(1,100)
91

random.randrange(): Returns a random element from the range created by start, stop and step arguments. The start , stop and step parameters behave similar to range() function.

>>> random.randrange(1,10)
2
>>> random.randrange(1,10,2)
3
>>> random.randrange(0,101,10)
40

Post a Comment

0 Comments