0% found this document useful (0 votes)
14 views57 pages

Built InFunctions

Uploaded by

karthika vasu
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)
14 views57 pages

Built InFunctions

Uploaded by

karthika vasu
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/ 57

മലയാളത്തിൽ

https://youtube.com/playlist?list=PL-Xzhg55p_hRislAkZAoxknxnhcar1oF4

A11- Python Programming


▪ Introduction to python, features, IDLE, python interpreter, Writing and executing
python scripts,
▪ comments, identifiers, keywords, variables, data type, operators, operator
precedence and associativity
▪ statements, expressions, user inputs, type function, eval function, print function
1. What is Python? What are the benefits of using Python?
Ans: https://youtu.be/1gOk4LwBYZw
▪ Python is a high-level, interpreted, general-purpose, dynamic programming
Language developed by Guido van Rossum .
▪ The language places strong emphasis on code reliability and simplicity so that
the programmers can develop applications rapidly
▪ Most programs in Python require considerably less number of lines of code to
perform the same task compared to other languages like C .
▪ Simple and Easy to learn
▪ It’s Opensource and Free
▪ Python is famous for being the “batteries are included” language.
▪ There are over 300 standard library modules and innumerous set of third
party libraries.
2. What is an identifier? Give examples [Nov 2020]
Ans:
▪ A Python identifier is the name given to a variable, function, class, module or other object.
▪ An identifier can begin with an alphabet (A – Z or a – z), or an underscore (_) and can
include any number of letters, digits, or underscores. Spaces are not allowed.
Ex: myname, My_Name,MyName ,sum, a1 →valid identifiers
3sd, my name, myname* →invalid identifiers
3. How do you write comments in Python?
▪ Comments in python can be used to
▪ Explain python code
▪ Make the code more readable
▪ Prevent execution when testing code
▪ Comments are ignored by Python interpreter
▪ Python uses the hash character (#) for comments. Putting # before a text ensures that the
text will not be parsed by the interpreter.
# Prints the words Hello Python
print(“Hello Python”)
3. What you meant by operator precedence?
Ans:https://youtu.be/HLOrEUvoOIo
▪ When an expression has two or more operators, we need to identify the correct
sequence to evaluate these operators.it is known as operator precedence.
▪ In python highest priotity is for exponential operator(**), then
multiplication,divison, followed by addition ,subtraction,etc.
▪ Lowest priority is for logical operators NOT,OR,AND
4. What is eval( ) in python? What is its syntax? [Nov 2020]
Ans
• eval() is a python built in function that parses the expression argument and
evaluate it as a python expression and runs python expression(code) within the
program.
• Eval() function consider the “string” as an expression and returns the result as
integer
• print(eval(“5==5”)) output:1
• Print(eval(“8+4-2*3”) output:6
5.What is a byte code? [Nov 2020]
Ans
Byte code is an intermediate code between the source code and machine code.
It is a low-level code that is the result of the compilation of a source code
which is written in a high-level language
It is processed by a virtual machine like python virtual machine(PVM)
It is compiled to run on virtual machine, any system having JVM can run it
irrespective of their operating system.
That’s why languages like Java ,python etc are platform-independent.
Byte code is referred to as a Portable code.
In python, bytecode is saved in the file named same as the source file but with
a different extension named as “pyc”.
6.Explain output statements in python[Nov 2020]
Ans https://youtu.be/RQRjCytWf4Y
print() is the widely used output statement in python
• print() prints the specified message to the screen or other standard output
device
• The message can be a string , or any other object, the object will be
converted into a string before written to the screen
• print("Hello", "how are you?")
• x = ("apple", "banana", "cherry")
• print(x)
7.Give the membership operator in python with example[Nov 2020]
Ans https://youtu.be/HLOrEUvoOIo
• in and not in are called membership operators
• These operators are used to check an item or an element that is part of a
string, a list or a tuple.
• A membership operator reduces the effort of searching an element in the list
• Suppose, x stores a value 20 and y is the list containing items 10, 20, 30, and
40.
8.Explain different relational operators in python [Nov 2020]
• These operators are used to compare values.
• Relational operators are also called Comparison operators .
• The result of these operators is always a Boolean value, that is, either true or
false
9.Write a program to find the area and circumference of a circle. Prompt user for
input [Nov 2020]
Ans
r=float(input("Input Radius : "))
area=3.14*r*r
perimeter=2*3.14*r
print("Area of Circle: ",area)
print("Perimeter of Circle: ",perimeter)
10. What are the key features of Python
https://youtu.be/1gOk4LwBYZw
11. Write a short note on Python IDLE.
12.What are the different data types available in Python?
https://youtu.be/vBEmP-b-50A
13.Explain different data types available in Python with example? [Nov 2020]
https://youtu.be/vBEmP-b-50A
14. Explain different types of operators in Python in detail.
https://youtu.be/HLOrEUvoOIo
▪ Boolean expressions, Simple if statement, if-elif-else statement, compound
boolean expressions, nesting, multi way decisions.
▪ Loops: The while statement, range functions, the for statement, nested loops,
break and continue statements, infinite loops.
▪ https://youtube.com/playlist?list=PL-Xzhg55p_hRislAkZAoxknxnhcar1oF4
15. What is the purpose of break statement in Python
Ans
break statement exits from the loop and transfers the execution from the loop
to the statement that is immediately following the loop.
▪ for letter in ‘Python’:
if letter == ‘h’: OUTPUT
Current Letter : P
break Current Letter : y
print (‘Current Letter :’, letter) Current Letter : t
16. Write the syntax of for loop[Nov 2020]
▪ Ans:
▪ The for loop is used to iterate over a sequence
▪ It goes through the elements in any ordered sequence list, i.e., string, lists,
tuples, the keys of dictionary and other iterables.
▪ Syntax
>>>for x in y :
Block 1
else: # Optional Output:
Current Letter : P
Block 2 # excuted only when the loop exits normally Current Letter : y
Current Letter : t
>>>for letter in 'python': Current Letter : h
print ('current letter:',letter) Current Letter : o
Current Letter : n
17.What are loop control statements[Nov 2020]
▪ Ans:
▪ Loop control statements can be used to alter or control the flow of loop
execution based on specified conditions.
▪ Python loop statements allows to execute a block of code repeatedly.
▪ But sometimes, we want to exit a loop completely or skip specific part of a
loop when it meets a specified condition. It can be done using loop control
mechanism.
▪ Break and continue statements are commonly used for this
▪ The break statement exits from the loop and transfers the execution from the
loop to the statement that is immediately following the loop.
▪ The continue statement causes execution to immediately continue at the start
of the loop, it skips the execution of the remaining body part of the loop
17.What are loop control statements
▪ Ans: OUTPUT
Current Letter : P
▪ for letter in ‘Python’: Current Letter : y
Current Letter : t
if letter == ‘h’:
break
print (‘Current Letter :’, letter)
▪ for letter in ‘Python’:
OUTPUT
Current Letter : P
if letter == 'h': Current Letter : y
continue Current Letter : t
Current Letter : o
print 'Current Letter :', letter Current Letter : n
18. Explain range() [Nov 2020]
▪ Ans:
▪ The range() function is a built-in function in Python that helps to iterate over
a sequence of numbers. It works only with integers
▪ Syntax:
▪ range(start,stop,step)
▪ range(3,40, 5) Output
0
▪ [3, 8, 13, 18, 23, 28, 33, 38] 1
▪ x = range(6) 2
3
for n in x: 4
print(n) 5
19.Write a Python program to check given number is Prime or not[Nov 2020]
number = int(input("Enter number: "))

if number > 1:
for i in range(2,(number//2)+1):
if (number % i) == 0:
print(number,"is not a prime number")
break
else:
print(number,"is a prime number")
else:
print(number,"is not a prime number")
20.Python Program to find Sum of all Even and Odd Numbers upto a number specified
by the user[Nov 2020]
Ans
maximum = int(input(" Please Enter the Maximum Value : "))
even_total = 0
odd_total = 0 Output
for number in range(1, maximum + 1): Please Enter the Maximum Value : 8
The Sum of Even Numbers from 1 to 8 is 20
if(number % 2 == 0): The Sum of Odd Numbers from 1 to 8 is 16
even_total = even_total + number
else:
odd_total = odd_total + number

print("The Sum of Even Numbers from 1 to “, maximum,”is ”,even_total)


print("The Sum of Odd Numbers from 1 to “, maximum,”is ”,odd_total)
21.What is a loop? Explain different looping statement in Python
https://youtu.be/eTVtf6-NfDg
22. Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N[Nov 2020]
Ans
n=int(input("Enter the number of terms: "))
sum=0
for i in range(1,n+1):
sum=sum+(1/i)
print("The sum of series is",round(sum,2))
▪ Functions, built-in functions, mathematical functions, date time functions,
random numbers,
▪ writing user defined functions, composition of functions, parameter and
arguments, default parameters,
▪ function calls, return statement, using global variables, recursion.
23. Write a short note on global keyword in Python
▪ Global keyword is a keyword that allows a user to modify a variable outside
of the current scope.
▪ It is used to create global variables from a non-global scope i.e inside a
function.
▪ If a variable is assigned a value anywhere within the function’s body, it’s
assumed to be a local unless explicitly declared as global.
▪ We Use global keyword to use a global variable inside a function.There is no
need to use global keyword outside a function
▪ Global keyword is used inside a function only when we want to do
assignments or when we want to change a variable. Global is not needed for
printing and accessing.
23. Write a short note on global keyword in Python # Python program showing to modify
# Python program showing no need to # a global value without using global
# use global keyword for accessing # keyword
# a global value
a = 15
# global variable # function to change a global value
a = 15 def change():
b = 10 a=a+5
print(a)
# function to perform addition
change()
def add(): Output:
c=a+b UnboundLocalError: local variable 'a'
print(c) referenced before assignment
# calling a function
add()
▪ Output:
▪ 25
23. Write a short note on global keyword in Python
#With global keyword
# Python program to modify a global
# value inside a function
x = 15
def change():
# using a global keyword
global x
x=x+5
print("Value of x inside a function :", x)
change()
print("Value of x outside a function :", x)
Output:
Value of x inside a function : 20
Value of x outside a function : 20
24. What is Random Number Generator in Python?
▪ Python defines a set of functions that are used to generate or manipulate
random numbers through the random module.
▪ Functions in the random module rely on a pseudo-random number generator
function random(), which generates a random float number between 0.0 and
1.0.
▪ These particular type of functions is used in a lot of games, lotteries, or any
application requiring a random number generation.
25.What is default parameter in Python?
In default parameters, we can assign a value to a parameter at the time of function
definition.
▪ This value is considered the default value to that parameter.
▪ When we call a function and pass an argument to the parameter that has a default
value, the function will use that argument instead of the default value.
▪ If we do not provide a value to the parameter at the time of calling, it will not
produce an error. Instead it will pick the default value and use it
▪ def function_name(param1, param2=value2, param3=value3, ...):
def print_info(name, age=35):
# function calling
... print “Name: “,name >>>print_info(name=’john’);
... print “Age: “, age Name: john # Output
... return Age: 35 # Output
26.What are the advantages of function[Nov 2020]
Ans
▪ Functions are self-contained programs that perform some particular tasks.
▪ Once a function is created by the programmer for a specific task, this function can
be called anytime to perform that task.
▪ There are many built-in functions provided by Python such as dir(), len(), abs(), etc.
▪ Users can also build their own functions, which are called user-defined functions.
▪ There are many advantages of using functions:
a) They reduce duplication of code in a program.
b) They break the large complex problems into small parts.
c) They help in improving the clarity of code (i.e., make the code easy to
understand).
d) A piece of code can be reused as many times as we want with the help of functions
27.Define positional arguments in python[Nov 2020]
Ans
▪ Positional argument means that the argument must be provided in a correct
position in a function call.It is also known as Required arguments
▪ When we assign the parameters to a function at the time of function
definition, at the time of calling, the arguments should be passed to a function
in correct positional order; furthermore, the number of arguments should
match the defined number of parameters
def print_lines(str)
print str
return;
print_lines() #functioncall will produce error because number of parameters
not matching
28.How function call is done in python[Nov 2020]
Ans
▪ A function is called using the function name, followed by a pair of parentheses (()).
▪ Any input parameters or arguments are to be placed within these calling
parentheses.
▪ All parameters (arguments) which are passed in functions are always passed by
reference in Python.
def mult(a,b):
... mul = a*b
... return mul
# Now calling the function here
>>> m = mult(4,3) # calling the mult function
>>>print(m)
>>>12 #output
29.Define local variable[Nov 2020]
Ans
▪ A variable declared inside the function is called local variable
▪ We can access a local variable inside but not outside the function.
▪ def sum()
a=4 #localvariable
s=a+1 Output
return s 5
res=sum() NameError: name 'a' is not defined
print(res)
print(a)
30. Explain recursion in Python with suitable examples?
Ans
▪ If a function, procedure or method calls itself, it is called recursive.
▪ In Python, also it is possible that a function calls itself.
▪ def fact_rec(x):
“Recursive function to find the factorial of an integer’’
if x == 1:
return 1
else:
return(x * fact_rec(x-1))
>>> fact_rec(4)
24 # Output
>>> fact_rec(10)
3628800 # Output
31.Write a program to add two numbers using python[Nov 2020]
Ans
▪ #program to add two numbers
def add_num(a,b):
sum=a+b
return sum
x=int(input(“enter first number:”)) Output
y=int(input(“enter second number:”)) enter first number:25
enter second number:45
res=add_num(x,y) sum of the numbers is 70
print(“sum of the numbers is”,res)
32.Describe the syntax for the following functions and explain with an
example
1) abs() 2)max() 3)pow() 4)len() 5)sort() [Nov 2020]
Ans
1) abs() is used to return the absolute value of a number
Syntax: abs(number)
number: Can be an integer, a floating-point number or a complex number
Ex:float = -54.26
Print('Absolute value of float is:', abs(float))
Absolute value of float is:,54.26 #output
2)max()
▪ The max() function returns the item with the highest value, or the item with
the highest value in an iterable.
▪ If the values are strings, an alphabetically comparison is done.
▪ Syntax
▪ max(n1, n2, n3, ...)
▪ Or:
▪ max(iterable)
▪ x = max(5, 10) #10
▪ x = max("Mike", "John", "Vicky") #Vicky
▪ a = (1, 5, 3, 9)
▪ x = max(a)
3)pow()
python pow() function can be used to derive the power of variable x to the variable
y.
Syntax: pow(a,b)
num=pow(2,3) #8
4)len()
len() return the length (the number of items) of an object.
syntax:len(object).The argument may be a sequence (such as a string, bytes, tuple,
list, or range) or a collection (such as a dictionary, set)
len(“computer”) #8
a=[“apple”,”banana”,”cherry”]
len(a) #3
b=(32,2.4,5,67,8)
len(b) #5
5)sort()
The sort() method sorts the list ascending by default.
You can also make a function to decide the sorting criteria(s).
Syntax: list.sort(reverse=True|False, key=myFunc)
reverse Optional. reverse=True will sort the list descending. Default is reverse=False
key Optional. A function to specify the sorting criteria(s)

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


cars.sort(reverse=True)
['Volvo', 'Ford', 'BMW’] #output
33) What is a function? Explain different functions available in Python with
suitable
examples
Ans
https://youtu.be/0xje52d4nps
https://youtu.be/KO4sCcsD8EM
34)Write a python program using function to find the value of nPr =n!/(n-1)! Without
using built in factorial() function [Nov 2020]
Ans
import math
def fact(n):
if (n <= 1):
return 1
return n * fact(n - 1)
Output
def nPr(n, r):
enter value of n:5
return math.floor(fact(n) / fact(n - r)) enter value of r:3
n=int(input("enter value of n:")) 5 P 3 = 60
r=int(input("enter value of r:"))
print(n, "P", r, "=", nPr(n, r))
▪ String and string operations,
▪ List- creating list, accessing, updating and deleting elements from a list, basic
list operations.
▪ Tuple- creating and accessing tuples in python, basic tuple operations.
▪ Dictionary, built in methods to access, update and delete dictionary values.
▪ Set and basic operations on a set
35. What is the difference between list and tuples in Python?
Ans
▪ In tuples items are separated by commas and enclosed in parenthesis()
▪ In lists items are separated by commas and enclosed in square brackets[]
▪ In lists items are mutable
▪ In tuples immutable
36. Write a Python program to calculate the length of a string?
s=input(“enter the string:”)
print(“length of string is”,len(s))
37. What is a set in Python?
Ans
▪ An unordered collection of data known as Set. A Set does not contain any
duplicate values or elements. The elements in the set are immutable(cannot be
modified) but the set as a whole is mutable.
▪ There is no index attached to any element in a python set. So they do not
support any indexing or slicing operation
▪ set1 = set([1, 2, 4, 8, 5])
▪ Set2=[‘a’,’b’,’c’]
38. Write a function to give the sum of all the numbers in the list?
▪ def sum_list():
numList = [1,2,3,4,5]
added = 0
for item in numList:
added+=item
print(added)
sum_list()
▪ The easiest way to find sum of all elements in any list, tuple or iterable is to use inbuilt sum()
function.The sum() function in Python takes an argument of an iterable (list, tuple, set etc.),
calculates the sum of all its elements and return sum value of type integer.
numList = [1,2,3,4,5]
total = sum(numList)
print(total)
39. What are Python dictionaries?
▪ The Python dictionary is an unordered collection of items or elements.
▪ The dictionary has a key: value pair. Each value is associated with a key.
▪ In dictionary, keys can be of any type.
▪ Dictionary is said to be a mapping between some set of keys and values. Each key is
associated to a value.
▪ The mapping of a key and value is called as a key-value pair and together they are called one
item or element.
▪ A key and its value are separated by a colon (:) between them. The items or elements in a
dictionary are separated by commas and all the elements must be enclosed in curly braces
▪ Ex:
▪ dict = {1:’red’,2:’yellow’,3:’green’}
40. What are different ways of creating strings in python? [Nov 2020]
▪ Strings can be created by enclosing characters inside a single quote or double-quotes.
▪ Even triple quotes can be used in Python but generally used to represent multiline
strings and docstrings.
▪ my_string = 'Hello’ #Hello
▪ my_string = "Hello“ #Hello
▪ my_string = '''Hello’’’ #Hello
▪ # triple quotes string can extend multiple lines
▪ my_string = """Hello, welcome to #Hello, welcome to
Computer Science Hub
Computer Science Hub"""
41. What are lists? [Nov 2020]
▪ List is a sequence of values called items or elements.
▪ The elements can be of any data type.
▪ The list is a most versatile data type available in Python which can be written as a
list of comma- separated values (items) between square brackets.
▪ List are mutable, meaning, their elements can be changed.
▪ In Python programming, a list is created by placing all the items (elements) inside a
square bracket [ ], separated by commas.
▪ It can have any number of items and they may be of different types (integer, float,
string etc.).
▪ my_list = [1, 2, 3]
▪ my_list = [1, "Hello", 3.4]
▪ my_list = [“welcome", [8, 4, 6]]
42. What are the rules for creating keys in a dictionary? [Nov 2020]
1.Each key is associated to a value.
2.A key and its value are separated by a colon (:) between them.
3.The values in a dictionary can be duplicated, but the keys in the dictionary are unique.
4.One key in a dictionary cannot have two values, i.e., duplicate keys are not allowed in the
dictionary; they must be unique.
▪ Whenever duplicate keys are assigned values in a dictionary, the latest value is considered
and stored whereas the previous one is lost.
>>> dict1 = {‘Name’:’John’,’Age’:30,’Name’:’Jinnie’}
>>> print dict1[‘Name’]
Jinnie # Output
5. Keys are immutable, i.e., we can use string, integers or tuples for dictionary keys, but
something like [‘key’] is not valid
>>> dict1 = {[‘Name’]:’John’,’Age’:30}
TypeError: unhashable type: ‘list’ #output
43. How the elements in a string can be accessed using for loop[Nov 2020]
Ans
▪ for loop is the easiest method to access a string
▪ Each time in the -for loop, the next character in the string will be assigned to the loop
variable
▪ The loop halts when the last character is processed.
▪ var=‘banana’
OUTPUT
▪ for char in var: b
print char a
n
a
n
a
44. What is a tuple ? Explain different tuple operations in Python.Ans
https://youtu.be/j2O91InDEaM
37. Write a Python program to remove an item from a set if it is present in the set.
Ans:
s=set([4,5,6,7])
print("original set",s)
a=int(input("enter the item you want to delete:"))
if a in s: Output
s.discard(a) original set {4, 5, 6, 7}
print("new set is:", s); enter the item you want to delete:5
else: new set is: {4, 6, 7}
print("item not present")
46. Write a program code to find the mean and variance from a list of numbers[Nov 2020]
Ans
def mean(data): data=[2,3,5,6,7,8]
n = len(data) print("data is:",data)
mean = sum(data) / n v,m=variance(data)
return mean print("mean of data is:",round(m,3))
def variance(data): print("variance of data is:",round(v,3))
n = len(data)
Output
me=mean(data)
data is: [2, 3, 5, 6, 7, 8]
sum_dev=0
mean of data is: 5.167
for x in data:
variance of data is: 4.472
deviation = (x - me) ** 2
sum_dev=sum_dev+deviation
variance = sum_dev/ n
return variance,me
47. Describe the syntax for the following functions and explain with example[Nov 2020]
1)replace() 2)rstrip() 3)reverse() 4)count() 5)join()
Ans:
1) The replace() method replaces a specified phrase with another specified phrase.
Syntax: string.replace(oldvalue, newvalue, count)
oldvalue Required. The string to search for
newvalue Required. The string to replace the old value with
count Optional. A number specifying how many occurrences of the old value you want to replace. Default is all
occurrences

txt = "one banana red banana, sweet banana."


x = txt.replace(“banana", “apple")
print(x)
one apple red apple, sweet apple. #output
47. Describe the syntax for the following functions and explain with example[Nov 2020]
1)replace() 2)rstrip() 3)reverse() 4)count() 5)join()
Ans:
2) rstrip() returns a copy of the string in which all chars have been stripped from the end
of the string (default whitespace characters).
Syntax
str.rstrip([chars])
chars − chars to be trimmed.
str = " this is string example....wow!!! ";
print(str.rstrip()) #this is string example....wow!!!
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8’) #88888888this is string example....wow!!!
47. Describe the syntax for the following functions and explain with example[Nov 2020]
1)replace() 2)rstrip() 3)reverse() 4)count() 5)join()
Ans:
3) The reverse() method reverses the sorting order of the elements in a list
Syntax
list.reverse()
l=[2,3,5]
l.reverse()
print(l) # [5,3,2]
47. Describe the syntax for the following functions and explain with example[Nov 2020]
1)replace() 2)rstrip() 3)reverse() 4)count() 5)join()
Ans:
4)count() :returns the number of times a specified value appears in a string or a list.
Syntax
string.count(value, start, end) or string.count(value)
list.count(value)
Value - This is the substring whose count in Python is to be found.
Start (Optional) - index value to start the search .default, 0
End (Optional) - index value to end the search. By default, it is the end of the string.
x=“apple”
x.count(‘p’) #2
x=[2,2,4,5,2,52]
x.count(2) #3
47. Describe the syntax for the following functions and explain with example[Nov 2020]
1)replace() 2)rstrip() 3)reverse() 4)count() 5)join()
Ans:
5)join() :The join() method takes all items in an iterable and joins them into one string.
A string must be specified as the separator..
Syntax
string.join(iterable)
myTuple = ("John", "Peter", "Vicky")
x = "#".join(myTuple)
print(x)
Output
John#Peter#Vicky
48. What is a string? Explain different string operations in Python with example
https://youtu.be/x_pBsbtlmK4
48.Write a python program to check for the presence of a key in the dictionary and sum
all its
values [Nov 2020]
Ans
OUTPUT
dic={'a':100,'b':200,'c':300} dictionary: {'a': 100, 'b': 200, 'c': 300}
print("dictionary:",dic) enter the key to be searched:b
x=input("enter the key to be searched:") key is present . Its value is 200
if x in dic.keys(): sum of values of dictionary is: 600
print("key is present.Its value is ",dic[x])
else:
print("key is not present")
print("sum of values of dictionary is:",sum(dic.values()))

You might also like