What is a function in Python?
A block of statements under a name that can execute independently.
What are the main purposes of using functions?
To perform specific tasks, divide larger problems into smaller parts, implement code reusability, and improve code readability.
1/198
p.1
User-defined Functions

What is a function in Python?

A block of statements under a name that can execute independently.

p.1
User-defined Functions

What are the main purposes of using functions?

To perform specific tasks, divide larger problems into smaller parts, implement code reusability, and improve code readability.

p.1
Built-in Functions

What are built-in functions in Python?

Functions and methods that are provided by Python and are always available.

p.1
User-defined Functions

What are user-defined functions in Python?

Functions and methods created by the user.

p.2
Commonly Used Modules

What is a module in Python?

A grouping of functions, created as a Python (.py) file containing function definitions.

p.2
Commonly Used Modules

How do you use a module in Python?

By importing the module using the import statement.

p.2
Commonly Used Modules

What is the syntax for importing a module?

import modulename1 [,modulename2, …]

p.2
Commonly Used Modules

How do you call a function from a module?

By using the syntax modulename.functionname().

p.2
Commonly Used Modules

What does importing a module allow you to do?

Access all the functions in the module.

p.3
Commonly Used Modules

What is the purpose of the math module in Python?

It contains different types of mathematical functions.

p.12
User-defined Functions

What is the purpose of the function 'add_cubes'?

To calculate and add the surface area of two cubes.

p.10
User-defined Functions

What is the purpose of the provided program?

To check if a 3-digit number is an Armstrong number.

p.4
Math Module Functions

What is the result of math.pow(6.5, 2)?

42.25

p.15
User-defined Functions

What is the output of the Student_info function when called with name='Raju', rollno=111, group='MSDs'?

{'name': 'Raju', 'rollno': 111, 'group': 'MSDs'}

p.23
String Operations and Methods

What does the method str1.isupper() check for?

Returns True if the string is non-empty and has all uppercase alphabets or at least one uppercase character with the rest being non-alphabet characters.

p.19
String Operations and Methods

What are the two membership operators in Python?

'in' and 'not in'.

p.4
Math Module Functions

What does math.sqrt(x) compute?

The square root of x.

p.18
String Operations and Methods

What operations can be performed on string data types in Python?

Concatenation, repetition, membership, and slicing.

p.15
Commonly Used Modules

What does the len(sys.argv) function return?

The number of command line arguments passed to the program.

p.18
String Operations and Methods

What symbol is used for the repetition operator in Python?

The symbol *.

p.24
String Operations and Methods

What does the join() method do?

It returns a string in which the characters in the string have been joined by a separator.

p.4
Random Module Functions

What is the output of random.randint(3, 7)?

4

p.21
String Operations and Methods

What is the effect of the lower() method?

It converts all uppercase letters in the string to lowercase.

p.10
User-defined Functions

What is an example of an Armstrong number?

153, since 1*1*1 + 5*5*5 + 3*3*3 = 153.

p.20
String Operations and Methods

Can negative indexes be used in string slicing?

Yes, negative indexes can be used for slicing.

p.3
Math Module Functions

What does the function math.floor(x) return?

The floor value of x.

p.19
String Operations and Methods

What does the 'in' operator do in Python?

It returns True if the first string appears as a substring in the second string, otherwise False.

p.8
User-defined Functions

What is the output format of the calcFact() function?

It prints 'Factorial of [num] is [fact]'.

p.4
Math Module Functions

What is the sine of 0 in radians using math.sin?

0

p.4
Random Module Functions

What does the random module do?

Generates random numbers.

p.21
String Operations and Methods

Provide an example of traversing a string using a while loop.

str1 = 'Hello World!'; index = 0; while index < len(str1): print(str1[index]); index += 1.

p.11
Variable Scope and Lifetime

What is the value of the local variable 'num' in the function 'myfunc'?

10.

p.9
User-defined Functions

What is a void function in Python?

A function that does not have a return statement and returns None.

p.3
Commonly Used Modules

How do you import the math module in Python?

Using the statement 'import math'.

p.23
String Operations and Methods

What does str1.replace('o', '*') do?

Replaces all occurrences of 'o' with '*'.

p.18
String Operations and Methods

What does it mean that a string is immutable in Python?

It means that the contents of the string cannot be changed after it has been created.

p.23
String Operations and Methods

What does the method str1.islower() return?

False if the string contains any uppercase letters or is empty.

p.22
String Operations and Methods

What does the method str1.count('Hello') return?

3, indicating the number of occurrences of 'Hello' in the string.

p.22
String Operations and Methods

What does the find() method return if the substring is not present?

-1.

p.12
User-defined Functions

What does the nested function 'cube_surfacearea' calculate?

The surface area of a cube given its side length.

p.21
Built-in Functions

What does the len() method return?

It returns the length of the given string.

p.11
Variable Scope and Lifetime

What will be printed when accessing 'num1' outside of 'myfunc'?

'Accessing num outside myfunc' will print 5.

p.6
User-defined Functions

What is the general syntax for calling a function in Python?

function_name(parameter_1, parameter_2,...)

p.4
Statistics Module Functions

How do you import the statistics module?

Using the statement: import statistics.

p.3
Math Module Functions

What is the output of math.gcd(10, 2)?

2

p.8
User-defined Functions

How does the calcFact() function calculate the factorial?

By multiplying 'fact' by each integer from 'num' down to 1 in a loop.

p.7
User-defined Functions

What does the 'sumSquares' function calculate?

The sum of the first n natural numbers.

p.21
String Operations and Methods

What does the join() string method do?

It returns a string by joining all the elements of an iterable, separated by a string separator.

p.23
String Operations and Methods

What will str1.isupper() return for the string 'HELLO 1234'?

True.

p.15
Commonly Used Modules

What is the purpose of the for loop in the command line arguments example?

To print each command line argument.

p.20
String Operations and Methods

What is the default starting index if the first index is not mentioned in slicing?

The slice starts from index 0.

p.26
String Formatting and Prototypes

What is the output of the print statements in the provided code?

Raju is 23 years old (three times).

p.4
Statistics Module Functions

What does the statistics module provide?

Functions for calculating statistics of numeric data.

p.21
String Operations and Methods

What does the upper() method do?

It converts all lowercase letters in the string to uppercase.

p.8
User-defined Functions

What is the initial value of the variable 'fact' in the calcFact() function?

1.

p.11
Variable Scope and Lifetime

What is a global variable?

A variable defined outside any function or block, accessible in any function defined afterwards.

p.19
String Operations and Methods

What is the purpose of slicing in Python?

To access some part of a string or substring.

p.7
User-defined Functions

What is the input for the 'sumSquares' function?

An integer value for n, provided by the user.

p.20
String Operations and Methods

What happens when the first index is greater than the second index in string slicing?

It results in an empty string.

p.24
String Operations and Methods

What is the output of str1.replace('Hello','Bye') when str1 is 'Hello World! Hello'?

'Bye World! Bye'

p.4
Random Module Functions

What does random.random() return?

A random real number (float) in the range 0.0 to 1.0.

p.14
User-defined Functions

What is the purpose of **kwargs in a Python function?

It allows passing a variable length of keyword arguments.

p.13
User-defined Functions

What advantage do keyword arguments provide?

They allow us to ignore the order of arguments.

p.20
String Operations and Methods

What is the default step size in string slicing?

The default step size is one.

p.3
Math Module Functions

What is the output of math.ceil(-9.7)?

-9

p.22
String Operations and Methods

What will str1.islower() return for str1 = 'hello world!'?

True.

p.8
User-defined Functions

What is the purpose of the user-defined function calcFact()?

To calculate and display the factorial of a number passed as an argument.

p.8
User-defined Functions

What does the input function do in the provided code?

It prompts the user to enter a number, which is then converted to an integer.

p.4
Math Module Functions

What is the output of math.sqrt(144)?

12.0

p.18
String Operations and Methods

How do you concatenate two strings in Python?

By using the concatenation operator +.

p.6
User-defined Functions

What is the general syntax for defining a function in Python?

def function_name(list_of_parameters): statement_1 statement_2 ...

p.22
String Operations and Methods

What is the purpose of the find(str, start, end) method?

It returns the index of the first occurrence of the substring str within the specified range.

p.14
User-defined Functions

How do you denote *args in a function definition?

By using an asterisk (*) before the parameter name.

p.23
String Operations and Methods

What does str1.lstrip() do?

Removes spaces only on the left side of the string.

p.10
User-defined Functions

What defines an Armstrong number?

The sum of cubes of each digit is equal to the number itself.

p.6
User-defined Functions

Provide an example of a function without arguments in Python.

def display(): print('Function without arguments') display()

p.15
User-defined Functions

What does the function Student_info do with **kwargs?

It prints the keyword arguments passed to it.

p.17
Variable Scope and Lifetime

How is the index of the last character in a string represented using negative indexing?

-n, where n is the length of the string.

p.6
User-defined Functions

What keyword is used to define a function in Python?

def

p.24
String Operations and Methods

What does the replace() method do in Python?

It replaces occurrences of a specified substring with another substring.

p.4
Random Module Functions

How do you import the random module?

Using the statement: import random.

p.12
User-defined Functions

What is the formula used in 'cube_surfacearea' to calculate surface area?

6 * pow(x, 2).

p.21
String Operations and Methods

What does the title() method do?

It returns the string with the first letter of every word in uppercase and the rest in lowercase.

p.9
User-defined Functions

What does the 'return' statement do in a function?

It returns values from the function to the caller.

p.24
String Operations and Methods

What does the partition() method do?

It partitions the string at the first occurrence of a substring and returns three parts: before the separator, the separator, and after the separator.

p.26
User-defined Functions

What happens in a function without parameters and without return value?

There is no data transfer; execution control jumps to the called function and returns to the calling function.

p.12
User-defined Functions

What is the output of 'addition(10, 20, 30)'?

The sum is 60.

p.12
User-defined Functions

What default values are used in the 'addition' function?

num2 = 9 and num3 = 5.

p.26
User-defined Functions

What is the output of the 'add' function when inputs are 10 and 20?

30.

p.7
User-defined Functions

What is the purpose of the user-defined function 'addnum'?

To add two numbers and display their sum.

p.19
String Operations and Methods

What does the 'not in' operator do in Python?

It returns True if the first string does not appear as a substring in the second string, otherwise False.

p.15
Commonly Used Modules

How does Python handle command line arguments?

It stores them in a list using the sys module, accessible via sys.argv.

p.11
Variable Scope and Lifetime

How long does a local variable exist?

It exists only until the function executes.

p.9
User-defined Functions

What is the formula to find the area of a trapezium?

Area = (1/2) * (a + b) * h, where a and b are the bases and h is the height.

p.18
String Operations and Methods

Does the original string change after using the repetition operator?

No, the original string remains the same.

p.6
User-defined Functions

What does every function in Python return by default?

None value.

p.22
String Operations and Methods

What does the index() method do differently than find()?

It raises an exception if the substring is not found.

p.14
User-defined Functions

How do you denote **kwargs in a function definition?

By using a double asterisk (**) before the parameter name.

p.13
User-defined Functions

How does the Python interpreter match keyword arguments to parameters?

By using the keyword provided to match with the respective parameter.

p.9
User-defined Functions

What is the output of the 'add' function when called with 2 and 2?

4.

p.24
String Operations and Methods

What does the capitalize() method do?

It returns a copy of the string with its first character capitalized and the rest lowercased.

p.25
String Operations and Methods

What method is used to convert a string to uppercase in Python?

str1.upper()

p.5
Statistics Module Functions

What is the output of statistics.mode([11,24,11,45,11])?

11

p.7
User-defined Functions

What inputs does the 'addnum' function require?

Two integers from the user.

p.11
Variable Scope and Lifetime

What happens when a global variable is changed?

Any change will impact all functions in the program where that variable can be accessed.

p.19
String Operations and Methods

How do you perform slicing on a string in Python?

By specifying an index range using the string variable name followed by square brackets.

p.19
String Operations and Methods

What components are specified in slicing to access a substring?

Starting index, ending index, and step.

p.14
User-defined Functions

What does *args allow in a Python function?

It allows passing a variable number of non-keyword arguments.

p.23
String Operations and Methods

What does the method str1.istitle() check?

Returns True if the string is non-empty and in title case, meaning the first letter of every word is uppercase and the rest are lowercase.

p.13
User-defined Functions

What are keyword arguments in Python?

Arguments passed as a value along with the parameter name (kwarg = value).

p.20
String Operations and Methods

What does the syntax str1[n:m:k] represent in string slicing?

It means every kth character is extracted from str1 starting from n and ending at m - 1.

p.3
Math Module Functions

What does the function math.ceil(x) return?

The ceiling value of x.

p.22
String Operations and Methods

What does the method isalnum() check for?

It returns True if the string contains only alphabets or numeric characters.

p.12
User-defined Functions

What is the output when 'addition(10, 20)' is called?

The sum is 39.

p.10
Variable Scope and Lifetime

What is a global variable?

A variable that has global scope.

p.5
Commonly Used Modules

How can you get a list of modules available in Python?

By using the statement help('module').

p.25
String Operations and Methods

What method removes leading whitespace from a string?

str1.lstrip()

p.17
Variable Scope and Lifetime

What does a negative index in Python allow you to do?

Access characters of a string from right to left.

p.18
String Operations and Methods

What happens if you try to change the contents of an immutable string in Python?

It would lead to an error.

p.11
Variable Scope and Lifetime

What is a local variable?

A variable defined inside a function or block, accessible only within that function or block.

p.14
User-defined Functions

What are the two special symbols used in Python to pass a variable number of arguments to a function?

*args and **kwargs.

p.11
Variable Scope and Lifetime

In the provided program, what is the value of the global variable 'num1'?

5.

p.9
User-defined Functions

What does the function 'area_trapezium' calculate?

It calculates the area of a trapezium using the provided bases and height.

p.24
String Operations and Methods

How would you join the string 'HelloWorld!' with a separator ' - '?

'H - e - l - l - o - W - o - r - l - d - !'

p.12
User-defined Functions

What are default parameters in Python?

Parameters that have a default value assigned in the function definition.

p.23
String Operations and Methods

What does str1.strip() accomplish?

Removes spaces from both the left and right sides of the string.

p.13
User-defined Functions

What is the purpose of the function 'student_info'?

To print the roll number, student name, and group of a student.

p.10
User-defined Functions

What is the output when the input is 121?

121 is not an Armstrong number.

p.5
Statistics Module Functions

What does the function statistics.median(x) return?

The median (middle value) of x.

p.12
User-defined Functions

What is the output when 'addition(10)' is called?

The sum is 24.

p.25
String Operations and Methods

How can you check if a string is alphanumeric?

Using str1.isalnum()

p.16
String Operations and Methods

How can strings be created in Python?

By enclosing characters in single, double, or triple quotes.

p.17
Variable Scope and Lifetime

Can the index in Python be an expression?

Yes, it can include variables and operators but must evaluate to an integer.

p.17
Variable Scope and Lifetime

What is the index of the first character from the right in a string?

-1.

p.7
User-defined Functions

How does the 'sumSquares' function compute the sum?

By iterating through a range from 1 to n and accumulating the sum.

p.21
String Operations and Methods

How can you traverse a string using a for loop?

By iterating through each character in the string using a for loop.

p.7
User-defined Functions

What is the output of the 'sumSquares' function?

The sum of the first n natural numbers.

p.6
User-defined Functions

How should parameters be separated in a function definition?

With a comma.

p.22
String Operations and Methods

What will str1.find('Hello', 10, 20) return if str1 = 'Hello World! Hello Hello'?

13.

p.12
User-defined Functions

What is the output of the 'main' function when 'add_cubes(2, 3)' is called?

The surface area after adding two cubes is 66.

p.23
String Operations and Methods

What is the purpose of str1.rstrip()?

Removes spaces only on the right side of the string.

p.9
User-defined Functions

What will the function 'add' return when called with arguments 2 and 2?

It will return 4.

p.24
String Operations and Methods

What is the output of str1.partition('is') when str1 is 'India is a Great Country'?

('India ', 'is', ' a Great Country')

p.26
User-defined Functions

In a function with parameters and without return value, what type of data transfer occurs?

Data transfer occurs from the calling function to the called function (parameters), but not back (no return value).

p.3
Math Module Functions

What is the output of math.floor(4.5)?

4

p.25
String Operations and Methods

How can you convert a string to lowercase in Python?

Using str1.lower()

p.25
String Operations and Methods

Which method is used to capitalize the first letter of each word in a string?

str1.title()

p.16
String Operations and Methods

What is a string in Python?

A sequence of characters enclosed in quotes.

p.16
String Operations and Methods

What is indexing in the context of strings?

A technique to access individual characters in a string using their position.

p.23
String Operations and Methods

What does the method str1.isspace() return?

True if the string is non-empty and all characters are white spaces.

p.15
Commonly Used Modules

What should you save the command line arguments example code as?

Filename.py (e.g., Command.py).

p.20
String Operations and Methods

What happens if the second index is not mentioned in string slicing?

The slicing is done till the length of the string.

p.6
User-defined Functions

What is required to execute a function's code in Python?

A function call.

p.22
String Operations and Methods

What does str1.endswith('World!') return if str1 = 'Hello World!'?

True.

p.12
User-defined Functions

What happens if a function with default parameters is called without providing all arguments?

It executes with the provided values for the arguments and uses default values for any missing arguments.

p.5
Statistics Module Functions

What does the function statistics.mean(x) return?

The arithmetic mean of x.

p.13
User-defined Functions

How is the 'student_info' function called with keyword arguments?

student_info(name = 'Raju', rollNo = 111, group = 'MSDs').

p.24
User-defined Functions

What does the case_conversion function do?

It converts uppercase letters to lowercase and vice versa in a given string.

p.5
Statistics Module Functions

What is the output of statistics.mode(('red','blue','red'))?

'red'

p.3
Math Module Functions

What is the output of math.pow(3, 2)?

9.0

p.26
User-defined Functions

What are the four classifications of functions based on data flow?

1. Function without Parameters and without Return value 2. Function with Parameters and without Return value 3. Function without Parameters and with Return value 4. Function with Parameters and with Return value

p.14
User-defined Functions

What does the example function 'largest' do?

It returns the maximum value from the passed numbers.

p.21
String Operations and Methods

What does the count() method return?

It returns the number of times a substring occurs in the given string within specified start and end indices.

p.5
Statistics Module Functions

What is the output of statistics.mean([11,24,32,45,51])?

32.6

p.3
Math Module Functions

What is the output of math.fabs(-6.7)?

6.7

p.3
Math Module Functions

What does the function math.gcd(x, y) return?

The greatest common divisor of x and y.

p.25
String Operations and Methods

How do you remove trailing whitespace from a string?

Using str1.rstrip()

p.14
User-defined Functions

What is the output of the function 'largest' when called with (20, 35)?

35.

p.22
String Operations and Methods

What does startswith() return?

True if the string starts with the supplied substring, otherwise False.

p.24
String Operations and Methods

What is the difference between lower() and casefold() methods?

casefold() converts more characters into lower case compared to lower().

p.5
Statistics Module Functions

What does the function statistics.mode(x) return?

The mode (the most repeated value) of x.

p.25
String Operations and Methods

How can you check if all characters in a string are uppercase?

Using str1.isupper()

p.20
String Operations and Methods

What does it mean to give a step size of -1 in string slicing?

It returns the string in reverse order.

p.10
User-defined Functions

What is the output when the input is 407?

407 is an Armstrong number.

p.5
Statistics Module Functions

What is the output of statistics.median([11,24,32,45,51])?

32

p.3
Math Module Functions

What is the output of math.factorial(5)?

120

p.5
Commonly Used Modules

What is the purpose of the from statement in Python?

To access only the required functions from a module.

p.25
String Formatting and Prototypes

What is the purpose of curly braces {} in f-strings?

They act as placeholders for values to be evaluated.

p.16
String Operations and Methods

What type of values can be used as an index in Python strings?

The index must be an integer (positive, zero, or negative).

p.23
String Operations and Methods

What will str1.replace('World', 'Country') return for the string 'Hello World!'?

'Hello Country!'.

p.25
String Operations and Methods

What method capitalizes the first letter of a string?

str1.capitalize()

p.25
String Operations and Methods

What method checks if all characters in a string are lowercase?

str1.islower()

p.16
String Operations and Methods

Are single quotes and double quotes treated the same in Python?

Yes, both are treated the same.

p.26
User-defined Functions

What is the purpose of the 'add' function in the example provided?

To take two inputs, add them, and print the result.

p.3
Math Module Functions

What does the function math.fabs(x) return?

The absolute value of x.

p.3
Math Module Functions

What does the function math.factorial(x) return?

The factorial of x.

p.5
Commonly Used Modules

How can you view the content of a specific module like math?

By typing help('math').

p.25
String Operations and Methods

How can you remove both leading and trailing whitespace from a string?

Using str1.strip()

p.24
String Operations and Methods

What is the output of str.capitalize() when str is 'hello world!'?

'Hello world!'

p.10
Variable Scope and Lifetime

What is the scope of a variable?

The part of the program where a variable is accessible.

p.10
Variable Scope and Lifetime

What is a local variable?

A variable that has local scope.

p.3
Math Module Functions

What does the function math.pow(x, y) calculate?

x raised to the power of y.

p.25
String Formatting and Prototypes

What is an f-string in Python?

A string formatting method introduced in Python 3.6 that uses an f prefix and {} brackets.

p.25
String Operations and Methods

What does the swapcase() method do?

It swaps the case of all characters in the string.

p.16
String Operations and Methods

What is the index of the first character in a string?

0.

p.25
String Formatting and Prototypes

What is the advantage of using f-strings for string formatting?

They are faster, more readable, concise, and less error-prone.

p.16
String Operations and Methods

How do you create a string variable in Python?

By assigning a string value to a variable, e.g., str1 = 'Hello World!'

p.16
String Operations and Methods

What happens if an index value is out of range?

An IndexError is raised.

Study Smarter, Not Harder
Study Smarter, Not Harder