Python - Preparation Notes
Python - Preparation Notes
o Python was developed by Guido van Rossum in late 80’s and released globally in 1991 by
Python Software Foundation.
o Python is a programming as well as a scripting language.
o This language has various purposes such as developing, scripting, generation, and software
testing.
Python Variable :
o Variable is a container that store value.
o Variable has the capacity to change its value during program execution
o Once an object is assigned to a variable, it can be referred to by that name.
o Python variable declaration does not require explicit mention of data-type as soon as assigning
value automatically concerned type is assigned to it by interpreter
Example:
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
DATA TYPES :
o A Data type indicates which type of value a variable has in a program.
o However a python variables can store data of any data type but it is necessary to identify the
different types of data they contain to avoid errors during execution of program.
o The most common data types used in python are str(string), int(integer) and float (floating-point)
o These statements are used to execute certain blocks of code based on specific conditions.
o These statements help to control the flow of a program, making it behave differently in different
situations based on given conditions.
o These statements otherwise called as decision making statements.
o These statement in python classified into
o Simple if
o if…else
o if….elif ladder
Importance of indentation:
Syntax of If Statement:
if condition:
# Statements to execute if
# condition is true
Example :
if Age>=18 :
Example :
if Age>=18 :
“print(“Major, and Can Vote.”)
else:
Syntax of if…elif:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true
else:
# code to execute if none of the above conditions are true
Example:
if no<0:
print(“Negative Value”)
print(“Single Digit”)
print(“Trible Digit”)
else
1. For Loop
A for loop in Python is used to iterate over a sequence (list, tuple, set, dictionary, and string).
Flowchart:
statement(s)
Example:
print(x)
2. While Loop
The while loop is used to execute a set of statements as long as a condition is true.
Flowchart:
statements
Example:
x =0
while x<5:
print(x)
x+=1
Until the value of x is less than 5, the loop continues and prints the numbers.
Python Functions:
Python Functions is a block of statements that return the specific task.
The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of
writing the same code again and again for different inputs, we can do the function calls to reuse code
contained in it over and over again.
• Built-in library function: These are Standard functions in Python that are available to use.
• User-defined function: We can create our own functions based on our requirements.
After creating a function in Python we can call it by using the name of the functions Python followed by
parenthesis containing parameters of that particular function.
Below is the example for calling def function Python.
fun()
o/p : Welcome to SRM
def pellNo(n):
if n==0:
return 0
elif n==1:
return 1
else:
return (2*pellNo(n-1)+pellNo(n-2))
x=int(input("Enter Pell-Series limit :"))
for i in range(x):
print(pellNo(i),end=" ")
def Lucas(n):
if n==0:
return 2
elif n==1:
return 1
else:
return (Lucas(n-1)+Lucas(n-2))
x = int(input("Enter upper limit :"))
for i in range(x):
print(Lucas(i),end=" ")
# Linear Search
return -1
arr = list(map(int,input("Enter space seperated arr :").split()))
x = int(input("Enter no to be Searched :"))
r = LinrSrch(arr,x)
if r==-1:
print("No not found in this array")
else:
print("No found at location :",r)
Exercise (a)
2. Which of the following functions is used to get the length of a list, string, or dictionary?
a) len()
b) size()
c) length()
d) count()
a) int
b) float
c) str
d) None
a) str()
b) float()
c) int()
d) eval()
min(4, 2, 8, 6)
a) 2
b) 4
c) 6
d) 8
6. Which built-in function is used to iterate over items in an iterable along with their index?
a) map()
b) zip()
c) enumerate()
d) filter()
7. What does the sorted() function return?
a) Using any()
b) Using all()
c) Using filter()
d) Using reduce()
round(3.456, 2)
a) 3.45
b) 3.46
c) 3.50
d) 4
a) list()
b) dict()
c) set()
d) tuple()
Solutions:
2. Which of the following functions is used to get the length of a list, string, or dictionary?
Answer: a) len()
Answer: c) str
Answer: b) float()
min(4, 2, 8, 6)
Answer: a) 2
6. Which built-in function is used to iterate over items in an iterable along with their index?
Answer: c) enumerate()
round(3.456, 2)
Answer: b) 3.46
Answer: c) set()
Exercise (b)
a) define
b) def
c) func
d) function
a) def func_name():
b) function func_name():
c) def func_name:
d) define func_name():
print(add(2, 3))
a) 5
b) None
c) (2, 3)
d) Error
6. What is the scope of a variable defined inside a function?
a) Global
b) Local
c) Universal
d) Dynamic
print(func(3))
a) 3
b) 6
c) Error
d) None
def func(x=[]):
x.append(1)
return x
print(func())
print(func())
a) [1] [1]
b) [1] [1, 1]
c) [] []
d) Error
Solutions:
Answer: b) def
print(add(2, 3))
Answer: a) 5
Answer: b) Local
print(func(3))
Answer: b) 6
def func(x=[]):
x.append(1)
return x
print(func())
print(func())
a) if (condition):
b) if condition:
c) if condition then:
d) if: condition
x = 10
if x > 5:
print("Greater")
else:
print("Smaller")
a) Greater
b) Smaller
c) None
d) Error
a) if condition: else:
b) if (condition) {} else {}
c)
if condition:
# do something
else:
# do something else
x=5
if x > 5:
print("A")
elif x == 5:
print("B")
else:
print("C")
a) A
b) B
c) C
d) Error
5. What is the purpose of the elif keyword in Python?
x = 10
y = 20
if x < y and x > 5:
print("Yes")
else:
print("No")
a) Yes
b) No
c) Error
d) None
x = 15
if x % 2 == 0:
print("Even")
else:
print("Odd")
a) Even
b) Odd
c) Error
d) None
8. Which of the following correctly checks for both conditions being true?
x=5
if x > 10:
print("A")
else:
print("B")
if x < 3:
print("C")
a) A
b) B
c) B C
d) Error
x=7
if x > 10:
print("Greater")
elif x == 7:
print("Equal")
else:
print("Smaller")
a) Greater
b) Equal
c) Smaller
d) None
Solutions :
Answer: b) if condition:
x = 10
if x > 5:
print("Greater")
else:
print("Smaller")
Answer: a) Greater
3. How do you write an if-else statement in Python?
Answer: c)
if condition:
# do something
else:
# do something else
x=5
if x > 5:
print("A")
elif x == 5:
print("B")
else:
print("C")
Answer: b) B
x = 10
y = 20
if x < y and x > 5:
print("Yes")
else:
print("No")
Answer: a) Yes
x = 15
if x % 2 == 0:
print("Even")
else:
print("Odd")
Answer: b) Odd
8. Which of the following correctly checks for both conditions being true?
x=5
if x > 10:
print("A")
else:
print("B")
if x < 3:
print("C")
Answer: b) B
x=7
if x > 10:
print("Greater")
elif x == 7:
print("Equal")
else:
print("Smaller")
Answer: b) Equal
Exercise (d)
a) for i to 10:
b) for i in range(10):
c) foreach i in range(10):
d) loop i in range(10):
for i in range(3):
print(i)
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) Error
a) stop
b) exit
c) break
d) terminate
i=1
while i < 5:
print(i, end=" ")
i += 1
a) 1 2 3 4
b) 1 2 3 4 5
c) Infinite loop
d) Error
a) [2, 4, 6, 8]
b) [2, 3, 4, 5, 6, 7]
c) [2, 4, 6]
d) [2, 6, 8]
a) for key in d:
b) for key in d.keys():
c) Both a and b
d) None of the above
for x in "Python":
if x == "h":
break
print(x, end="")
a) Pyt
b) Pyth
c) P
d) Pyt
for i in range(5):
if i == 3:
continue
print(i, end=" ")
a) 0 1 2 3 4
b) 0 1 2 4
c) 1 2 4
d) 0 1 4
i=1
while i < 3:
print(i, end=" ")
i += 1
else:
print("Done")
a) 1 2 Done
b) 1 2
c) Done
d) Error
Solutions:
for i in range(3):
print(i)
Answer: a) 0 1 2
Answer: c) break
Answer: b) To skip the rest of the current iteration and move to the next iteration
i=1
while i < 5:
print(i, end=" ")
i += 1
Answer: a) 1 2 3 4
Answer: c) [2, 4, 6]
for x in "Python":
if x == "h":
break
print(x, end="")
Answer: c) P
for i in range(5):
if i == 3:
continue
print(i, end=" ")
Answer: b) 0 1 2 4
i=1
while i < 3:
print(i, end=" ")
i += 1
else:
print("Done")
Answer: a) 1 2 Done
Exercise (e)
a) int
b) float
c) complex
d) str
x = 10
y=3
print(x / y)
a) 3.0
b) 3
c) 3.33
d) 3.33...
a) /
b) //
c) %
d) **
a) 6
b) 8
c) 9
d) 23
a) //
b) **
c) %
d) &
x = "5"
y=3
print(x + str(y))
a) 53
b) 8
c) 5
d) Error
a) int
b) bool
c) str
d) float
a) list
b) set
c) tuple
d) dictionary
x=2
x += 3
print(x)
a) 2
b) 3
c) 5
d) 23
10. Which of the following is the correct way to create a set in Python?
a) x = {1, 2, 3}
b) x = [1, 2, 3]
c) x = (1, 2, 3)
d) x = set(1, 2, 3)
Solutions :
Answer: b) float
x = 10
y=3
print(x / y)
Answer: a) 3.0
Answer: b) //
Answer: b) 8
Answer: c) %
x = "5"
y=3
print(x + str(y))
Answer: a) 53
Answer: b) bool
8. Which of the following data types is immutable in Python?
Answer: c) tuple
x=2
x += 3
print(x)
Answer: c) 5
10. Which of the following is the correct way to create a set in Python?
Answer: a) x = {1, 2, 3}