0% found this document useful (0 votes)
8 views

Python Test Study Material

Test material

Uploaded by

velu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Test Study Material

Test material

Uploaded by

velu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

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:

Element - Each item stored in an array is called an element.

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])

Accessing array elements


We can access the array elements using the respective index's of those elements.
import array as arr
a = arr.array('i', [2, 4, 5, 6])
print("First element is:", a[0])
print("Second element is:", a[1])
print("Third element is:", a[2])
print("Forth element is:", a[3])
print("last element is:", a[-1])
print("Second last element is:", a[-2])
print("Third last element is:", a[-3])
print("Forth last element is:", a[-4])
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element
can be added at the beginning, end, or any given index of array.

from array import *


OUTPUT:
array1 = array('i', [10,20,30,40,50]) 10
array1.insert(1,60) 60
for x in array1: 20
print(x) 30
40
50
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
To remove a data element at the middle of the array using the python in-built remove() method.
from array import * OUTPUT:
array1 = array('i', [10,20,30,40,50]) 10
array1.remove(40) 20
for x in array1: 30
print(x) 50

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.

from array import *


OUTPUT:
array1 = array('i', [10,20,30,40,50])
3
print (array1.index(40))

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.

from array import * OUTPUT:


10
array1 = array('i', [10,20,30,40,50]) 20
array1[2] = 80 80
for x in array1: 40
print(x) 50
2. Looping statements/ Iterative statements
We can run a single statement or set of statements repeatedly using a loop command.

Sr.No. Name of the loop Loop Type & Description

1 While loop Repeats a statement or group of statements while a given condition is


TRUE. It tests the condition before executing the loop body.

2 For loop This type of loop executes a code block multiple times and
abbreviates the code that manages the loop variable.

3 Nested loops We can iterate a loop inside another loop.

The for Loop:

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.

Syntax: for value in sequence:

{ code block }
Example:
n=4 OUTPUT:
for i in range(0, n): 0
print(i) 1
2
3

While Loop in Python:

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 :

 Python Functions is a block of statements that return the specific task.


 A function is a block of code which only runs when it is called.
 You can pass data, known as parameters, into a function.
 A function can return data as a result.

Types of Functions in Python

There are mainly two types of functions in Python.


 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.

Creating a Function 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.

Calling a Python Function:

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")

# Driver code to call a function


fun() ----------------------------> Function call

Python Function Arguments

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")

# Driver code to call the function


evenOdd(2)
evenOdd(3)

You might also like