0% found this document useful (0 votes)
2 views22 pages

4. Introduction to Python Programming23

The document provides an introduction to Python programming, detailing its features, syntax, and various programming concepts such as variables, data types, control structures, and functions. It highlights Python's readability, versatility across platforms, and its use in web development, data handling, and rapid prototyping. Additionally, it includes examples and explanations of comments, loops, and arrays, along with a series of questions to test understanding of the material.

Uploaded by

irfancse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views22 pages

4. Introduction to Python Programming23

The document provides an introduction to Python programming, detailing its features, syntax, and various programming concepts such as variables, data types, control structures, and functions. It highlights Python's readability, versatility across platforms, and its use in web development, data handling, and rapid prototyping. Additionally, it includes examples and explanations of comments, loops, and arrays, along with a series of questions to test understanding of the material.

Uploaded by

irfancse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

INSPIRE ACADEMY

Ramanagara
Diploma CET[Statistics and Analytics] Date : 22/07/2023
IV. Introduction to Python Programming[6Marks]
Python is a popular programming language. It other programming languages.
was created by Guido vanRossum, and released  Python runs on an interpreter system,
in 1991. meaning that code can be executed as soon
It is used for: as it is written. This means that prototyping
 web development (server-side) can be very quick.
 software development  Python can be treated in a procedural way,
 mathematics an object-oriented way or afunctional way.
 system scripting. Python Syntax compared to other
What can Python do? programming languages
 Python was designed for readability, and has
 Python can be used on a server to create web
some similarities to the English language
applications.
with influence from mathematics.
 Python can be used alongside software to
 Python uses new lines to complete a
create workflows.
command, as opposed to other programming
 Python can connect to database systems. It
languages which often use semicolons or
can also read and modify files.
parentheses.
 Python can be used to handle big data and
 Python relies on indentation, using
perform complex mathematics.
whitespace, to define scope; such as the
 Python can be used for rapid
scope of loops, functions and classes. Other
prototyping, or for
programming languages often use curly-
production-ready software
brackets for this purpose.
development.
Python Syntax
Why Python?
Python syntax can be executed by writing
 Python works on different
directly in the Command Line:
platforms (Windows, Mac,
>>> print("Hello, World!")
Linux, RaspberryPi, etc).
Or by creating a python file on the server,
 Python has a simple syntax similar to the
using the .py file extension, and running it in
English language.
the Command Line:
 Python has syntax that allows developers to
C:\Users\Your Name>python myfile.py
write programs with fewer lines than some

INSPIRE ACADEMY DCET[ Statistical and Analytics] 1


Python Indentation print("Hello, World!") #This is a comment
Indentation refers to the spaces at the beginning Multiline Comments
of a code line.
"""
Where in other programming languages the
This is a
indentation in code is for readability only, the
commentwritten
indentation in Python is very important.
in
Python uses indentation to indicate a block of
more than just one
code.
line"""
Correct
Wayif 5 > print("Hello, World!")
2:
print("Five is greater than two!") Variables in Phython :
Error Variables are containers for storing data
values. Python has no command for
if 5 > 2: declaring a variable.
print("Five is greater than two!")
A variable is created the moment you first
assign a value to it.
Comments in Python :
The Python print() function is often used to
 Comments can be used to explain Python
output variables.
code. Comments can be used to make the
x = "Python is
code more readable.
awesome"print(x)
 Comments can be used to prevent execution
when testing code.
x=5
 Comments starts with a #, and Python will
y =
ignore them
Single Line Comments: "John

#This is a comment "

print("Hello, print(

World!") x)
print(
Comments can be placed at the end of a line,
and Python will ignore the rest ofthe line: y)

INSPIRE ACADEMY DCET[ Statistical and Analytics] 2


Casting : print
If you want to specify the data type of a (z)
variable, this can be done with casting. x = str(3) Global Variables :
# x will be '3' Variables that are created outside of a function

y = int(3) # y will be 3 (as in all of the examples above) are known as

z = float(3) # z will global variables.

be 3.0Get the Type


Global variables can be used by everyone, both
You can get the data type of a variable with the
inside of functions and outside.x =
type() function.
"awesome"
x=5
def myfunc():
y =
print("Python is " +
"John"
x)myfunc()
print(typ
e(x)) Python Data Types :
print(typ Variables can store data of different types, and
e(y)) different types can do differentthings.
Python allows you to assign values to multiple Python has the following data types built-in
variables in one linex, y, by default, in these categories:
z = "Orange", "Banana", "Cherry" Text Type: str
print Numeric int,

(x) Types: flo

print at,
co
(y)
mp
print
lex
(z)
Sequence list
And you can assign the same value to multiple
Types: ,
variables in one linex = y
tup
= z = "Orange"
le,
print(x) ran
print ge
(y)

INSPIRE ACADEMY DCET[ Statistical and Analytics] 3


Mapping Type: dict x = True bool
Set Types: set, frozenset

Boolean Type: bool x = b"Hello" bytes


Binary Types: bytes, bytearray,
memoryview
x = bytearray(5) bytearray
None Type: NoneType

x = memoryview
memoryview(byte
Example Data Type
s(5))
x = "Hello World" str
x = None NoneType

x = 20 int

The if statement
x = 20.5 float The if statement is used to test a particular
condition and if the condition is true, it executes
x = 1j complex
a block of code known as if-block. The
condition of if statement can be any valid
x = ["apple", "banana", list logical expression which can be either evaluated
"cherry"] to true or false.

x = ("apple", "banana", tuple


"cherry")

x = range(6) range

x = {"name" : "John", dict


"age" : 36}

x = {"apple", "banana", set


"cherry"}

x = frozenset({"apple", frozenset
"banana", "cherry"}) The syntax of the if-statement is given below.

INSPIRE ACADEMY DCET[ Statistical and Analytics] 4


if # Simple Python Program to check whether a
expressi person is eligible to vote or not. age = int
on: (input("Enter your age: "))
statement # Here, we are taking an integer num and
Example 1 taking input dynamically
num = int(input("enter the number:")) if age>=18:
if num%2 == 0:
# Here, we are checking the condition. If the
print("The Given number is an even number") condition is true, we will enter theblock
The if-else statement print("You are eligible to vote !!");
The if-else statement provides an else block else:
combined with the if statement which is print("Sorry! you have to wait !!");
executed in the false case of the condition. The elif
statement if
If the condition is true, then the if-block is expression 1:
executed. Otherwise, the else-blockis executed.
# block of statements
elif expression 2:

# block of statements
elif expression 3:

# block of statements
else:
# block of statements

if condition:

#block of statements
else:
#another block of statements (else-block)
Example 1 : Program to check whether a person
is eligible to vote or not.

INSPIRE ACADEMY DCET[ Statistical and Analytics] 5


Python Loops : A function can return data as a result.
Python has two primitive loop commands: def my_function():
 while loops print("Hello from a function")
 for loops my_function()
 The while loop : What is an Array?
 With the while loop we can execute a set of An array is a special variable, which can hold
statements as long as a condition istrue. more than one value at a time.cars =
 Print i as long as i is less than 6: ["Ford", "Volvo", "BMW"]
 i=1 Access the Elements of an Array
 while i < 6: You refer to an array element by referring to
print the index number.x = cars[0]
The Length of an Array
(i) i
Use the len() method to return the length of an
+= 1
array (the number of elements inan array).
The break Statement x=
With the continue statement we can stop the len(cars)
current iteration, and continue withthe next for x in
i=0 cars:
while i < print(x)
6:i += 1 Array Methods :
if i == 3: Python has a set of built-in methods that you can
continue use on lists/arrays.
print(i)
Method Description
Python For Loops :
Print each fruit in a fruit list:
append() Adds an element at the end of the
fruits = ["apple", "banana", "cherry"]
list
for x in fruits:
print(x)
clear() Removes all the elements from
Python Functions :
the list
A function is a block of code which only runs
when it is called. You can pass data, known as
copy() Returns a copy of the list
parameters, into a function.

INSPIRE ACADEMY DCET[ Statistical and Analytics] 6


2. Which symbol is used to begin a single-line
count() Returns the number of elements comment in Python?
with the specified value 1) #
2) //
extend() Add the elements of a list (or any 3) /*
iterable), to the end of the current 4) --
list 3. Which of the following is an example of a
multi-line comment in Python?
index() Returns the index of the first 1) # This is a comment
element with the specified value 2) /* This is a comment */
3) ''' This is a comment '''
insert() Adds an element at the specified 4) // This is a comment
position 4. Which of the following statements is true
about Python comments?
pop() Removes the element at the 1. Comments are executed by the Python
specified position interpreter
2. Comments are ignored by the Python
remove() Removes the first item with the interpreter
specified value 3. Comments are used to indicate errors in
the code
reverse() Reverses the order of the list 4. Comments are used to store variable
values
5. What is the purpose of doc strings in Python?
sort() Sorts the list
1. To specify the version of Python being
used
Questions : 2. To provide additional documentation about
a function, module, or class
1. What is the purpose of comments in Python?
1) To improve the performance of the code 3. To comment out a block of code

2) To make the code easier to read and temporarily

understand 4. To add special characters to the code


3) To hide certain parts of the code from other 6. Which symbol is used to begin a docstring in
developers Python?
4) To add decorative elements to the code

INSPIRE ACADEMY DCET[ Statistical and Analytics] 7


1) # of a function
2) // 3.Removing or updating outdated comments
3) ''' 4. Using comments to disable code
4) """ temporarily
7. Can comments be nested in Python? Introduction to Python programming:
1. Which of the following is true about Python?
1. Yes, comments can be nested
1) Python is a low-level programming
2. No, comments cannot be nested
language
3. Comments can only be nested within
2) Python is a compiled language
docstrings
3) Python is an interpreted language
4. Nested comments are not supported in
4) Python is only used for web development
Python
2. What is the purpose of the print() function in
8. Which of the following is the recommended
Python?
way to write comments for readability?
1) To take input from the user
1. Write long descriptive comments for each
2) To perform mathematical calculations
line of code
3) To display output on the console
2. Avoid writing comments altogether
4) To define a new function
3. Keep comments short and concise,
3. Which of the following is a valid variable
focusing on the why and not the how
name in Python?
4. Use comments as a way to write
1) 1variable
pseudocode
2) my_variable
9. What happens if a comment is present after a
3) variable1@
statement in a Python program?
4) var-i-able
1. The comment will be executed by the
4. What does the "Hello, World!" program do?
Python interpreter
1) It prints the phrase "Hello, World!" to the
2. The comment will cause a syntax error
console
3. The comment will be ignored by the
2) It calculates the factorial of a number
Python interpreter
3) It creates a new file named "Hello, World!"
4. The comment will be displayed as output
4) It asks the user to enter their name
10. Which of the following is NOT a best
5. How do you declare a variable in Python?
practice for writing Python comments?
1) Using the var keyword
1. Commenting on the purpose of a piece of
2) Using the let keyword
code
3) By assigning a value to it
2. Commenting on the implementation details
4) By using the declare keyword

INSPIRE ACADEMY DCET[ Statistical and Analytics] 8


6. What is the output of the following code? Python data types:
``` python x = 5 1. Which of the following is not a built-in data

y=2 type in Python?


result = x + y * 2 print(result) 1) int
2) float
```
1) 14 3) list
2) 10 4) string
3) 9 2. What is the data type of the variable "x" in the
4) 7 expression: x = 10.5?
7. What does the if statement do in Python? 1) int
1) It repeats a block of code a certain number 2) float
of times 3) str
2) It defines a new function 4) bool
3) It checks a condition and executes code 3. Which data type is used to store a sequence of
based on the result characters in Python?
4) It performs mathematical calculations 1) int
8. What is the correct way to create a single-line 2) float
comment in Python? 3) str
1) /* This is a comment */ 4) list
2) // This is a comment 4. Which data type is used to store true or false
3) # This is a comment values in Python?
4) ''' This is a comment ''' 1) int
9. Which of the following is NOT a built-in data 2) float
type in Python? 3) bool
1) int 4) str
2) float 5. What is the output of the following code
snippet?
3) string
```python
4) array my_list = [1, 2, 3, 4]
10. How do you get user input in Python? print(len(my_list))
1) Using the input() function ```
1) 1
2) By declaring a variable
2) 2
3) By importing a specific module
3) 3
4) By using the read() function
4) 4

INSPIRE ACADEMY DCET[ Statistical and Analytics] 9


6. Which of the following is an example of an 11. What is the output of the following code
immutable data type in Python? snippet?
1) list ``` python
2) dictionary my_string = "Hello, World!"
3) tuple print(my_string[7])
4) set ```
7. What is the result of the following 1) H
2) W
expression?
3) o
``` python 10 / 3
4) d
```
12. Which data type is used to store a collection
1) 3.3333333333333335
2) 3 of key-value pairs in Python?

3) 3.0 1) int
4) 3.333 2) float
8. What is the data type of the result of the 3) tuple
expression: 10 / 3? 4) dictionary
1) int
13. What is the output of the following code
2) float
snippet?
3) str
``` python
4) bool
my_tuple = (1, 2, 3, 4)
9. Which of the following data types is mutable
print(my_tuple[2])
in Python?
1) int ```
2) float 1) 1
3) tuple 2) 2
4) list 3) 3
10. What is the purpose of the None data type 4) 4
in Python? 14. Which data type is used to store an
1) It represents an empty value or no value at unordered collection of unique elements in
Python?
all
1) int
2) It represents a Boolean value
2) float
3) It represents a numeric value
3) str
4) It represents a string value
4) set

INSPIRE ACADEMY DCET[ Statistical and Analytics] 10


15. Which of the following is not a valid way to 19. Which data type is used to represent a
create a list in Python? group of individual elements as a single entity
1) my_list = [1, 2, 3] in Python?
2) my_list = list(1, 2, 3) 1) int
3) my_list = [] 2) float
4) my_list = list() 3) str
16. What is the output of the following code 4) tuple
snippet? 20. What is the output of the following code
```python snippet?
my_dict = {'name': 'John', 'age': 25}
```python
print(my_dict['name'])
my_set = {1, 2, 3, 4}
```
print(len(my_set))
1) 'name'
2) 'John' ```
3) 'age' 1) 1
4) 25 2) 2
17. Which data type is used to store a sequence 3) 3
of characters that cannot be changed in 4) 4
Python? Python variables:
1) int 1. Which of the following is true about variables
2) float in Python?
3) str 1) Variables are used to store only numbers
4) list 2) Variables are case-sensitive
18. What is the result of the following 3) Variables cannot be reassigned once a
expression? value is assigned
4) Variables do not require a specific data type
```python
declaration
"Hello" + " " + "World"
2. What is the correct way to create a variable in
```
Python?
1) "Hello"
1) var x = 5
2) "World"
2) x = 5
3) "Hello World"
3) variable x = 5
4) "Hello,World"
4) x 5

INSPIRE ACADEMY DCET[ Statistical and Analytics] 11


3. Which of the following is a valid variable 7. Which of the following is a valid way to assign
name in Python? multiple variables in a single line in Python?
1) 123variable 1) x = y = z = 10
2) my_variable 2) x, y = 10
3) $variable 3) x = 10, y = 20
4) variable-name 4) x, y = y, x
4. What is the result of the following code 8. What is the data type of the variable in the
snippet? expression: x = 10.5?
``` python x = 5 1) int

y = 10 2) float
3) str
x = y print(x)
4) bool
``` 9. What is the result of the following
1) 5 expression?
2) 10
``` python x = 5,
3) Error: invalid syntax
Y=2
4) Error: variable names cannot be reassigned
result= x % y print(result)
5. What is the purpose of variable naming
conventions in Python? ```
1) To confuse other developers 1) 2.5
2) To indicate the data type of the variable 2) 0.5
3) To make the code more readable and 3) 0
understandable 4) 1
4) To prevent the variable from being modified 10. Which of the following is a reserved
6. What is the output of the following code keyword in Python and cannot be used as a
snippet? variable name?
``` python name = "John"
1) var
print("Hello, " + name) 2) if
``` 3) def
1) Hello, 4) try
2) Hello, John 12. What is the output of the following code
3) name snippet?
4) Error: invalid syntax

INSPIRE ACADEMY DCET[ Statistical and Analytics] 12


1) 2.5
``` python x = 5 2) 0.5
3) 0
y = "10"
4) 2
result= x + y print(result)
15. What is the purpose of assigning a value to
``` a variable in Python?
1) 15
1) To reserve memory space for the variable
2) 510
2) To indicate the size of the variable
3) Error: unsupported operand type(s) for +:
3) To calculate a mathematical expression
'int' and 'str'
4) To associate a meaningful name with a
4) Error: invalid syntax
value for later use
12. Which of the following is a valid way to
16. What is the output of the following code
convert a variable to a string in Python?
snippet?
1) str(x)
2) int(x) ``` python x = 10
3) float(x)
y=5
4) bool(x)
x = x + y print(x)
13. What is the scope of a variable in Python?
```
1) The geographic location where the variable
1) 10
is defined
2) 5
2) The specific line of code where the variable
3) 15
is declared
4) Error: invalid syntax
3) The block of code where the variable is
17. What is the result of the following
accessible
expression?
4) The specific data type assigned to the ``` python x= "5"
variable y=2
14. What is the output of the following code result = int(x) + y print(result)

snippet? ```
1) 7
``` python x= 5
2) 52
y=2
3) Error: unsupported operand type(s) for +:
result = x// y print(result)
'str' and 'int'
```
4) Error: invalid syntax

INSPIRE ACADEMY DCET[ Statistical and Analytics] 13


18. Which of the following is a valid way to 1) fun
assign a string value to a variable in Python? 2) define
3) def
1) x = 'Hello, World!'
4) function
2) x = "Hello, World!"
3. What is the purpose of a return statement in a
3) x = '''Hello, World!'''
function?
4) All of the above
1) To terminate the execution of a function
19. What is the output of the following code
2) To define the parameters of a function
snippet?
3) To specify the data type of the function
``` python x=10 4) To return a value from a function

y=3 4. What is the correct way to call a function in

result = x/ y print(result) Python?


1) Call my_function()
``` 2) function my_function()
1) 3.3333333333333335 3) my_function()
2) 3 4) def my_function()
3) 3.0 5. What is an array in Python?
4) 3.333 1) A built-in data type used to store multiple
20. Which of the following is not a valid data values
type for a variable in Python? 2) A string that represents a list of elements
3) A function that performs a specific task
1) int
4) A loop structure used to iterate over a
2) float
sequence of values
3) void
6. Which keyword is used to create an empty
4) str
array in Python?
Python functions and arrays:
1) empty
1. What is a function in Python?
2) create
1) A data type used to store multiple values
3) initialize
2) A variable that stores a single value
4) []
3) A reusable block of code that performs a
7. What is the output of the following code
specific task
snippet?
4) A statement used to create loops
``` python
2. Which keyword is used to define a function in
my_array =[1, 2, 3, 4]
Python?

INSPIRE ACADEMY DCET[ Statistical and Analytics] 14


print(len(my_array)) my_array = [1, 2, 3, 4]
my_array.append(5)
``` print(my_array)
1) 1
```
2) 2 1) [1, 2, 3, 4]
3) 3 2) [1, 2, 3, 4, 5]
4) 4 3) [5, 4, 3, 2, 1]
8. How do you access an element in an array in 4) [1, 5, 2, 3, 4]
Python? 12. Which of the following is a method used to
1) By using the element's index remove an element from an array in Python?
2) By using the element's value 1) append()
3) By using the array's length 2) remove()
4) By using the array's name 3) pop()
9. What is the result of the following 4) sort()
expression? 13. What is the output of the following code
```python snippet?
my_array = [1, 2, 3, 4] ```python
my_array[2] = 5 my_array = [1, 2, 3, 4]
print(my_array) my_array.remove(3)

``` ```
1) [1, 2, 3, 4]
1) [1, 2, 3, 4]
2) [1, 2, 4]
2) [1, 2, 5, 4]
3) [3, 2, 1]
3) [1, 2, 5]
4) [1, 2, 3]
4) [1, 5, 3, 4]
14. What is the purpose of the sort() method for
10. Which of the following is a method used to
arrays in Python?
add an element to an array in Python?
1) To remove duplicate elements from the
1) append()
array
2) remove()
2) To reverse the order of elements in the
3) pop()
array
4) sort()
3) To sort the elements in ascending or
11. What is the output of the following code
descending order
snippet?
4) To find the index of a specific element in
```python
the array

INSPIRE ACADEMY DCET[ Statistical and Analytics] 15


15. What is the output of the following code 19. What is the output of the following code
snippet? snippet?
```python ```python
my_array = [3, 2, 1, 4] my_array = [1, 2, 3, 4] for element
my_array.sort() print(my_array) in my_array:

``` print(element)
1) [3, 2, 1, 4]
```
2) [4, 3, 2, 1]
3) [1, 2, 3, 4] 1) 1

4) [1, 4, 2, 3] 2) 2

16. Which of the following is a method used to 3) 3

find the index of an element in an array in 4) 4

Python? 20. What is the purpose of the len() function in

1) append() Python?
1) To return the number of elements in an
2) remove()
3) pop() array

4) index() 2) To calculate the sum of elements in an


array
17. What is the output of the following code
snippet? 3) To find the maximum value in an array

```python 4) To determine the data type of an array


Python data types:
my_array = [1, 2, 3, 4] index =
1. Which of the following is not a built-in data
my_array.index(3) print(index)
type in Python?
```
1) int
1) 1
2) 2 2) float
3) 3 3) string

4) 4 4) array

18. What is the purpose of using loops with 2. What is the data type of the variable "x" in the

arrays in Python? expression: x = 10.5?


1) To modify the elements of the array 1) int
2) To perform mathematical calculations on 2) float
the array
3) str
3) To access and process each element of the
array 4) bool
4) To add or remove elements from the array

INSPIRE ACADEMY DCET[ Statistical and Analytics] 16


3. Which data type is used to store a sequence of 1) 3.3333333333333335
characters in Python? 2) 3
1) int 3) 3.0
2) float 4) 3.333
3) str 8. What is the data type of the result of the
4) list expression: 10 / 3?
4. Which data type is used to store true or false 1) int
values in Python? 2) float
1) int 3) str
2) float 4) bool
3) bool 9. What is the output of the following code
4) str snippet?
5. What is the output of the following code ``` python x = 5
snippet?
y=2
```python
result = x % y print(result)
my_string = "Hello, World!"
```
print(my_ string[7])
1) 2.5
``` 2) 0.5
1) H 3) 0
2) W 4) 1
3) o 10. Which data type is used to represent a
4) d collection of key-value pairs in Python?
6. Which data type is used to store multiple 1) int
values in an ordered sequence in Python? 2) float
1) int 3) str
2) float 4) dictionary
3) str 11. What is the output of the following code
4) list snippet?
7. What is the result of the following ```python
expression? my_list = [1, 2, 3, 4]
``` python10 / 3 print(len(my_list))

```

INSPIRE ACADEMY DCET[ Statistical and Analytics] 17


1) 1 1) 1
2) 2 2) 2
3) 3 3) 3
4) 4 4) 4
12. Which data type is used to represent a 16. Which data type is used to represent a
collection of unique elements in Python? sequence of characters enclosed in quotation
1) int marks?
2) float 1) int
3) str 2) float
4) set 3) str
13. What is the output of the following code 4) bool
snippet? 17. What is the output of the following code
```python snippet?
my_tuple = (1, 2, 3, 4) ```python
print(my_tuple[2]) my_dict = {'name': 'John', 'age': 25}
print(my_dict['name'])
```
1) 1 ```
2) 2 1) 'name'
3) 3 2) 'John'
4) 4 3) 'age'
14. What is the purpose of the None data type 4) 25
in Python? 18. What is the result of the following
1) It represents an empty or missing value expression?
2) It represents a Boolean value ```python
3) It represents a numeric value "Hello" + " " + "World"
4) It represents a string value ```
15. What is the output of the following code 1) "Hello"
snippet? 2) "World"
```python 3) "Hello World"
my_set = {1, 2, 3, 4} 4) "Hello, World"
print(len(my_set)) 19. Which data type is used to represent a
collection of elements that cannot be
```
changed?

INSPIRE ACADEMY DCET[ Statistical and Analytics] 18


1) int ``` python x = 5
2) float
if x > 10:
3) str
print("Greater than 10") else:
4) tuple
print("Less than or equal to 10")
20. What is the output of the following code
snippet? ```
1) Greater than 10
```python x = True y =
2) Less than or equal to 10
False
3) 5
print(x and y)
4) Error: invalid syntax

``` 4. Which control statement is used to repeat a

1) True block of code multiple times in Python?

2) False 1) if statement

3) Error: invalid syntax 2) for statement

4) Error: unsupported operand type(s) for and 3) while statement

Python control statements: 4) switch statement

1. Which control statement is used to make 5. What is the purpose of the for loop in

decisions based on certain conditions in Python?

Python? 1) To execute a block of code repeatedly

1) if statement 2) To make decisions between two

2) for statement alternatives

3) while statement 3) To iterate over a sequence of values

4) switch statement 4) To define a new function

2. What is the purpose of the if-else statement in 6. What is the output of the following code

Python? snippet?

1) To execute a block of code repeatedly ```python

2) To iterate over a sequence of values my_list = [1, 2, 3, 4] for num in


3) To make decisions between two my_list:
alternatives
print(num)
4) To define a new function ```
3. What is the result of the following code 1) 1
2) 2
snippet?
3) 3
4) 4

INSPIRE ACADEMY DCET[ Statistical and Analytics] 19


7. What is the purpose of the break statement in 1) 1
Python? 2) 2
1) To exit the current loop 3) 3
2) To skip the current iteration and move to 4) 4
the next one 11. Which control statement is used to handle
3) To restart the current iteration from the exceptions in Python?
beginning 1) if statement
4) To define a new function 2) for statement
8. What is the result of the following code 3) while statement
snippet? 4) try-except statement
``` python x = 0 12. What is the purpose of the try-except
while x < 5: print(x) statement in Python?
x += 1 1) To execute a block of code repeatedly
2) To make decisions between two
```
alternatives
1) 0
3) To handle potential errors or exceptions
2) 1
4) To define a new function
3) 4
13. What is the output of the following code
4) 5
snippet?
9. What is the purpose of the continue statement
``` python try:
in Python?
x = 5/ 0 print(x)
1) T o exit the current loop
except ZeroDivisionError: print("Cannot
2) To skip the current iteration and move to
divide by zero")
the next one
3) To restart the current iteration from the ```
beginning 1) 5
2) 0
4) To define a new function
3) Cannot divide by zero
10. What is the output of the following code
4) Error: invalid syntax
snippet?
14. What is the purpose of the else block in a
``` python x = 0
try-except statement?
while x < 5: x += 1
1) To define the alternative path if an
if x ==3: continue
exception occurs
print(x) 2) To handle potential errors or exceptions
```

INSPIRE ACADEMY DCET[ Statistical and Analytics] 20


3) To execute a block of code if no exceptions 18. What is the purpose of the finally block in a
occur try-except statement?
4) To define a new function 1) To execute a block of code repeatedly
15. What is the output of the following code 2) To handle potential errors or exceptions
snippet? 3) To define a new function
``` python x = 10 4) To execute a block of code regardless of
if x > 5: whether an exception occurs or not
print("Greater than 5") elif x < 15: 19. What is the output of the following code
print("Less than15") else: snippet?
print("Greater than or equal to 15")
``` ```python
1) Greater than 5 for x in range(3) : print(x)
2) Less than 15
if x == 1: break
3) Greater than or equal to 15
else:
4) 10
print("Loop finished")
16. What is the purpose of the pass statement in
```
Python?
1) 0
1) To execute a block of code repeatedly
2) 1
2) To skip the current iteration and move to
3) Loop finished
the next one
4) 2
3) To exit the current loop
20. Which control statement is used to specify
4) To indicate an empty block of code
alternative paths in Python?
17. What is the result of the following code
1) if statement
snippet?
2) for statement
``` python x = 0
3) while statement
while x < 5: if x == 3:
4) switch statement
break print(x) x
+=1
```
1) 0
2) 1
3) 2
4) Error: invalid syntax

INSPIRE ACADEMY DCET[ Statistical and Analytics] 21


INSPIRE ACADEMY DCET[ Statistical and Analytics] 22

You might also like