Python Test Study Material
Python Test Study Material
Arrays in python
The Array can be handled in Python by a module named Array.
The following are the terms to understand the concept of an array:
Index - The location of an element in an array has a numerical index, which is used to
identify the element's position.
An array can be declared in various ways and in different languages. The important points
that should be considered are as follows:
1. The index starts with 0.
2. We can easily find any elements within this Array using the Index value.
3. The length of the Array defines the capacity to store the elements. It is written like
x[100], which means the length of array x is specified by 100.
Array operations
Some of the basic operations supported by an array are as follows:
o Traverse - It prints all the elements one by one.
o Insertion - It adds an element at the given index.
o Deletion - It deletes an element at the given index.
o Search - It searches an element using the given index or by the value.
o Update - It updates an element at the given index.
The Array can be created in Python by importing the array module to the python program.
Syntax:
from array import *
arrayName = array(typecode, [initializers])
Search Operation
You can perform a search for an array element based on its value or its index.
Here, we search a data element using the python in-built index() method.
Update Operation
Update operation refers to updating an existing element from the array at a given index.
Here, we simply reassign a new value to the desired index we want to update.
2 For loop This type of loop executes a code block multiple times and
abbreviates the code that manages the loop variable.
Python's for loop is designed to repeatedly execute a code block while iterating through a list,
tuple, dictionary, or other iterable objects of Python. The process of traversing a sequence is
known as iteration.
{ code block }
Example:
n=4 OUTPUT:
for i in range(0, n): 0
print(i) 1
2
3
In python, a while loop is used to execute a block of statements repeatedly until a given
condition is satisfied
Syntax:
while (expression) :
statement(s).
All the statements indented by the same number of character spaces after a programming
construct are considered to be part of a single block of code
count = 0 OUTPUT:
while (count < 3): Welcome to I-CS
count = count + 1 Welcome to I-CS
print("Welcome to I-CS") Welcome to I-CS
3. Functions in python :
We can create a user-defined function in Python, using the def keyword. We can add any
type of functionalities and properties to it as we require.
After creating a function in Python we can call it by using the name of the function
followed by parenthesis containing parameters of that particular function.
def fun():
print("Welcome to Computer Science")
Arguments are the values passed inside the parenthesis of the function. A function can have
any number of arguments separated by a comma.
Example:
# A simple Python function to check
# whether x is even or odd
def evenOdd(x):
if (x % 2 == 0):
print("even")
else:
print("odd")