0% found this document useful (0 votes)
22 views7 pages

Python Iteration and List Operations Guide

The document provides a comprehensive guide on Python programming concepts, including list operations, iteration statements, indexing, functions, and file handling. It explains key features such as list repetition, slicing, variable scope, and error types, along with examples and syntax. Additionally, it includes a series of questions and answers to test understanding of these concepts.

Uploaded by

PANKAJ KUMAR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Python Iteration and List Operations Guide

The document provides a comprehensive guide on Python programming concepts, including list operations, iteration statements, indexing, functions, and file handling. It explains key features such as list repetition, slicing, variable scope, and error types, along with examples and syntax. Additionally, it includes a series of questions and answers to test understanding of these concepts.

Uploaded by

PANKAJ KUMAR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hssc Guide Ankit Grewal ( Cet 2024 Rank 01)

Hssc Guide Search @Guidehssc Hssc Guide App from play store

List Operations
Iteration Statement
(Looping)
🔹 List Repetition
List repetition is done using * operator.
An iteration statement repeats a block of
code until a certain condition is met. Example:

Iteration is also called looping. [1, 2, 3] * 3

Python supports iteration over: 👉 Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]


●​ List 📌 This repeats the list 3 times.
●​ Tuple
●​ String
List Slicing (Fragment of
●​ Set
●​ Dictionary List)
Slicing is used to shorten or extract part of a
Indexing in Python (Left & list.

Right) Syntax: list[start : end]

Python follows zero-based indexing. Example:

lst = [10, 20, 30, 40, 50]


➤ Left to Right Indexing
lst[1:4]
●​ Starts from 0
👉 Output: [20, 30, 40]
➤ Right to Left Indexing
List Functions
●​ Starts from -1

🔹 Example: 🔹 append() Function


lst = [10, 20, 30, 40, 50] ●​ Adds one element at the end of the
list.
Value 10 20 30 40 50
Example:
Index 0 1 2 3 4
(Left) lst = [10, 12, 14, 16]
[Link](18)
Index
(Right)
-5 -4 -3 -2 -1
👉 Output: [10, 12, 14, 16, 18]
📌 Negative indexing starts from the right
side.
Hssc Guide Ankit Grewal ( Cet 2024 Rank 01)

🔹 Updating List Value (Index


Hssc Guide Search @Guidehssc Hssc Guide App from play store
A function is a named block of code that
Assignment) performs a specific task.

Used to change an existing value.


Types of Functions in
Example: Python
lst = [10, 12, 14, 16]
lst[2] = 22
[Link] Functions

👉 Output: [10, 12, 22, 16] ●​ Stored in modules


●​ Must be imported before use
📌 Means: Put 22 at index 2 Example:
🔹 extend() Function import math
[Link](25)
●​ Adds multiple elements
●​ append() → only one element Other examples:
●​ extend() → more than one element
●​ log10()
Example:
●​ sin()
lst = [1, 2] ●​ cos()
[Link]([3, 4, 5])

👉 Output:[1, 2, 3, 4, 5] [Link]-in Functions

●​ Already available in Python


Variable Scope – LEGB ●​ No need to import
Rule
Examples: max(), min(), len(), str(), sum()
Python searches for a variable using the
LEGB rule. [Link]-Defined Functions

LEGB stands for: ●​ Created by the programmer


●​ Defined using def keyword
1.​ L – Local (inside function)
2.​ E – Enclosing (function inside Example:
another function)
3.​ G – Global (top level of script) x = 20
4.​ B – Built-in (predefined functions) y = 30
def sum():
📌 Python checks variables in this exact z=x+y
print(z)
order.
sum()

Functions in Python
Files in Python
➤ Definition:
Hssc Guide Ankit Grewal ( Cet 2024 Rank 01)

Hssc Guide Search @Guidehssc Hssc Guide App from play store
Python allows us to create two types of files:

1.​ Text File


seek() and tell() Methods
2.​ Binary File
Used for random access in files.

File Access Modes ➤ seek(position, mode)


Mode Meaning Mode Meaning

r / rb Read 0 Beginning of file

w / wb Write 1 Current position

2 End of file
r+ / rb+ Read + Write

a / ab Append ➤ tell()
a+ / ab+ Append + Read ●​ Returns current cursor position

wt Write text

Types of Errors in Python


File Methods
🔹 flush() 🔹 IOError
Occurs when file is not found or inaccessible.

🔹 EOFError
●​ Transfers data from buffer to file

🔹 read(size) Occurs when trying to read beyond end of


file.
●​ Reads specified number of characters
🔹 TypeError
🔹 readline() Occurs when wrong data type is used.
●​ Reads one line from file Example: "5" + 10

🔹 write() 🔹 ValueError
Occurs when correct type but invalid value is
●​ Writes a string into file
given.

🔹 writelines() Example: int("abc")

●​ Writes multiple lines into file


Hssc Guide Ankit Grewal ( Cet 2024 Rank 01)

Hssc Guide Search @Guidehssc Hssc Guide App from play store
Q1. What is an iteration statement in Python? (b) -1
(a) Executes code once (c) 1

🔵
(b) Repeats code until condition is met (d) -2
(c) Stops program execution (b)

🔵
(d) Defines a function
(b) Q8. Which operator is used for list repetition?
(a) +
Q2. Iteration in Python is also known as: (b) %
(a) Selection (c) *

🔵
(b) Recursion (d) /
(c) Looping (c)

🔵
(d) Branching
(c) Q9. What is the output of [1,2]*3?
(a) [1,2,3]
Q3. Which of the following supports iteration (b) [1,2,1,2,1,2]
in Python? (c) Error

🔵
(a) List (d) [3,6]
(b) Tuple (b)
(c) String

🔵
(d) All of these Q10. List slicing is used to:
(d) (a) Delete list
(b) Sort list
Q4. Python uses which type of indexing? (c) Extract part of list

🔵
(a) One-based (d) Reverse list
(b) Zero-based (c)
(c) Random

🔵
(d) Two-based Q11. Which is the correct slicing syntax?
(b) (a) list(start,end)
(b) list[start-end]
Q5. What is the left-side index of the first (c) list[start:end]

🔵
element in a list? (d) slice[list]
(a) 1 (c)
(b) -1
(c) 0 Q12. lst[1:4] will return elements from index:

🔵
(d) None (a) 1 to 4
(c) (b) 1 to 3
(c) 0 to 4

🔵
Q6. Negative indexing in Python starts from: (d) 2 to 4
(a) 0 (b)
(b) 1
(c) -2 Q13. Which function adds only one element to

🔵
(d) -1 a list?
(d) (a) insert()
(b) extend()
Q7. What is the right-side index of the last (c) append()

🔵
element in a list? (d) add()
(a) 0 (c)
Hssc Guide Ankit Grewal ( Cet 2024 Rank 01)

Hssc Guide Search @Guidehssc Hssc Guide App from play store
(a) function
Q14. Which function adds multiple elements (b) define
to a list? (c) def

🔵
(a) append() (d) fun
(b) extend() (c)
(c) push()

🔵
(d) update() Q21. Which type of function must be
(b) imported?
(a) Built-in
Q15. What does lst[2] = 22 do? (b) User-defined
(a) Adds new element (c) Module function

🔵
(b) Deletes element (d) Local function
(c) Updates existing value (c)

🔵
(d) Sorts list
(c) Q22. [Link]() belongs to which function
type?
Q16. Variable scope in Python follows which (a) Built-in
rule? (b) User-defined
(a) LEBG (c) Module

🔵
(b) GLEB (d) Anonymous
(c) LEGB (c)

🔵
(d) BELG
(c) Q23. Which of the following is a built-in
function?
Q17. In LEGB rule, what does L stand for? (a) sqrt()
(a) Loop (b) sin()
(b) Local (c) max()

🔵
(c) Library (d) log10()

🔵
(d) List (c)
(b)
Q24. User-defined functions are created by:
Q18. Which scope is checked first in Python? (a) Python
(a) Global (b) Compiler
(b) Built-in (c) Programmer

🔵
(c) Local (d) Interpreter

🔵
(d) Enclosing (c)
(c)
Q25. How many types of files are there in
Q19. Built-in scope contains: Python?
(a) User variables (a) One
(b) Loop variables (b) Two
(c) Predefined functions (c) Three

🔵 🔵
(d) File objects (d) Four
(c) (b)

Q20. A function is defined using which Q26. Which is a text file mode?
keyword? (a) rb
Hssc Guide Ankit Grewal ( Cet 2024 Rank 01)

Hssc Guide Search @Guidehssc Hssc Guide App from play store

🔵
(b) wb (d) close()
(c) wt (b)

🔵
(d) ab
(c) Q33. tell() method returns:
(a) File size
Q27. Which mode opens file for reading only? (b) File name
(a) w (c) Current cursor position

🔵
(b) r (d) File mode
(c) a (c)

🔵
(d) r+
(b) Q34. In seek(), mode 0 means:
(a) End of file
Q28. Which file mode is used for appending (b) Current position
data? (c) Beginning of file

🔵
(a) w (d) Middle of file
(b) r (c)
(c) a

🔵
(d) rb Q35. Which error occurs when file is not
(c) found?
(a) EOFError
Q29. flush() method is used to: (b) TypeError
(a) Close file (c) IOError

🔵
(b) Clear file (d) ValueError
(c) Transfer buffer data to file (c)

🔵
(d) Read file
(c) Q36. EOFError occurs when:
(a) File is empty
Q30. Which method reads a single line from (b) File is deleted
file? (c) End of file is crossed

🔵
(a) read() (d) File is closed
(b) readline() (c)
(c) readlines()

🔵
(d) write() Q37. "5" + 10 causes which error?
(b) (a) ValueError
(b) NameError
Q31. Which method writes multiple lines at (c) TypeError

🔵
once? (d) IOError
(a) write() (c)
(b) read()
(c) writelines() Q38. int("abc") raises:

🔵
(d) readline() (a) TypeError
(c) (b) ValueError
(c) EOFError

🔵
Q32. Which function moves the file cursor? (d) IOError
(a) tell() (b)
(b) seek()
(c) open()
Hssc Guide Ankit Grewal ( Cet 2024 Rank 01)

Hssc Guide Search @Guidehssc Hssc Guide App from play store
Q39. Which error is related to invalid value
with correct type?
(a) TypeError
(b) IOError
(c) EOFError

🔵
(d) ValueError
(d)

Q40. Which statement is TRUE?


(a) append() adds multiple values
(b) extend() adds one value
(c) Negative indexing starts from -1

🔵
(d) Indexing starts from 1
(c)

You might also like