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

Informatics-Practices-Looping-and-Lists

This document serves as a comprehensive guide to using loops and lists in Python, detailing their syntax, practical examples, and applications. It covers both 'for' and 'while' loops, nested loops, list creation and manipulation, and list comprehension. Additionally, it highlights real-world applications such as data processing, file handling, web scraping, game development, and machine learning.

Uploaded by

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

Informatics-Practices-Looping-and-Lists

This document serves as a comprehensive guide to using loops and lists in Python, detailing their syntax, practical examples, and applications. It covers both 'for' and 'while' loops, nested loops, list creation and manipulation, and list comprehension. Additionally, it highlights real-world applications such as data processing, file handling, web scraping, game development, and machine learning.

Uploaded by

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

Informatics Practices: Looping and Lists

This document provides a comprehensive guide to looping and lists in Python, covering fundamental concepts, syntax,
practical examples, and real-world applications. It aims to equip you with the necessary skills to effectively use loops and
lists for manipulating data and automating tasks in your Informatics Practices projects.

by Nobita
Introduction to Loops in
Python
Loops are a fundamental programming concept that allows you to execute a
block of code repeatedly. They are essential for automating repetitive tasks,
processing data sets, and creating dynamic programs. Python provides two
main types of loops: **for** loops and **while** loops.

For loops are used to iterate over a sequence of items, such as a list, tuple, or
string. Each item in the sequence is processed one by one, and the loop
continues until all items have been processed. While loops, on the other hand,
execute a block of code as long as a given condition is true. The condition is
checked before each iteration, and the loop terminates when the condition
becomes false.
For Loop: Iterating Over Sequences
The **for** loop in Python provides a convenient way to iterate over a sequence of elements. It allows you to perform a
specific action on each element in the sequence. The basic syntax of a for loop is as follows:

for item in sequence:


# Code to be executed for each item

For example, the following code snippet iterates over a list of numbers and prints each number to the console:

numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)

This will output:

1
2
3
4
5
While Loop: Conditional Looping
The **while** loop in Python is used to repeatedly execute a block of code as long as a specified condition remains true. The
basic syntax of a while loop is as follows:

while condition:
# Code to be executed while condition is true

For instance, the following code snippet uses a while loop to print numbers from 1 to 5:

i=1
while i <= 5:
print(i)
i += 1

This will output:

1
2
3
4
5
Nested Loops: Looping
Within Loops
Nested loops are loops that are placed inside other loops. They are useful for
iterating over multiple dimensions of data or creating complex patterns. For
example, to print a 2x2 grid of stars, you can use nested **for** loops:

for i in range(2):
for j in range(2):
print("*", end="")
print()

This will output:

**
**
Lists: Creating and Manipulating
Lists are ordered collections of items in Python. They are mutable, meaning that their elements can be changed after the list
is created. Lists can contain different data types, such as numbers, strings, and even other lists. Here's how to create and
manipulate lists:

**Creating Lists:**

my_list = [1, 2, 3, "hello", True]


empty_list = []

**Accessing Elements:**

print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: True

**Modifying Elements:**

my_list[1] = 4
print(my_list) # Output: [1, 4, 3, "hello", True]
List Comprehension: Concise List Creation
List comprehension provides a concise and efficient way to create lists based on existing iterables. It allows you to create
new lists by applying operations to each element of an existing iterable. The basic syntax is:

[expression for item in iterable if condition]

For example, to create a list of squared numbers from 1 to 5, you can use list comprehension:

squares = [x**2 for x in range(1, 6)]


print(squares) # Output: [1, 4, 9, 16, 25]
Looping Through Lists
You can iterate through the elements of a list using a **for** loop. The
following code snippet demonstrates how to loop through a list of fruits and
print each fruit:

fruits = ["apple", "banana", "cherry"]


for fruit in fruits:
print(fruit)

This will output:

apple
banana
cherry
Reading and Writing Lists to Files
You can store lists in files to persist data. Python provides functions for reading and writing lists to files.

**Writing to a File:**

fruits = ["apple", "banana", "cherry"]


with open("fruits.txt", "w") as file:
for fruit in fruits:
file.write(fruit + "\n")

**Reading from a File:**

fruits = []
with open("fruits.txt", "r") as file:
for line in file:
fruits.append(line.strip())
Practical Examples and Applications
Loops and lists have numerous applications in Informatics Practices. Some common examples include:

**Data Processing:** Looping through datasets to perform calculations, transformations, and analysis.
**File Handling:** Reading data from files and writing results to files.
**Web Scraping:** Extracting data from websites using loops to navigate through web pages and gather information.
**Game Development:** Creating game logic, animating characters, and managing game objects.
**Machine Learning:** Training machine learning models by iterating over large datasets to learn patterns.

You might also like